Part10 - reference_array

uglyduck.dev·2020년 9월 27일
0

예제로 알아보기 👀

목록 보기
10/22
post-thumbnail
post-custom-banner

Person

package com.mywork.ex;

public class Person {
	// Field
	String name;
	int age;
	double height;
	char gender;
	
	// Constructor
	Person() { }
	Person(String name, int age, double height, char gender){
		this.name = name;
		this.age = age;
		this.height = height;
		this.gender = gender;
	}
	
	// Method
	void output() {
		System.out.println("성명 : " + name);
		System.out.println("나이 : " + age);
		System.out.println("신장 : " + height);
		System.out.println("성별 : " + gender);
	}
}

PersonMain

package com.mywork.ex;

public class PersonMain {

	public static void main(String[] args) {
		Person[] people = new Person[3];
		// people : 참조변수(0x10)
		// 0x10 : people[0], people[1], people[2]
		
		for(int i = 0; i < people.length; i++) {
			people[i] = new Person();
		}
		
		people[0].name = "홍길동";
		people[0].age = 20;
		people[0].height = 168.5;
		people[0].gender = '남';
		
		people[0].output();
		
	}

}

Student

package com.mywork.ex;

public class Student {

	// Field
	String name;
	String dept;
	String score1;
	String score2;
	double average;
	boolean isPass;
	
	// Constructor
	Student(){}
	Student(String name, String dept, String score1, String score2){
		this.name = name;
		this.dept = dept;
		this.score1 = score1;
		this.score2 = score2;
		this.average = getAverage();
		this.isPass = getPass();
	}
	
	// Method
	double getAverage() {
		double a = Double.parseDouble(score1);
		double b = Double.parseDouble(score2);
		return (a + b) / 2;
	}
	boolean getPass() {
		return getAverage() >= 80 ? true : false;
	}
	void output() {
		System.out.println("이름 : " + name);
		System.out.println("학과 : " + dept);
		System.out.println("평균 : " + average);
		System.out.println("합격 유무 : " + (isPass ? "합격" : "불합격"));
	}
}

StudentMain

package com.mywork.ex;


public class StudentMain {

	public static void main(String[] args) {
		StudentManager manager = new StudentManager(3);
		
		//Student student = manager.input();
		//manager.addNewStudent(student);
		
		manager.addNewStudent(manager.input());
		manager.addNewStudent(manager.input());
		manager.addNewStudent(manager.input());
		
		manager.outputAllStudents();
		
		manager.outputAverage();
		
		manager.output(manager.findStudent());
		// 찾을 학생의 이름 >> 나길동
		// 나길동 정보 출력
		
	}

}

StudentManager

package com.mywork.ex;
import java.util.Scanner;

public class StudentManager {
	
	// Field
	Student[] arr;
	int idx;
	int studentNum;
	
	Scanner scanner = new Scanner(System.in);
	
	// Constructor
	StudentManager(int numOfStudents){
		arr = new Student[numOfStudents];
		for(int i = 0; i < arr.length; i++) {
			arr[i] = new Student();
		}
	}
	
	// Method
	Student input() {
		System.out.print("학생 성명 >> ");
		String name = scanner.nextLine();
		System.out.print("학생 학과 >> ");
		String dept = scanner.nextLine();
		System.out.print("중간 점수 >> ");
		String score1 = scanner.nextLine();
		System.out.print("기말 점수 >> ");
		String score2 = scanner.nextLine();
		return new Student(name, dept, score1, score2);
	}
	void addNewStudent(Student student) {
		arr[idx++] = student;
	}
	void outputAllStudents() {
		for(int i = 0; i < idx; i++) {
			System.out.println((i + 1) + "번째 학생 정보 ----------");
			arr[i].output();
		}
	}
	void outputAverage() {
		double total = 0;
		for(int i = 0; i < idx; i++) {
			total += arr[i].getAverage();
		}
		System.out.println("학생" + idx + "명의 총 평균은 " + (total / idx) + "입니다.");
	}
	Student findStudent(){
		System.out.print("찾을 학생 이름 입력 >> ");
		String name = scanner.nextLine();
		for(int i = 0; i < idx; i++) {
			if(name.equals(arr[i].name)) {
				return arr[i];
			}
		}
		return null;
	}
	void output(Student student) {
		if(student != null) {
			student.output();
		}else{
			System.out.println("찾는 학생이 존재하지 않습니다.");
		}
	}
}

Triangle

package com.mywork.ex;

public class Triangle {
	// Field
	int width;
	int height;
	
	// Constructor
	Triangle(){}
	Triangle(int width, int height){
		this.width = width;
		this.height = height;
	}
	
	// Method
	double calcArea() {
		return width * height / 2.0;
	}
	void output() {
		System.out.println("너비 : " + width);
		System.out.println("높이 : " + height);
		System.out.println("크기 : " + calcArea());
	}
}

TriangleMain

package com.mywork.ex;

import java.util.Scanner;

public class TriangleMain {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		Triangle[] arr = new Triangle[3];  // Triangle 을 3개 생성하겠다.
		
		// 일반 for 문 입력(객체 내용의 수정)
		for(int i = 0; i < arr.length; i++) {
			System.out.print("삼각형 너비 입력 >> ");
			int width = scanner.nextInt();
			System.out.println("삼각형 높이 입력 >> ");
			int height = scanner.nextInt();
			arr[i] = new Triangle(width, height);  // 실제 Triangle 객체의 생성!
		}
		
		// 일반 for 문 출력(객체 내용의 수정은 없다.)
		for(int i = 0; i < arr.length; i++) {
			System.out.println((i + 1) + "번째 삼각형 ----- ");
			arr[i].output();
		}
		scanner.close();
	}

}

TriangleMain2

package com.mywork.ex;

import java.util.Scanner;

public class TriangleMain2 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		Triangle[] arr = new Triangle[3];  // Triangle 을 3개 생성하겠다.
		
		for(int i = 0; i < arr.length; i++) {
			arr[i] = new Triangle();  // 실제 Triangle 객체의 생성! 
		}
		
		// for - each 문 입력(객체 내용의 수정이 있다.)
		for(Triangle semo : arr) {
			System.out.print("삼각형 너비 입력 >> ");
			semo.width = scanner.nextInt();
			System.out.println("삼각형 높이 입력 >> ");
			semo.height = scanner.nextInt();
		}
		
		// for - each 문 출력(객체 내용의 수정은 없다.)
		for(Triangle semo : arr) {
			semo.output();
		}
		scanner.close();
	}

}
profile
시행착오, 문제해결 그 어디 즈음에.
post-custom-banner

0개의 댓글