Personal tools
You are here: Home Information Systems Java Platform Introduction Exercises Exercises 3

Exercises 3

Document Actions
  • Send this
  • Print this
  • Content View
  • Bookmarks

Exercises 3

<-- Previous

Table of Contents

Next -->

EXERCISE A.

Add another class Cone dependent on class Circle to the circular solids classes. Note that:

  1. 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).
  2. The surface area of a cone is: (π * radius * slant height) + base area
  3. The volume of a cone is: (base area * height)/3

In Java, 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 DialogCylinder class).

 

EXERCISE B.

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

Use the 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.

EXERCISE C.

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 NewGreeter class.

EXERCISE D.

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.

<-- Previous

Table of Contents

Next -->

© G. Hearn, & University of the Western Cape, 2006

Copyright 2007-2008, by the Contributing Authors. Cite/attribute Resource. Exercises 3. (2008, July 16). Retrieved May 26, 2013, from UWC Free Courseware Web site: http://free.uwc.ac.za/freecourseware/information-systems/java-platform-introduction/exercises/exercises-3. This work is licensed under a Creative Commons License : Attribution-ShareAlike 3.0. Creative Commons License : Attribution-ShareAlike 3.0