Add another class
Cone dependent on class
Circle to the circular solids classes. Note that:
- The slant height of a cone is the distance from the base up one side to the apex and is calculated:
(radius² + height²)
i.e. the square root of (radius squared + height squared).
- The surface area of a cone is:
(π * radius * slant height) + base area
- The volume of a cone is:
(base area * height)/3
java.lang.Math.sqrt(double num) returns the correctly rounded positive square root of the argument
num; as a static method of a class in the java.lang package we don't need to import anything to use Math.sqrt().
Re-document the dependent classes. Adjust
DialogCylinder to also request the data required for the instantiation of a
Cone object, and return the volume and surface area of the cone (you may want to rename the
A single character in Java is a special kind of string that has its own data type
char. A character literal is bounded by single quotes:
char myChar = 'A'; char my2ndChar = "B"; //illegal assignation of String to char if(myChar == 'C') //correct expression - note equality operator == if(myChar == "D") //illegal comparison of char with String
String object methods to implement a class
CutHtmlTags that removes HTML tags from a string. Your class should have one method
cleanHtmlString(String strHtml) that returns the cleaned string. For example:
System.out.println(myCutHtmlTags.cleanHtmlString( "<span style='color:red'>Some formatted text</span>"));
should return the string
Some formatted text to the screen.
Implement a class
TestCutHtmlTags that uses
JOptionPane.showInputDialog() and tests the
CutHtmlTags class. Compile and test your classes.
Adjust the constructor of your
Greeter class so that the name of the person to be greeted can now be entered by means of an input dialog. Name the adjusted class
NewGreeter; write a class to test the
Implement a Java class Primes that calculates and counts all the prime numbers between 0 and 1000, given that the first four prime numbers are 1, 2, 3, 5. (A prime number is an integer that leaves no remainder only when divided by 1 or itself, i.e. there is no integer between 1 and the prime number that divides into the prime number exactly.) Your class should print the primes to the standard output in rows of seven prime numbers, and print the number of primes found after the list of primes - see below:
Hints: You are given the 1st 4 primes 1, 2, 3, and 5 so start calculating primes from the integer 6. There is no need to divide any integer by 1 or itself - if it cannot be divided exactly by any integer from 2 to itself minus 1, it's prime by definition. Logically then, to find out whether any given integer is a prime number, you need to divide it by every integer from 2 to one less than itself. Also, consider the usefulness of the Java modulus operator %, and remember that as soon as any integer divides into a second without remainder, the second integer is immediately recognisable as not prime.
© G. Hearn, & University of the Western Cape, 2006