Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Heads up don't use throws IOException use a try catch Exercise 17-3 Save rectangle data in a file In this exercise, youll modify the Area

Heads up don't use "throws IOException" use a "try catch"

Exercise 17-3 Save rectangle data in a file In this exercise, youll modify the Area and Perimeter application so it stores the results of its calculations in a text file. Review the application 1. Open the project named ch17_ex3_AreaAndPerimeter. 2. Review the code. Note that the Rectangle class provides methods to get the length, width, area, and perimeter for the rectangle. 3. Run the application to make sure it works correctly. Create a file I/O class 4.Add a class named RectangeIO. 5.Add the following methods to the RectangleIO class: public static void save(Rectangle r) public static void printFile() 6.Add code to the save method so that it appends the length, width, area, and perimeter of the specified Rectangle object to a file named rectangles.txt thats stored in the root directory of the application. 7.Add code to the printFile method that reads each line of the text file for the application and prints each line to the console. Use the file I/O class 8.In the Main class, add code to the while loop that uses the ProductIO class to save the Rectangle object. 9.After the code that prints the Bye! message, add code that uses the ProductIO class to print the contents of the text file for the application. 10.Run the application to make sure it works correctly. If this application has made four calculations in its lifetime, it should display data like this after the Bye! message: RECTANGLES (length|width|area|perimeter): 100.0|200.0|20000.0|600.0 300.0|400.0|120000.0|1400.0 100.0|50.0|5000.0|300.0 100.0|75.0|7500.0|350.0 ------------------------------------------------------------ package murach.rectangle; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println("Welcome to the Area and Perimeter Calculator"); System.out.println(); Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // 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()); Rectangle r = new Rectangle(length, width); // format and display output String message = "Area: " + r.getAreaNumberFormat() + " " + "Perimeter: " + r.getPerimeterNumberFormat() + " "; System.out.println(message); // see if the user wants to continue System.out.print("Continue? (y/n): "); choice = sc.nextLine(); System.out.println(); } System.out.println("Bye!"); } } package murach.rectangle; import java.text.NumberFormat; public class Rectangle { private double length; private double width; public Rectangle() { length = 0; width = 0; } public Rectangle(double length, double width) { this.length = length; this.width = width; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getArea() { double area = width * length; return area; } public String getAreaNumberFormat() { NumberFormat number = NumberFormat.getNumberInstance(); number.setMinimumFractionDigits(3); String numberFormatted = number.format(this.getArea()); return numberFormatted; } public double getPerimeter() { double perimeter = 2 * width + 2 * length; return perimeter; } public String getPerimeterNumberFormat() { NumberFormat number = NumberFormat.getNumberInstance(); number.setMinimumFractionDigits(3); String numberFormatted = number.format(this.getPerimeter()); return numberFormatted; } }

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

Question

Which of the following describes a one-to-one function? 51-1: 1

Answered: 1 week ago

Question

What is electric dipole explain with example

Answered: 1 week ago