Answers are in boldface.
Questions about classes.
The Circle class should have data fields giving the circle's radius, x-coordinate of center, and y-coordinate of center.
There should be methods to return each of these fields, as well the methods getArea() and getCircumference(). (Recall for a circle of radius r, the area is pi * r 2, and the circumference is 2 * pi * r. You should also use Math.PI as the source of the constant pi.)
There should be two constructors, the second one letting the coordinates of the center default to zeros. In both constructors, if the parameter for the radius is negative, set it equal to zero.
Fill in the blanks below to complete the Java implementation of this class:
public class Circle {
// data fields for the object
private double myRadius;
private double myX; // x coordinate of center
private double myY; // y coordinate of center
// constructors
public Circle(double radius, double x, double y) {
if (radius < 0) myRadius = 0;
else
myRadius = radius;
myX = x;
myY = y;
}
// center defaults to (0, 0)
public Circle(double radius) {
if (radius < 0) myRadius = 0;
else
myRadius = radius;
myX = 0;
myY = 0;
}
// alternative code for above constructor ...
// (Can't have both of these definitions, though)
// (Answer by Jared Rodriguis.)
// Note: if a constructor invokes a constructor, the
// constructor invokation must be the first thing
public Circle(double radius) {
this(radius, 0, 0);
}
//accessors
public double getRadius( ) {
return myRadius;
}
public double getX( ) {
return myX;
}
public double getY( ) {
return myY;
}
// other methods
public double getArea( ) {
return Math.PI*myRadius*myRadius;
}
public double getCircumference( ) {
return 2*Math.PI*myRadius;
}
}
public class CircleTest {
public static void main (String[] args) {
Circle circle1 = new Circle(10, 2, 3);
System.out.println(circle1.getArea());
Circle circle2 = new Circle(5);
System.out.println(circle2.getCircumference());
}
}
Circle:
Radius: 10.0,
Center: (2.0, 3.0),
Area: 314.1592653589793,
Circumference: 62.83185307179586
public String toString() {
return "Circle:" +
"\n Radius: " + myRadius + "," +
"\n Center: (" + myX + ", " + myY + ")," +
"\n Area: " + getArea() + "," +
"\n Circumference: " + getCircumference();
}
This method compareTo involves two objects that are instances of the Circle class. The first is the circle object that the method is inside, and the second is the parameter c. The radius of the first is just myRadius, while the radius of the second is given by c.getRadius(). (In this case this.myRadius also works for the first, and c.myRadius also works for the second.) Here is a solution:
public int compareTo(Circle c) {
if (myRadius == c.getRadius()) return 0;
else if (myRadius < c.getRadius()) return -1;
else return 1;
}
Here is a second version:
public int compareTo(Circle c) {
if (this.myRadius == c.myRadius) return 0;
else if (this.myRadius < c.myRadius) return -1;
else return 1;
}
Finally, here is extra code for the main function to test some of the above. (This was not asked for on the actual exam.)
System.out.println(circle1);
System.out.println(circle2);
if (circle1.compareTo(circle2) < 0)
System.out.println("First circle is smaller than second.");
else if (circle1.compareTo(circle2) > 0)
System.out.println("Second circle is smaller than first.");
else
System.out.println("Circles are the same size.");
Circle circle3 = new Circle(-4, 2, 3);
System.out.println(circle3);
Notice that when one writes circle1.compareTo(circle2), the variable circle1 refers to an instance of the Circle class, and circle1.compareTo ... is a method inside that instance. Then circle2 is fed in as a parameter, becoming c inside the method.
Here is the output:
Circle: Radius: 10.0, Center: (2.0, 3.0), Area: 314.1592653589793, Circumference: 62.83185307179586 Circle: Radius: 5.0, Center: (0.0, 0.0), Area: 78.53981633974483, Circumference: 31.41592653589793 Second circle is smaller than first. Circle: Radius: 0.0, Center: (2.0, 3.0), Area: 0.0, Circumference: 0.0
Here are two solutions:
// first version
int n = 10;
int sum = 0;
for (int i = 1; i <= n; i++)
if (i%2 != 0) sum = sum + i;
System.out.println("n: " + n + ", sum: " + sum);
// second version, doesn't require %2 stuff
n = 8;
sum = 0;
i = 1;
while (i <= n) {
sum = sum + i;
i = i + 2;
}
System.out.println("n: " + n + ", sum: " + sum);
// third version, destroys n. Show-off version
n = 20;
System.out.print("n: " + n + ", ");
sum = 0;
do {
if (n%2 == 1) sum += n;
} while (n-- >= 0);
System.out.println("sum: " + sum);
and here is the output:
n: 10, sum: 25 n: 8, sum: 16 n: 20, sum: 100
Give an if statement that will check if string s comes before string t in lexicographic order and will print "s precedes t" if it is true.
String s = "One string";
String s1 = "One string";
System.out.println ("s == s1 is " + (s == s1));
String t = "One string more".substring(0,10); // t is "One string";
System.out.println ("s == t is " + (s == t));
System.out.println ("s.equals(t) is " + s.equals(t));
if (s.equals(t)) System.out.println("They are equal");
else System.out.println("They are NOT equal");
t = "One stupid string";
if (s.compareTo(t) < 0) System.out.println("s precedes t");
else if (s.compareTo(t) > 0) System.out.println("t precedes s");
else System.out.println("s equals t");
Output:
s == s1 is true s == t is false s.equals(t) is true They are equal s precedes t
String s = "UTSA";
and will print each character of s on a separate line, so that in this case it will print:
U T S A
You must use a loop and it should work no matter what s is.
String s = "UTSA"; for (int j = 0; j < s.length(); j++) System.out.println(s.charAt(j));