Input is from a file. The input is all doubles. You are to input these doubles 2
ID: 3881799 • Letter: I
Question
Input is from a file. The input is all doubles. You are to input these doubles 2 at a time (the length and width), create a Rectangle and determine if it is the largest. Output the largest Rectangle
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Lab8Num2 {
public static class Rectangle
{
private double length, width;
public Rectangle()
{
length=0;
width=0;
}
public Rectangle(double len, double wid)
{
length=len;
width=wid;
}
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 area()
{
return length*width;
}
public double perimeter()
{
return 2*(length+width);
}
public String toString()
{
return "Length: " + length + " Width: " + width;
}
public int compareTo(Rectangle r)
{
}
}
public static void main(String[] args) {
File inFile = new File("lab8.in");
Scanner fileInput = null;
try {
fileInput = new Scanner(inFile);
} catch (FileNotFoundException ex) {
//Logger.getLogger(Lab10.class.getName()).log(Level.SEVERE, null, ex);
}
//get first Rectangle and make it the biggest;
while ()
{ //get more data from file
//make Rectangle
//see if it is bigger than biggest so far
//if so, it is the new biggest
}
System.out.println("The biggest rectangle was " + biggest);
}
}
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Lab8Num2 {
public static class Rectangle
{
private double length, width;
public Rectangle() {
length=0;
width=0;
}
public Rectangle(double len, double wid){
length=len;
width=wid;
}
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 area() {
return length*width;
}
public double perimeter() {
return 2*(length+width);
}
public String toString() {
return "Length: " + length + " Width: " + width;
}
public int compareTo(Rectangle r) {
//compare area of current object to the area of r
return (r.area()>area())?1:0;
}
}
public static void main(String[] args) {
File inFile = new File("lab8.in");
Scanner fileInput = null;
try {
fileInput = new Scanner(inFile);
} catch (FileNotFoundException ex) {
//Logger.getLogger(Lab10.class.getName()).log(Level.SEVERE, null, ex);
}
//get first Rectangle and make it the biggest;
//Input is read assuming that each contains length and breadth separated with space
double length = Double.parseDouble(fileInput.next());
double breadth = Double.parseDouble(fileInput.nextLine());
Rectangle biggest = Rectangle(length, breadth);
while () {
//get more data from file
length = Double.parseDouble(fileInput.next());
breadth = Double.parseDouble(fileInput.nextLine());
//make Rectangle
Rectangle tempRectangle = Rectangle(length, breadth);
//see if it is bigger than biggest so far
if (biggest.compareTo(tempRectangle) == 1) {
//if so, it is the new biggest
biggest = tempRectangle;
}
}
System.out.println("The biggest rectangle was " + biggest);
}
}