본문 바로가기
IT/프로그래밍

파워 자바 컴팩트 5장 연습문제 해설 power java compact

by nutrient 2020. 12. 14.
728x90
728x170

 

 

 

 

파워 자바 컴팩트 5장 연습문제 해설 power java compact

 

파워 자바 컴팩트 5장 연습문제 해설 power java compact

 

1. MyMetric이라는 클래스를 작성하고 여기에 킬로미터를 마일로 변환하는 정적 메소드인 kiloToMile()을 작성하라. 또, 반대로 마일을 킬로미터로 변환하는 정적 메소드 mileToKilo()도 작성하라. MyMetricTest 클래스에서 이들 정적 메소드를 호출하여 테스트해보자.

class MyMetric{
	static void kiloToMile(double input) {
		System.out.println(input + "을 마일로 바꾸면 " + input*0.621371 + "[mile]");
	}
	static void mileToKilo(double input) {
		System.out.println(input + "을 km으로 바꾸면 " + input*1.609344 + "[km]");
	}
}

public class MyMetricTest {
	public static void main(String[] args) {
		MyMetric.kiloToMile(1);
		MyMetric.mileToKilo(1);
	}
}

 

2. 배열을 이용하여 간단한 극장 예약 시스템을 작성하여 보자. 아주 작은 극장이라서 좌석이 10개 밖에 되지 않는다. 사용자가 예약을 하려고 하면 먼저 좌석 배치표를 보여준다. 즉, 예약이 끝난 좌석은 1로, 예약이 되지 않은 좌석은 0으로 나타낸다.

import java.util.Scanner;

public class TheaterTest {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] line = new int[10];

for(int i = 0; i < 10; i++) { // 배열 0으로 초기화
line[i] = 0;
}

System.out.println("----------------------");
for(int i = 0; i < 10; i++) {
System.out.print(i + " ");
}System.out.println();
System.out.println("----------------------");
for(int i = 0; i < 10; i++) {
System.out.print(line[i] + " ");
}System.out.println();
System.out.println("----------------------\n");

System.out.print("몇 번째 좌석을 예약하시겠습니까? ");
int input = scan.nextInt();
System.out.println("예약되었습니다.");

line[input] = 1;

System.out.println("----------------------");
for(int i = 0; i < 10; i++) {
System.out.print(i + " ");
}System.out.println();
System.out.println("----------------------");
for(int i = 0; i < 10; i++) {
System.out.print(line[i] + " ");
}System.out.println();
System.out.println("----------------------\n");

scan.close();
}
}

 

 

3. 학생들의 성적을 받아서 평균을 구하는 프로그램을 작성하라.

import java.util.*;

class Sungjuk{
float score;

Sungjuk(){ // 생성자
score = 0;
}

void InputSungjuk(float input) {
this.score = input;
}
}

public class PE5_3 {
public static void main(String[] args) {
Scanner POWERSCAN = new Scanner(System.in);
float tot = 0;

Sungjuk[] sj = new Sungjuk[5]; // 객체 배열 선언
for(int i = 0; i < 5; i++) { // 객체 배열 초기화
sj[i] = new Sungjuk();
}

for(int i = 0; i < 5; i++) { // 입력
System.out.print("성적을 입력하세요:");
sj[i].InputSungjuk(Float.parseFloat(POWERSCAN.nextLine()));
}

for(int i = 0; i < 5; i++) { // 합계내기
tot += sj[i].score;
}

System.out.println("합계: " + tot);
System.out.println("평균: " + tot/sj.length);

POWERSCAN.close();
}
}

4번

import java.util.*;

class Plane{
private String Manufacturer;
private String Model;
private int Max;
private static int planes = 0;
////////////////////////////////////////////// 생성자
protected Plane(String m, String mo, int p){
this.Manufacturer = m;
this.Model = mo;
this.Max = p;
planes++;
}
protected Plane(String m, String mo){
this.Manufacturer = m;
this.Model = mo;
planes++;
}
protected Plane(String m){
this.Manufacturer = m;
planes++;
}
protected void Plane(){
planes++;
}
////////////////////////////////////////////// 생성자

protected String getmanu() { // 제작사 정보 획득
return this.Manufacturer;
}

protected String getmodel() { // 모델명  획득
return this.Model;
}

protected int getMax() { // 최대 승객수 정보 획득
return this.Max;
}
protected int getplanes() { // planes 값 반환
return planes;
}
}

public class PlaneTest {
public static void main(String[] args) {
Scanner POWERSCAN = new Scanner(System.in); // 스캐너 선언

Plane p1 = new Plane("가나다 산업", "aa", 123);
System.out.println("식별번호: " + p1.getplanes() + " 모델: " + p1.getmodel() + " 승객 수: " + p1.getMax());

Plane p2 = new Plane("종이비행기", "bb", 300);
System.out.println("식별번호: " + p2.getplanes() + " 모델: " + p2.getmodel() + " 승객 수: " + p2.getMax());

Plane p3 = new Plane("종이비행기", "cc", 150);
System.out.println("식별번호: " + p3.getplanes() + " 모델: " + p3.getmodel() + " 승객 수: " + p3.getMax());

POWERSCAN.close();
}
}

 

 

5. 은행 계좌를 나타내는 BankAccount 클래스에 다음과 같은 기능을 하는 메소드를 추가하고 테스트하라.

class BankAccount{
private int balance;

////////////////////////////////////////////////////////////
protected BankAccount(int input) {
this.balance = input;
}
protected BankAccount() {
this.balance = 0;
}
////////////////////////////////////////////////////////////


protected void myAccount(){
System.out.println("현재 잔액은 " + balance + "입니다.");
}
public int transfer(int amount, BankAccount otherAccount) {
otherAccount.balance += amount;
this.balance -= amount;
return amount;
}
}

public class BankAccountTest {
public static void main(String[] ar) {
BankAccount a = new BankAccount(10000); // 계좌에 10000원 있는채로 생성
BankAccount b = new BankAccount(); // 흙수저

System.out.print("myAccount1: ");
a.myAccount();//계좌 정보 조회

System.out.print("myAccount2: ");
b.myAccount();


System.out.println("transfer(" + a.transfer(5000, b) + ")"); // 계좌 이체


System.out.print("myAccount1: ");
a.myAccount();//계좌 정보 조회

System.out.print("myAccount2: ");
b.myAccount();
}
}
728x90
그리드형

댓글