📌240105 과제 Solution
- 각 과목 총점을 구하는 getTotal() 메소드를 만들지 않아도 된다.
Student 클래스의 생성자를 정의할 때 필드변수로 각 과목의 Total이 누적되도록 작성하면서 효율이 늘었다.
public class Student {
String name;
int kor, eng, math;
static int korTotal, mathTotal, engTotal;
Student(String name, int kor, int eng, int math) {
this.name = name;
this.kor = kor;
this.eng = eng;
this.math = math;
korTotal+=kor;
engTotal+=eng;
mathTotal+=math;
}
}
public class Student_Make_Method_Solution_Array_Sort {
public static void main(String[] args) {
Student[] students = { new Student("강호동", 85, 60, 70),
new Student("이승기", 90, 95, 80),
new Student("유재석", 75, 80, 100),
new Student("하하", 80, 70, 95),
new Student("이광수", 100, 65, 80) };
sort(students);
printStudent(students);
}
private static void printStudent(Student[] students) {
System.out.println("========= 학생별 / 과목별 총점 구하기 =========");
System.out.println("이름\t국어\t영어\t수학\t총점\t평균");
for (Student obj : students) {
printInfo(obj);
}
for (int j = 0; j < 45; j++) {
System.out.print("=");
}
System.out.println();
System.out.printf("%s\t%d\t%d\t%d\t", "총점", Student.korTotal, Student.engTotal, Student.mathTotal);
}
static void printInfo(Student student) {
System.out.print(student.name + "\t" + student.kor + "\t" + student.eng + "\t" + student.math + "\t"
+ student.getTotal() + "\t");
System.out.printf("%.1f\n", student.getAverage());
}
private static void sort(Student[] students) {
for (int i = 0; i < students.length - 1; i++) {
for (int j = i + 1; j < students.length; j++) {
if (students[i].kor+students[i].eng+students[i].math < students[j].kor+students[j].eng+students[j].math) {
Student imsi = students[i];
students[i] = students[j];
students[j] = imsi;
}
}
}
}
}
📌Exception / throw(s)
- new Exception("구체적인 메시지") 객체를 생성한다.
- throw는 에러를 발생시키는 키워드고, throws나 try~catch문으로 처리한다.
- java.lang.ArithmeticException은 new를 통해 선언을 하지 않아도 객체가 생성되는 건가??
- 그렇게 생각하는 이유는, e가 참조변수 역할을 하는 것처럼 보이기 때문이다.
e.getMessage를 보면 참조변수 e를 통해 getMessage() 메서드를 호출하고 있다.
- new로 선언하지 않고도 객체 생성할 수 있는 방법이 singleton에 있지 않았나??
⭐ 이클립스에서 ArithmeticException 위에 마우스를 올려보면, 답을 알 수 있다.
ArithmeticException objects may be constructed by the virtual machine...
즉, 이 클래스의 객체는 JVM에서 생성해주는 것이므로,
사용자는 그 인스턴스의 주소를 가리킬 변수만 지정해주면 된 것이었다!!
class ExceptionExample2_3 {
public static void main(String[] args) {
int num1 = 3, num2 = 0;
try {
int result = num1 / num2;
System.out.println(result);
} catch (java.lang.ArithmeticException e) {
String message = e.getMessage();
System.out.println(message);
}
}
}
class ExceptionExample3_3 {
public static void main(String[] args) {
try {
int result = add(0, -1);
System.out.println(result);
}catch (Exception e) {
System.out.println(e.getMessage());
}
}
static int add(int a, int b) throws Exception {
int result = a + b;
if (result < 0)
throw new Exception("0보다 작아 에러 발생합니다.");
System.out.println("나는 출력이 될까요?");
return result;
}
}
📌간단한 입출금 문제
public class Account {
String accountNo;
String ownerName;
int balance;
Account(String accountNo, String ownerName, int balance) {
this.accountNo = accountNo;
this.ownerName = ownerName;
this.balance = balance;
}
void deposit(int amount) {
this.balance += amount;
}
int withdraw(int amount) throws Exception {
if (balance < amount) {
throw new Exception("잔액이 부족합니다.");
}
balance -= amount;
return amount;
}
}
public class Account_Make {
public static void main(String[] args) {
Account obj = new Account("777-777-7777777", "최대박", 10);
try {
int amount = obj.withdraw(100000000);
System.out.println("인출액:" + amount);
} catch (Exception e) {
String msg = e.getMessage();
System.out.println(msg);
}
}
}
📌상속(Inheritance)
- 기존의 클래스를 재사용해서 새로운 클래스를 작성하는 것이다.
코드의 재사용성을 높이고 코드의 중복을 제거하여 프로그램의 생산성과 유지 보수에 크게 기여하는 기능이다.
class Account {
String accountNo;
String ownerName;
int balance;
Account(String accountNo, String ownerName, int balance) {
this.accountNo = accountNo;
this.ownerName = ownerName;
this.balance = balance;
}
void deposit(int amount) {
this.balance += amount;
}
int withdraw(int amount) throws Exception {
if (balance < amount)
throw new Exception("잔액이 부족합니다.");
balance -= amount;
return amount;
}
}
- 부모클래스로 상속 받은 필드와 메서드를 참조하는 super() 메소드
- 자손클래스의 생성자 선언 첫줄에는 조상클래스의 생성자를 호출해야 한다.
- 조상클래스의 생성자를 호출하면, 조상클래스의 멤버들이 먼저 초기화되어 있어야 한다.
public class CheckingAccount extends Account {
CheckingAccount2(String accountNo,
String ownerName,
int balance,
String cardNo) {
super(accountNo, ownerName, balance);
this.cardNo = cardNo;
int pay(String cardNo, int amount) throws Exception {
if (!cardNo.equals(this.cardNo) || (balance < amount))
throw new Exception("지불이 불가능합니다.");
return super.withdraw(amount);
}
}
📌Method overriding
class Account {
String accountNo;
String ownerName;
int balance;
Account(String accountNo, String ownerName, int balance) {
this.accountNo = accountNo;
this.ownerName = ownerName;
this.balance = balance;
}
void deposit(int amount) {
this.balance += amount;
}
int withdraw(int amount) throws Exception {
if (balance < amount)
throw new Exception("잔액이 부족합니다.");
balance -= amount;
return amount;
}
}
package ex08_04_overriding;
public class CreditLineAccount extends Account {
int creditLine;
CreditLineAccount(String accountNo,
String ownerName,
int balance,
int creditLine) {
super(accountNo, ownerName, balance);
this.creditLine = creditLine;
}
int withdraw(int amount) throws Exception {
if ((balance + creditLine) < amount)
throw new Exception("인출이 불가능합니다.");
balance -= amount;
return amount;
}
}
📌final
- 클래스에 붙으면 상속 등 불가 / 메소드에 붙으면 오버라이딩 등 불가
📌abstract
public abstract class MessageSender {
String title;
String senderName;
MessageSender (String title, String senderName) {
this.title = title;
this.senderName = senderName;
}
abstract void sendMessage(String recipient);
}
class EMailSender extends MessageSender {
String senderAddr;
String emailBody;
EMailSender(String title, String senderName,
String senderAddr, String emailBody) {
super(title, senderName);
this.senderAddr = senderAddr;
this.emailBody = emailBody;
}
void sendMessage(String recipient) {
System.out.println("------------------------------");
System.out.println("제목: " + title);
System.out.println("보내는 사람: " + senderName + "\n"
+ "보낸 주소 : " + senderAddr);
System.out.println("받는 사람: " + recipient);
System.out.println("내용: " + emailBody);
}
}
📌240108 과제 : System.out.println 의미
- Object 클래스를 상속받고 있는 System 클래스는 몇 가지 유용한 필드와 메서드를 포함하고 있다.
Public final로 정의되어 있으므로 인스턴스화 할 수 없다.
- System 클래스의 필드변수인 PrintStream out은 Modifier 설정이 static으로 되어 있으므로,
'클래스명.필드변수(System.out)' 형태로 호출할 수 있다.
- 참조변수인 out을 통해 PrintStream 클래스의 멤버를 참조할 수 있고,
따라서 PrintStream 클래스에 존재하는 인스턴스 메서드인 println()을 호출할 수 있다.
import java.io.PrintStream;
public class Test {
public static void main(String[] args) {
PrintStream out = System.out;
out.println("모니터에 출력합니다");
}
}