JAVA WithNetBEANs package rubixcup import javautilScanner p
J.A.V.A _ W.i.t.h.Net.B.E.A.N.s
package rubix.cup;
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 stash.mop;
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
Rectangle.java
import java.text.NumberFormat;
public class Rectangle {
//Declaring private variables
private double length;
private double width;
//Default constructor
public Rectangle() {
length = 0;
width = 0;
}
//Parameterized constructor
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
//Setters and getters
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;
}
//This method will calculate the area
public double getArea() {
double area = width * length;
return area;
}
//This method will format the Output.
public String getAreaNumberFormat() {
NumberFormat number = NumberFormat.getNumberInstance();
number.setMinimumFractionDigits(3);
String numberFormatted = number.format(this.getArea());
return numberFormatted;
}
//This method will calculate the perimeter
public double getPerimeter() {
double perimeter = 2 * width + 2 * length;
return perimeter;
}
//This method will format the Output.
public String getPerimeterNumberFormat() {
NumberFormat number = NumberFormat.getNumberInstance();
number.setMinimumFractionDigits(3);
String numberFormatted = number.format(this.getPerimeter());
return numberFormatted;
}
}
________________________________________
RectangleIO.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class RectangleIO {
//Declaring private variable
private Rectangle r;
//Thsi method will write the contents to the file
public static void save(Rectangle r) throws IOException
{
//Creating the file
File f=new File(\"D:\\\ ectangles.txt\");
f.createNewFile();
FileWriter fw=new FileWriter(f,true);
fw.write(r.getLength()+\"|\"+r.getWidth()+\"|\"+r.getArea()+\"|\"+r.getPerimeter()+\"\ \ \");
fw.close();
}
//This method will read the data from the file and print it on the console
public static void printFile()
{
File file = new File(\"D:\\\ ectangles.txt\");
System.out.println(\"RECTANGLES(length|width|area|perimeter)\");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
//Displaying the output
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
________________________________________
TestClass.java
import java.io.IOException;
public class TestClass {
public static void main(String[] args) throws IOException {
//Creating and array of Rectangle class Objects
Rectangle rect[] ={ new Rectangle(100, 200),
new Rectangle(300, 400),
new Rectangle(100, 50),
new Rectangle(100, 75)};
//This for loop will calculate the save method on RectangleIO class object
for(int i=0;i<rect.length;i++)
RectangleIO.save(rect[i]);
//Calling the printFile() method on theRectangleUIO class object
RectangleIO.printFile();
}
}
________________________________________
Output:
rectangles.txt (This file will be created under D Drive )
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
____________________________________
Output On cosole:
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
________________Thank You
![J.A.V.A _ W.i.t.h.Net.B.E.A.N.s package rubix.cup; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println(\ J.A.V.A _ W.i.t.h.Net.B.E.A.N.s package rubix.cup; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println(\](/WebImages/31/java-withnetbeans-package-rubixcup-import-javautilscanner-p-1088209-1761572547-0.webp)
![J.A.V.A _ W.i.t.h.Net.B.E.A.N.s package rubix.cup; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println(\ J.A.V.A _ W.i.t.h.Net.B.E.A.N.s package rubix.cup; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println(\](/WebImages/31/java-withnetbeans-package-rubixcup-import-javautilscanner-p-1088209-1761572547-1.webp)
![J.A.V.A _ W.i.t.h.Net.B.E.A.N.s package rubix.cup; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println(\ J.A.V.A _ W.i.t.h.Net.B.E.A.N.s package rubix.cup; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println(\](/WebImages/31/java-withnetbeans-package-rubixcup-import-javautilscanner-p-1088209-1761572547-2.webp)
![J.A.V.A _ W.i.t.h.Net.B.E.A.N.s package rubix.cup; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println(\ J.A.V.A _ W.i.t.h.Net.B.E.A.N.s package rubix.cup; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println(\](/WebImages/31/java-withnetbeans-package-rubixcup-import-javautilscanner-p-1088209-1761572547-3.webp)
![J.A.V.A _ W.i.t.h.Net.B.E.A.N.s package rubix.cup; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println(\ J.A.V.A _ W.i.t.h.Net.B.E.A.N.s package rubix.cup; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println(\](/WebImages/31/java-withnetbeans-package-rubixcup-import-javautilscanner-p-1088209-1761572547-4.webp)