Chapter 7의 모든 내용을 한 번 복습하였다.

 

그리고 그 중 몇개의 예제를 기본베이스로 하여, 배웠던 기능을 조금 추가해서 프로그램을 만들어 보았음

 

Ex1) SmartTv

import java.util.Scanner;

class Tv {
	boolean power = false;
	int channel = 9;
	int soundvolume = 10;
	
	void power() {
		power = !power;
		if (power=true) {
			System.out.println("전원이 켜졌습니다.");
		} else {
			System.out.println("전원이 꺼졌습니다.");
		}
	}
	
	void channelUp() {
		++channel;
		System.out.println(channel+"번");
	}
	
	void channelDown() {
		--channel;
		System.out.println(channel+"번");
	}
	
}

class SmartTv extends Tv {
	void search() {
		System.out.println("검색할 내용을 입력하세요");
		Scanner sc = new Scanner(System.in);
		String searchobject = sc.nextLine();
		System.out.println(searchobject + "에 대한 검색결과 입니다.");
	}
}

public class SmartTv만들기 {

	public static void main(String[] args) {
		
		SmartTv stv1 = new SmartTv();
		stv1.power();
		stv1.channelDown();
		stv1.search();

	}

}

실행 결과

전원이 켜졌습니다.
8번
검색할 내용을 입력하세요
Java
Java에 대한 검색결과 입니다.

 

 

Ex2) 스타크래프트 유닛 사용

abstract class Unit {
	abstract void move(int x, int y);
	void stop() {
		System.out.println("현재 자리에 정지합니다.");
	}
}

class Marine extends Unit {
	void move(int x, int y) {
		System.out.println("Marine을 "+x+", "+y+"로 이동합니다.");
	}
	void stimPack() {
		System.out.println("스팀팩을 사용합니다.");
	}
}

class Tank extends Unit {
	
	void move (int x, int y) {
		System.out.println("Tank를 "+x+", "+y+"로 이동합니다.");
	}
	
	void siegeMode() {
		System.out.println("시즈모드로 변경합니다.");
	}
	void tankMode() {
		System.out.println("탱크모드로 변경합니다.");
	}
}

class Dropship extends Unit {
	void move (int x, int y) {
		System.out.println("Dropship을 "+x+", "+y+"로 이동합니다.");
	}
	
	void load(Unit u) {
		System.out.println("선택한 대상을 태웁니다.");
	}
	
	void unload(Unit u) {
		System.out.println("선택한 대상을 내립니다.");
	}
}

public class 유닛그룹 {

	public static void main(String[] args) {
		Unit[] group = {new Marine(), new Tank(), new Dropship() };
		
		for(int i=0; i<group.length; i++) {
			group[i].move(100, 200);
		}
		
		
		Marine m = (Marine)group[0];
		m.stimPack();
		
		Tank t = (Tank)group[1];
		t.siegeMode();
		
		Dropship d = (Dropship)group[2];
		d.load(m);
		
	}

}

실행 결과

Marine을 100, 200로 이동합니다.
Tank를 100, 200로 이동합니다.
Dropship을 100, 200로 이동합니다.
스팀팩을 사용합니다.
시즈모드로 변경합니다.
선택한 대상을 태웁니다.

 

 

 

Ex3) 생성자 활용

class Point {
	int x;
	int y;
	
	Point() {
		this(0,0);
	}
	
	Point (int x, int y) {
		super();
		this.x=x;
		this.y=y;
	}
	
	String getLocation() {
		return "x : " + x + ", y : " + y;
	}
}

class Point3D extends Point {
	int z;
	
	Point3D() {
		super(1,1);
		this.z=1;
	}
	
	Point3D(int x, int y, int z) {
		super(x,y);
		this.z=z;
	}
	
	String getLocation() {
		return "x : " + x + ", y : " + y + ", z : " + z;
	}
}
public class 조상의생성자 {

	public static void main(String[] args) {
		Point3D p = new Point3D(1,2,3);
		System.out.println(p.getLocation());;
		
		Point3D p2 = new Point3D();
		System.out.println(p2.getLocation());
		
		
	}

}

실행 결과

x : 1, y : 2, z : 3
x : 1, y : 1, z : 1

 

 

 

Ex4) 스타크래프트 유닛 생산

import java.util.Scanner;

class GatewayUnit {
	int mineral;
	int gas;
	int psi;
	
	GatewayUnit(int mineral,int gas) {
		this.mineral = mineral;
		this.gas = gas;
		this.psi = 2;
	}
}

class Zealot extends GatewayUnit {
	Zealot() {
		super(100,0);
	}
	public String toString() {
		return "Zealot";
	}
}
class Dragoon extends GatewayUnit {
	Dragoon() {
		super(125,50);
	}
	public String toString() {
		return "Dragoon";
	}
}
class Hightemplar extends GatewayUnit {
	Hightemplar() {
		super(50,150);
	}
	public String toString() {
		return "Hightemplar";
	}
}
class Darktemplar extends GatewayUnit {
	Darktemplar() {
		super(125,100);
	}
	public String toString() {
		return "Darktemplar";
	}
}

class User {
	int mineral = 1000;
	int gas = 1000;
	int psi = 0;
	GatewayUnit[] group = new GatewayUnit[20];
	int i = 0;
	
	User(int mineral, int gas) {
		this.mineral=mineral;
		this.gas=gas;
	}
	
	void Summon (GatewayUnit g) {
		if(mineral < g.mineral) {
			System.out.println("미네랄이 모자랍니다. 미네랄을 더 채취하세요");
			return;
		} else if (gas < g.gas) {
			System.out.println("가스가 모자랍니다. 가스를 더 채취하세요");
			return;
		}
		
		mineral = mineral - g.mineral;
		gas = gas - g.gas;
		psi = psi + g.psi;
		group[i++] = g;
		 
		System.out.println(g+"를 소환하였습니다.");
	}
	
	void summary() {
		int totalmineral = 0;
		int totalgas = 0;
		int totalpsi = 0;
		String unitList="";
		
		for (int i = 0; i<group.length; i++) {
			if (group[i]==null) {
				break;
			}
			totalmineral = totalmineral + group[i].mineral;
			totalgas = totalgas + group[i].gas;
			totalpsi = totalpsi + group[i].psi;
			unitList = unitList + group[i] + ", ";
		}
		
		System.out.println("총 미네랄 소비량은 " + totalmineral + "입니다.");
		System.out.println("총 가스 소비량은 " + totalgas + "입니다.");
		System.out.println("소환한 유닛은 " + unitList + "입니다.");
		
		System.out.println("현재 보유 미네랄 : " + mineral);
		System.out.println("현재 보유 가스 : " + gas);
		System.out.println("현재 인구수 : " + psi);
		
	}
}

public class 유닛생산 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		User u1 = new User(500,300);
		
		while (u1.mineral>=50) {
			
			System.out.println("현재 보유하고 있는 미네랄은 "+u1.mineral+ ", 가스는 "+u1.gas+" 입니다.");
			System.out.println("어떤 유닛을 소환하시겠습니까?(단축기를 입력하세요)");
			System.out.println("z : Zealot\nd : Dragoon\nt : Hightemplar\nk : Darktemplar");
			System.out.println("(나가려면 q를 입력하세요)");
			
			String summon = sc.nextLine();
			
			if (summon.equals("z")) {
				u1.Summon(new Zealot());
			} else if (summon.equals("d")) {
				u1.Summon(new Dragoon());
			} else if (summon.equals("t")) {
				u1.Summon(new Hightemplar());
			} else if (summon.equals("k")) {
				u1.Summon(new Darktemplar());
			} else if (summon.equals("q")) {
				break;
			} else {
				continue;
			}
			
			u1.summary();
			System.out.println();
			
		} System.out.println("Gateway를 나갑니다.");
		
	}

}

실행 결과

현재 보유하고 있는 미네랄은 500, 가스는 300 입니다.
어떤 유닛을 소환하시겠습니까?(단축기를 입력하세요)
z : Zealot
d : Dragoon
t : Hightemplar
k : Darktemplar
(나가려면 q를 입력하세요)
z
Zealot를 소환하였습니다.
총 미네랄 소비량은 100입니다.
총 가스 소비량은 0입니다.
소환한 유닛은 Zealot, 입니다.
현재 보유 미네랄 : 400
현재 보유 가스 : 300
현재 인구수 : 2

현재 보유하고 있는 미네랄은 400, 가스는 300 입니다.
어떤 유닛을 소환하시겠습니까?(단축기를 입력하세요)
z : Zealot
d : Dragoon
t : Hightemplar
k : Darktemplar
(나가려면 q를 입력하세요)
d
Dragoon를 소환하였습니다.
총 미네랄 소비량은 225입니다.
총 가스 소비량은 50입니다.
소환한 유닛은 Zealot, Dragoon, 입니다.
현재 보유 미네랄 : 275
현재 보유 가스 : 250
현재 인구수 : 4

현재 보유하고 있는 미네랄은 275, 가스는 250 입니다.
어떤 유닛을 소환하시겠습니까?(단축기를 입력하세요)
z : Zealot
d : Dragoon
t : Hightemplar
k : Darktemplar
(나가려면 q를 입력하세요)
d
Dragoon를 소환하였습니다.
총 미네랄 소비량은 350입니다.
총 가스 소비량은 100입니다.
소환한 유닛은 Zealot, Dragoon, Dragoon, 입니다.
현재 보유 미네랄 : 150
현재 보유 가스 : 200
현재 인구수 : 6

현재 보유하고 있는 미네랄은 150, 가스는 200 입니다.
어떤 유닛을 소환하시겠습니까?(단축기를 입력하세요)
z : Zealot
d : Dragoon
t : Hightemplar
k : Darktemplar
(나가려면 q를 입력하세요)
t
Hightemplar를 소환하였습니다.
총 미네랄 소비량은 400입니다.
총 가스 소비량은 250입니다.
소환한 유닛은 Zealot, Dragoon, Dragoon, Hightemplar, 입니다.
현재 보유 미네랄 : 100
현재 보유 가스 : 50
현재 인구수 : 8

현재 보유하고 있는 미네랄은 100, 가스는 50 입니다.
어떤 유닛을 소환하시겠습니까?(단축기를 입력하세요)
z : Zealot
d : Dragoon
t : Hightemplar
k : Darktemplar
(나가려면 q를 입력하세요)
z
Zealot를 소환하였습니다.
총 미네랄 소비량은 500입니다.
총 가스 소비량은 250입니다.
소환한 유닛은 Zealot, Dragoon, Dragoon, Hightemplar, Zealot, 입니다.
현재 보유 미네랄 : 0
현재 보유 가스 : 50
현재 인구수 : 10

Gateway를 나갑니다.

 


내일은 Chapter 6을 복습할 예정

'간단 실습' 카테고리의 다른 글

220317 Java - 토이 프로그램  (0) 2022.03.18
220312 Java - 퀴즈  (0) 2022.03.13

+ Recent posts