Please add appropriate code comments, and I compile using NetBeans. This assignm
ID: 3553905 • Letter: P
Question
Please add appropriate code comments, and I compile using NetBeans.
This assignment will be to allow the user to input the width and length of a rectangle. The program will then calculate the perimeter of the rectangle and the area of the rectangle. Then, the program will output all four dimensions (width, length, perimeter, and area) in a user-friendly dialog. Finally, the program will ask the user if he/she wants to calculate another rectangle. If so, the program will repeat the above process. If not, the program will say goodbye and stop.
Instructions
In this assignment, students will develop two classes. The first class will contain:
Explanation / Answer
// Class1.java
import java.util.*;
public class Class1{
public static void greet(){ // method to display greeting message
System.out.println("Welcome to the Rectangle Calculator. This program will accept input for the width and length of triangles, then calculate the area perimeter of each rectangle.");
}
public static void input(rect obj){ // method to take input for the passed rect object 'obj'
Scanner in = new Scanner(System.in); // new Scanner variable for input
System.out.print(" Enter the width of your rectangle: "); // Prompt the user to input width
obj.width = in.nextDouble(); // input width
System.out.print("Enter the length of your rectangle: "); // Prompt the user to input height
obj.height = in.nextDouble(); // input height
}
public static void display(rect obj){ // method to display the details of the passed rect object 'obj'
System.out.println(" The width you entered is: " + obj.width);
System.out.println("The length you entered is: " + obj.height);
System.out.println("The area of your rectangle is: " + obj.area());
System.out.println("The perimeter of your rectangle is: " + obj.perimeter());
}
public static void goodBye(){ // Method to print goodbye message
System.out.println(" Thank you for using the Rectangle Calculator. Goodbye.");
}
public static void main (String[]args){ // main method
greet(); // display greeting message
String more; // defining the variable more used to ask user for more data
Scanner in = new Scanner(System.in); // declaring new Scanner variable for input
while(true){ // loop to ask the user for data repeatedly
System.out.print(" Would you like to continue Y/N? "); /* Asking the user whether he/she wants to enter more data */
more = in.next(); // store the user choice Y/N in variable 'more'
if(!more.equals("Y")){ // if user choice is not 'Y', print goodbye message and end the loop
goodBye();
break;
}
rect object = new rect(); // if user enters 'Y', define a new rect object named 'object'
input(object); // take input for the rect type 'object'
display(object); // display the details for rect type 'object'
}
}
}
//rect.java
public class rect { // defining the class named rect
double width, height; // declaring the data field for rect class
rect(){ /* default constructor for rect class which sets width and height as zero*/
width = 0;
height = 0;
}
public void setWidth(double w){ // method to set the value of width equal to the parameter passed
width = w;
}
public void setHeight(double h){ // method to set the value of height equal to the parameter passed
height = h;
}
public double area(){ // method which returns the area of the rectangle
return width * height;
}
public double perimeter(){ // method which returns the perimeter of the rectangle
return 2 * (width + height);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.