11.1 ๊ฐ’์ด ์—†๋Š” ์ƒํ™ฉ์„ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ๊นŒ?

public class Person {
	private Car car;
    public Car getCar(){ return car;}
}

public class Car {
	private Insurance insurance;
    public Insurance getInsurance(){ return insurance;}
}

public class Insurance{
	private String name;
    public String getName(){ return name;}
}

์œ„์™€ ๊ฐ™์ด ์ž๋™์ฐจ์™€ ์ž๋™์ฐจ ๋ณดํ—˜์„ ๊ฐ–๊ณ  ์žˆ๋Š” ์‚ฌ๋žŒ ๊ฐ์ฒด๋ฅผ ์ค‘์ฒฉ ๊ตฌ์กฐ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค๊ณ  ํ•˜์ž.

 

public String getCarInsurance(Person person){
	return person.getCar().getInsurance().getName();
}
  • ์ฐจ๋ฅผ ์†Œ์œ ํ•˜์ง€ ์•Š์€ ์‚ฌ๋žŒ์ด ์žˆ์ง€๋งŒ getCar์„ ํ˜ธ์ถœํ•˜๋ฉด
  • person์ด null์ด๋ผ๋ฉด
  • getInsurance๊ฐ€ null์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋ฉด

 

11.1.1 ๋ณด์ˆ˜์ ์ธ ์ž์„ธ๋กœ NullPointerException ์ค„์ด๊ธฐ

NullPointerException์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด์„œ..

null ํ™•์ธ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€

if (person != null) {
	Car car = person.getCar();
    if (car != null) {
    	...

๋ฐ˜๋ณต ํŒจํ„ด ์ฝ”๋“œ๋ฅผ ๊นŠ์€ ์˜์‹ฌ์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์ฝ”๋“œ์˜ ๊ตฌ์กฐ๊ฐ€ ์—‰๋ง์ด๋˜๊ณ  ๊ฐ€๋…์„ฑ๋„ ๋–จ์–ด์ง„๋‹ค.

 

 

if (person == null) {
	return "Unknown"
}
Car car = person.getCar();
if (car == null){
	...

์œ„ ์ฒ˜๋Ÿผ ์กฐ๊ธˆ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ if ๋ธ”๋ก์„ ์—†์• ๊ณ  null ๋ณ€์ˆ˜๊ฐ€ ์žˆ์œผ๋ฉด ์ฆ‰์‹œ "Unknown"์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

ํ•˜์ง€๋งŒ ์ถœ๊ตฌ๊ฐ€ ๋งŽ์•„์ ธ ์œ ์ง€๋ณด์ˆ˜์— ์–ด๋ ค์›€์ด ์žˆ๋‹ค.

 

11.1.2 null ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ

  • ์—๋Ÿฌ์˜ ๊ทผ์›์ด๋‹ค.
  • ์ฝ”๋“œ๋ฅผ ์–ด์ง€๋ŸฝํžŒ๋‹ค.
  • ์•„๋ฌด ์˜๋ฏธ๊ฐ€ ์—†๋‹ค.
  • ์ž๋ฐ”์˜ ์ฒ ํ•™์— ์œ„๋ฐฐ๋œ๋‹ค - ๋ชจ๋“  ํฌ์ธํ„ฐ๋ฅผ ์ˆจ๊ฒผ์ง€๋งŒ nullํฌ์ธํ„ฐ๋Š” ์˜ˆ์™ธ๋‹ค.
  • ํ˜•์‹ ์‹œ์Šคํ…œ์— ๊ตฌ๋ฉ์„ ๋งŒ๋“ ๋‹ค. - null์ด ์–ด๋–ค ์˜๋ฏธ๋กœ ์‚ฌ์šฉ๋˜์—ˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์—†๋‹ค.

 

 

11.2 Optional ํด๋ž˜์Šค ์†Œ๊ฐœ

java.util.Optional<T> ๋ผ๋Š” ์ƒˆ๋กœ์šด ํด๋ž˜์Šค ์ œ๊ณตํ•œ๋‹ค.

Optional์€ ์„ ํƒํ˜• ๊ฐ’์„ ์บก์Šํ™”ํ•˜๋Š” ํด๋ž˜์Šค์ด๋‹ค.

 

๊ฐ’์ด ์žˆ์œผ๋ฉด Optional ํด๋ž˜์Šค๋Š” ๊ฐ’์„ ๊ฐ์‹ผ๋‹ค. 

๋ฐ˜๋ฉด ๊ฐ’์ด ์—†์œผ๋ฉด Optional.empty ๋ฉ”์„œ๋“œ๋กœ Optional์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

Optional.empty๋Š” Optional์˜ ํŠน๋ณ„ํ•œ ์‹ฑ๊ธ€ํ„ด ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋‹ค.

 

public class Person {
	// ์‚ฌ๋žŒ์ด ์ฐจ๋ฅผ ์†Œ์œ ํ–‡์„ ์ˆ˜๋„, ์†Œ์œ ํ•˜์ง€ ์•Š์•˜์„ ์ˆ˜๋„ ์žˆ์œผ๋ฏ€๋กœ Optional์ •์˜
	private Optional<Car> car;
    public Optional<Car> getCar(){ return car;}
}

public class Car {
	// ์ž๋™์ฐจ๊ฐ€ ๋ณดํ—˜์— ๊ฐ€์ž…๋˜์–ด ์žˆ์„ ์ˆ˜๋„ ์•„๋‹ ์ˆ˜ ๋„ ์žˆ์œผ๋ฏ€๋กœ
	private Optional<Insurance> insurance;
    public Optional<Insurance> getInsurance(){ return insurance;}
}

public class Insurance{
	// ๋ณดํ—˜ํšŒ์‚ฌ์—๋Š” ๋ฐ˜๋“œ์‹œ ์ด๋ฆ„์ด ์žˆ๋‹ค.
	private String name;
    public String getName(){ return name;}
}

 

 

11.3 Optional ์ ์šฉ ํŒจํ„ด

 

11.3.1 Optional ๊ฐ์ฒด ๋งŒ๋“ค๊ธฐ

 

  • ๋นˆ Optional

Optional<Car> optCar = Optional.empty();

 

  • null์ด ์•„๋‹Œ ๊ฐ’์œผ๋กœ Optional ๋งŒ๋“ค๊ธฐ

Optional.of๋กœ null์ด ์•„๋‹Œ ๊ฐ’์„ ํฌํ•จํ•˜๋Š” Optional์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

Optional<Car> optCar = Optional.of(car);

car๊ฐ€ null์ด๋ผ๋ฉด ์ฆ‰์‹œ NullPointerException ๋ฐœ์ƒ

 

  • null๊ฐ’์œผ๋กœ Optional ๋งŒ๋“ค๊ธฐ

Optional<Car> optCar = Optional.ofNullable(car);

car์ด null์ด๋ฉด ๋นˆ Optional ๊ฐ์ฒด๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.

 

 

11.3.2 ๋งต์œผ๋กœ Optional์˜ ๊ฐ’์„ ์ถ”์ถœํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜๊ธฐ

์ด๋ฆ„ ์ •๋ณด์— ์ ‘๊ทผํ•˜๊ธฐ ์ „์— insurance๊ฐ€ null์ธ์ง€ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค.

Optional์€ map ๋ฉ”์„œ๋“œ๋ฅผ ์ง€์›ํ•œ๋‹ค.

 

Optional<Insurance> optInsurance = Optional,.ofNullable(insurance);
Optional<String> name = optInsurance.map(Insurance::getName);

 

ํ•ญ๊ณต๊ธฐ

 

โŠ™ AmsField

package ams;

public class AmsField {
	//				Key
	// ํ•ญ๊ณต์‚ฌ, ํ•ญ๊ณต๊ธฐ๋ฒˆํ˜ธ, ์ตœ๋Œ€์Šน๊ฐ์ˆ˜, ์ถœ๋ฐœ์ง€, ๋„์ฐฉ์ง€
	String[][] arrPlane = new String[100][5];
	int insertCnt;
	int updateIndex;

	// ์ถ”๊ฐ€
	// 2์ฐจ์› ๋ฐฐ์—ด์— 5๊ฐœ์˜ ์ •๋ณด๋ฅผ ๋‹ด๊ธฐ
	void insert(String[] arPlane) {
		arrPlane[insertCnt] = arPlane;
		insertCnt++;
	}

	// ์ˆ˜์ •
	void update(int index, String newValue) {
		boolean updateCheck = true;
		//ํ•ญ๊ณต์‚ฌ, ํ•ญ๊ณต๊ธฐ๋ฒˆํ˜ธ, ์Šน๊ฐ์ˆ˜, ์ถœ๋ฐœ์ง€, ๋„์ฐฉ์ง€
		//0			1				2			3			4
		//index : 0, 1
			arrPlane[updateIndex][index+3] = newValue;
	}

	// ์‚ญ์ œ
	void delete() {
	}

	// ๊ฒ€์ƒ‰
	String select(int index, String keyword) {
		int[] arIndex = null;
		int searchCnt = 0;
		String result = "";
		updateIndex = -1;
		
		for (int i = 0; i < insertCnt; i++) {
			if (keyword.equals(arrPlane[i][index])) {
				// i >> ํ–‰๋ฒˆํ˜ธ : ๊ฒ€์ƒ‰ํ•  ๋น„ํ–‰๊ธฐ ๋ฒˆํ˜ธ
				searchCnt++;
				updateIndex = i;
				//๋ฐ”๋กœ int๋ฐฐ์—ด์— ๋‹ด์„ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์—(๊ฒ€์ƒ‰ ๊ฑด์ˆ˜๋ฅผ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ)
				//๋ฌธ์ž์—ด์— ๊ฒ€์ƒ‰๋œ ํ–‰๋ฒˆํ˜ธ๋ฅผ ์—ฐ๊ฒฐ์‹œ์ผœ ๋‹ด๋Š”๋‹ค.(", "๋Š” ๊ตฌ๋ถ„์ ์ด๋‹ค.)
				//๊ตฌ๋ถ„์ ์ด ์žˆ์–ด์•ผ์ง€๋งŒ ๋ฐ‘์—์„œ ๊ฐ ๊ฐ’์„ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
				result += i + ", ";
			}
		}
		//๊ฒ€์ƒ‰ ๊ฑด์ˆ˜๋ฅผ for๋ฌธ์ด ๋๋‚œ ํ›„์— ์•Œ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—
		//for๋ฌธ ๋ฐ‘์—์„œ new ํ•ด์ค€๋‹ค.
		arIndex = new int[searchCnt];
		for (int i = 0; i < arIndex.length; i++) {
			//	result๋Š” ๋ฌธ์ž์—ด์ด๊ณ  split() ์‚ฌ์šฉ์‹œ 
			//์ „์ฒด๋ฅผ ๋ฐฐ์—ด๋กœ ๋ณด๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ๋’ค์— ๋ฐ”๋กœ []๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
			arIndex[i] = Integer.parseInt(result.split(", ")[i]);
		}
		//list(int[] arIndex)๋ฉ”์„œ๋“œ์— ๊ฐ’์„ ์ „๋‹ฌํ•˜๊ณ  list(int[] arIndex)์—์„œ ๋ฆฌํ„ด๋œ
		//๊ฒฐ๊ณผ๊ฐ’์„ select()์—์„œ ๋ฆฌํ„ดํ•œ๋‹ค.
		return list(arIndex);
	}

	// ๋ชฉ๋ก
	String list() {
		String result = "ํ•ญ๊ณต์‚ฌ, ํ•ญ๊ณต๊ธฐ๋ฒˆํ˜ธ, ์ตœ๋Œ€์Šน๊ฐ์ˆ˜(๋ช…), ์ถœ๋ฐœ์ง€, ๋„์ฐฉ์ง€\n";
		for (int i = 0; i < insertCnt; i++) {
			result += "♥";
			for (int j = 0; j < arrPlane[0].length; j++) {
				result += arrPlane[i][j];
				result += j == arrPlane[0].length - 1 ? "" : ", ";
			}
			result += "\n";
		}

		if (insertCnt == 0)
			result = "๋ชฉ๋ก ์—†์Œ";

		return result;
	}
	
	String list(int[] arIndex) {
		String result = "ํ•ญ๊ณต์‚ฌ, ํ•ญ๊ณต๊ธฐ๋ฒˆํ˜ธ, ์ตœ๋Œ€์Šน๊ฐ์ˆ˜(๋ช…), ์ถœ๋ฐœ์ง€, ๋„์ฐฉ์ง€\n";
		for (int i = 0; i < arIndex.length; i++) {
			result += "♥";
			for (int j = 0; j < arrPlane[0].length; j++) {
				result += arrPlane[arIndex[i]][j];
				result += j == arrPlane[0].length - 1 ? "" : ", ";
			}
			result += "\n";
		}

		if (arIndex.length == 0)
			result = "๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ์—†์Œ";

		return result;
	}
	
}

 


 

โŠ™ AmsMain

package ams;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class AmsMain {
	public static void main(String[] args) {
		String title = "ํ•ญ๊ณต๊ธฐ ๊ด€๋ฆฌ ํ”„๋กœ๊ทธ๋žจ";
		String[] menu = {"์ถ”๊ฐ€ํ•˜๊ธฐ", "๊ฒ€์ƒ‰ํ•˜๊ธฐ", "์ˆ˜์ •ํ•˜๊ธฐ", "์‚ญ์ œํ•˜๊ธฐ", "๋ชฉ๋ก๋ณด๊ธฐ"};
		String[] updateMenu = {"์ถœ๋ฐœ์ง€ ์ˆ˜์ •", "๋„์ฐฉ์ง€ ์ˆ˜์ •"};
		String[] updateMsg = {"์ถœ๋ฐœ์ง€", "๋„์ฐฉ์ง€"};
		AmsField af = new AmsField();
		ImageIcon icon = new ImageIcon("src/img/main.gif");
		int choice = 0;
		String[] arPlane = new String[5];
		String keyword = "";
		
		while(true) {
			choice = JOptionPane.showOptionDialog(null, "", title, JOptionPane.DEFAULT_OPTION,
					JOptionPane.PLAIN_MESSAGE, icon, menu, null);
			
			if(choice == -1) break;
			
			switch(choice) {
			//์ถ”๊ฐ€
			//ํ•ญ๊ณต์‚ฌ, ํ•ญ๊ณต๊ธฐ๋ฒˆํ˜ธ, ์ตœ๋Œ€์Šน๊ฐ์ˆ˜(๋ช…), ์ถœ๋ฐœ์ง€, ๋„์ฐฉ์ง€
			case 0:
				//5๊ฐœ์˜ ๊ฐ’์„ ", "๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ํ•œ๋ฒˆ์— ์ž…๋ ฅํ•˜๊ธฐ
				//split("๊ตฌ๋ถ„์ ")์€ ๋ฆฌํ„ดํƒ€์ž…์ด ๋ฌธ์ž์—ด ๋ฐฐ์—ด์ด๋‹ค.
				//๊ตฌ๋ถ„์ ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์€ ์ตœ์†Œํ•œ ๊ฐ’์ด 2๊ฐœ ์ด์ƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.
				arPlane = ("" + JOptionPane.showInputDialog(null,
						"ํ•ญ๊ณต์‚ฌ, ํ•ญ๊ณต๊ธฐ๋ฒˆํ˜ธ, ์ตœ๋Œ€์Šน๊ฐ์ˆ˜(๋ช…), ์ถœ๋ฐœ์ง€, ๋„์ฐฉ์ง€", title, JOptionPane.PLAIN_MESSAGE,
						icon,	null, null)).split(", ");
				
				af.insert(arPlane);
				break;
			//๊ฒ€์ƒ‰
			case 1:
				break;
			//์ˆ˜์ •
			case 2:
				String newValue = "";
				
				choice =JOptionPane.showOptionDialog(null, "", title, JOptionPane.DEFAULT_OPTION,
						JOptionPane.PLAIN_MESSAGE, icon, updateMenu, null);
				
				if(choice == -1) break;
				
				keyword = "" + JOptionPane.showInputDialog(null,
						"์ˆ˜์ •ํ•˜์‹ค ํ•ญ๊ณต๊ธฐ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”", title, JOptionPane.PLAIN_MESSAGE,
						icon,	null, null);
				
				if(af.select(1, keyword).equals("๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ์—†์Œ")) {
					JOptionPane.showMessageDialog(null, "์ˆ˜์ • ์‹คํŒจ");
				}else {
					newValue = "" + JOptionPane.showInputDialog(null,
							"์ƒˆ๋กœ์šด " + updateMsg[choice] + "๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”", title, JOptionPane.PLAIN_MESSAGE,
							icon,	null, null);
					af.update(choice, newValue);
					JOptionPane.showMessageDialog(null, "์ˆ˜์ • ์„ฑ๊ณต");
				}
				
				break;
			//์‚ญ์ œ
			case 3:
				break;
			//๋ชฉ๋ก
			case 4:
				JOptionPane.showMessageDialog(null, af.list());
				break;
			}
		}
	}
}

 

์ƒ์„ฑ์ž์˜ ๋ชฉ์ 

1. ํž™ ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์— ํด๋ž˜์Šค ํ•„๋“œ๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๋Š” ๋ชฉ์ 

2. ์ดˆ๊ธฐํ™” ์—ญํ• 

 

๋ฉ”์„œ๋“œ์˜ ๋ชจ์–‘ :  name ( )

์ƒ์„ฑ์ž์˜ ๋ชจ์–‘ : class name ( )

 

์œ„์ฒ˜๋Ÿผ ์ƒ์„ฑ์ž๋„ ๋ฉ”์„œ๋“œ์™€ ์ƒ๊น€์ƒˆ๊ฐ€ ๋น„์Šทํ•˜๋„ค?

์ƒ์„ฑ์ž๋„ ๋ฉ”์„œ๋“œ๋‹ค

๊ทธ๋ ‡๋‹ค๋ฉด ์™œ ์ƒ์„ฑ์ž๋Š” ๋ฉ”์„œ๋“œ๋ผ๊ณ  ํ•˜์ง€ ์•Š์„๊นŒ?

 


 

์ƒ์„ฑ์ž๋Š” ๋ฉ”์„œ๋“œ์˜ ๊ธฐ๋Šฅ๊ณผ ๋˜‘๊ฐ™์ง€๋งŒ return์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”์„œ๋“œ๋ผ๊ณ  ๋ถ€๋ฅด์ง€ ์•Š๋Š”๋‹ค.

 


ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋Š” ์ˆœ๊ฐ„ ๊ตณ์ด ์ƒ์„ฑ์ž๋ฅผ ์ผ๋ถ€๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์ง€ ์•Š์•„๋„ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ผ๋Š” ๊ฒƒ์ด ์ƒ๊ธฐ๋Š”๋ฐ, ๋ณด์ด์ง„ ์•Š์ง€๋งŒ ๋‚ด๋ถ€์ ์œผ๋กœ ์˜ฌ๋ผ๊ฐ„ ๊ฒƒ์ด๋‹ค.

๋”ฐ๋ผ์„œ ์„ ์–ธ์—†์ด ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

๊ธฐ๋ณธ ์ƒ์„ฑ์ž

1. ํด๋ž˜์Šค ์„ ์–ธ ์‹œ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ๋‹ค.

2. ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์„ ์–ธํ•˜์ง€ ์•Š์•„๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

3. ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์ƒ์„ฑ์ž๋ฅผ ์„ ์–ธํ•˜๋Š” ์ˆœ๊ฐ„ ๊ทธ๊ฒƒ์„ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋กœ ์—ฌ๊ฒจ์„œ ๋”ฐ๋กœ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๊ฐ€ ์ƒ๊ธฐ์ง„ ์•Š๋Š”๋‹ค.

 


< ๋ณ€์ˆ˜ >

๋งค๊ฐœ๋ณ€์ˆ˜ : { } ์•ˆ, ๋‹ซ๋Š” ์ค‘๊ด„ํ˜ธ๋ฅผ ๋งŒ๋‚  ๋•Œ ๋๋‚œ๋‹ค

๋งค๊ฐœ ๋ณ€์ˆ˜์™€ ์ง€์—ญ๋ณ€์ˆ˜๋Š” stack์— ์ €์žฅ์ด๋œ๋‹ค.

์ „์—ญ ๋ณ€์ˆ˜๋Š” data ์˜์—ญ์— ์ €์žฅ๋œ๋‹ค.

 


์˜ค๋ฒ„ ๋กœ๋”ฉ Overloading

๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๋‚˜ ํƒ€์ž…์ด ๋‹ค๋ฅผ ๋•Œ, ๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค.

         →  ๋ฉ”์„œ๋“œ์˜ ์ด๋ฆ„์€ ๊ฐ™์œผ๋‚˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐฏ์ˆ˜ ํ˜น์€ ํƒ€์ž…์ด ๋‹ค๋ฅด๋ฉด ์„ ์–ธ ๊ฐ€๋Šฅ

load : ๋‚˜๊ฐ”๋‹ค๊ฐ€ ๋‹ค์‹œ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ

over : ๋„˜์น˜๊ฒŒ

overload : ๋„˜์น˜๊ฒŒ ๋ถˆ๋Ÿฌ์˜จ๋‹ค (๊ฐ™์€ ์ด๋ฆ„์ด๋ผ) 

 

ใ€Š ์˜ค๋ฒ„ ๋กœ๋”ฉ์€ ๋ฉ”์„œ๋“œ์˜ ์ฒซ ๋ฒˆ์งธ ๊ธฐ๋Šฅ โ‰ซ

 


Car์™€ Road๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž

 

package studyalone;

public class Car {
	//Car ํด๋ž˜์Šค๋Š” ๋ธŒ๋žœ๋“œ, ์ƒ‰, ๊ฐ€๊ฒฉ, ๋น„๋ฒˆ์„ ๊ฐ–๊ณ  ์žˆ๋‹ค
	String brand;
	String color;
	int price;
	String pw="1122";
	
	//์ƒ์„ฑ์ž , ๋น„๋ฒˆ ์ƒˆ๋กœ
	public Car(String b, String c ,int p,String pw) {
		this.brand=b; this.color=c; this.price=p;this.pw=pw;
	}
	
	//์ƒ์„ฑ์ž , ์ดˆ๊ธฐ ๋น„๋ฒˆ ๊ทธ๋Œ€๋กœ
	public Car(String b, String c ,int p) {
		this.brand=b; this.color=c; this.price=p;
	}
	
	//์™ธ๋ถ€์—์„œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ๋ฐ›๊ธฐ
	//์ž…๋ ฅ๋ฐ›์€ ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ์ž๋™์ฐจ์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋น„๊ตํ•˜๊ธฐ
	//๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•œ๋‹ค๋ฉด ์‹œ๋™ ์ผœ์ฃผ๊ธฐ
	//์ด๋ฏธ ์‹œ๋™์ด ์ผœ์ ธ์žˆ๋‹ค๋ฉด "์‹œ๋™์ด ์ด๋ฏธ ์ผœ์ ธ์žˆ์Šต๋‹ˆ๋‹ค"์ถœ๋ ฅ
	//์ด๋ฏธ ์‹œ๋™์ด ๊บผ์ ธ์žˆ๋‹ค๋ฉด "์‹œ๋™์ด ์ด๋ฏธ ๊บผ์ ธ์žˆ์Šต๋‹ˆ๋‹ค"์ถœ๋ ฅ
	//๋น„๋ฒˆ 3ํšŒ ์˜ค๋ฅ˜ ์‹œ ๊ฒฝ์ฐฐ ์ถœ๋™
	
	boolean isOn=false;
	int policeCnt;
	
	//์‹œ๋™ํ‚ค๋Š” ๋ฉ”์„œ๋“œ
	boolean engineStart(String pw ){
		boolean policeCheck=false;
		
		if(this.pw.equals(pw)) {
			if(!isOn) {
				System.out.println(this.brand+" ์‹œ๋™ ํ‚ด");
				isOn=true;
				policeCnt=0;
			}else {
				System.out.println(this.brand+" ์‹œ๋™์ด ์ด๋ฏธ ์ผœ์ ธ์žˆ์Œ");
			}
		}else {
			policeCnt++;
			if(policeCnt==3) {
				System.out.println("๊ฒฝ์ฐฐ ์ถœ๋™");
				policeCheck=true;
			}else {
				System.out.println("๋น„๋ฐ€๋ฒˆํ˜ธ ์˜ค๋ฅ˜");
			}
		}
		return policeCheck;
	}
	
	
	//์‹œ๋™๋„๋Š” ๋ฉ”์„œ๋“œ
	void engineStop(){
		if(!isOn) {
			System.out.println(this.brand+" ์‹œ๋™ ๋”");
		}else {
			System.out.println("์‹œ๋™์ด ์ด๋ฏธ ๊บผ์ ธ์žˆ์Šต๋‹ˆ๋‹ค.");
		}
	}
	
	
	//์ž๋™์ฐจ ์ •๋ณด ์ถœ๋ ฅ ๋ฉ”์„œ๋“œ
	void show() {
		System.out.println(brand+", "+color+", "+price+"๋งŒ์›");
		//์ง€์—ญ๋ณ€์ˆ˜๊ฐ€ ๊ฐ™์€ ์ด๋ฆ„์ด ์•„๋‹ˆ๋ผ์„œ ๊ตณ์ด this.brand๋“ฑ๋“ฑ์œผ๋กœ ํ•˜์ง€ ์•Š์€ ๊ฒƒ
	}

	
}
package studyalone;

import java.util.Scanner;

public class Road {
	public static void main(String[] args) {
		Car myCar=new Car("๋ฒคํ‹€๋ฆฌ","blue",1500,"981122");
		
		String menu= "1. ์‹œ๋™ ์ผœ๊ธฐ\n2. ์‹œ๋™ ๋„๊ธฐ";
		Scanner sc =new Scanner(System.in);
		int choice=0;
		String tryPw="";
		
		myCar.show();
		
		//๋ฌดํ•œ๋ฐ˜๋ณต 
		//์‹œ๋™์„ ํ•œ๋ฒˆ์ด๋ผ๋„ ํ‚จ ํ›„ ์‹œ๋™์„ ๋„๋ฉด  break
		while(true) {
			System.out.println(menu);
			
			choice=sc.nextInt();
			
			if(choice==1) {
				System.out.println("๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”");
				tryPw=sc.next();
				
				if(myCar.engineStart(tryPw))
					break;
			}else if(choice==2) {
				//์‹œ๋™์„ ํ•œ๋ฒˆ์ด๋ผ๋„ ์ผœ๊ธฐ ์ „์—” isOn์ด false๋‹ˆ๊นŒ break์•ˆํ•จ
				//ํ•œ๋ฒˆ์ด๋ผ๋„ ์‹œ๋™์„ ๊ฒผ๋‹ค๋ฉด isOn์€ true๋‹ˆ๊นŒ breakํ•จ
				if(myCar.isOn) {
					myCar.engineStop();
				}else {
					myCar.engineStop();
				}
			}
		}
		
	}
}

 

alt shift n ์ƒˆ๋กœ์šด ํ”„๋กœ์ ํŠธ ๋งŒ๋“ค๊ธฐ
ctrl n ์ƒˆ๋กœ์šด ํด๋ž˜์Šค ๋งŒ๋“ค๊ธฐ ( ํ•ด๋‹น ํ”„๋กœ์ ํŠธ์˜ src ์„ ํƒ ํ›„ )
alt ใ… ํ”„๋กœ์ ํŠธ, ํด๋ž˜์Šค, ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ํ•ด๋‹น ์ด๋‹ˆ์…œ๋กœ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Œ
ctrl shift + ๊ธ€์”จ ํ‚ค์šฐ๊ธฐ
ctrl shift - ๊ธ€์”จ ์ค„์ด๊ธฐ
ctrl m ์ „์ฒดํ™”๋ฉด
insert ์“ฐ๋Š”๊ฒƒ์ด ๊ฒน์ณ์ง€๋ฉด์„œ ์ง€์›Œ์ง€๊ฒŒํ•˜๋Š” ์ปค์„œ
alt shift s o ์ƒ์„ฑ์ž ๋งŒ๋“ค๊ธฐ
alt shift r ์ „์ฒด์ ์œผ๋กœ ์ด๋ฆ„ ๋ฐ”๊พธ๊ธฐ ( class ์ด๋ฆ„๋„ )
ctrl space๋ฐ” ์ž๋™์™„์„ฑ
ctrl z ๋’ค๋กœ๊ฐ€๊ธฐ
ctrl s ์ €์žฅ
ctrl x ์ €์žฅํ•˜๊ณ  ํ•ด๋‹น ์˜์—ญ ์‚ญ์ œ
ctrl t ๊ฐ€์กฑ ๊ด€๊ณ„๋„
ctrl shift o ์ž๋™ import
ctrl shift f ์ค„ ๋งž์ถค
alt ์œ„ ์•„๋ž˜ ํ•ด๋‹น์˜์—ญ ์œ„ ๋˜๋Š” ์•„๋ž˜๋กœ ์ด๋™
ใ…Šํ•œ์ž ์ œ๊ณฑ๊ทผ์ด๋‚˜ 1/2๊ฐ™์€ ๊ธฐํ˜ธ ์‚ฌ์šฉ

 


 

์ „์—ญ ๋ณ€์ˆ˜๋Š” data ์˜์—ญ์— ์ €์žฅ๋œ๋‹ค.

์ „์—ญ๋ณ€์ˆ˜๋Š” ์ž๋™ ์ดˆ๊ธฐํ™”๊ฐ€ ๋œ๋‹ค.

 


%f ๋ฅผ ์“ฐ๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ์†Œ์ˆ˜์  6์ž๋ฆฌ๊ฐ€ ๋‚˜์˜จ๋‹ค.

float๋Š” 4๋ฐ”์ดํŠธ์ด์ž 32๋น„ํŠธ๋‹ค.

 

3.7์ด๋ž€ ์ˆซ์ž๋ฅผ float๋กœ ์ €์žฅํ•œ๋‹ค๊ณ  ํ•˜๋ฉด

1bit 8bit 23bit
์–‘์ˆ˜๋ฉด 0 / ์Œ์ˆ˜๋ฉด 1 exp ์ง„์ˆ˜ ์˜์—ญ f ๊ฐ€์ˆ˜ ์˜์—ญ
3.7์€ ์–‘์ˆ˜๋‹ˆ๊นŒ 0 3.7์˜ ์ •์ˆ˜๋Š” 3์ด๋‹ˆ๊นŒ 0011

3.7์˜ 0.7์ด ๊ฐ€์ˆ˜์˜์—ญ์ด๋‹ˆ๊นŒ ์ด์ง„์ˆ˜๋กœ ๊ตํ™˜ // ๊ฐ€์ˆ˜์˜์—ญ * 2 ํ•œ ๊ฐ’์ด 1๋ณด๋‹ค ํฌ๋ฉด 1์„ ๋„ฃ๊ณ  (๊ณ„์‚ฐ๊ฐ’-1)*2๋ฅผ ํ•ด์ค€๋‹ค. 1.0์ด ๋˜๋Š” ์ˆœ๊ฐ„๊นŒ์ง€ ๋ฌดํ•œ ๋ฐ˜๋ณต์„ ํ•˜๊ฑฐ๋‚˜ 24๋น„ํŠธ์—์„œ ๋ฐ˜์˜ฌ๋ฆผ ๋œ๋‹ค.

0.7*2=1.4 ---> 1์จ์ง ---> 1.4-1=0.4

0.4*2=0.8 ---> 0์จ์ง

0.8*2=1.6 ---> 1์จ์ง ---> 1.6-1=0.6

           . . .

0 0000 0011 10110...

 

์ด๋Ÿฐ ์‹์œผ๋กœ ๋น„ํŠธ๊ฐ€ ๋ฐ˜์˜ฌ๋ฆผ๋˜๋Š” ์ปดํ“จํ„ฐ ์—ฐ์‚ฐ์˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ๋‹ค.

 

๊ทธ๋ž˜์„œ 7์ž๋ฆฌ ๊นŒ์ง„ ์ •ํ™•ํ•˜์ง€ ์•Š๊ณ  ๊ธฐ๋ณธ์ ์œผ๋กœ 6์ž๋ฆฌ๊นŒ์ง€ ์ •ํ™•ํ•˜๋‹ค.

๋”ฐ๋ผ์„œ %f ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ 6์ž๋ฆฌ๊นŒ์ง€ ์ถœ๋ ฅ์ด ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 


eclipse๋ฅผ ์ฒ˜์Œ ๊น”๋ฉด perspective๋ฅผ java๋กœ ์„ ํƒ์ด ๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค.

java๋กœ ์„ ํƒํ•ด๋†“์œผ๋ฉด package explorer์ด ์ผœ์ง€๋Š” ๊ฒƒ์ด๋‹ค.

+ Recent posts