Study/Java

Power Java 10장 프로그래밍

Houkibosi 2012. 6. 28. 15:03

1. 12개월 동안의 카드 사용 금액을 double형의 배열에 저장하는 클래스 CreaditCard를 설계하라.

프로그램은 다음과 같은 정보를 반환하는 메소드를 가져야 한다.

완전한 프로그램을 작성하여 테스트하라.

사용자로부터 월별 사용 금액을 입력받을 때는 음수를 체크하도록 하라.

1)1년 동안의 전체 사용 금액

2) 월별 평균 사용 금액

3) 가장 지출이 많았던 월

4) 가장 지출이 적었던 월


import java.util.*;


class Card  

{

double[] pay = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

double all_money=0;

public double[] Get_pay()

{ return pay; }  

public void Set_pay( int i, double set_pay )

pay[i] = set_pay; 

all_money += set_pay;

}

public double all_money()

{ return all_money; }

public double average()

{ return all_money/12; }

public double many()

{

double a=pay[0];

for(int i=1; i<12; i++)

a = a > pay[i] ? a : pay[i]; 

return a; 

}

public double less()

{

double a=pay[0];

for(int i=1; i<12; i++)

a = a > pay[i] ? pay[i] : a; 

return a; 

}


}


public class test_1 

{

public static void main(String[] args) 

{

Scanner scan = new Scanner(System.in);

Card card = new Card();

for(int i=0; i<12; i++)

{

System.out.println( (i+1) + "월 금액을 입력해 주세요." );

card.Set_pay(i, scan.nextInt() );

}

System.out.println( card.all_money() );

System.out.println( card.average() );

System.out.println( card.many() );

System.out.println( card.less() );

}

}



2. 객관식 문제의 시험 점수를 채점하는 클래스 Exam를 작성하여 보자.

Exam에는 10개의 객관식 문제와 답이 저장되어 있다.

학생들은 시험을 통과하기 위하여 7문제 이상을 맞추어야 한다. 

Exam 클래스 안에 배열을 선언하고 문제와 답을 저장한 후에 학생들에게 문제를 제시하고

학생들의 답도 역시 배열에 저장한다.

다음과 같은 메소드를 제공하여야 한다.

- isPassed() : 학생이 시험에 통과했는지를 true와 false로 반환한다.

- correctAnsewers() : 정답 횟수

- incorrectAnswers() : 오답 횟수
완전한 프로그램을 작성하여서 Exam클래스를 테스트하도록 하고 학생들의 답변은 a, b, c, d만 입력이 가능하도록 프로그램을 작성하라.

import java.util.*;
import java.io.*;

class Exam  
{
String[] Quiz = 
{
"1+1은?", "2+1은?", "3+1은?", "4+1은?", "5+1은?", 
"6+1은?", "7+1은?", "8+1은?", "9+1은?", "10+1은?"
};
char[] answer = { 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b' };
char[] user = { 0,0,0,0,0,0,0,0,0,0 };
boolean[] ox = {false,false,false,false,false,false,false,false,false,false};
int count=0;
public void Set_user( int i, char set_user )
user[i] = set_user; 
ox[i] = user[i] == answer[i] ? true : false;
if(ox[i]==true)
count++;
}
public boolean isPassed(int i)
return ox[i]; 
}
public int correctAnsewers()
{ return count; }
public int incorrectAnswers()
{ return 10-count; }
}

public class test_1 
{
public static void main(String[] args) throws Exception
{
//Scanner scan = new Scanner(System.in);
InputStreamReader insr = new InputStreamReader(System.in);
Exam exam = new Exam();
for(int i=0; i<10; i++)
{
System.out.println( exam.Quiz[i] );
char ch = (char)insr.read();
if ( ch=='a' || ch=='A' || ch=='b' || ch=='B' || ch=='c' || ch=='C' || ch=='d' || ch=='D')
{
System.out.println( "정답을 다시 입력해주세요." );
i--;  
}
switch(ch)
{
case 'a' : 
case 'A' : exam.Set_user( i, ch ); break;
case 'b' : 
case 'B' : exam.Set_user( i, ch ); break;
case 'c' : 
case 'C' : exam.Set_user( i, ch ); break;
case 'd' : 
case 'D' : exam.Set_user( i, ch ); break;
}
}
for(int i=0; i<10; i++)
System.out.println( (i+1) + "번 문제는 " + exam.isPassed( i ) );
System.out.println( exam.correctAnsewers() + "개 맞았습니다." );
System.out.println( exam.incorrectAnswers() + "개 틀렸습니다.");
}
}