본문 바로가기

자바/명품 자바 프로그래밍

명품 자바 프로그래밍 7장 실습문제 / 2022.01.25

1.

package Chapter7;
import java.util.*;

public class Exercise {
	public static void main(String [] args) {
		Scanner sc = new Scanner(System.in);
		Vector<Integer> num = new Vector<>();
		
		System.out.print("정수(-1이 입력될 때까지) >> ");
		while(true) {
			int number = sc.nextInt();
			
			if(number == -1)
				break;
			
			num.add(number);
		}
		
		int max = num.get(0);
		
		for(int i=1; i<num.size(); i++) {
			if(max < num.get(i))
				max = num.get(i);
		}
		
		System.out.println("가장 큰 수는 " + max);
	}
}

2.

package Chapter7;
import java.util.*;

public class Exercise {
	public static void main(String [] args) {
		Scanner sc = new Scanner(System.in);
		ArrayList<Character> grade = new ArrayList<>();
		
		System.out.print("6개의 학점을 빈 칸으로 분리 입력(A/B/C/D/F)>>");
		for(int i=0; i<6; i++) {
			char alpha = sc.next().charAt(0);  // 문자 입력 받기
			grade.add(alpha);
		}
		
		double sum = 0;
		
		for(int i=0; i<6; i++) {
			double score = 0; 
			
			if(grade.get(i).equals('A'))
				score = 4.0; 
			else if(grade.get(i).equals('B'))
				score = 3.0;
			else if(grade.get(i).equals('C'))
				score = 2.0;
			else if(grade.get(i).equals('D'))
				score = 1.0;
			else
				score = 0;
			
			sum += score;
		}
		
		System.out.println(sum/6.0);
	}
}

3.

package Chapter7;
import java.util.*;

public class Exercise {
	public static void main(String [] args) {
		Scanner sc = new Scanner(System.in);
		HashMap<String, Integer> nations = new HashMap<>();
		
		System.out.println("나라 이름과 인구를 입력하세요.(예 : Korea 5000)");
		
		while(true) {
			System.out.print("나라 이름, 인구 >> ");
			String nara = sc.next();
			
			if(nara.equals("그만"))
				break;
			
			int ingu = sc.nextInt();
				
			nations.put(nara, ingu);		
		}
		
		while(true) {
			System.out.print("인구 검색 >> ");
			String nara2 = sc.next();
			if(nara2.equals("그만"))
				break;
			
			Integer ingu2 = nations.get(nara2);
			if(ingu2 == null)
				System.out.println(nara2 + " 나라는 없습니다.");
			else {
				System.out.println(nara2 + "의 인구는 " + ingu2);
			}
			sc.nextLine();
		}
	}
}

4.

package Chapter7;
import java.util.*;

public class Exercise {
	public static void main(String [] args) {
		Scanner sc = new Scanner(System.in);
		Vector<Integer> precipitation = new Vector<>();
		
		while(true) {
			System.out.print("강수량 입력 (0 입력시 종료)>> ");
			int amount = sc.nextInt();
			if(amount == 0)
				break;
			
			precipitation.add(amount);
			
			int hap = 0;
			for(int i=0; i<precipitation.size(); i++){
				System.out.print(precipitation.get(i)+" ");
				hap += precipitation.get(i);
			}
			
			System.out.println("\n현재 평균 " + hap/precipitation.size());
		}
	}
}

5.

(1) 

import java.util.*;

class Student{
	private String name;
	private String subject;
	private int studentNum;
	private double average;
	
	public Student(String name, String subject, int studentNum, double average) {
		this.name = name; this.subject = subject;
		this.studentNum = studentNum; this.average = average;
	}
	
	public String getName() {
		return name;
	}

	public String getSubject() {
		return subject;
	}

	public int getStudentNum() {
		return studentNum;
	}

	public double getAverage() {
		return average;
	}

}
public class Main {
    public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
    	ArrayList<Student> student = new ArrayList<Student>();
    	
    	// 4명의 학생 정보 입력
    	System.out.println("학생 이름, 학과, 학번, 학점평균 입력하세요.");
    	for(int i=0; i<4; i++) {
    		System.out.print(">> ");
    		String info = sc.nextLine();
    		String[] infoArr = info.split(", ");
    		student.add(new Student(infoArr[0], infoArr[1], Integer.parseInt(infoArr[2]), Double.parseDouble(infoArr[3])));
    	}
    	
    	// 4명의 학생 정보 출력
    	System.out.println("-------------------");
    	for(int i=0; i<4; i++) {
    		Student person = student.get(i);
    		System.out.println("이름 : " + person.getName());
    		System.out.println("학과 : " + person.getSubject());
    		System.out.println("학번 : " + person.getStudentNum());
    		System.out.println("학점평균 : " + person.getAverage());
    		System.out.println("-------------------");
    	}
    	
    	// 검색
    	while(true) {
    		System.out.print("학생 이름 >> ");
    		String name = sc.nextLine();
    		for(int i=0; i<4; i++) {
    			Student person = student.get(i);
    			if(name.equals("그만"))
    				return;
    			if(name.equals(person.getName())) {
    				System.out.println(person.getName() + ", " + person.getSubject() +
    						", " + person.getStudentNum() + ", " + person.getAverage());
    			}
    		}
    	}
    	
    } 
}

(2)

import java.util.*;

class Student{
	private String subject;
	private int studentNum;
	private double average;
	
	public Student(String subject, int studentNum, double average) {
		this.subject = subject;
		this.studentNum = studentNum; this.average = average;
	}

	public String getSubject() {
		return subject;
	}

	public int getStudentNum() {
		return studentNum;
	}

	public double getAverage() {
		return average;
	}

}
public class Main {
    public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
    	HashMap<String, Student> student = new HashMap<>();
    	
    	// 4명의 학생 정보 입력
    	System.out.println("학생 이름, 학과, 학번, 학점평균 입력하세요.");
    	for(int i=0; i<4; i++) {
    		System.out.print(">> ");
    		String info = sc.nextLine();
    		String[] infoArr = info.split(", ");
    		student.put(infoArr[0], new Student(infoArr[1], Integer.parseInt(infoArr[2]), Double.parseDouble(infoArr[3])));
    	}
    	
    	// 4명의 학생 정보 출력 (해시맵의 전체 검색 이용)
    	Set<String> keys = student.keySet(); // 해시맵 student에 있는 모든 키를 Set 컬렉션으로 리턴
    	Iterator<String> it = keys.iterator(); // Set의 각 문자열을 순차 검색하는 Iterator 리턴
    	while(it.hasNext()) {
    		String key = it.next(); // 키 
    		Student person = student.get(key);
    		System.out.println("학과 : " + person.getSubject());
    		System.out.println("학번 : " + person.getStudentNum());
    		System.out.println("학점평균 : " + person.getAverage());
    		System.out.println("-------------------");
    	}
    	
    	// 검색
    	while(true) {
    		System.out.print("학생 이름 >> ");
    		String name = sc.nextLine();
    		if(name.equals("그만"))
    				return;
    		Student person = student.get(name);
    		
    		
    		System.out.println(name + ", " + person.getSubject() + ", "
    						+ person.getStudentNum() + ", " + person.getAverage());
    	
    		}
    } 
}

6.

import java.util.*;

class Location{
	private String city;
	private double longitude;
	private double latitude;
	
	public Location(String city, double longitude, double latitude) {
		this.city = city;
		this.longitude = longitude;
		this.latitude = latitude;
	}
	
	public String getCity() {
		return city;
	}
	public double getLongitude() {
		return longitude;
	}

	public double getLatitude() {
		return latitude;
	}
}
public class Main {
    public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
    	HashMap<String, Location> country = new HashMap<>();
    	
    	System.out.println("도시,경도,위도를 입력하세요.");
    	for(int i=0; i<4; i++) {
    		System.out.print(">> ");
    		String text = sc.nextLine();
    		StringTokenizer st = new StringTokenizer(text, ",");
    		String city = st.nextToken().trim();
    		double logitude = Double.parseDouble(st.nextToken().trim());
    		double latitude = Double.parseDouble(st.nextToken().trim());
    		Location loc = new Location(city, logitude, latitude);
    		country.put(city, loc); //해시맵에 저장
    	}
    	
    	Set<String> key = country.keySet();
    	Iterator<String> it = key.iterator();
    	System.out.println("---------------------------");
    	while (it.hasNext()) {
    		String city = it.next(); // 도시 이름 알아냄
    		Location loc = country.get(city); 
    		System.out.print(loc.getCity() + "\t");
    		System.out.print(loc.getLongitude() + "\t");
    		System.out.println(loc.getLatitude());
    	}
    	System.out.println("---------------------------");
    	
    	while(true) {
    		System.out.print("도시 이름 >> ");
    		String cityName = sc.next();
    		
    		if(cityName.equals("그만"))
    			return;
    		
    		Location loc2 = country.get(cityName);
    		if(loc2==null)
    			System.out.println(cityName+"은 없습니다.");
    		else {
    			System.out.println(cityName + " " + loc2.getLongitude() +" "+loc2.getLatitude());
    		}
    	}
    } 
}

7.

package Chapter7;
import java.util.*;

public class Exercise {
	public static void main(String [] args) {
		Scanner sc = new Scanner(System.in);
		HashMap<String, Double> student = new HashMap<>();
		
		System.out.println("미래장학금관리시스템입니다.");
		for(int i=0; i<5; i++) {
			System.out.print("이름과 학점 >> ");
			String name = sc.next();
			double grade = sc.nextDouble();
			student.put(name, grade);
		}
		
		System.out.print("장학생 선발 학점 기준 입력>> ");
		double standard = sc.nextDouble();
		
		Set<String> keys = student.keySet();
		Iterator<String> it = keys.iterator();
		
		System.out.print("장학생 명단 : ");
		while(it.hasNext()) {
			String stuName = it.next();
			double score = student.get(stuName);
			if(score >= standard) {
				System.out.print(stuName+" ");
			}
		}
	}
}

8.

package Chapter7;
import java.util.*;

public class Exercise {
	public static void main(String [] args) {
		Scanner sc = new Scanner(System.in);
		HashMap<String, Integer> client = new HashMap<>();
		
		System.out.println("** 포인트 관리 프로그램입니다 **");
		while(true) {
			System.out.print("이름과 포인트 입력 >> ");
			String name = sc.next();
			if(name.equals("그만"))
				return;
			int point = sc.nextInt();
			
			if(client.get(name) == null) {
				client.put(name, point);
			}
			else {
				client.put(name, client.get(name) + point);
			}
			
			Set<String> keys = client.keySet();
			Iterator<String> it = keys.iterator();
			while(it.hasNext()) {
				String hasName = it.next();
				Integer hap = client.get(hasName);
				System.out.print("("+hasName+","+hap+")");
			}
			System.out.println();
		}
	}
}