class Tv {
	String color;
	boolean power;
	int channel;
	void power() {power = !power;}
	void channelUp() { ++channel;}
	void channelDown() { --channel;}

}

class TvTest{
	public static void main(String[] args) {
		Tv t;
		t = new Tv(); 
		t.channel = 7;
		t.channelDown();
		System.out.println("현재 채널은 " + t.channel + " 입니다.");
	}
}
현재 채널은 6 입니다.

 

 

 

class Tv {
	String color;
	boolean power;
	int channel;
	void power() {power = !power;}
	void channelUp() { ++channel;}
	void channelDown() { --channel;}

}
public class TvTest2 {
	public static void main(String[] args) {
		Tv t1 = new Tv();
		Tv t2 = new Tv();
		System.out.println("t1의 channel값은 " + t1.channel + "입니다:");
		System.out.println("t2의 channel값은 " + t2.channel + "입니다:");
		
		t1.channel = 7;
		System.out.println("t1의 channel값을 7로 변경하였습니다.");
		
		System.out.println("t1의 channel값은 " + t1.channel + "입니다:");
		System.out.println("t2의 channel값은 " + t2.channel + "입니다:");
	}
}
		
		System.out.println("t1의 channel값은 " + t1.channel + "입니다:");
		System.out.println("t2의 channel값은 " + t2.channel + "입니다:");
	}

}
t1의 channel값은 0입니다:
t2의 channel값은 0입니다:
t1의 channel값을 7로 변경하였습니다.
t1의 channel값은 7입니다:
t2의 channel값은 0입니다:

 

 

 

class Tv {
	String color;
	boolean power;
	int channel;
	void power() {power = !power;}
	void channelUp() { ++channel;}
	void channelDown() { --channel;}

}
public class TvTest3 {
	public static void main(String[] args) {
		Tv t1 = new Tv();
		Tv t2 = new Tv();
		
		System.out.println("t1의 channel값은 " + t1.channel + "입니다");
		System.out.println("t2의 channel값은 " + t2.channel + "입니다");
		
		t2 = t1;
		t1.channel = 7;
		System.out.println("t1의 channel값을 7로 변경하였습니다.");
		
		System.out.println("t1의 channel값은 " + t1.channel + "입니다:");
		System.out.println("t2의 channel값은 " + t2.channel + "입니다:");
		
		Tv[] tvArr = new Tv[3];
		tvArr[0].channel = 10;
	}
}
t1의 channel값은 0입니다
t2의 channel값은 0입니다
t1의 channel값을 7로 변경하였습니다.
t1의 channel값은 7입니다:
t2의 channel값은 7입니다:

 

 

 

public class TvTest4 {
	public static void main(String[] args) {
		Tv[] tvArr = new Tv[3];
		
		for(int i=0; i <tvArr.length;i++) {
			tvArr[i] = new Tv();
			tvArr[i].channel = i+10;
		}
		
		for(int i=0; i<tvArr.length;i++) {
			tvArr[i].channelUp();
			System.out.printf("tvArr[%d].channel=%d%n",i,tvArr[i].channel);
		}
	}
}
class Tv {
	String color;
	boolean power;
	int channel;
	void power() {power = !power;}
	void channelUp() { ++channel;}
	void channelDown() { --channel;}

}
tvArr[0].channel=11
tvArr[1].channel=12
tvArr[2].channel=13

 

 

public class CardTest {
	public static void main(String[] args) {
		System.out.println("Card.width = " + Card.width);
		System.out.println("Card.width = " + Card.height);
		
		Card c1 = new Card();
		c1.kind = "Heart";
		c1.number = 7;
		
		Card c2 = new Card();
		c2.kind = "Spade";
		c2.number = 4;
		
		System.out.println("c1은 " + c1.kind + ", " + c1.number + "이며, 크기는 (" + c1.width + ", " + c1.height + ")");
		System.out.println("c2은 " + c2.kind + ", " + c2.number + "이며, 크기는 (" + c2.width + ", " + c2.height + ")"); //c1 c2를Card로 바꿔도 값이 같다
		System.out.println("c1의 width와 height를 각각 50, 80으로 변경합니다.");
		
		c1.width = 50;
		c1.height = 80;
		
		System.out.println("c1은 " + c1.kind + ", " + c1.number + "이며, 크기는 (" + c1.width + ", " + c1.height + ")") ;
		System.out.println("c2은 " + c2.kind + ", " + c2.number + "이며, 크기는 (" + c2.width + ", " + c2.height + ")") ;
	}
}
class Card {
	String kind;
	int number;
	static int width = 100;
	static int height = 250;
}
Card.width = 100
Card.width = 250
c1은 Heart, 7이며, 크기는 (100, 250)
c2은 Spade, 4이며, 크기는 (100, 250)
c1의 width와 height를 각각 50, 80으로 변경합니다.
c1은 Heart, 7이며, 크기는 (50, 80)
c2은 Spade, 4이며, 크기는 (50, 80)

 

 

 

public class MyMathTest {
	public static void main(String[] args) {
		MyMath mm = new MyMath();
		
		long result1 = mm.add(5L, 3L);
		long result2 = mm.subtract(5L, 3L);
		long result3 = mm.multiply(5L, 3L);
		double result4 = mm.divide(5L, 3L);
		
		System.out.println("add(5L, 3L) = " + result1);
		System.out.println("subtract(5L, 3L) = " + result2);
		System.out.println("multiply(5L, 3L) = " + result3);
		System.out.println("divide(5L, 3L) = " + result4);
	}
}

class MyMath {long add(long a, long b) {
	long result = a+b;
	return result;
	}
	long subtract(long a, long b) {return a -b;}
	long multiply(long a, long b) { return a * b;}
	double divide(double a, double b) {return a / b;}
}
add(5L, 3L) = 8
subtract(5L, 3L) = 2
multiply(5L, 3L) = 15
divide(5L, 3L) = 1.6666666666666667

 

 

 

public class CallStackTest {
	public static void main(String[] args) {
		firstMethod();
	}
	static void firstMethod() {
		secondMethod();
	}
	static void secondMethod() {
		System.out.println("secondMethod()");
	}
}
secondMethod()

 

 

public class CallStackTest2 {
	public static void main(String[] args) {
		System.out.println("main(String[] args)이 시작되었음.");
		firstMethod();
		System.out.println();
	}
	static void firstMethod() {
		System.out.println("firstMethod()이 시작되었음.");
		secondMethod();
		System.out.println("firstMethod()이 끝났음.");
	}
	static void secondMethod() {
		System.out.println("secondMethod()이 시작되었음.");
		System.out.println("secondMethod()이 끝났음.");
	}
}
main(String[] args)이 시작되었음.
firstMethod()이 시작되었음.
secondMethod()이 시작되었음.
secondMethod()이 끝났음.
firstMethod()이 끝났음.

 

 

public class PrimitiveParamEx {
	public static void main(String[] args) {
		Data d = new Data();
		d.x = 10;
		System.out.println("main() : x = " + d.x);
		
		change(d.x);
		System.out.println("After change(d.x)");
		System.out.println("main() : x = " + d.x);
		
	}
	static void change(int x) {
		x = 1000;
		System.out.println("change1() : x = " + x);
	}
}
main() : x = 10
change1() : x = 1000
After change(d.x)
main() : x = 10

 

 

 

public class ReferenceParamEx {
	public static void main(String[] args) {
		Data d = new Data();
		d.x = 10;
		System.out.println("main() : x = " + d.x);

	}

	static void change(Data d) {
		d.x = 1000;
		System.out.println("change() : x = " + d.x);

	}
}
main() : x = 10

 

public class ReferenceParamEx2 {
	public static void main(String[] args) {
		int[] x = {10};
		System.out.println("main() : x = "+ x[0]);
		
		change(x);
		System.out.println("After change(x)");
		System.out.println("main() : x = "+ x[0]);
	}
	
	static void change(int[] x) {
		x[0] = 1000;
		System.out.println("change() : x = " +x[0]);
	}
}
main() : x = 10
change() : x = 1000
After change(x)
main() : x = 1000

 

 

 

public class ReturnTest {
	public static void main(String[] args) {
		ReturnTest r = new ReturnTest();

		int result = r.add(3, 5);
		System.out.println(result);

		int[] result2 = { 0 };
		r.add(3, 5, result2);
		System.out.println(result2[0]);
	}

	int add(int a, int b) {
		return a + b;
	}

	void add(int a, int b, int[] result) {
		result[0] = a + b;
	}
}

 

 

 

 

class Date {int x;}

public class ReferenceReturnEx_1 {
	public static void main(String[] args) {
		Date d = new Date();
		d.x = 10;
		
		Date d2 = copy(d);
		
		System.out.println("d.x ="+d.x);
		System.out.println("d2.x="+d2.x);
	}

	static Date copy(Date Date) {
		Date tmp = new Date();
		tmp.x = Date.x;
		
		return tmp;
	}
}
d.x =10
d2.x=10

 

 

 

public class FactorialTest {
	public static void main(String[] args) {
		int result = factorial(4);
		
		System.out.println(result);
		
	}
	
	static int factorial(int n) {
		int result=0;
		if(n==1)
			result = 1;
		else
			result = n * factorial(n-1);
		
		return result;
	}
}
24

 

 

 

public class FactorialTest2 {
	static long factorial(int n) {
		if(n<=0 || n>20) return -1;
		if(n<=1) return 1;
		return n * factorial(n-1);
	}
	
	public static void main(String[] args) {
		int n =21;
		long result = 0;
		
		for(int i = 1; i <= n; i++) {
			result = factorial(i);
			
			if(result==-1) {
				System.out.printf("유효하지 않은 값입니다.(0<n<=20):%d%n", n);
				break;
			}
			
			System.out.printf("%2d!=%20d%n", i, result);
		}
	}
}
 1!=                   1
 2!=                   2
 3!=                   6
 4!=                  24
 5!=                 120
 6!=                 720
 7!=                5040
 8!=               40320
 9!=              362880
10!=             3628800
11!=            39916800
12!=           479001600
13!=          6227020800
14!=         87178291200
15!=       1307674368000
16!=      20922789888000
17!=     355687428096000
18!=    6402373705728000
19!=  121645100408832000
20!= 2432902008176640000
유효하지 않은 값입니다.(0<n<=20):21

 

 

 

public class MainTest {
	public static void main(String[] args) {
		main(null);
	}
}
Exception in thread "main" java.lang.StackOverflowError
	at a210715.MainTest.main(MainTest.java:5)
	at a210715.MainTest.main(MainTest.java:5)
    ...
	at a210715.MainTest.main(MainTest.java:5)
	at a210715.MainTest.main(MainTest.java:5)
	at a210715.MainTest.main(MainTest.java:5)

 

 

 

public class PowerTest {
	public static void main(String[] args) {
		int x = 2;
		int n = 5;
		long result = 0;
		
		for(int i=1; i<=n; i++) {
			result += power(x, i);
		}
		System.out.println(result);
	}
	
	static long power (int x, int n) {
		if(n==1) return x;
		return x * power(x, n-1);
	}
}
62

 

 

 

class MyMath2 {
	long a, b;
	
	long add()		{ return a + b;	}
	long subtract()	{ return a - b;	}
	long multiply()	{ return a * b;	}
	double divide()	{ return a / b;	}
	
	static long		add(long a, long b)			{ return a + b;	}
	static long		subtract(long a, long b)	{ return a - b;	}
	static long		multiply(long a, long b)	{ return a * b;	}
	static double	divide(double a, double b)	{ return a / b;	}
}

class MyMathTest2 {
	public static void main(String[] args) {
		System.out.println(MyMath2.add(200L, 100L));
		System.out.println(MyMath2.subtract(200L, 100L));
		System.out.println(MyMath2.multiply(200L, 100L));
		System.out.println(MyMath2.divide(200.0, 100.0));
		
		MyMath2 mm = new MyMath2();
		mm.a = 200L;
		mm.b = 100L;
		
		System.out.println(mm.add());
		System.out.println(mm.subtract());
		System.out.println(mm.multiply());
		System.out.println(mm.divide());
	}
}
300
100
20000
2.0
300
100
20000
2.0

 

 

 

public class OverloadingTest {
	public static void main(String[] args) {
		MyMath3 mm = new MyMath3();
		System.out.println("mm.add(3, 3) 결과:" + mm.add(3,3));
		System.out.println("mm.add(3L, 3) 결과: " + mm.add(3L,3));
		System.out.println("mm.add(3, 3L) 결과: " + mm.add(3,3L));
		System.out.println("mm.add(3L, 3L) 결과: " + mm.add(3L,3L));
		
		int[] a = {100, 200, 300};
		System.out.println("mm.add(a) 결과: " + mm.add(a));
	}

}

class MyMath3 {
	int add(int a, int b) {
		System.out.print("int add(int a, int b) -");
		return a+b;
	}
	
	long add(int a, long b) {
		System.out.print("long add(int a, long b) - ");
		return a+b;
	}
	
	long add(long a, int b) {
		System.out.print("long add(long a, int b) - ");
		return a+b;
	}
	long add(long a, long b) {
		System.out.print("long add(long a, long b) - ");
		return a+b;
	}
	int add(int[] a ) {
		System.out.print("int add(int[] a) - ");
		int result = 0;
		for(int i=0; i < a.length;i++) {
			result +=a[i];
		}
		return result;
	}
}
int add(int a, int b) -mm.add(3, 3) 결과:6
long add(long a, int b) - mm.add(3L, 3) 결과: 6
long add(int a, long b) - mm.add(3, 3L) 결과: 6
long add(long a, long b) - mm.add(3L, 3L) 결과: 6
int add(int[] a) - mm.add(a) 결과: 600

 

 

 

public class VarArgsEx {
	public static void main(String[] args) {
		String[] strArr = { "100", "200", "300"	};
		
		System.out.println(concatenate("", "100", "200", "300"));
		System.out.println(concatenate("-", strArr));
		System.out.println(concatenate(",", new String[] {"1", "2", "3"}));
		System.out.println("["+concatenate(",", new String[0])+"]");
		System.out.println("["+concatenate(",")+"]");
	}
	
	static	String concatenate(String delim, String...args) {
		String result = "";
		
		for(String str : args) {
			result += str + delim;
		}
		
		return result;
	}
//	
//	static String concatenate(String...strings args) {
//		return concatenate("", args);
//	}
	
}
100200300
100-200-300-
1,2,3,
[]
[]

 

 

 

public class ConstructorTest {
	public static void main(String[] args) {
		Data1 d1 = new Data1();
		Data2 d2 = new Data2();	//오류를 고치기 위해 int타입의 인자를 기입해주면된다.
		System.out.println(d2.value);
	}
}

class Data1 {
	int value;
	Data1() {} //생성자가 기본으로 만들어져있음
	
}

class Data2 {
	int value;
	Data2(){}  //6번줄의 오류를 고치기 위해생성자를만들어줌 즉 초기화를시킴
	
	Data2(int x) {
		value = x;
	}
}
0

 

 

 

class Car {
	String color;
	String gearType;
	int door;
	
	Car(){}
	Car(String c, String g, int d) {
		color = c;
		gearType = g;
		door = d;
	}
}
public class CarTest {
	public static void main(String[] args) {
		Car c1 = new Car();
		c1.color	= "white";
		c1.gearType	= "auto";
		c1.door 	= 4;
		
		Car c2	= new Car("white", "auto", 4);
		
		System.out.println("c1의 color=" + c1.color + ", gearType=" +c1.gearType+ ", door"+c1.door);
		System.out.println("c2의 color=" + c2.color + ", gearType=" +c2.gearType+ ", door"+c2.door);
	}
}
c1의 color=white, gearType=auto, door4
c2의 color=white, gearType=auto, door4

 

 

 

class Car {
	String color;
	String gearType;
	int door;
	
	Car() {
		this("white", "auto", 4);
	}

	Car(String color) {
			this(color, "auto", 4);
		}
		Car(String color, String gearType, int door) {
			this.color = color;
			this.gearType = gearType;
			this.door = door;
		}
		Car(Car c) {
			color = c.color;
			gearType = c.gearType;
			door = c.door;
		}
}

public class CarTest2 {
	public static void main(String[] args) {
		Car c1 = new Car();
		Car c2 = new Car("blue");
		
		System.out.println("c1의 color=" + c1.color + ", gearType=" + c1.gearType+ ", door="+c1.door);
		System.out.println("c2의 color=" + c2.color + ", gearType=" + c2.gearType+ ", door="+c2.door);
		
	}
}
c1의 color=white, gearType=auto, door=4
c2의 color=blue, gearType=auto, door=4

 

 

 

class Car {
	String color;
	String gearType;
	int door;

	Car() {
		this("white", "auto", 4);
	}

	Car(Car c) {
		color = c.color;
		gearType = c.gearType;
		door = c.door;
	}

	Car(String color, String gearType, int door) {
		this.gearType = gearType;
		this.door = door;
	}
}

class CarTest3 {
	public static void main(String[] args) {
		Car c1 = new Car();
		Car c2 = new Car(c1);
		System.out.println("c1의 color=" + c1.color + ", gearType=" + c1.gearType + ", door" + c1.door);
		System.out.println("c1의 color=" + c1.color + ", gearType=" + c1.gearType + ", door" + c1.door);
		c1.door = 100;
		System.out.println("c1.door=100; 수행 후");
		System.out.println("c1의 color=" + c1.color + ", gearType=" + c1.gearType + ", door=" + c1.door);
		System.out.println("c2의 color=" + c2.color + ", gearType=" + c2.gearType + ", door=" + c2.door);
	}
}
c1의 color=null, gearType=auto, door4
c1의 color=null, gearType=auto, door4
c1.door=100; 수행 후
c1의 color=null, gearType=auto, door=100
c2의 color=null, gearType=auto, door=4

+ Recent posts