Question

In: Computer Science

Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a...

Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods.

Details:

Create a class called Rectangle containing the following:

  • Two instance variables,
    • An instance variable of type double used to hold the rectangle’s width.
    • An instance variable of type double used to hold the rectangle’s height.
  • Provide a constructor with two parameters used to initializes each instance variable. The constructor should verify that the specified width and height values are greater than 0.0 and less than or equal to 20.0. If they are not, indicate an exception has occurred.
  • Provide get methods that return the values of each instance variables.
  • Provide set methods that set the instance variables to new values. The methods should also verify that the specified width and height values are greater than 0.0 and less than or equal to 20.0. If they are not, indicate an exception has occurred.
  • Provide a method called calculatePerimeter that calculates the perimeter of the rectangle and return that value as a double.
  • Provide a method called calculateArea that calculates the area of the rectangle and returns that value as a double.

Create a second class called RectangleTest that contains the main method, and thoroughly tests the Rectangle class’s methods. This test class does not need to ask users for input. Just create the needed Rectangle objects to ensure that you test the Rectangle class’s methods well. The thoroughness of your testing in will impact your grade.

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

public class Rectangle {

    private double width;
    private double height;


    //Provide a constructor with two parameters used to initializes each instance variable.


    public Rectangle(double width, double height) {

        if (width < 0 || width > 20 || height < 0 || height > 20) {
            throw new IllegalArgumentException("Error: Invalid dimension(s) provided.");
        }
        this.width = width;
        this.height = height;
    }

    //Provide get methods that return the values of each instance variables.


    public double getWidth() {
        return width;
    }

    public double getHeight() {
        return height;
    }

    //Provide set methods that set the instance variables to new values.

    public void setHeight(double height) {
        if (height < 0 || height > 20) {
            throw new IllegalArgumentException("Error: Invalid height provided.");
        }
        this.height = height;
    }

    public void setWidth(double width) {
        if (width < 0 || width > 20) {
            throw new IllegalArgumentException("Error: Invalid width provided.");
        }
        this.width = width;
    }

    //Provide a method called calculatePerimeter that calculates the perimeter
    public double calculatePerimeter() {
        return 2 * width + 2 * height;
    }

    //Provide a method called calculateArea that calculates the area of the rectangle a
    public double calculateArea() {
        return width * height;
    }

}

==============================================================

public class RectangleTest {


    public static void main(String[] args) {

        System.out.println("Test 1: Invalid height");
        try {
            Rectangle r1 = new Rectangle(10, -10);
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        }

        System.out.println("\nTest 2: Invalid width");
        try {
            Rectangle r1 = new Rectangle(22, 10);
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        }

        System.out.println("\nTest 3: Set invalid height");
        try {
            Rectangle r1 = new Rectangle(12, 10);
            r1.setHeight(-8);
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        }

        System.out.println("\nTest 4: Set invalid width");
        try {
            Rectangle r1 = new Rectangle(12, 10);
            r1.setWidth(-8);
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        }

        System.out.println("\nTest 5: calculate area");
        Rectangle r = new Rectangle(12, 15);
        System.out.println("Expected area = 180.0" + ", Actual area = " + r.calculateArea());
        System.out.println("\nTest 6: calculate perimeter");
        System.out.println("Expected perimeter = 54.0" + ", Actual perimeter = " + r.calculatePerimeter());

    }
}

==============================================================


Related Solutions

You have written a JAVA program that creates Shoe object with three instance variables. You will...
You have written a JAVA program that creates Shoe object with three instance variables. You will need to use the exception you created to go along with it. So far you have been using my driver classes. Now it is time for you to create your own driver class that will implement an ArrayList of Shoe objects. This assignment uses the Shoe.java and ShoeException.java to create the driver ShoeStore.Java You will display a menu as follows: Add shoes Print all...
CODE IN JAVA: Write a ProductTester class that creates one Product object named "Juicer" with a...
CODE IN JAVA: Write a ProductTester class that creates one Product object named "Juicer" with a price of $50.99, prints the name and price of the product, reduces its price by 4.0, and then prints its price again. public class Product { private String name; private double price; public Product(String productName, double productPrice) { name = productName; price = productPrice; } public String getName() { return name; } public double getPrice() { return price; } public void reducePrice(double amount) {...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
in bluej java Write an application with two classes. Class NumberUtility has one instance variable n...
in bluej java Write an application with two classes. Class NumberUtility has one instance variable n of type int. Constructor initializes instance variable n by using input parameter n. public NumberUtility(int n) Class also has the following methods:   public int getN()                              // Returns instance variable n public boolean isOdd()                  // Returns true if number n is odd and returns false otherwise. public boolean isEven()               // Returns true if number n is even and returns false if it is odd.      // Implement method by...
(a) What is a class? What is an object? What is the relationship? (b) What are the instance variables and methods of a class?
 (a) What is a class? What is an object? What is the relationship? (b) What are the instance variables and methods of a class? (c) What is the effect of declaring instance variables and methods public or private? (d) Why do we often declare the instance variables of classes private? (e) Could we declare methods private? Would we want to do so?  (f) What does the identifier this mean? Give an example of its use (g) What is a constructor? (h) What is inheritance? Why is...
JAVA Write a Temperature class that has two instance variables: a temperature value (a floating-point number)...
JAVA Write a Temperature class that has two instance variables: a temperature value (a floating-point number) and a character for the scale, either C for Celsius or F for Fahrenheit. The class should have four constructor methods: one for each instance variable (assume zero degrees if no value is specified and Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument constructor (set to zero degrees Celsius). Include the following: (1) two...
Java - Write a test program that creates an Account object with an account number of...
Java - Write a test program that creates an Account object with an account number of AC1111, a balance of $25,000, and an annual interest rate of 3.5. Use the withdraw method to withdraw $3,500, use the deposit method to deposit $3,500, and print the balance, the monthly interest, and the date when this account was created.
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date...
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date object, a ClockWithAudio object, a BMI object, a Day object, and a FigurePane object. Then display all elements in the list. Assume that all classes (i.e. Date, Account, etc.) have their own no-argument constructor.
You need to make an AngryBear class.(In java) The AngryBear class must have 2 instance variables....
You need to make an AngryBear class.(In java) The AngryBear class must have 2 instance variables. The first instance variable will store the days the bear has been awake. The second instance variable will store the number of teeth for the bear. The AngryBear class will have 1 constructor that takes in values for days awake and number of teeth. The AngryBear class will have one method isAngry(); An AngryBear is angry if it has been awake for more than...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT