Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write the following in Netbeans IDE 8.1 (The Rectangle class) Following t

ID: 3935436 • Letter: P

Question

Please write the following in Netbeans IDE 8.1

(The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains:

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.

A method named getPerimeter() that returns the perimeter.

2. (Use the Date class) Write a program that creates a Date object, sets its elapsed time to 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, and 100000000000, and displays the date and time using the toString() method, respectively.

Explanation / Answer

import java.util.Date;

//class rectangle
class Rectangle{
   private double width;
   private double height;
  
   //default constructor
   public Rectangle() {
       this.width=1;
       this.height=1;
   }
  
   //paramaterized constructor
   public Rectangle(double width,double height){
       this.width=width;
       this.height=height;
   }
  
   //method to find the area of the rectangle
   public double getArea(){
       return this.width*this.height;
   }
  
   //method to find the perimeter of the rectangle
   public double getPerimeter(){
       return 2*(this.width+this.height);
   }
}


public class Demo {

   public static void main(String[] args) {
       Rectangle rect=new Rectangle(3,4);// creating the object of the rectangle class
       System.out.println("Area of rectangle : "+rect.getArea()); // getting the area of rectangle
       System.out.println("Perimeter of rectangle : "+rect.getPerimeter());// getting the perimeter of rectangle
       System.out.println(" ");
       System.out.println("----------------------------Calling displayDateTime method--------------------------");
       displayDateTime();//calling the displayDateTime() method
   }
  
   public static void displayDateTime(){
           long j = 0;
   Date date=new Date(j); // creatin the object of date class
   for (j = 10000; j < 100000000000l; j *= 10) { // loop for elapsed time
   System.out.println("Elapsed time: " + j + " milliseconds");
   date.setTime(j);// setting the elapsed time
   System.out.println("Datetime: " + date.toString());//printing the date time with toString method
   }
   }

}

---------------------------------------output--------------------------------------------------------------------------------

Area of rectangle : 12.0
Perimeter of rectangle : 14.0


----------------------------Calling displayDateTime method--------------------------
Elapsed time: 10000 milliseconds
Datetime: Thu Jan 01 05:30:10 IST 1970
Elapsed time: 100000 milliseconds
Datetime: Thu Jan 01 05:31:40 IST 1970
Elapsed time: 1000000 milliseconds
Datetime: Thu Jan 01 05:46:40 IST 1970
Elapsed time: 10000000 milliseconds
Datetime: Thu Jan 01 08:16:40 IST 1970
Elapsed time: 100000000 milliseconds
Datetime: Fri Jan 02 09:16:40 IST 1970
Elapsed time: 1000000000 milliseconds
Datetime: Mon Jan 12 19:16:40 IST 1970
Elapsed time: 10000000000 milliseconds
Datetime: Sun Apr 26 23:16:40 IST 1970