본문 바로가기

Backend/Java

[Java] 문제풀이 4장 1~6번

문제 #1

다음 main() 메소드를 실행했을 때 예시와 같이 출력되도록 TV 클래스를 작성하라.

LG에서 만든 2017년형 32인치 TV

 

 

package test;
class TV{
	String company;
	int year;
	int size;
	
	public TV(String company, int year, int size) {
		this.company=company;
		this.year=year;
		this.size=size;
	}
	public void show() {
		System.out.println(company+"에서 만든 "+year+"년형 "+size+"인치 TV");
	}
	
}
public class TVEx {
	public static void main(String[] args) {
		TV myTV = new TV("LG", 2017, 32); 
		myTV.show();
	}


}

 

문제 #2

3과목의 점수를 입력받아 Grade 객체를 생성하고 성적 평균을 출력하는 프로그램을 완성하라.

package test;

import java.util.Scanner;
class Grade{
	int math;
	int sci;
	int eng;
	public Grade(int math, int sci, int eng){
		this.math=math;
		this.sci=sci;
		this.eng=eng;
	}
	public int average() {
		return (math+sci+eng)/3;
	}
}
public class Input3ScoreEx {

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

		System.out.print("수학, 과학, 영어 순으로 3개의 점수 입력 >> ");
		int math = scan.nextInt();
		int sci = scan.nextInt();
		int eng = scan.nextInt();
		Grade me = new Grade(math, sci, eng);
		System.out.println("평균은 " + me.average() + " 입니다.");

		scan.close();
	}


}

 

문제 #3

노래 한 곡을 나타내는 Song 클래스를 작성하라.

- 클래스 내 저장 내용 -> title: 노래 제목, artist: 가수 이름, year: 발표 연도, country: 가수 국적

- 생성자 2: 기본 생성자와 매개변수로 모든 필드를 초기화하는 생성자

- 노래 정보를 출력하는 show() 메소드

- main() 메소드에서는 1978, Sweden 국적의 ABBA가 부른 “Dancing Queen”Song 객체로 생성하고 show()를 이용하여 노래 정보를 다음과 같이 출력

package test;
class Song{
	String title; String artist;
	int year; String country;
	
	public Song() {}
	public Song(String title, String artist, int year, String country) {
		this.title=title;
		this.artist=artist;
		this.year=year;
		this.country=country;
	}
	public void show() {
		System.out.println(year+"년 "+country+" 국적의 "+artist+"가 부른 "+title);
	}
}
public class SongEx {
	public static void main(String[] args) {
		Song song = new Song("Dancing Queen", "ABBA", 1978, "Sweden");
		song.show();
	}
}

 

문제 #4

다음 멤버를 가지고 직사각형을 표현하는 Rectangle 클래스를 작성하시오.
- int x, y, width, height: 사각형을 구성하는 점과 크기 정보
- int square(): 사각형의 넓이 반환
- void show(): 사각형의 좌표와 넓이를 화면에 출력
- Boolean contains(Rectangle r): r이 현재의 사각형 안에 있으면 true 반환

 

(2, 2)에서 크기가 8x7인 사각형
s의 면적은 36
t는 r을 포함합니다.
package test;
class Rectangle{
	int x, y, width, height;
	int square() {int area=width*height; return area;}
	void show() {
		System.out.println("("+x+", "+y+")에서 크기가 "+width+"x"+height+"인 사각형");
	}
	Boolean contains(Rectangle r) {
			if(x+width>r.x+r.width && y+height>r.y+r.height)
			return true;
		return false;}
	
	public Rectangle(int x, int y, int width, int height) {
		this.x=x;
		this.y=y;
		this.width=width;
		this.height=height;
	}
}
public class RectangleAreaEx {
	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

하루의 할 일을 표현하는 클래스 Day는 다음과 같다. 한 달의 할 일을 표현하는 MonthSchedule 클래스를 작성하라.

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 + “입니다.”);
}

 

[코드]

package test;

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 + "입니다.");
	}
}

public class MonthSchedule extends Day{
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.print("이번달 스케줄 관리 프로그램.");
		
		String[] haveToDo = new String[30];
		for(int i = 0; i < 30; i++)
			haveToDo[i] = "";
		while(true) {
			System.out.print("\n할일(입력:1, 보기:2, 끝내기:3) >> ");
			int num = scan.nextInt();
			int date;
			
			switch(num) {
			case 1:
				System.out.print("날짜(1~30)? ");
				date = scan.nextInt();
				System.out.print("할일(빈칸없이입력)? ");
				haveToDo[date] = scan.next();
				break;
			case 2:
				System.out.print("날짜(1~30)? ");
				date = scan.nextInt();
				System.out.println(date+"일의 할 일은 "+haveToDo[date]+"입니다.");
				break;
			case 3:
				System.out.println("프로그램을 종료합니다.");
				return;
			}
		}
		
	}

}

 

문제 #6

이름(name), .전화번호(tel) 필드와 생성자 등을 가진 Phoe 클래스를 작성하고, 실행 예시와 같이 동작하는 PhoneBook 클래스를 작성하라.

- PhoneBook 클래스에서 저장할 사람의 수를 입력받고, Phone 객체 배열을 생성
- 한사람의 정보는 하나의 Phone 객체에 저장
- 문자열 a와 b가 같은지 비교할 때 a.equals(b)가 true 인지를 확인

 

[코드]

package test;

import java.util.Scanner;

class Phone{
	String name;
	String tel;
	
	public Phone() {}
	public Phone(String name, String tel) {
		this.name = name; this.tel = tel;
	}
	public String getName() {return name;}
	public String getTel() {return tel;}
}
public class PhoneBook extends Phone{

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int people; String name; String tel;
		System.out.print("인원수>> ");
		people = scan.nextInt();
		Phone[] phone = new Phone[people];
		int i;
		
		for(i = 0; i < people; i++) {
			System.out.print("이름과 전화번호 입력 >> ");
			name = scan.next(); tel = scan.next();
			phone[i] = new Phone(name, tel);
		}
		System.out.println("저장되었습니다.");

		while(true) {
			System.out.print("검색할 이름>> ");
			String search = scan.next();
			for(i = 0; i < people; i++) {
				if(search.equals(phone[i].getName())) {
					System.out.println(search+"의 전화번호는 "+phone[i].getTel()+" 입니다.");
					i--; break;
				}
			}
			if(search.equals("그만")) return;
			if(i == people){
				System.out.println(search+"이 없습니다.");
				continue;				
			}
		}
	}
}

'Backend > Java' 카테고리의 다른 글

[Java] 버블 정렬  (0) 2024.02.05
[Java] BufferedReader, BufferedWriter  (0) 2024.02.02
[Java] 문제풀이 3장 실습 1~2번  (0) 2024.01.04
[Java] ArrayList 사용 정리  (0) 2023.11.08
[Java] Scanner와 BufferedReader의 차이점  (0) 2023.07.28