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;
}
}

Complete the requirements Add a class named RectangeIO. . Add the following methods to the RectangleIO class: public static void save (Rectangle r) public static void printFile () . Add code to the save method so that it appends the length, width, area, and perimeter of the specified Rectangle obiect to a file named \'rectangles.txt\" that\'s stored in the root directory of the application. * . Add code to the printFile method that reads each line of the text file for the * In the Main class, add code to the while loop that uses the ProductIO class to . After the code that prints the \"Bye!\" message, add code that uses the * Run the application to make sure it works correctly. If this application has application and prints each line to the console. save the Rectangle obiect. ProductIO class to print the contents of the text file for the application made four calculations in its lifetime, it should display data like this after the Bve!\" message: RECTANGLES (lengthlwidthjarealperimeter): 100.0200.020000.0600.0 300.0400.0 120000.01400.0 100.050.0 5000.0300.0 100.0175.017500.0350.0

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(\
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(\
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(\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site