쌓고 쌓다

[JAVA] 명품 자바 프로그래밍 4장 실습 문제 풀이 본문

프로그래밍/java

[JAVA] 명품 자바 프로그래밍 4장 실습 문제 풀이

승민아 2022. 5. 5. 17:36

1번 문제

import java.util.Scanner;
class TV{
	String name;
	int year;
	int inch;
	
	TV(String name, int year, int inch)
	{
		this.name=name;
		this.year=year;
		this.inch=inch;
	}
	
	void show()
	{
		System.out.print(name+"에서 만든 "+year+"년형 "+inch+"인치 TV");
	}
	
}
public class Test {
		public static void main(String[] args) {
			TV myTV = new TV("LG", 2017, 32);
			myTV.show();
	}

}

 

2번 문제

import java.util.Scanner;

class Grade{
	
	private int math;
	private int science;
	private int english;
	
	Grade(int m, int s, int e)
	{
		math=m;
		science=s;
		english=e;
	}
	
	int average()
	{
		return (math+science+english)/3;
	}
	
}

public class Test {
		public static void main(String[] args) {
			
			Scanner scanner = new Scanner(System.in);
			
			System.out.print("수학, 과학, 영어 순으로 3개의 점수 입력>>");
			int math = scanner.nextInt();
			int science = scanner.nextInt();
			int english = scanner.nextInt();
			Grade me = new Grade(math, science, english);
			System.out.println("평균은 "+me.average());
	}

}

 

3번 문제

import java.util.Scanner;

class Song{
	String title;
	String artist;
	int year;
	String country;
	
	Song()
	{
		title="???";
		artist="???";
		year=0;
		country="???";
	}
	Song(String title, String artist, int year, String country)
	{
		this.title=title;
		this.artist=artist;
		this.year=year;
		this.country=country;
	}
	void show()
	{
		System.out.println("Title:"+title+" artist:"+artist+" year:"+year+" country:"+country);
	}
	
}

public class Test {
		public static void main(String[] args) {	
			Scanner scanner = new Scanner(System.in);
			Song song = new Song("Dcancing Queen","ABBA",1978,"스웨덴");
			song.show();
			
	}
}

 

4번 문제

import java.util.Scanner;

class Rectangle{
	
	int x,y,width,height;
	
	Rectangle(int x, int y, int width, int height)
	{
		this.x=x;
		this.y=y;
		this.width=width;
		this.height=height;
	}
	
	int square()
	{
		return width*height;
	}
	
	void show()
	{
		System.out.println("("+x+","+y+")에서 크기가 "+width+"x"+height+"인 사각형");
	}
	
	boolean contains(Rectangle r)
	{
		if((x<r.x&&r.x<x+width && x<r.x+r.width&&r.x+r.width<x+width))
		{
			if(y<r.y&&r.y<y+height&&y<r.y+r.height&&r.y+r.height<y+height)
			{
				return true;
			}
		}
		return false;
	}
	
}

public class Test {
		public static void main(String[] args) {	
			Rectangle r = new Rectangle(2,2,8,7);
			Rectangle s = new Rectangle(5,5,6,6);	
			Rectangle t = new Rectangle(1,1,10,10);
			r.show();
			System.out.println("s의 면적은"+s.square());
			if(t.contains(r)) 
				System.out.println("t는 r을 포함합니다");
			if(t.contains(s))
				System.out.println("t는 s를 포함합니다.");
	}
}

빨간색 사각형안에 주황색 사각형은 포함되지 않는다.

좌표 비교시 같은 경우는 포함되지 않는 거다.

 

5번 문제

import java.util.Scanner;

class Circle{
	private double x,y;
	private int radius;
	public Circle(double x, double y, int radius)
	{
		this.x=x;
		this.y=y;
		this.radius=radius;
	}
	public void show()
	{
		System.out.println("("+x+","+y+")"+radius);
	}
}

public class CircleManager {
		public static void main(String[] args) {	
			Scanner scanner = new Scanner(System.in);
			Circle c[]=new Circle[3];
			for(int i=0;i<c.length;i++)
			{
				System.out.print("x, y, radius >>");
				double x = scanner.nextDouble();
				double y = scanner.nextDouble();
				int radius = scanner.nextInt();
				c[i]=new Circle(x,y,radius);
			}
			for(int i=0;i<c.length;i++) c[i].show();
		
	}
}

 

6번 문제

import java.util.Scanner;

class Circle{
	private double x,y;
	private int radius;
	public Circle(double x, double y, int radius)
	{
		this.x=x;
		this.y=y;
		this.radius=radius;
	}
	public void show()
	{
		System.out.println("("+x+","+y+")"+radius);
	}
	public int getArea()
	{
		return radius*radius;
	}
}

public class CircleManager {
		public static void main(String[] args) {	
			Scanner scanner = new Scanner(System.in);
			Circle c[]=new Circle[3];
			for(int i=0;i<c.length;i++)
			{
				System.out.print("x, y, radius >>");
				double x = scanner.nextDouble();
				double y = scanner.nextDouble();
				int radius = scanner.nextInt();
				c[i]=new Circle(x,y,radius);
			}
			int area=0;
			Circle big=null;
			for(int i=0;i<c.length;i++)
			{
				if(c[i].getArea()>area) {
					big=c[i];
					area=c[i].getArea();
					}
			}
			System.out.print("가장 면적이 큰 원은");
			big.show();
		
	}
}

 

7번 문제

import java.util.Scanner;

class Day{
	private String work;
	public void set(String work) { this.work = work; }
	public String get() { return work; }
	public void show()
	{
		if(work==null) System.out.println("없습니다.");
		else System.out.println(work + "입니다.");
	}
}

class MonthSchedule{
	
	Day day[];
	
	MonthSchedule(int x)
	{
		day = new Day[x+1];
		for(int i=0;i<x+1;i++)
		{
			day[i] = new Day();
		}
	}
	
	void input()
	{
		Scanner scanner = new Scanner(System.in);
		System.out.print("날짜(1~30)?");
		int n = scanner.nextInt();
		System.out.print("할일(빈칸없이입력)?");
		String str = scanner.next();
		day[n].set(str);
	}
	void view()
	{
		Scanner scanner = new Scanner(System.in);
		System.out.print("날짜(1~30)?");
		int n = scanner.nextInt();
		System.out.print(n+"일의 할 일은 ");
		day[n].show();
	}
	void run()
	{
		Scanner scanner = new Scanner(System.in);
		
		int ans=0;
		System.out.println("이번달 스케쥴 관리 프로그램.");
		
		while(true) {
		System.out.print("할일(입력:1, 보기:2, 끝내기:3)>>");
		ans = scanner.nextInt();
		if(ans==1)
			input();
		else if(ans==2)
			view();
		else
			break;
		
		}
	}
	
	
}

public class Test {
		public static void main(String[] args) {	
			Scanner scanner = new Scanner(System.in);
			MonthSchedule april = new MonthSchedule(30);
			april.run();
		
	}
}

 

8번 문제

import java.util.Scanner;

class Phone{
	
	String name;
	String tel;
	Phone(String name, String tel)
	{
		this.name=name;
		this.tel=tel;
	}
}

class PhoneBook{
	
	Phone pbook[];
	
	PhoneBook(int x)
	{
		pbook = new Phone[x];
	}
}

public class Test {
		public static void main(String[] args) {	
			Scanner scanner = new Scanner(System.in);
			System.out.print("인원수>>");
			int n = scanner.nextInt();
			PhoneBook pb = new PhoneBook(n);
			for(int i=0;i<n;i++)
			{
				System.out.print("이름과 전화번호(이름과 번호는 빈 칸 없이 입력)>>");
				String name = scanner.next();
				String tel = scanner.next();
				pb.pbook[i] = new Phone(name,tel);
			}
			System.out.println("저장되었습니다...");
			while(true)
			{
				System.out.print("검색할 이름>>");
				String name = scanner.next();
				if(name.equals("그만"))
					break;
				
				for(int i=0;i<n;i++)
				{
					if(pb.pbook[i].name.equals(name))
					{
						System.out.println(name+"의 전화번호는 "+pb.pbook[i].tel+"입니다.");
						break;
					}
					if(i==n-1)
						System.out.println(name+"이 없습니다.");
				}
				
			}
			
			
	}
}

 

9번 문제

import java.util.Scanner;

class ArrayUtil{
	
	public static int[] concat(int a[], int b[]) {
		int arr[] = new int[a.length+b.length];
		
		for(int i=0;i<a.length;i++)
			arr[i]=a[i];
		
		for(int i=0;i<b.length;i++)
			arr[a.length+i]=b[i];
		return arr;
		
	}
	
	public static void print(int a[])
	{
		System.out.print("[ ");
		for(int n : a)
			System.out.print(n+" ");
		System.out.print("]");
	}
}

public class Test {
		public static void main(String[] args) {	
			Scanner scanner = new Scanner(System.in);
			int array1[] = {1,5,7,9};
			int array2[] = {3,6,-1,100,77};
			int array3[] = ArrayUtil.concat(array1, array2);
			ArrayUtil.print(array3);
			
	}
}

 

 

10번 문제

import java.util.Scanner;

class Dictionary{
	private static String kor[] = {"사랑", "아기", "돈","미래","희망"};
	private static String eng[] = {"love", "baby", "momney","future","hope"};
	public static String kor2Eng(String word)
	{
		for(int i=0;i<kor.length;i++)
		{
			if(kor[i].equals(word))
				return eng[i];
		}
		return word+"는 저의 사전에 없습니다.";
	}	
}

class DicApp{
	
	void run()
	{
		Scanner scanner = new Scanner(System.in);
		System.out.println("한영 단어 검색 프로그램입니다.");
		while(true)
		{
			System.out.print("한글 단어?");
			String ans = scanner.next();
			if(ans.equals("그만"))
				break;
			System.out.println(Dictionary.kor2Eng(ans));
		}
		
	}
	
}
public class Test {
		public static void main(String[] args) {	
			Scanner scanner = new Scanner(System.in);
			DicApp dic = new DicApp();
			dic.run();

			
	}
}

 

11번 문제

import java.util.Scanner;

class Add{
	int a,b;
	int res;
	
	void setValue(int a, int b)
	{
		res=a+b;
	}
	
	int calculate()
	{
		return res;
	}
}

class Sub{
	int a,b;
	int res;
	
	void setValue(int a, int b)
	{
		res=a-b;
	}
	
	int calculate()
	{
		return res;
	}
}
class Mul{
	int a,b;
	int res;
	
	void setValue(int a, int b)
	{
		res=a*b;
	}
	
	int calculate()
	{
		return res;
	}
}
class Div{
	int a,b;
	int res;
	
	void setValue(int a, int b)
	{
		res=a/b;
	}
	
	int calculate()
	{
		return res;
	}
}

public class Test {
		public static void main(String[] args) {	
			Scanner scanner = new Scanner(System.in);
			System.out.print("두 정수와 연산자를 입력하시오>>");
			int a=scanner.nextInt();
			int b=scanner.nextInt();
			char c=scanner.next().charAt(0);
			if(c=='+') {
				Add add = new Add();
				add.setValue(a, b);
				System.out.println(add.calculate());
			}
			else if(c=='-') {
				Sub sub = new Sub();
				sub.setValue(a, b);
				System.out.println(sub.calculate());
			}
			else if(c=='*')
			{
				Mul mul = new Mul();
				mul.setValue(a, b);
				System.out.println(mul.calculate());
			}
			else if(c=='/')
			{

				Div div = new Div();
				div.setValue(a, b);
				System.out.println(div.calculate());
			}
	}
}

 

12번 문제

import java.util.Scanner;

class Seat{
	
	String S[];
	String A[];
	String B[];
	
	Seat()
	{
		S = new String[10];
		A = new String[10];
		B = new String[10];
		for(int i=0;i<10;i++)
		{
			S[i] = "---";
			A[i] = "---";
			B[i] = "---";
		}
	}
	
	void printSeat(int x)
	{
		if(x==1)
		{
			System.out.print("S>> ");
			for(String str : S)
				System.out.print(str+" ");
			System.out.println();
		}
		else if(x==2)
		{			
			System.out.print("A>> ");
			for(String str : A)
				System.out.print(str+" ");
			System.out.println();
		}
		else if(x==3) {
			System.out.print("B>> ");
			for(String str : B)
				System.out.print(str+" ");
			System.out.println();
		}
		
	}
	
	void seatReserve(int x)
	{
		Scanner scanner = new Scanner(System.in);
		System.out.println("이름>>");
		String name = scanner.next();
		System.out.println("번호>>");
		int num = scanner.nextInt();
		if(x==1)
		{
			if(!S[num-1].equals("---"))
			{
				System.out.println("이미 예약된 좌석입니다.");
				return;
			}
			S[num-1]=name;
		}
		else if(x==2)
		{
			if(!A[num-1].equals("---"))
			{
				System.out.println("이미 예약된 좌석입니다.");
				return;
			}
			A[num-1]=name;
		}
		else
		{
			if(!B[num-1].equals("---"))
			{
				System.out.println("이미 예약된 좌석입니다.");
				return;
			}
			B[num-1]=name;
		}
		
		
	}
	
	void cancel()
	{
		Scanner scanner = new Scanner(System.in);
		System.out.println("좌석 S:1, A:2, B:3>>");
		int x = scanner.nextInt();
		printSeat(x);
		System.out.print("이름>>");
		String name = scanner.next();
		if(x==1)
		{
			for(int i=0;i<10;i++)
			{
				if(S[i].equals(name))
					S[i]="---";
			}
			
		}
		else if(x==2)
		{
			for(int i=0;i<10;i++)
			{
				if(A[i].equals(name))
					A[i]="---";
			}
		}
		else if(x==3)
		{
			for(int i=0;i<10;i++)
			{
				if(B[i].equals(name))
					B[i]="---";
			}
		}
		else
			System.out.println("잘못된 입력입니다.");
	}
	
	void reserve()
	{
		Scanner scanner = new Scanner(System.in);
		System.out.print("좌석 구분 S(1), A(2), B(3)>>");
		int ans = scanner.nextInt();
		if(ans==1) {
			printSeat(1);
			seatReserve(1);
		}
		else if(ans==2) {
			printSeat(2);
			seatReserve(2);
		}
		else if(ans==3){
			printSeat(3);
			seatReserve(3);
		}
		else
		{
			System.out.println("잘못된 입력");
		}
		
	}
	
}


public class Test {
		public static void main(String[] args) {	
			Scanner scanner = new Scanner(System.in);
			Seat seat = new Seat();
			
			System.out.println("명품 콘서트 홀 예약 시스템입니다.");
			while(true)
			{
				System.out.println("예약:1, 조회:2, 취소:3, 끝내기:4>>");
				int ans = scanner.nextInt();
				if(ans==1)
				{
					seat.reserve();
				}
				else if(ans==2)
				{
					seat.printSeat(1);
					seat.printSeat(2);
					seat.printSeat(3);
				}
				else if(ans==3)
				{
					seat.cancel();
				}
				else if(ans==4)
					break;
				else
					System.out.println("잘못된 입력입니다.");
			}
			
	}
}

Comments