Study/Java

2012. 06. 26 중간고사&과제

Houkibosi 2012. 6. 26. 22:11

import javax.swing.JOptionPane;
import java.util.Random; 

class Complex
{
 double realPart, imaginaryPart; // 실수, 허수 선언 ( 프린트물 참조 )
 
 public Complex() // 매개변수가 없는 생성자
 {
  realPart = 0; // 초기화
  imaginaryPart = 0; // 초기화
 }
 
 public Complex(double a, double b) // 매개변수를 2개 받는 생성자
 {
  realPart = a; // 사용자한테 받은 변수 a로 realPart를 초기화
  imaginaryPart = b; // 사용자한테 받은 변수 b로 imaginaryPart 초기화
 }
 
 public String Complex_plus (double realPart_1, double imaginaryPart_2) // 더하는 기능의 Complex_plus 함수
 {
  return (realPart + realPart_1) + " + " + (imaginaryPart + imaginaryPart_2) + "i";
  // 실수 realPart(초기값)과 realPart_1(입력받은 값)을 더하고
  // 허수 imaginaryPart(초기값)과 imaginaryPart_2(입력받은 값)에 i를 붙여
  // 문자열로 반환
 }
 
 public String Complex_mienus (double realPart_1, double imaginaryPart_2)
 {
  return (realPart - realPart_1) + " - " + (imaginaryPart - imaginaryPart_2) + "i";
 }
}


public class test
{
 public static void main(String[] args)
 {
   
  JOptionPane windows = new JOptionPane(); // 창 생성
  Complex com = new Complex(9.9, 7.7);  // Complex클래스 객체생성
  
  double number1=1.2, number2=3.1;
  
  windows.showMessageDialog(null,    // 창에 생성할 내용 입력 (showMessageDialog 사용)
    "a = " + com.realPart + ", " + com.imaginaryPart + "\n" + // 1번째 줄 출력
    "b = " + number1 + ", " + number2 + "\n" +     // 2번째 줄 출력
    "a + b = " + com.Complex_plus(number1, number2) + "\n" + // 3번째 줄 출력
    "a - b = " + com.Complex_mienus(number1, number2) + "\n" // 4번째 줄 출력
    , "Houkibosi", JOptionPane.OK_CANCEL_OPTION);    // 타이틀 출력, 창 옵션 출력
 }

}