Question: All these are with Java Problem 5) Below is the outline of a simple class called BusyWork. In the blank space provided in the class,
All these are with Java
Problem 5) Below is the outline of a simple class called BusyWork. In the blank space provided in the class, add two methods to the class. The two methods that you add can make use of the two given methods setNumber() and getNumber(). (a) Add a method increment() that adds 1 to the value of m. (b) Add a method decrement() that returns a BusyWork object whose value of m is one less than the value stored in the calling object. This method should not modify the m in the object the method is called on. class BusyWork { private int m = 0; public void setNumber(int newM) { this.m = newM; } public int getNumber() { return this.m; } // write the methods increment() and decrement() }//BusyWork Problem 6) Here is a simple Point class and a partially completed Circle class. class Point { private double x, y; public Point(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public double getY() { return y; } public void setX(double x) { this.x = x; } public void setY(double y) { this.y = y; } } class Circle { private Point c; // center private double r; // radius public Circle(double x, double y, double r) { } public Circle(Point c, double r) { } public Circle(Point c) { this.r = 1.0; this.c = c; } public boolean equals(Circle roundThing) { } // more stuff } (a) The third constructor in Circle has a "privacy leak". Explain why. Hint: Consider the following code. Point p = new Point(1,2); Circle c = new Circle(p); p.setX(100); (b) Complete the first two Circle constructors. Be sure to avoid another "privacy leak". (c) Complete the equals method in the Circle class. Problem 7) Consider the following class definition. The public interface of this class is that it ``stores'' three numbers, area, length, and width, but the actual implementation of the class contains a single private field, area, and a public constant, WIDTH. Write implementations for the constructor and the two set and the two get methods in such a way that value for length is derived from the variable area. class RestrictedRectangle { public final double WIDTH = 3.0; private double area; public RestrictedRectangle(double length) { } public double getArea() { } public double getLength() { } public void setArea(double area) { } public void setLength(double length) { } }//RestrictedRectangle Problem 8) Suppose that we define the following two classes. public class Class1 public class Class2 { { int x = 10; static int x = 0; public int methodA(int y) public static int methodA() { return y + this.x; } { return x; } public void methodB(int y) public static void methodB(int y) { this.x = y + this.x; } { x = y; } } } Draw a diagram of Java's memory after the execution of the following code (in, for example, DrJava's interactions pane). Be sure that your memory diagram includes the (final) values of all the instance and static variables. Clearly denote in your drawing any non-referenced objects that will be garbage collected. > Class1 ref1 = new Class1() > Class1 ref2 = new Class1() > Class1 ref3 = ref2 > int x = 5 > ref1.methodB(x) > ref2.methodB( ref1.methodA(2) ) > ref1 = ref3 > Class2.methodB( ref1.methodA(Class2.methodA()) ) Problem 9) Suppose that we have classes A, B, C and D. Suppose that B is a subclass of A, that C is a subclass of B, and D is a subclass of A. A / \ B D | C class A {} class B extends A {} class C extends B {} class D extends A {} Suppose that we make the following declarations. A a1 = new A(); A a2 = new C(); D d1 = new D(); For each statement below, explain what, if any, errors would be caused by that statement (be sure to consider both compile time and run time errors). A a3 = new B(); B b1 = new A(); B b2 = (B) a1; B b3 = (B) a2; B b4 = (B) d1; Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
