Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java: In this exercise, youll modify the Area and Perimeter application so it uses a constant and some methods of the Math class. Add a

Java:

In this exercise, youll modify the Area and Perimeter application so it uses

a constant and some methods of the Math class.

Add a constant to the Rectangle class

1.

Open the project named ch07_ex4_AreaAndPerimeter

thats in the extra_ex_starts folder. Then,

review the code for this project.

2.

Open the Rectangle class and add a constant that stores the minimum

fraction digits for the area and perimeter.

3.

Modify the getAreaNumberFormat and getPerimeterNumberFormat methods so they

use the constant defined in the previous step to set the minimum fraction digits for the

NumberFormat object.

4.

Run the application to make sure it still works correctly.

5.

Change the minimum fraction digits to 0 by

modifying the value thats stored in the constant.

6.

Run the application to make sure it still works correctly.

Add another calculation

7.

In the Rectangle class, add a method named getDiagonal

that calculates the diagonal of a rectangle.

Search the Internet to find the formula for calculating the

diagonal of a rectangle.

Hint:To perform this calculation, you can use the

pow and sqrt methods of the Math class.

8.

In the Rectangle class, add a method named

getDiagonalNumberFormat. This method

should apply number formatting to the value thats returned by the

getDiagonal method. This method should work like the other methods in

this class that apply number formatting.

9.

In the Main class, modify the code

so it displays the diagonal after the area and

perimeter.

10.

Run the application and

make sure it works correctly.

The console should display the

user input and the results of the calculations like this:

Enter length: 100

Enter width: 100

Area: 10,000

Perimeter: 400

Diagonal: 141.421

Here is the code that needs to be modified:

image text in transcribed

image text in transcribed

Main.java x| Rectangle.java x! Source History package murach.rectangle: 3 import java.util. Scanner; 5 public class Main public static void main (String args[]) i System.out.println ("Welcome to the Area and Perimeter Calculator") System.out.println ) 10 Scanner sc = new Scanner (System.in); String choice = "y" ; while (choice.equalsIgnoreCase ("y)) 12 13 14 15 16 17 18 19 20 21 // get input from user System.out.print ("Enter length: ) double length = Double.parseDouble(sc.nextLine()); System.out.print ("Enter width: ) double width = Double. parseDouble (sc.nextLine ( ) ) ; / calculate total Rectangle r = new Rectangle (length, width); 23 24 // format and display output String message = " +r.getAreaNumberFormat) + " " + 26 27 28 Area: "Perimeter "+ r.getPerimeterNumberFormat ) + "n" System.out.println (message) 30 31 32 // see if the user wants to continue System.out.print ("Continue? (y) ") choice = sc.nextLine(); System.out.println ) 34 System.out.println("Bye!") 36 37

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions