Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

32. Consider the following class definition. The public interface of this class

ID: 3870644 • Letter: 3

Question

32. Consider the following class definition. The public interface of this class is that it has three fields named area, length, and WIDTH (which is a public constant), but the implementation should be that the class contains a single (private) double area. Write implementations for the constructor and the two set and the two get methods in such a way that 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) {

}

}

28. Suppose I have defined a class called Thing. The following line performs three distinct steps. What are they? How would you rewrite this single line as two lines? What distinct steps do each of those two lines perform?

Thing rose = new Thing();

33. Write a definition for a class Point that contains two private data fields, called x and y, of type double. The class should have two constructors, a default one and a constructor that takes two doubles. Objects of this class should be immutable. Provide appropriate get methods and a toString() method.

34. Write a method called sumSubArray that accepts an array of doubles and two ints. If the two ints are valid indices, then the method should return the sum of the values stored in the array whose indices are between the two ints (inclusive). If the two ints are not valid indices, then the method should return zero

Explanation / Answer

package com.vassarlabs.test.practise;

public class RestrictedRectangle {
   public final double WIDTH = 3.0;

   private double area;
  
   public RestrictedRectangle(double length) {
       this.area = length * WIDTH;
   }
  
   public double getArea(){
       return area;
   }

   public double getLength() {
       return area / WIDTH;
   }
  
   public void setArea(double area) {
       this.area = area;
   }
  
   public void setLength(double length) {
       this.area = length * WIDTH;
   }
}


------------------------------------------------------------------------

Thing rose = new Thing();

1. It first create a new instance of the given class, "Things" and allocates memory for it.
2. The object and it's member variables are initialized, and then appropriate constructor is invoked to performs any initialization operations.
3. Assigns the reference of the object to a variable called rose.

The other way to write the above statement is
Thing rose = null;
rose = new Thing();

1. It first intializes an empty variable 'rose' of type Thing to null reference
2. steps are same as above block

------------------------------------------------------------------------

public class Point {
  
   private double x;
   private double y;
  
   public Point() {
       super();
   }

   public Point(double x, double y) {
       super();
       this.x = x;
       this.y = y;
   }

   public double getX() {
       return x;
   }

   public double getY() {
       return y;
   }
  
   @Override
   public String toString() {
       return "Point [x=" + x + ", y=" + y + "]";
   }
}

------------------------------------------------------------------------

    public static double sumSubArray(double[] array, int x, int y) {
       if (x >=0 && x < array.length && y >=0 && y < array.length) {
           int min = Math.min(x, y);
           int max = Math.max(x, y);
          
           double sumSubArray = 0;
           for (int i = min; i<=max; i++) {
               sumSubArray += array[i];
           }
           return sumSubArray;
       }
       return 0;
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote