package section2;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Code09 {
static MyRectangle2[] rects= new MyRectangle2 [100];
static int n = 0;
public static void main(String [] args){
try {
Scanner in = new Scanner(new File("data.txt"));
while(in.hasNext()){
int x = in.nextInt();
int y = in.nextInt();
int w = in.nextInt();
int h = in.nextInt();
rects[n] = new MyRectangle2( x, y, w, h);
//축약 버전
//rects[n] = new MyRectangle2(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt() );
n++;
}
in.close();
} catch (FileNotFoundException e) {
System.out.println("No data file.");
System.exit(1);
e.printStackTrace();
}
bubbleSort();
for(int i = 0; i < n; i++ )
System.out.println( rects[i].toString() );
}
private static void bubbleSort() {
for(int i=n-1; i>0; i--){
for(int j=0; j<i; j++){
if(rects[j].calcArea() > rects[j+1].calcArea()){
MyRectangle2 tmp = rects[j];
rects[j] = rects[j+1];
rects[j+1] =tmp;
}
}
}
}
}
package section2;
public class MyPoint2 {
public int x;
public int y;
public MyPoint2( int x, int y)
{
this.x = x;
this.y = y;
}
}
package section2;
public class MyRectangle2 {
public MyPoint2 lu;
public int width;
public int height;
public MyRectangle2(int x, int y, int w, int h){
lu = new MyPoint2( x, y );
width = w;
height = h;
}
public MyRectangle2( MyPoint2 p, int w, int h){
lu = p;
width = w;
height = h;
}
public int calcArea() // code09에서 이 메서드를 가져와 파라미터의 MyRectangle2 r는 자기자신이기 때문에 지워준다
{
return width * height;
}
public String toString(){ // toString 메소드는 특별
return "("+ lu.x + ", " + lu.y +") " + width + " " + height;
}
}