Notice
Recent Posts
Recent Comments
Link
«   2025/03   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Tags
more
Archives
Today
Total
관리 메뉴

archive

15장 제네릭과 컬렉션 연습 문제 및 LAB 문제-2 본문

Java/스터디 예제 풀이

15장 제네릭과 컬렉션 연습 문제 및 LAB 문제-2

안정민 2023. 3. 3. 12:49

8. Vector 컬렉션을 이용하여 강수량의 평균을 유지 관리하는 프로그램을 작성하라. 강수량을 입력하면 벡터에 추가하고 현재 입력된 모든 강수량과 평균을 출력한다.

package 제네릭과컬렉션;

import java.util.*;

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

9. 하나의 학생 정보를 나타내는 Student 클래스에는 이름, 학과, 학번, 학점 평균을 저장하는 필드가 있다.

(1) 학생마다 Student 객체를 생성하고 4명의 학생 정보를 ArrayList<Student> 컬렉션에 저장한 후에, ArrayList<Student>의 모든 학생(4명) 정보를 출력하고 학생 이름을 입력받아 해당 학생의 학점 평균을 출력하는 프로그램을 작성하라.

학생 이름, 학과, 학번, 학점평균 입력하세요.

>> 황기태, 모바일, 1, 4.1

>> 이재문, 안드로이드, 2, 3.9

>> 김남윤, 웹공학, 3, 3.5

>> 최찬미, 빅데이터, 4, 4.25

----------------------------------

이름: 황기태

학과: 모바일

학번: 1

학점평균: 4.1

----------------------------------

이름: 이재문

학과: 안드로이드

학번: 2

학점평균: 3.9

----------------------------------

이름: 김남윤

학과: 웹공학

학번: 3

학점평균: 3.5

----------------------------------

이름: 최찬미

학과: 빅데이터

학번: 4

학점평균: 4.25

----------------------------------

학생 이름 >> 최찬미

최찬미, 빅데이터, 4, 4.25

학생 이름 >> 이재문

이재문, 안드로이드, 2, 3.9

학생 이름 >> 그만

package 제네릭과컬렉션;
import java.util.*;

class Student{
	private String name;
	private String major;
	private int studentcode;
	private double average;
	
	public Student(String name, String major, int studentcode, double average) {
		this.name=name;
		this.major=major;
		this.studentcode=studentcode;
		this.average=average;
	}
	
	public String getName() {
		return name;
	}
	public String getMajor() {
		return major;
	}
	public int getStudentcode() {
		return studentcode;
	}
	public double getAverage() {
		return average;
	}
}

public class StudentInfo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student[] Student=new Student[4];
		ArrayList<Student> student=new ArrayList<Student>();
		Scanner input=new Scanner(System.in);
		
		System.out.println("학생 이름, 학과, 학번, 학점평균을 입력하세요.");
		for(int i=0;i<4;i++) {
			System.out.print(">> ");
			String s=input.nextLine();
			String p[]=s.split(", ");
			String name = p[0];
			String major = p[1];
			int studentcode = Integer.valueOf(p[2]);
			double average = Double.parseDouble(p[3]); 
			Student[i] = new Student(name, major, studentcode, average);
			student.add(Student[i]);
		}
		
		System.out.println("---------------------------------------");
		
		for(int i=0;i<4;i++) {
			System.out.println("이름: "+Student[i].getName());
			System.out.println("학과: "+Student[i].getMajor());
			System.out.println("학번: "+Student[i].getStudentcode());
			System.out.println("학점평균: "+Student[i].getAverage());
			System.out.println("---------------------------------------");
		}
		
		
		
		while(true) {
			System.out.print("학생 이름 >> ");
			String writtenName=input.next();
			
			if(writtenName.equals("그만"))
				break;
			for(int i=0; i<student.size(); i++) {
				if(writtenName.equals(Student[0].getName())) {
					System.out.println( Student[0].getName()+", "+Student[0].getMajor()+", "+Student[0].getStudentcode()+", "+Student[0].getAverage());
					break;
				}
					
				else if(writtenName.equals(Student[1].getName())) {
					System.out.println( Student[1].getName()+", "+Student[1].getMajor()+", "+Student[1].getStudentcode()+", "+Student[1].getAverage());
					break;
				}
					
				else if(writtenName.equals(Student[2].getName())) {
					System.out.println( Student[2].getName()+", "+Student[2].getMajor()+", "+Student[2].getStudentcode()+", "+Student[2].getAverage());
					break;
				}
					
				else {
					System.out.println( Student[3].getName()+", "+Student[3].getMajor()+", "+Student[3].getStudentcode()+", "+Student[3].getAverage());
				 	break;
				}
					
			}

10.도시 이름, 위도, 경도 정보를 가진 Location 클래스를 작성하고, 도시 이름을 '키'로 하는 HashMap<String, Location> 컬렉션을 만들고, 사용자로부터 입력 받아 4개의 도시를 저장하라. 그리고 도시 이름으로 검색하는 프로그램을 작성하라.

도시, 경도, 위도를 입력하세요.

>> 서울, 37, 126

>> LA, 34, -118

>> 파리, 2, 48

>> 시드니, 151, -33

----------------------------------

서울 37 126

LA 34 -118

파리 2 48

시드니 151 -33

----------------------------------

도시 이름 >> 피리

피리는 없습니다.

도시 이름 >> 파리

파리 2 48

도시 이름 >> 그만

 

*** 얘도 진짜 애먹었음

package 제네릭과컬렉션;

import java.util.*;

public class NationHashMap {
	public static void main(String[] args) {
		HashMap<String, Integer> nations=new HashMap<String, Integer>();
		System.out.println("나라 이름과 인구를 입력하세요. (예: Korea 5000) ");
		Scanner input=new Scanner(System.in);
		
		while(true) {
			System.out.print("나라이름, 인구 >>  ");
			
			String nation = input.next();
			if(nation.equals("그만"))
				break; 
			int population = input.nextInt();
			nations.put(nation, population);
		}
		
		while(true) {
			System.out.print("인구 검색>> ");
			String s=input.next();
			if(s.equals("그만")) {
				break;
			}
			int realpop=nations.get(s);
			if(s==null) //뭐가 문제야 도대체
				System.out.println(s + " 나라는 없습니다.");
			else 
				System.out.println(s+"의 인구는 "+realpop);
		}
	}
}