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

Also please add comments on the code and complete in C and also please use your...
Also please add comments on the code and complete in C and also please use your last name as key. The primary objective of this project is to increase your understanding of the fundamental implementation of Vigenere Cipher based program to encrypt any given message based on the Vignere algorithm. Your last name must be used as the cipher key. You also have to skip the space between the words, while replicating the key to cover the entire message. Test...
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, 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.
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.
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 I don't understand this code. Can you put comments to explain the statements. Also, if...
please I don't understand this code. Can you put comments to explain the statements. Also, if there any way to rewrite this code to make it easier, that gonna help me a lot. import java.io.*; import java.util.*; public class State {    private int citi1x,citi1y; private int pop1; private int citi2x,citi2y; private int pop2; private int citi3x,citi3y; private int pop3; private int citi4x,citi4y; private int pop4; private int plantx,planty; public int getCity1X(){ return citi1x; } public int getCity1Y(){ return citi1y;...
JAVA CODE, USE COMMENTS TO EXPLAIN PLEASE Write a Bottle class. The Bottle will have one...
JAVA CODE, USE COMMENTS TO EXPLAIN PLEASE Write a Bottle class. The Bottle will have one private int that represents the countable value in the Bottle. Please use one of these names: cookies, marbles, M&Ms, pennies, nickels, dimes or pebbles. The class has these 14 methods: read()(please use a while loop to prompt for an acceptable value), set(int), set(Bottle), get(), (returns the value stored in Bottle), add(Bottle), subtract(Bottle), multiply(Bottle), divide(Bottle), add(int), subtract(int), multiply(int), divide(int), equals(Bottle), and toString()(toString() method will be...
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 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;   ...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT