Exercise 173 Save rectangle data in a file In this exercise
Exercise 17-3 Save rectangle data in a file
In this exercise, you’ll 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” that’s 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;
}
}
Solution
Kindly find Below code and it\'s output.
RectangleIO.java
=====================
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class RectangeIO {
public static void save(Rectangle r) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter(\"rectangles.txt\", true));
FileInputStream fis = new FileInputStream(new File(\"rectangles.txt\"));
if (fis.read() == -1) {
bw.write(\"RECTANGLES (length|width|area|perimeter):\ \");
}
bw.write(r.getLength() + \",\" + r.getWidth() + \",\" + r.getArea() + \",\" + r.getPerimeter());
bw.newLine();
bw.flush();
}
public static void printFile() throws IOException {
FileReader fr = new FileReader(\"rectangles.txt\");
BufferedReader br = new BufferedReader(fr);
String s;
while ((s = br.readLine()) != null) {
System.out.println(s);
}
fr.close();
}
}
==================
Main.java
==================
import java.io.IOException;
import java.text.NumberFormat;
import java.util.Scanner;
public class Main {
public static void main(String args[]) throws IOException {
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() + \"\ \";
RectangeIO.save(r);
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!\");
RectangeIO.printFile();
}
}
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;
}
}
======O/P=======
Welcome to the Area and Perimeter Calculator
Enter length: 10
Enter width: 10
Area: 100.000
Perimeter: 40.000
Continue? (y/n): 202
Bye!
RECTANGLES (length|width|area|perimeter):
4.0,5.0,20.0,18.0
10.0,20.0,200.0,60.0
30.0,40.0,1200.0,140.0
60.0,90.0,5400.0,300.0
12.0,12.0,144.0,48.0
10.0,10.0,100.0,40.0
Let me know if you have any doubts.
The output is taken in second time. First five entried done in first run. I have given output of 2nd execution .




