7일차 - 생성자

|
package Constructor;

import java.util.Scanner;

/*
 * 문제] 자동차 클래스가 있습니다.
 * 이 클래스에 색상과 가격을 입력하여 출력하세요. (임의적으로).
 * 단, 생성자 이용하여. 
 */

public class constSample2 {
//Field area : class variable, constructor, class method...
	
	static String col; //class variable
	static int pri;

	public void constSample2(String col, int pri) { // constructor
		this.col = col;
		this.pri = pri;
	}
	
	public static String Display() { //class method
		return "색상은 " + col + "이고, 가격은" + pri + "입니다.";
	}
	
	public static void main(String[] args) {
		System.out.println("원하는 자동차의 색상과 가격을 입력하세요.");
		Scanner sc = new Scanner(System.in);
		col = sc.next();
		pri = sc.nextInt();
		constSample2 col = new constSample2(); 
		constSample2 pri = new constSample2();
		System.out.println(col.Display()); //객체명, 클래스 메소드
		System.out.println(pri.Display());
		System.out.println(constSample2.Display()); //클래스명.클래스메소드

	}

}

---------------------------------------------------------

package Constructor;

import java.util.Scanner;

//문제] 책제목, 저자, 출판사, 가격 -> 입력을 생성자로 받아서 출력.
public class constSample3 {
	//class variable
	static String title;
	static String aut;
	static String pub;
	static int pri;
	
	//constructor
	public void constSample3(String title, String aut, String pub, int pri){
		this.title = title;
		this.aut = aut;
		this.pub = pub;
		this.pri = pri;
	}
	
	//method
	public static void output() {
		System.out.println("책제목은 " + title + "이고, 저자는 " + aut + " 이고, 출판사는 " + pub + " 이고, 가격은 " + pri + " 원 입니다.");
	}
	
	private int operation() {
		int sum = pri * 10;
		return sum;
	}


	public static void main(String[] args) {
	//data input
		System.out.println("원하는 책의 제목, 저자, 출판사, 가격을 입력하세요.");
		Scanner sc = new Scanner(System.in);
		title = sc.next();
		aut = sc.next();
		pub = sc.next();
		pri = sc.nextInt();
		
		constSample3 cs3 = new constSample3();
		cs3.output();
		cs3.operation();

	}


}
--------------------------------------------------------------------

package Constructor;

import java.util.Scanner;

//문제] 저축은행이 있습니다. 이 은행에 계좌 개설하고, 저금을 합니다.
// 1. 계좌생성
// 2. 저축 : 1000 + 2000 + 3000
// 3. 출금 : 500
// 4. 잔액 : 5500
// 5. 입금액에 대한 체크, 출금액도 체크 합니다.
// 생성자 이용하여 코딩.
// 

class Account{
	
	int money;
	int amount;
	
	public Account(int money) { //초기 계좌 개설
		this.money = money;
	}
	
	public void deposit(int money) { // 저축입금
		if(amount>0)
			money+=amount;
	}
	
	public void withdraw(int amount) { //출금
		if(amount > 0 && money - amount >= 0)
		money-=amount;
	}
	
	public double getMoney() { // 잔액반환
		return money;
	}
	
}

public class constSample4 {

	public static void main(String[] args) {
		
		Account ac = new Account(1000);
		ac.deposit(2000); //입금
		ac.deposit(3000); //입금
		ac.withdraw(500); //출금
		System.out.println(ac.money); //잔액
		System.out.println(ac.getMoney()); //잔액

	}

}

--------------------------------------------------------------------

package Constructor;

import java.util.Scanner;

/* 생성자 오버로딩
 * 인자형과 갯수에 따라서 구분합니다.
 */

class OverLoad {
	
	static int kor, math, age; //private : 클래스 변수의 접근에 대한 해제.
	static double eng;
	static String hakjum, name, addr;
	
	public OverLoad() {
		//default
	}
	
	public OverLoad(int kor) {
		this.kor = kor;
	}
	
	public OverLoad(double eng) {
		this.eng = eng;
	}
	
	public OverLoad(int kor, double eng) {
		this.kor = kor;
		this.eng = eng;
	}
	
	public OverLoad(int kor, int math) {
		this.kor = kor;
		this.math = math;
	}
	
	public OverLoad(int kor, double eng, int math) {
		this.kor = kor;
		this.eng = eng;
		this.math = math;
	}
	
	public OverLoad(int kor, double eng, int math, String hakjum) {
		this.kor = kor;
		this.eng = eng;
		this.math = math;
		this.hakjum = hakjum;
	}
	
	public OverLoad(String name, int age, String addr) {
		this.name = name;
		this.age = age;
		this.addr = addr;
	}
}

public class constSample5 { //main class

	public static void main(String[] args) {
		
		//문제] 국어, 영어, 수학 점수, 학점을 입력받아서 처리하되, 오버로딩으로 구현해 보세요.
		
		Scanner sungjuk = new Scanner(System.in);
		int kor = sungjuk.nextInt();
		double eng = sungjuk.nextDouble();
		int math = sungjuk.nextInt();
		String hakjum = sungjuk.next();
		
		OverLoad over1 = new OverLoad(); //sub class
		OverLoad over2 = new OverLoad(kor);
		OverLoad over3 = new OverLoad(eng);
		OverLoad over4 = new OverLoad(kor, eng);
		OverLoad over5 = new OverLoad(kor, math);
		OverLoad over6 = new OverLoad(kor, eng, math);
		OverLoad over7 = new OverLoad(kor, eng, math, hakjum);
		
		System.out.println("===생성자 오버로딩 출력===");
		System.out.println("1. 오브젝트를 이용한 출력");
		System.out.println("Over7에 대한 국어 출력 : " + over7.kor);
		System.out.println("Over7에 대한 영어 출력 : " + over7.eng);
		System.out.println("Over7에 대한 수학 출력 : " + over7.math);
		System.out.println("Over7에 대한 학점 출력 : " + over7.hakjum);
		System.out.println("2. 클래스 이름를 이용한 출력");
		System.out.println("국어: " +OverLoad.kor);
		System.out.println("영어: " +OverLoad.eng);
		System.out.println("수학: " +OverLoad.math);
		System.out.println("학점: " +OverLoad.hakjum);
		
		//문제] 이름과 나이, 주소를 입력받아서, 생성자 오버로딩 로 저장하고, 출력해 보세요.
		
		System.out.println("이름, 나이, 주소를 입력하세요.");
		Scanner sc = new Scanner(System.in);
		String name = sc.next();
		int age = sc.nextInt();
		String addr = sc.next();
		
		OverLoad ol = new OverLoad(name, age, addr);
		
		System.out.println("===생성자 오버로딩 출력===");
		System.out.println("1. 오브젝트를 이용한 출력");
		System.out.println("이름 : " + ol.name);
		System.out.println("나이 : " + ol.age);
		System.out.println("주소 : " + ol.addr);
		System.out.println("2. 클래스 이름를 이용한 출력");
		System.out.println("이름 : " + OverLoad.name);
		System.out.println("나이 : " + OverLoad.age);
		System.out.println("주소 : " + OverLoad.addr);
		
	}

}

'Bitcamp > BITCAMP - Java' 카테고리의 다른 글

7일차 - 과제  (0) 2019.07.02
7일차 - 중요메소드  (0) 2019.07.02
6일차 - 과제  (0) 2019.07.02
6일차 - 생성자  (0) 2019.07.01
6일차 - 메소드 연습6  (0) 2019.07.01
And