본문 바로가기
Language/JAVA

JAVA 11일차 - 실습 문제 01

by 코젼 2022. 7. 4.
728x90
반응형

2022-07-04(11일차)

💾 소스코드

실습문제.docx
0.02MB
Report01.zip
0.01MB


🗒️입력한 문자열을 역순으로 출력되게 하시오.

💬 조건

▪️ 모든 영문자를 대문자로 바꾼다.

 

📝 CharTest 클래스

// java.util 패키지에 있는 Scanner 클래스를 사용하기 위해 import를 통해 호출한다.
import java.util.Scanner;

// CharTest 클래스 선언
public class CharTest { // CharTest 클래스 시작점 지정
	public static void main(String[] args) { // main 메서드 시작점 지정
		Scanner s = new Scanner(System.in);
		String str = s.next();
		str = str.toUpperCase();
		char[] arr = new char[str.length()];
		
		for(int i=0; i<str.length(); i++) {
			arr[i] = str.charAt((str.length()-1)-i);
			System.out.print(arr[i]);
		} // 반복문 for문의 끝을 알려줌
	} // main메서드의 끝을 알려줌
} // CharTest 클래스의 끝을 알려줌

 


🗒️ 학생의 정보에 대한 예제

💬 조건

▪️ 나이의 평균

▪️ 신장의 평균

▪️ 몸무게의 평균

▪️ 나이가 가장 많은 학생, 나이가 가장 적은 학생

▪️ 신장이 가장 많은 학생, 신장이 가장 적은 학생

▪️ 몸무게가 가장 많은 학생, 몸무게가 가장 적은 학생

name 나이 신장 몸무게
홍길동 15 170 80
한사람 13 180 70
임걱정 16 175 65

 

📝 Student 클래스

// Student 클래스 선언
public class Student { // Student 클래스 시작점 지정
	// String형 name지역변수 선언
	String name;
	// int형 age지역변수 선언
	int age;
	// int형 height지역변수 선언
	int height;
	// int형 weight지역변수 선언
	int weight;
	
	// Student 클래스명과 동일하고 매개변수가 없는 생성자를 정의한다.
	Student() {}
	// Student 클래스명과 동일하고 String형 name매개변수, int형 age매개변수, int형 height매개변수, int형 weight매개변수를 가지는 생성자를 정의한다.
	Student(String name, int age, int height, int weight) { // 생성자 Student의 시작점 지정
		// this키워드를 이용하여 Student 클래스가 가지고 있는 name지역변수에 name 매개변수 값을 지정한다.
		this.name = name;
		// this키워드를 이용하여 Student 클래스가 가지고 있는 age지역변수에 age 매개변수 값을 지정한다.
		this.age = age;
		// this키워드를 이용하여 Student 클래스가 가지고 있는 height지역변수에 height 매개변수 값을 지정한다.
		this.height = height;
		// this키워드를 이용하여 Student 클래스가 가지고 있는 weight지역변수에 weight 매개변수 값을 지정한다.
		this.weight = weight;
	} // 생성자 Student의 끝을 알려줌
	
	// 반환타입이 void형이고 String형 name매개변수를 가지는 setName메소드를 정의한다.
	void setName(String name) { // setName메소드 시작점 지정
		// this키워드를 이용하여 Student 클래스가 가지고 있는 name지역변수에 name 매개변수 값을 지정한다.
		this.name = name;
	} // setName메소드의 끝을 알려줌
	// 반환타입이 void형이고 int형 age매개변수를 가지는 setAge메소드를 정의한다.
	void setAge(int age) { // setAge메소드 시작점 지정
		// this키워드를 이용하여 Student 클래스가 가지고 있는 age지역변수에 age 매개변수 값을 지정한다.
		this.age = age;
	} // setAge메소드의 끝을 알려줌
	// 반환타입이 void형이고 int형 height매개변수를 가지는 setHeight메소드를 정의한다.
	void setHeight(int height) { // setHeight메소드 시작점 지정
		// this키워드를 이용하여 Student 클래스가 가지고 있는 height지역변수에 height 매개변수 값을 지정한다.
		this.height = height;
	}// setHeight메소드의 끝을 알려줌
	// 반환타입이 void형이고 int형 weight매개변수를 가지는 setWeight메소드를 정의한다.
	void setWeight(int weight) { // setWeight메소드 시작점 지정
		// this키워드를 이용하여 Student 클래스가 가지고 있는 weight지역변수에 weight 매개변수 값을 지정한다.
		this.weight = weight;
	}// setWeight메소드의 끝을 알려줌
	// 반환타입이 String형이고 매개변수가 없는 getName메소드를 정의한다.
	String getName() { // getName메소드 시작점 지정
		// this키워드를 이용하여 Student 클래스가 가지고 있는 name변수를 return한다.
		return this.name;
	}// getName메소드의 끝을 알려줌
	// 반환타입이 int형이고 매개변수가 없는 getAge메소드를 정의한다.
	int getAge() { // getAge메소드 시작점 지정
		// this키워드를 이용하여 Student 클래스가 가지고 있는 age변수를 return한다.
		return this.age;
	}// getAge메소드의 끝을 알려줌
	// 반환타입이 int형이고 매개변수가 없는 getHeight메소드를 정의한다.
	int getHeight() { // getHeight메소드 시작점 지정
		// this키워드를 이용하여 Student 클래스가 가지고 있는 height변수를 return한다.
		return this.height;
	}// getHeight메소드의 끝을 알려줌
	// 반환타입이 int형이고 매개변수가 없는 getWeight메소드를 정의한다.
	int getWeight() { // getWeight메소드 시작점 지정
		// this키워드를 이용하여 Student 클래스가 가지고 있는 weight변수를 return한다.
		return this.weight;
	}// getWeight메소드의 끝을 알려줌
	// 반환타입이 String형이고 매개변수가 없는 studentInfo메소드를 정의한다.
	String studentInfo() { // studentInfo메소드 시작점 지정
		// this키워드를 이용하여 Student 클래스가 가지고 있는 name변수, age변수, height변수, weight변수를 포함한 문자열 return한다.
		return (this.name + "\t" +
				this.age + "\t" +
				this.height + "\t" +
				this.weight);
	}// studentInfo메소드의 끝을 알려줌
} // Student 클래스의 끝을 알려줌

📝 StudentTest 클래스

// StudentTest 클래스 선언
class StudentTest { // StudentTest 클래스 시작점 지정
	// static을 이용하여 동적으로 동작하게 하고 int형 MAX_NUM이 3인 상수를 정의한다.
	static final int MAX_NUM = 3;
	
	// String형 1차원 배열을 이용하여 main()을 선언
	public static void main(String[] args) { // main 메서드 시작점 지정
		// studentArray라는 객체 이름을 이용하여 new 키워드를 이용하여 크기가 [MAX_NUM]인 Student배열 객체를 생성한다.
		Student[] studentArray = new Student[MAX_NUM];
		
		// {"홍길동", "한사람", "임걱정"}으로 이루어진 String형 name배열을 선언한다.
		String[] name = {"홍길동", "한사람", "임걱정"};
		// {15, 13, 16}으로 이루어진 int형 age배열을 선언한다.
		int[] age = {15, 13, 16};
		// {170, 180, 175}으로 이루어진 int형 height배열을 선언한다.
		int[] height = {170, 180, 175};
		// {80, 70, 65}으로 이루어진 int형 weight배열을 선언한다.
		int[] weight = {80, 70, 65};
		
		// double형 age_average, age_sum 선언
		double age_average = 0.0, age_sum = 0.0;
		// double형 height_average, height_sum 선언
		double height_average = 0.0, height_sum = 0.0;
		// double형 weight_average, weight_sum 선언
		double weight_average = 0.0, weight_sum = 0.0;
		// System 클래스의 out 객체에 있는 println메서드를 사용하여 문자열을 출력한다.
		System.out.println("이름\t나이\t신장\t몸무게");
		
		// 반복문 for문을 사용하여 studentArray배열에 name, age, height, weight 배열의 값을 모두 넣는다.
		// 초기식 : 지역변수 i를 선언하고 값을 0으로 초기화한다.
		// 조건식 : i가 studentArray변수의 문자열의 길이가 될 때까지만 for문이 진행된다.
		// 증감식 : 블록 안이 모두 실행되고 난 후, i를 1씩 증가시킨다.(i++)
		for(int i=0; i<studentArray.length; i++) { // for문 시작점 지정
			// Student 객체를 3개 생성하여 배열에 넣는다.
			// 매개변수의 값은 각각 위에서 선언했던 name, age, height, weight 배열의 값이다.
			studentArray[i] = new Student(name[i], age[i], height[i], weight[i]);
			// System 클래스의 out 객체에 있는 println메서드를 사용하여 문자열을 출력한다.
			// 배열에 있는 객체 정보를 studentInfo 메소드 사용 하여 출력한다.
			System.out.println(studentArray[i].studentInfo());
			// age_sum지역변수에 각각의 age배열의 값을 더하여 저장한다.
			age_sum += age[i];
			// height_sum지역변수에 각각의 height배열의 값을 더하여 저장한다.
			height_sum += height[i];
			// weight_sum지역변수에 각각의 weight배열의 값을 더하여 저장한다.
			weight_sum += weight[i];
		}// 반복문 for문의 끝을 알려줌
		// Student 객체들의 나이의 평균 출력
		// Math.round()를 이용하여 소수점 이하 4째 자리에서 반올림하고 3째자리까지 표현한다.
		age_average = Math.round((age_sum / MAX_NUM)*1000)/1000.0;
		System.out.println("\n나이 평균 : " + age_average);
		// Student 객체들의 신장의 평균 출력
		height_average = Math.round((height_sum / MAX_NUM)*100)/100.0;
		System.out.println("신장 평균 : " + height_average);
		// Student 객체들의 몸무게의 평균 출력
		weight_average = Math.round((weight_sum / MAX_NUM)*1000)/1000.0;
		System.out.println("몸무게 평균 : " + weight_average);
		
		// 학생들 중 나이가 가장 적은 학생과 많은 학생을 출력
		for(int i=0; i<studentArray.length-1; i++) { // for문 시작점 지정
			for(int j=i; j<studentArray.length; j++) { // 이중 for문 시작점 지정
				int temp1, temp2, temp3;
				String temp4;
				// age배열 중 나이의 값을 비교하고, 값이 크다면 뒤의 값과 변경한다.
				if(age[i] > age[j]) { // if문 시작점 지정
					// if문에 따른 나이 변경
					temp1 = age[i];
					age[i] = age[j];
					age[j] = temp1;
				
					// 나이에 따른 신장 변경
					temp2 = height[i];
					height[i] = height[j];
					height[j] = temp2;
					
					// 나이에 따른 몸무게 변경
					temp3 = weight[i];
					weight[i] = weight[j];
					weight[j] = temp3;
					
					// 나이에 따른 이름 변경
					temp4 = name[i];
					name[i] = name[j];
					name[j] = temp4;
				}// if문의 끝을 알려줌
			} // 이중 for문의 끝을 알려줌
		}// for문의 끝을 알려줌
		System.out.println("\n나이가 가장 많은 학생 : " + name[studentArray.length-1]);
		System.out.println("나이가 가장 적은 학생 : " + name[0]);
		
		// 학생들 중 신장이 가장 적은 학생과 큰 학생을 출력
		for(int i=0; i<studentArray.length-1; i++) { // for문 시작점 지정
			for(int j=i; j<studentArray.length; j++) { // 이중for문 시작점 지정
				int temp1, temp2, temp3;
				String temp4;
				// height배열 중 신장의 값을 비교하고, 값이 크다면 뒤의 값과 변경한다.
				if(height[i] > height[j]) { // if문 시작점 지정
					// 신장에 따른 나이 변경
					temp1 = age[i];
					age[i] = age[j];
					age[j] = temp1;
				
					// if문에 따른 신장 변경
					temp2 = height[i];
					height[i] = height[j];
					height[j] = temp2;
					
					// 신장에 따른 몸무게 변경
					temp3 = weight[i];
					weight[i] = weight[j];
					weight[j] = temp3;

					// 신장에 따른 이름 변경
					temp4 = name[i];
					name[i] = name[j];
					name[j] = temp4;
				}// if문의 끝을 알려줌
			}// 이중 for문의 끝을 알려줌
		}// for문의 끝을 알려줌
		
		System.out.println("\n신장이 가장 긴 학생 : " + name[studentArray.length-1]);
		System.out.println("신장가 가장 짧은 학생 : " + name[0]);
		
		// 학생들 중 몸무게가 가장 적은 학생과 많이 나가는 학생 을 출력
		for(int i=0; i<studentArray.length-1; i++) { // for문 시작점 지정
			for(int j=i; j<studentArray.length; j++) {// 이중 for문 시작점 지정
				int temp1, temp2, temp3;
				String temp4;
				if(weight[i] > weight[j]) {// if문 시작점 지정
					temp1 = age[i];
					age[i] = age[j];
					age[j] = temp1;
				
					temp2 = height[i];
					height[i] = height[j];
					height[j] = temp2;
					
					temp3 = weight[i];
					weight[i] = weight[j];
					weight[j] = temp3;

					temp4 = name[i];
					name[i] = name[j];
					name[j] = temp4;
				}// if문의 끝을 알려줌
			}// 이중for문의 끝을 알려줌
		}// for문의 끝을 알려줌
		
		System.out.println("\n몸무게가 가장 많이 나가는 학생 : " + name[studentArray.length-1]);
		System.out.println("몸무게가 가장 많이 나가는 학생 : " + name[0]);
	}
}

💻 실행 화면

728x90
반응형

댓글