Solutions to other chapters:
Java Methods A & AB:
Object-Oriented Programming and Data Structures
Answers and Solutions to Exercises
Chapter 11
4. |
|
public class Diploma
{
private String name, subject;
public Diploma(String nm, String subj) { name = nm; subject = subj; }
public String toString()
{
return "This certifies that\n" + name + "\n" +
"has completed a course in " + subject;
}
}
public class DiplomaWithHonors extends Diploma
{
public DiplomaWithHonors(String nm, String subj) { super(nm, subj); }
public String toString()
{
return super.toString() + "\n*** with honors ***";
}
} |
6. |
(b) |
The program shows that the ratio of the area
to the perimeter in the right isosceles triangle (1.757)
is greater than that ratio in the equilateral triangle (1.732).
Therefore the right isosceles triangle holds a larger inscribed circle. |
9. |
|
Define a class that implements Place .
For example:
public class Point1D implements Place
{
private int myX;
public Point1D(int x) { myX = x; }
public int getX() { return myX; }
public int distance(Object other)
{
return Math.abs(getX() - ((Point1D)other).getX());
}
}
Then write a program that creates several objects
of your class and calls hasMiddle for
different triplets. For example:
public class Test
{
public boolean hasMiddle(Place p1, Place p2, Place p3)
{
return p1.distance(p2) == p1.distance(p3) ||
p2.distance(p1) == p2.distance(p3) ||
p3.distance(p1) == p3.distance(p2);
}
public static void main(String[] args)
{
Point1D p1 = new Point1D(1);
Point1D p2 = new Point1D(2);
Point1D p3 = new Point1D(3);
Point1D p4 = new Point1D(4);
System.out.println(hasMiddle(p1, p2, p3));
System.out.println(hasMiddle(p1, p2, p4));
}
} |
|