Question: Create a file I/O class 1.Add a class named RectangeIO. 2.Add the following methods to the RectangleIO class: public static void save(Rectangle r) public static

Create a file I/O class

1.Add a class named RectangeIO.

2.Add the following methods to the RectangleIO class:

public static void save(Rectangle r)

public static void printFile()

3.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.

4.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

5.In the Main class, add code to the while loop that uses the RectangleIO class to save the Rectangle object.

6.After the code that prints the Bye! message, add code that uses the RectangleIO class to print the contents of the text file for the application.

7.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

//---------------------------------------------main.java

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!"); } }

//--------------------------------------------------rectangle.java

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!