Question: Instructions/Design: Below is a partially written program named AreaRectangle.java . Your job is to complete the program. When it is complete, the program will: Ask
Instructions/Design: Below is a partially written program named AreaRectangle.java. Your job is to complete the program. When it is complete, the program will:
Ask the user to enter the width of a rectangle
Ask the user to enter the length of a rectangle
Display the rectangle' area
I have written the main method, you can copy it from here (that is a dot at end which is shortcut for current dir): cp /pub/cs/cconner/cs111a/SourceCode/Chapter05/AreaRectangle.java . The program calls the following 4 methods, which have not been written. It is your job to write these 4 method definitions:
getLength - This method should ask the user to enter the rectangle's length, and then return that value as a double.
getWidth - This method should ask the user to enter the rectangles' width, and then return that value as a double.
getArea - This method should accept the rectangle's length and width as arguments, and return the rectangle's area as a double. The area is calculated by multiplying the length by the width.
displayData - This method should accept the rectangle's length, width, and area as arguments, and display them in an appropriate message on the screen.
/** You must complete this program by providing 4 methods so it calculates and displays the area of a rectangle. */ import java.util.Scanner; public class AreaRectangle {
public static void main(String[] args) {
double length, // The rectangle's length width, // The rectangle's width area; // The rectangle's area // Get the rectangle's length from the user. length = getLength(); // Get the rectangle's width from the user. width = getWidth(); // Get the rectangle's area. area = getArea(length, width); // Display the rectangle data. displayData(length, width, area);
}
} Sample Output: Here is first run of program (program output in italics and user input in bold/italics ): Enter the rectangle's length: 10 Enter the rectangle's width: 20 The length is 10.0, the width is 20.0 and the area is 200.0. Here is second run of program: Enter the rectangle's length: 12.5 Enter the rectangle's width: 9.5 The length is 12.5, the width is 9.5 and the area is 118.75. Hint: Code that can help you with this assignment can be copied into your own directory by doing this on hills: cp /pub/cs/cconner/cs111a/CupConvertInput.java . (that is dot at end which is shortcut for your current directory) This is update to CupConverter.java in our book but uses standard input instead of JOpotionPane for user input.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
