Question

In: Computer Science

The java.awt.Rectangle class of the standard Java library does not supply a method to compute the...

The 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 of the Rectangle class that has getPerimeter and getArea methods. Do not add any instance variables. In the constructor, call the setLocation and setSize methods of the Rectangle class.

In Main class, provide 3 test 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.

Solutions

Expert Solution

BetterRectangle.java

import java.awt.Rectangle;

public class BetterRectangle extends Rectangle {
  
   //constructor
   public BetterRectangle(int locx, int locy, int sizex, int sizey) throws Exception{
       //checking if size is less than zero
       if(sizex < 0 || sizey < 0) {
           throw new Exception("Size less than zero\n\n");
       }
      
       //checking if both sizes are zero
       if(sizex == 0 && sizey == 0) {
           throw new Exception("Size is zero\n\n");
       }
       //setting location ans size
       this.setLocation(locx, locy);
       this.setSize(sizex, sizey);
   }
   //calculating area
   public double getArea() {
       return this.getWidth() * this.getHeight();
   }
   //calculating perimeter
   public double getPerimeter() {
       return 2 * (this.getWidth() + this.getHeight());
   }


}

Main.java


public class Main {

   public static void main(String[] args) throws Exception {
       //valid test caes
       BetterRectangle betRec1 = new BetterRectangle(1,1,5,5);
       System.out.println("Perimeter 1 Expected: 20 Actual: " + betRec1.getPerimeter());
       System.out.println("Area 1 Expected: 25 Actual: " + betRec1.getArea() + "\n\n");
      
       BetterRectangle betRec2 = new BetterRectangle(1,1,1,1);
       try {
           //invalid test case with size less than zero
           betRec2 = new BetterRectangle(1,1,-4,5);
          
       }catch(Exception ex) {
           System.out.println("Perimeter 2 Expected: 2 Actual: " + betRec2.getPerimeter());
           System.out.println("Area 2 Expected: -20 Actual: " + betRec2.getArea());
           System.out.println("Error 2:" + ex.getMessage());
          
       }
      
       BetterRectangle betRec3 = new BetterRectangle(1,1,1,1);
       try {
           //invalid test case with size equals 0
           betRec3 = new BetterRectangle(1,1,0,0);
          
       }catch(Exception ex) {
           System.out.println("Perimeter 3 Expected: 0 Actual: " + betRec3.getPerimeter());
           System.out.println("Area 3 Expected: 0 Actual: " + betRec3.getArea());
           System.out.println("Error 3:" + ex.getMessage());
          
       }

   }

}

Sample Output:

Perimeter 1 Expected: 20 Actual: 20.0
Area 1 Expected: 25 Actual: 25.0


Perimeter 2 Expected: 2 Actual: 4.0
Area 2 Expected: -20 Actual: 1.0
Error 2:Size less than zero


Perimeter 3 Expected: 0 Actual: 4.0
Area 3 Expected: 0 Actual: 1.0
Error 3:Size is zero



Related Solutions

using java Define a class Library based on the following specifications: a. A library can have...
using java Define a class Library based on the following specifications: a. A library can have multiple books. Decide on the best possible data structure to store books. b. A library has a name. c. A library has an address. Define a 2-argument constructor for the Library class. Decide on the arguments needed for this constructor. e. Define a method that can add new books to the library. f. Define a method that allows a book to be borrowed (checked...
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside...
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside of main declare two static final variables and integer for number of days in the week and a double for the revenue per pizza (which is $8.50). Create a method called main Inside main: Declare an integer array that can hold the number of pizzas purchased each day for one week. Declare two additional variables one to hold the total sum of pizzas sold...
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
This is a Java program assignment. In the GradeCalculator class, compute the final average and letter...
This is a Java program assignment. In the GradeCalculator class, compute the final average and letter grade for a particular student. The final average is calculated according to the following rules: 1) There are ten exams scored out of 100 points 2) The lowest exam score is not included in the calculation of the final average 3) The highest exam score is not included in the calculation of the final average 4) An average of 91-100 is an A 5)...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** *...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each symbol (e.g., the * reverse complement of "GTCA" is "TGAC"). * * @param dna a char array representing...
Required components: A controller class with Java main() method and a PaymentCalculator class to hold the...
Required components: A controller class with Java main() method and a PaymentCalculator class to hold the data and methods for the application. Scenario/Information: If you carry a balance on a credit card, it can be nice to see how long it would take to payoff the card. For this scenario, you should design a Java application the prints a credit card payment schedule. Inputs for your program should include: Customer’s name (first and last), the account number (as an integer),...
please write a java code, one for method and another for the Demo to: -Compute the...
please write a java code, one for method and another for the Demo to: -Compute the average age of all female students. -Compute the least amount of credits completed among males. Store the three arrays in the demo file. Print the results within the demo file. String []gender ={"F", "F", "M", "F", "F", "M", "M", "M", "M", "F", "M", "F", "M", "F", "F", "M", "M", "F", "M", "F"}; int []age = {18, 19, 19, 21, 20, 18, 24, 19, 21,...
Java Apply inheritance to write a super class and subclass to compute the triangle area and...
Java Apply inheritance to write a super class and subclass to compute the triangle area and the surface area of triangular pyramid, respectively. Assume that each side has the same length in the triangle and triangular pyramid. You need also to override toString() methods in both of super class and subclass so they will return the data of an triangle object and the data of the pyramid object, respectively. Code a driver class to test your classes by creating at...
Write a class file called lastname_digits.java. In it, write a method to compute and return the...
Write a class file called lastname_digits.java. In it, write a method to compute and return the total instances where the sum of digits of the array equals to the target. Write a demo file called lastname_digits_demo.java. Create an array of random 10 integers between 301 and 999. Also create an integer variable called target that is equal to a value between 5 and 10. Print the answer within this file. Example: If five of the 20 integers are equal to...
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the...
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the main method), define and implement the following methods: public static void printArray. This method accepts a two-dimensional double array as its argument and prints all the values of the array, separated by a comma, each row in a separate line. public static double getAverage. This method accepts a two-dimensional double array as its argument and returns the average of all the values in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT