티스토리 뷰
오늘 예정 진도 -> static멤버 전까지
package com;
public class FirstClass {
public static void main(String[] args) {
Student st1 = new Student("부엉이", 2020158004, 4.0);
st1.introduce();
st1.study();
System.out.println(st1.getGpa());
System.out.println(st1.setAndGetName("씨부엉"));
st1.introduce();
st1.study();
st1.setGpa(st1.getGpa() + 0.5);
System.out.println(st1.getGpa());
st1.name = "부엉부엉이";
st1.introduce();
Student st2 = new Student();
st2.introduce();
System.out.println();
School school = new School();
school.makeStudentArray(3);
school.addStudent(st1);
school.addStudent(st2);
school.setClassroom("부엉s");
Student[] studentArray = school.getStudent();
studentArray[2] = new Student("김승진", 1234567890, 10.0);
school.printAll();
School tukorea = new School((school.getStudent()), school.getClassroom());
System.out.println();
System.out.println(tukorea.getAllGpa()[0]);
for(double d : tukorea.getAllGpa()){
System.out.print(d + " ");
}
System.out.println();
System.out.println();
Student[] sArray = new Student[10];
for(int i = 0; i < sArray.length; i++){
sArray[i] = new Student("학생" + i, i + 1234567890, i * 1.0);
}
tukorea.setStudent(sArray);
tukorea.printAll();
}
}
package com;
public class Student {
public String name;
private int studentId;
private double gpa;
public Student(String name, int studentId, double gpa) {
this.name = name;
this.studentId = studentId;
this.gpa = gpa;
}
public Student(){}
public void introduce() {
System.out.println("내 학번은 " + studentId + "이고 " + name + "이라고 해");
}
public void study() {
System.out.println(name + " 공부중");
}
public String setAndGetName(String name) {
this.name = name;
return this.name;
}
public double getGpa() {
return gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
}
package com;
public class School {
private Student[] student;
private String classroom;
private int index = 0;
public School(){}
public School(Student[] student, String classroom) {
this.student = student;
this.classroom = classroom;
}
public void makeStudentArray(int length){
student = new Student[length];
}
public Student[] getStudent(){
return this.student;
}
public void setClassroom(String classroom){
this.classroom = classroom;
}
public String getClassroom(){
return this.classroom;
}
public void setStudent(Student[] studentArray){
this.student = studentArray;
}
public void addStudent(Student student){
if (this.student.length > index)
this.student[index++] = student;
}
public void printAll(){
System.out.println("반 이름은: " + classroom);
for (Student s : student) {
s.introduce();
}
}
public double[] getAllGpa(){
double[] gpa = new double[student.length];
for(int i = 0; i < student.length; i++){
gpa[i] = student[i].getGpa();
}
return gpa;
}
}
같은 패키지(com) 아래에 SpecialClass 클래스를 하나 만들어주세요!
package com;
public class SpecialClass {
public static void main(String[] args) {
InnerClass innerClass = new InnerClass();
innerClass.print();
}
private static class InnerClass {
InnerClass(){
System.out.println("내부 클래스 생성");
}
private void print(){
System.out.println("I'm a class which locate inside of the SpecialClass!");
class InnerInnerClass {
void print(){
System.out.println("Hello");
}
}
InnerInnerClass innerInnerClass = new InnerInnerClass();
innerInnerClass.print();
}
}
}
'멘토링 준비 자료 > 자바 스터디 멘토링' 카테고리의 다른 글
자바 스터디 4주차 - 클래스와 객체, 상속 (0) | 2022.04.01 |
---|---|
자바 스터디 2주차 - 반복문과 배열, 예외처리 (0) | 2022.03.21 |
자바 스터디 1주차 - 자바 기본 프로그래밍_2 (0) | 2022.03.10 |
자바 스터디 1주차 - 자바 기본 프로그래밍_1 (0) | 2022.03.10 |
자바 스터디 1주차 - 자바 개요 (0) | 2022.03.09 |