클래스 오답노트

Java

목록 보기
8/26
post-thumbnail

1.

public class Student {

	String name;
	int ban;
	int no;
	int kor;
	int eng;
	int math;
	
	Student(String name, int ban, int no, int kor, int eng, int math) {
		this.name = name;
		this.ban = ban;
		this.no = no;
		this.kor = kor;
		this.eng = eng;
		this.math = math;
	}
	
	
	int getTotal() {
		return kor + eng + math;
	}
	
	int getAverage() {
		return  (kor + eng + math)/3  ;
	}
	
	String info() {
		return this.name+this.ban+this.no+this.kor+this.eng+this.math;
	}
	
}

내가 틀린 부분은 여기서

String info() {
		return this.name+this.ban+this.no+this.kor+this.eng+this.math;

this를 붙여야 그 객체의 값이 return되는지 몰랐음
그리고 retun이 아니라 System.out.println( ) 으로 하려는했는데 이것도 가능함! 단, String 이 아니라 void로 했으면 됐음.

🧃 숫자의 각 자릿수가 각 0~9보다 큰지 확인하는 방법

int a = 123456;
String b = 123456 + "";
char ch = b.charAt(0);
ch > 0 또는 ~9 ;     

2. 싱글톤 만들기

public class ShopService {	
	private static ShopService singleton = new ShopService();
	
	private ShopService() {}

	public static ShopService getInstance() {
		return singleton;
	}
}
public class ShopServicExample {
	public static void main(String[] args) {
		ShopService obj1 = ShopService.getInstance();
		ShopService obj2 = ShopService.getInstance();
	}
}

3. 유효한 값만 set하라

	private int balance;
	public static final int MAX_BALANCE = 1000000;
	public static final int MIN_BALANCE = 0;

	public int getBalance() {
		return balance;
	}

	public void setBalance(int balance) {
		if (balance >=0 && balance <1000000) {
		this.balance =  balance;}
	}
	
	public void setBalance2(int balance) {
		if (balance<Account.MIN_BALANCE || 
		balance>Account.MAX_BALANCE) { 
			return;  ❗❗❗❗❗❗❗❗❗❗❗❗❗❗❗❗❗❗❗
		} else {	
		}
	}
public class AccountExmaple {

	public static void main(String[] args) {
		Account account = new Account();
		
		account.setBalance(1000);
		System.out.println("현재 잔고: "+ account.getBalance());
		account.setBalance(-100);
		System.out.println("현재 잔고: "+ account.getBalance());
		account.setBalance(2000);
		System.out.println("현재 잔고: "+ account.getBalance());

	}

}

4. 클래스 변수 만들기 //초기화

public class StudentService {
	
	Student stu;
	
	Scanner input = new Scanner(System.in);
	
	🔴//생성자 : 멤버 필드 초기화
	StudentService() {
		stu = new Student();
	}
	🔴 나는 여기서 Student stu = new Student(); 해서 틀림.
    Student stu 이 부분은 이미 위에 선언되어있음.
    
	void insertInfo() {
		String name = input.nextLine();
		int age = input.nextInt();
		stu.name = name;
		stu.age = age;
	}
	
	void insertScore() {
		int math = input.nextInt();
		int eng = input.nextInt();
		int kor = input.nextInt();
		stu.math = math;
		stu.eng = eng;
		stu.kor = kor;
	}

	void printStu(){
		System.out.println(stu.name + stu.age);
	}
}

0개의 댓글