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

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...
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),...
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...
USING JAVA: complete these one method in the BasicBioinformatics class /** * Class BasicBioinformatics contains static...
USING JAVA: complete these one method in the BasicBioinformatics class /** * Class BasicBioinformatics contains static methods for performing common DNA-based operations in * bioinformatics. * * */ 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...
USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static...
USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static methods for performing common DNA-based operations in * bioinformatics. * * */ public class BasicBioinformatics { /** * Calculates and returns the number of times each type of nucleotide occurs in a DNA sequence. * * @param dna a char array representing a DNA sequence of arbitrary length, containing only the * characters A, C, G and T * * @return an int array...
Question(Design Java Method). There is a java code what created a "Student" class and hold all...
Question(Design Java Method). There is a java code what created a "Student" class and hold all the books owned by the student in the inner class "Book". Please propose a new method for the Student class so that given a Student object "student", a program can find out the title of a book for which student.hasBook(isbn) returns true. Show the method code and the calls to it from a test program. The Student class is following: import java.lang.Integer; import java.util.ArrayList;...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds:...
THE LANGUAGE IS JAVA Download the class ArrayManager.java and fill out the method shells with the...
THE LANGUAGE IS JAVA Download the class ArrayManager.java and fill out the method shells with the specifications below. After creating the methods 2) through 5), test them by running the application. The main method allows users to enter a command and, when necessary, additional command specifications. 1) a method displayArray: accepts as a parameter an int array named data prints the array elements to the console, separated by commas and spaces, with the entire array enclosed in braces, e.g., the...
Create a generic Linked List that does NOT use the Java library linked list. Make sure...
Create a generic Linked List that does NOT use the Java library linked list. Make sure it contains or access a subclass named Node (also Generic). And has the methods: addFirst(), addLast(), add(), removeFirst(), removeLast() and getHead(). In a separate Java class provide a main that creates an instance of your LinkedList class that creates an instance of your LinkedList that contains String types. Add the five names (you pick them) to the list and then iterate through the list...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT