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

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 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.
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod,...
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod, and loanPeoriod. The following methods should be included: • Constructor(s), Accessors and Mutators as needed. • public double computeFine() => calculates the fine due on this item The fine is calculated as follows: • If the loanPeriod <= maximumLoanPeriod, there is no fine on the book. • If loanPeriod > maximumLoanPeriod o If the subject of the book is "CS" the fine is 10.00...
object oriented programming java Create a Student class that have two data members id (assign to...
object oriented programming java Create a Student class that have two data members id (assign to your ID) and name (assign to your name). Create the object of the Student class by new keyword and printing the objects value. You may name your object as Student1. The output should be like this: 20170500 Asma Zubaida
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT