Question

In: Computer Science

Java Code - Please explain each step through comments. Also, make sure Main (P3_13) is separate...

Java Code - Please explain each step through comments. Also, make sure Main (P3_13) is separate from your class BetterRectangle. Lastly, post a picture of your code inside an IDE along with your output. My code is below, needs modification.

E9.13The java.awt.Rectangle class of the standard Java library does not supply a method to compute the area or perimeter of a rectangle. Provide a subclass BetterRectangle ofthe Rectangle class that has getPerimeter and getArea methods. Do not add any instance variables. In the constructor, call the setLocation and setSizemethods of the Rectangleclass. In E9_13 class, provide 3test cases that tests the methods that you supplied printing expected and actual results. The test cases are: 1) One positive test case testing your method with valid inputs 2) Two negative test cases, testing your methods for invalid inputs, and printing proper error message indicating proper error handling.

Please Fix my code with comments and with instructions.

/BetterRectangle.java

import java.awt.Rectangle;

public class BetterRectangle extends Rectangle{

      

// default constructor

       public BetterRectangle()

       {

             super();

       }

      

       public BetterRectangle(int x, int y, int width, int height)

       {

             super();

             setSize(width,height);

             setLocation(x,y);

       }

      

       public double getArea()

       {

             return(getWidth()*getHeight());

       }

      

       public double getPerimeter()

       {

             return(2*(getWidth()+getHeight()));

       }

      

       public static void main(String[] args) {

              // test the class

             BetterRectangle rect1 = new BetterRectangle(0,0,20,30);

             System.out.println("Area : "+rect1.getArea());

             System.out.println("Perimeter : "+rect1.getPerimeter());

       }

}

//end of BetterRectangle.java

Solutions

Expert Solution

// BetterRectangle.java

import java.awt.Rectangle;

public class BetterRectangle extends Rectangle{

   // default constructor that sets the left-top coordinate to (0,0) and height and width to 1
   public BetterRectangle()
   {
       super(); // call default constructor of Rectangle class
       setSize(1,1); // set width and height of rectangle to 1
}
  
   // parameterized constructor that sets the left-top coordinate to (x,y) and width and height to passed values
   public BetterRectangle(int x, int y, int width, int height)
   {
       super(); // call default constructor of Rectangle class
      
       // validate both width and height of the Rectangle > 0, if not throw exception
       if(width <= 0 || height <= 0)
           throw new IllegalArgumentException("ERROR: Width or/and height of the rectangle must be greater than 0.");
       setSize(width,height); // set width and height of rectangle to width and height respectively
       setLocation(x,y); // set left-top coordinate to (x, y)
}
  
   // method that calculates the area of the rectangle and return it
   public double getArea()
   {
       return(getWidth()*getHeight());
}

   // method that calculates the perimeter of the rectangle and return it
   public double getPerimeter()
   {
       return(2*(getWidth()+getHeight()));
}
  
}

//end of BetterRectangle.java

// BetterRectangleMain.java

public class BetterRectangleMain {

   public static void main(String[] args) {

       // create a rectangle with valid inputs
       BetterRectangle rect1 = new BetterRectangle(0,0,20,30);
       System.out.println("Expected area: 600.0 Actual area: "+rect1.getArea());
       System.out.println("Expected perimeter: 100.0 Actual perimeter: "+rect1.getPerimeter());
      
       // create a rectangle with invalid width
       System.out.println("Expected error message: ERROR: Width or/and height of the rectangle must be greater than 0.");
       try {
           rect1 = new BetterRectangle(0,0,-20,30);
          
       }catch(IllegalArgumentException e)
       {
           System.out.println("Actual: "+e.getMessage());
       }
      
       // create a rectangle with invalid height
       System.out.println("Expected error message: ERROR: Width or/and height of the rectangle must be greater than 0.");
       try {
           rect1 = new BetterRectangle(0,0,20,0);
          
       }catch(IllegalArgumentException e)
       {
           System.out.println("Actual: "+e.getMessage());
       }
   }
}

//end of BetterRectangleMain.java

Output:


Related Solutions

JAVA CODE, BEGINERS; Please use comments to explain For all of the following words, if you...
JAVA CODE, BEGINERS; Please use comments to explain For all of the following words, if you move the first letter to the end of the word, and then spell the result backwards, you will get the original word: banana dresser grammar potato revive uneven assess Write a program that reads a word and determines whether it has this property. Continue reading and testing words until you encounter the word quit. Treat uppercase letters as lowercase letters.
Make sure to include comments that explain all your steps (starts with #) Make sure to...
Make sure to include comments that explain all your steps (starts with #) Make sure to include comments that explain all your steps (starts with #) Write a program that prompts the user for a string (a sentence, a word list, single words etc.), counts the number of times each word appears and outputs the total word count and unique word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt...
JAVA CODE BEGINNER , Please use comments to explain, please Repeat the calorie-counting program described in...
JAVA CODE BEGINNER , Please use comments to explain, please Repeat the calorie-counting program described in Programming Project 8 from Chapter 2. This time ask the user to input the string “M” if the user is a man and “W” if the user is a woman. Use only the male formula to calculate calories if “M” is entered and use only the female formula to calculate calories if “W” is entered. Output the number of chocolate bars to consume as...
Please explain each step very carefully and make sure your handwriting is easy to read. Thank...
Please explain each step very carefully and make sure your handwriting is easy to read. Thank you Question: Suppose p(x) is a polynomial of degree n with coefficients in R and suppose p(x) has exactly n real roots. Show that p'(x) has exactly n-1 real roots.
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
4. Please walk me through this problem. Step by step thoroughly. Also explain the equations used....
4. Please walk me through this problem. Step by step thoroughly. Also explain the equations used. also an explanation of how you came to the answer. Thanks i will love you if you do this. You have been asked by your boss to evaluate three mutually exclusive projects. The cash flow estimates and costs of each project are given below: T=0 1 2 3 4 5 6 7 Project A -3790 200 600 300 1000 2800 Project B -3790 1000...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ *...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ * This program takes a puzzle board and returns true if the * board is solvable and false otherwise * * Example: board 3 6 4 1 3 4 2 5 3 0 * The goal is to reach the 0. You can move the number of spaces of your * current position in either the positive / negative direction * Solution for this game...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three integer values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three int||eger values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT