Question

****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

****Here is the assignment ****
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.


Note: Ensure that your program is properly formatted and it follows all Java naming conventions.


*****we are suppose to take this old code and update it to this ****
Details:
Rewrite the Rectangle class you wrote for Chapter 8 programming assignment to throw an IllegalArgumentException indicating there is a problem with the input parameters in the constructor and the set methods.

Also, rewrite the RectangleTest class from Chapter 8 to handle the exceptions thrown by the Rectangle class. 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.

*************Rectangle class**********************

//edit chapter 08

import java.util.Scanner;

public class Rectangle {

private double width;

private double height;

public Rectangle(double height, double width)//constructor

{

this.height = height;

this.width = width;

}

public void setHeight(double height)

{

if (height < 0.0 || height > 20.0) // validation

throw new IllegalArgumentException("Height should be between 0.0 and 20.0");

this.height = height;

}

public double getHeight()

{

return height;

}

public void setWidth(double width)

{

if (width < 0.0 || width > 20.0) // validation

throw new IllegalArgumentException("Width should be between 0.0 and 20.0");

this.width = width;

}

public double getWidth()

{

return width;

}

public double calculateArea()

{

return height*width;

}

public double calculatePerimeter()

{

return 2*(height+width);

}

public void display()

{

System.out.println("Rectangle Length : "+height+ " Width : "+width+" Area : "+calculateArea()+" Perimeter : "+calculatePerimeter());

}

}

*******RectangleTest******

import java.util.Scanner;

public class RectangleTest2 {

public static void main (String[] args)

{

Scanner input = new Scanner(System.in);

double height,width;

try { Rectangle r = new Rectangle () ; //create the obj of rectangle class

System.out.println("Height or Width of the rectangle should be in range of 0.0 and 20.0");

Rectangle rect = new Rectangle (16,5);

//call the method and print the Perimeter of the rectangle

System.out.println("Perimeter of Rectangle is : " +rect.calculatePerimeter());

//call the method and print area of Rectangle

System.out.println("Area of rectangle is : " +rect.calculateArea());

//create obj of rectangle class

rect.setHeight(21);;

r.display(); }

catch(IllegalArgumentException e )

{

}

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Rectangle.java

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

public class Rectangle {

private double width;

private double height;

//throw an IllegalArgumentException indicating there is a problem with the input parameters in the constructor.
public Rectangle(double height, double width)//constructor

{
   if (height < 0.0 || height > 20.0) // validation

       throw new IllegalArgumentException("Height should be between 0.0 and 20.0");
   else if (width < 0.0 || width > 20.0) // validation
       throw new IllegalArgumentException("Width should be between 0.0 and 20.0");
  
   this.height = height;
   this.width = width;

}
//throw an IllegalArgumentException indicating there is a problem with the input parameters in the set()
public void setHeight(double height)

{

if (height < 0.0 || height > 20.0) // validation

throw new IllegalArgumentException("Height should be between 0.0 and 20.0");

this.height = height;

}

public double getHeight()

{

return height;

}

//throw an IllegalArgumentException indicating there is a problem with the input parameters in the set()
public void setWidth(double width)

{

if (width < 0.0 || width > 20.0) // validation

throw new IllegalArgumentException("Width should be between 0.0 and 20.0");

this.width = width;

}

public double getWidth()

{

return width;

}

public double calculateArea()

{

return height*width;

}

public double calculatePerimeter()

{

return 2*(height+width);

}

public void display()

{

System.out.println("Rectangle Length : "+height+ " Width : "+width+" Area : "+calculateArea()+" Perimeter : "+calculatePerimeter());

}

}

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

RectangleTest.java

import java.util.Scanner;

public class RectangleTest {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Scanner input = new Scanner(System.in);

       double height,width;

       try {
             

       System.out.println("Height or Width of the rectangle should be in range of 0.0 and 20.0");
       Rectangle r = new Rectangle (2,3) ; //create the obj of rectangle class
       r.display();
      
       //create obj of rectangle class
       Rectangle rect = new Rectangle (21,50);

       //call the method and print the Perimeter of the rectangle

       System.out.println("Perimeter of Rectangle is : " +rect.calculatePerimeter());

       //call the method and print area of Rectangle

       System.out.println("Area of rectangle is : " +rect.calculateArea());

      

      
      
       }

       catch(IllegalArgumentException e )

       {
           System.out.println(e);

       }

   }

}

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

Output

Add a comment
Know the answer?
Add Answer to:
****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class...

    Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class and then use it in a Java program. The program should create two Rectangle objects and find their area and perimeter. Instructions Make sure the class file named Rectangle.java is open. In the Rectangle class, create two private attributes named lengthand width. Both length and width should be data type double. Write public set methods to set the values for length and width. Write...

  • Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then...

    Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then use it in a C++ program. The program should create two Rectangle objects and find their area and perimeter. Instructions Ensure the class file named Rectangle.cpp is open in your editor. In the Rectangle class, create two private attributes named length and width. Bothlength and width should be data type double. Write public set methods to set the values for lengthand width. Write public...

  • I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a...

    I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side;    //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); }    //setter and getter methods for Square public void setSide(double side) { this.side = side; }    public double getSide() { return...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

  • Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

    Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color. The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor. The ColoredRectangle class should have an Equals and toString methods. The ColoredRectangle class should have mutators and accessors for all three fields. public class Rectangle...

  • This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets...

    This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets and sets methods for the variables. Create a No-Arg constructor and a constructor that accepts all three values. At the end of the class add a main method that accepts variables from the user as input to represent the length and width of a room in feet and the price of carpeting per square foot in dollars...

  • This exercise guides you through the process of converting an Area and Perimeter application from a...

    This exercise guides you through the process of converting an Area and Perimeter application from a procedural application to an object-oriented application. Create and use an object 1. Open the project named ch04_ex2_Area and Perimeter that’s stored in the ex_starts folder. Then, review the code for the Main class. 2. Create a class named Rectangle and store it in the murach.rectangle package. 3. In the Rectangle class, add instance variables for length and width. The, code the get and set...

  • Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default...

    Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default constructor -- that calls the default super class constructor and sets the default height to 0- Overloaded constructor with three parameters (length, width and height) – that calls the two parameterized super class constructor passing in length and width passed and sets the height to passed height.- setDimension method with three parameters (length, width, height) – call the super class setDimension and pass in...

  • Question 1 1 pts Which of the following is not a valid class name in Java?...

    Question 1 1 pts Which of the following is not a valid class name in Java? O MyClass MyClass1 My_Class MyClass# Question 2 1 pts Which of the following statements is False about instance variables and methods? Instance variables are usually defined with private access modifier. Instance variables are defined inside instance methods. Instance methods are invoked on an object of the class that contains the methods. A class can have more than one instance variables and methods. Question 3...

  • please do in java and comments the code so i can understand Requirements: Create a Java...

    please do in java and comments the code so i can understand Requirements: Create a Java class named “MyRectangle2D.java”. Your class will have double two variables named x and y. These will represent the center point of your rectangle. Your class will have two double variables named width and height. These will represent the width and height of your rectangle. Create getter and setter methods for x, y, width, and height. Create a “no argument” constructor for your class that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT