본문 바로가기
솔루션모음/파워자바 프로그래밍

[파워자바] 10장 프로그래밍 솔루션 답지

by 이얏호이야호 2023. 2. 15.

 

1.

import java.util.Scanner;

class Theater {
	int[] seats;
	int size;
	public Theater(int size)
	{
		this.size=size;
		seats = new int[size]; 
	}
	public void print()
	{
		System.out.println("----------------------------");
		for(int i=0; i<size; i++)
			System.out.print(i+" ");
		System.out.println("\n----------------------------");
		for(int i=0; i<size; i++)
			System.out.print(seats[i]+" ");
		System.out.println("\n----------------------------");
	}
	public void reserve()
	{
		System.out.println("몇번째 좌석을 예약하시겠습니까?");
		Scanner scan = new Scanner(System.in);
		int s = scan.nextInt();
		if( seats[s] == 0 ){
			seats[s] = 1;
			System.out.println("예약되었습니다.");
		}
	}
}

public class TheaterTest {
	public static void main(String args[]) {
		Theater t = new Theater(10);
		t.print();
		t.reserve();
		t.print();
	}
}

 

2.

2.

import java.util.Scanner;

class Histogram {
	int[] freq;
	int size;
	public Histogram(int size)
	{
		this.size = size;
		freq = new int[size]; 
	}
	public void print()
	{
		for(int i=0; i<size; i++){
			System.out.print((i*10+1)+"-"+(i+1)*10);
			for(int k=0; k<freq[i]; k++)
				System.out.print("*");
			System.out.println("");
		}
	}
	public void input()
	{
		System.out.println("점수를 입력하시오");
		Scanner scan = new Scanner(System.in);
		int s = scan.nextInt();
		if( s!= 0 )
			freq[(s-1)/10]++;
		else
			freq[0]++;
	}
}

public class HistogramTest {
	public static void main(String args[]) {
		Histogram t = new Histogram(10);
		for(int i=0;i<10;i++)
			t.input();
		t.print();
	}
}

 

3.

import java.util.Scanner;
public class ScoreTest {
	static int[] num = new int[5];
	static int sum =0;
	static double avg;
	public static void main(String[] args){
		Scanner s = new Scanner(System.in);
		for(int i=0; i<5; i++){
			System.out.println("성적을 입력하세요");
			num[i] = s.nextInt();
		}
		getTotal();
		getAverage();
	}

	private static void getAverage() {
		avg = sum / 5.0;
		System.out.println("평균 : "+avg);
	}

	private static void getTotal() {
		for(int i =0; i < 5; i++)
			sum += num[i];
		System.out.println("합계 : "+sum);
	}
}

 

4.

import java.util.Scanner;

class Hexa2Bin {
	String[] hexa2bin = {"0000", "0001", "0010", "0011",
            "0100", "0101", "0110", "0111",
            "1000", "1001", "1010", "1011",
            "1100", "1101", "1110", "1111"};
	public void print(String s)
	{
		for(int i=0;i<s.length();i++){
			char c = s.charAt(i);
			int index = 0;
			if( c >='0' && c <='9') index = (c - '0');
			if( c >='a' && c <='f') index = 10 + (c - 'a');
			System.out.print(hexa2bin[index]+" ");
		}
		System.out.print("");
	}
}

public class HistogramTest {
	public static void main(String args[]) {
		Hexa2Bin t = new Hexa2Bin();
		t.print("1abc");
		
	}
}

 

 

 

 

댓글