Starting from:

$30

Assignment #2 String() and equals() methods

Assignment #2

Be sure to include all source code
listings and at least one sample run for each question.
1. Write a program, which prints a table of contents. Use the following data
structure:
public class TocEntry
{
 // Specify the needed methods
private String chapter;
private int page;
}
 In your driver/test program define:
 public final int TOCSIZE = 100;
 TocEntry toc[] = new TocEntry[TOCSIZE];
 int toc_curlen = 0;
Next develop the necessary code, in your TocEntry class, to read in a chapter name and
page number until “****” is entered. From this generate an output, e.g.:
 My Story Starts...............1
 Growing up...................35
 Conquering the World........103
 Winding Down................186
Sample run:
 % java useTocEntry

 Enter chapter title: Camelot
 Enter starting page number: 1
 Enter chapter title: King Arthur's Court
 Enter starting page number: 3
 Enter chapter title: Knights of the Table Round
 Enter starting page number: 8
 Enter chapter title: Sir Dinadan the Humorist
 Enter starting page number: 12
 Enter chapter title: An Inspiration
 Enter starting page number: 14
 Enter chapter title: The Eclipse
 Enter starting page number: 23
 Enter chapter title: A Postscript by Clarence
 Enter starting page number: 274
 Enter chapter title: ****
 Camelot..........................................1
 King Arthur's Court..............................3
 Knights of the Table Round.......................8
 Sir Dinadan the Humorist........................12
 An Inspiration..................................14
 The Eclipse.....................................23
 A Postscript by Clarence.......................274
2. Given:
public class Point
{
 public Point(double xx, double yy, double zz)
 {
 x = xx;
 y = yy;
 z = zz;
 }
 public double getX()
 {
 return x;
 }
 public double getY()
 {
 return y;
 }
 public double getZ()
 {
 return z;
 }
 public double distance(Point p)
 {
 return Math.sqrt((x - p.x) * (x - p.x)
 + (y - p.y) * (y - p.y) + (z - p.z) * (z - p.z));
 }
private double x, y, z;
}
public class Sphere
{
 public Sphere(Point cntr, double rad)
 {
 center = cntr;
 radius = rad;
 }
 public Point getCenter()
 {
 return center;
 }
 public double getRadius()
 {
 return radius;
 }
private Point center;
private double radius;
}
 a. Write toString() and equals() methods for class Sphere. Test your methods, in a driver program, by creating two
 separate Spheres with two separate centers at (1,2,3) each with a radius of 5.
 b. Eplain how to determine if objects are equal. Next, show that neither the centers nor the Spheres are ‘==’ but that they
 are equals(). Finally, use toString() to output the salient properties of one of the Spheres.
 c. Create a derived class, VSphere which includes a method to calculate the volume of a sphere; write a test driver & test it.