☰ Contents Java Details

Java Details in Summary

With these two methods, which one runs for whatType(r)?

String whatType(Region r) { return "Got Region"; }
String whatType(RectRegion rr) { return "Got RectRegion"; }

Region r = new RectRegion();
  1. whatType(Region r)
    Java picks among overloads by the type of the argument as written, and r is declared a Region.
  2. whatType(RectRegion rr)
    The object is a RectRegion, but r is declared a Region, and the type is what chooses the overload.
  3. Neither: it doesn’t compile
    Two methods with the same name are fine when their parameters differ.
Finish PointC: the first constructor sets both fields, and the second makes a point whose x and y are both both, by calling the first.
class PointC {
double x;
double y;
PointC(double x, double y) {

}
PointC(double both) {

}
}
PointC p = new PointC(1.0, 2.0);
PointC q = new PointC(3.0);
PointC has no equals method written. What is new PointC(1.0, 2.0).equals(new PointC(1.0, 2.0))?
  1. false
    Without its own equals, a class compares references, like ==, and these are two different objects.
  2. true
    That’s what a record would say: a record writes an equals that compares the fields. A class doesn’t get one.
  3. It doesn’t compile
    Every class has an equals; it’s just the one that compares references.
Px.size is static. Which of these calls it the way Pict.java does?
  1. Px.size("width", w)
    A static method is called on the class’s name, with no object in front.
  2. new Px().size("width", w)
    A static method doesn’t need an object, and new Px() doesn’t compile: the constructor is private.
  3. size("width", w)
    size needs Px. in front, unless the call is inside Px itself.
What does each one evaluate to? (Write a double with its .0.)
(int) 7.9 is
(int) Math.round(7.5) is
(double) 1 / 4 is
(double) (1 / 4) is
In your own program, c is a Circle and r is a Raster. Which lines compile? Select all that do.
  1. int rad = c.radius();
    A record writes a public accessor for each field.
  2. int rad = c.radius;
    A record’s fields are private, and Circle is in another file, Pict.java.
  3. int h = r.height();
    height() is a method Raster lets anyone call.
  4. int h = r.height;
    Raster declares height as a private field.