Person
package com.mywork.ex;
public class Person {
String name;
int age;
double height;
char gender;
Person() { }
Person(String name, int age, double height, char gender){
this.name = name;
this.age = age;
this.height = height;
this.gender = gender;
}
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];
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 {
String name;
String dept;
String score1;
String score2;
double average;
boolean isPass;
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();
}
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);
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 {
Student[] arr;
int idx;
int studentNum;
Scanner scanner = new Scanner(System.in);
StudentManager(int numOfStudents){
arr = new Student[numOfStudents];
for(int i = 0; i < arr.length; i++) {
arr[i] = new Student();
}
}
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 {
int width;
int height;
Triangle(){}
Triangle(int width, int height){
this.width = width;
this.height = height;
}
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];
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);
}
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];
for(int i = 0; i < arr.length; i++) {
arr[i] = new Triangle();
}
for(Triangle semo : arr) {
System.out.print("삼각형 너비 입력 >> ");
semo.width = scanner.nextInt();
System.out.println("삼각형 높이 입력 >> ");
semo.height = scanner.nextInt();
}
for(Triangle semo : arr) {
semo.output();
}
scanner.close();
}
}