Study/Java

2학기 중간고사

Houkibosi 2012. 6. 28. 15:13

import javax.swing.JOptionPane;

class Rectangle
{
 double length=1, width=1;
 
 // 생성자
 public Rectangle(){} 
 
 // Get 메소드
 public double get_length() { return length; }
 public double get_width() { return width;  }
 
 // Set 메소드
  public void set_length(double length_in)
 {
  if(0 < length_in && length_in < 50)
   length = length_in;  
 } 
 public void set_width(double width_in)
 {
  if(0 < width_in && width_in < 50)
   width = width_in;  
 }
 
 // 둘레를 구하는 함수
 public double perimeter()
 { return (length + width) * 2; }
 
 // 넓이를 구하는 함수
 public double area()
 { return length * width;   }
}

public class Rectangle_Test
{
 public static void main(String[] args)
 {
  JOptionPane windows = new JOptionPane();  // 창 생성
  Rectangle rect = new Rectangle();   // Rectangle 생성
  
  rect.set_length(getNumber()); // getNumber() 함수로 숫자를 받아옴
  rect.set_width(getNumber2()); // getNumber() 함수로 숫자를 받아옴

  windows.showMessageDialog(null, // 메시지 내용
    "Length : " + rect.get_length() + "\n" + // 1번째 줄
    "Width : " + rect.get_width() + "\n" +  // 2번째 줄
    "Perimeter :" + rect.perimeter() + "\n" + // 3번째 줄
    "Area : " + rect.area() + "\n"    // 4번째 줄
    , "Test", JOptionPane.INFORMATION_MESSAGE); // 타이틀 출력, 창 옵션 출력
  
  System.exit(0); // 종료
 }
 
 public static int getNumber()
 {
  return Integer.parseInt(JOptionPane.showInputDialog("Enter the length."));
 }
 public static int getNumber2()
 {
  return Integer.parseInt(JOptionPane.showInputDialog("Enter the width."));
 }

}