class BlockTest
{
static {
System.out.println("static{}");
}
{
System.out.println("{}");
}
public BlockTest(){
System.out.println("생성자");
}
public static void main(String[] args)
{
System.out.println("BlockTest bt = new BlockTest();");
BlockTest bt = new BlockTest();
System.out.println("BlockTest bt2 = new BlockTest();");
BlockTest bt2 = new BlockTest();
}
}
----------------------------------------------------------------------------------------
class CallStackTest
{
public static void main(String[] args)
{
firstMethod();
}
static void firstMethod(){
secondMethod();
}
static void secondMethod(){
System.out.println("secondMethod()");
}
}
----------------------------------------------------------------------------------------
class CallStackTest2
{
public static void main(String[] args)
{
System.out.println("main(String[] args) 이 시작되었음.");
firstMethod();
System.out.println("main(String[] args) 이 끝났음.");
}
static void firstMethod(){
System.out.println("firstMethod() 이 시작되었음.");
secondMethod();
System.out.println("firstMethod() 이 끝났음.");
}
static void secondMethod(){
System.out.println("secondMethod() 이 시작되었음.");
System.out.println("secondMethod() 이 끝났음.");
}
}
----------------------------------------------------------------------------------------
class CardTest
{
public static void main(String[] args)
{
System.out.println("Card.width = " + Card.width);
System.out.println("Card.height = " + Card.height);
Card c1 = new Card();
c1.kind = "Heart";
c1.number = 7;
Card c2 = new Card();
c2.kind = "Space";
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.width = 50; //Card.width = 50;
c1.height = 80; //Card.height = 80;
System.out.println("c1의 width와 height를 각각 50,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;
}
----------------------------------------------------------------------------------------
class Car{
String color;
String gearType;
int door;
Car() {};
Car(String color, String gearType, int door) {
this.color = color;
this.gearType = gearType;
this.door = door;
};
}
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 + "c1의 gearType=" + c1.gearType + "c1의 door=" + c1.door);
System.out.println("c2의 color=" + c2.color +"c2의 gearType=" + c2.gearType +"c2의 door=" + c2.door);
}
}
----------------------------------------------------------------------------------------
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;
};
}
class CarTest2
{
public static void main(String[] args)
{
Car c1 = new Car();
Car c2 = new Car("blue");
System.out.println("c1의 color=" + c1.color + ", c1의 gearType=" + c1.gearType + ", c1의 door=" + c1.door);
System.out.println("c2의 color=" + c2.color +", c2의 gearType=" + c2.gearType +", c2의 door=" + c2.door);
}
}
----------------------------------------------------------------------------------------
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.color = color;
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 + ", c1의 gearType=" + c1.gearType + ", c1의 door=" + c1.door);
System.out.println("c2의 color=" + c2.color +", c2의 gearType=" + c2.gearType +", c2의 door=" + c2.door);
c1.door = 100;
System.out.println("c1.door = 100; 수행 후");
System.out.println("c1의 color=" + c1.color + ", c1의 gearType=" + c1.gearType + ", c1의 door=" + c1.door);
System.out.println("c2의 color=" + c2.color +", c2의 gearType=" + c2.gearType +", c2의 door=" + c2.door);
}
}
----------------------------------------------------------------------------------------
class Date1 {
int value;
}
class Date2{
int value;
Date2(int x){
value = x;
}
}
class ConstructorTest
{
public static void main(String[] args)
{
Date1 d1 = new Date1();
Date2 d2 = new Date2(5);
//Date2 d2 = new Date2(); //error
}
}
----------------------------------------------------------------------------------------
class Document{
static int count=0;
String name;
public Document() {
this("제목없음" + ++count);
//System.out.println("문서 제목없음" + ++count + "가 생성되었습니다.");
}
public Document(String name){
this.name = name;
System.out.println("문서 " + name + "가 생성되었습니다.");
}
}
class DocumentTest
{
public static void main(String[] args)
{
Document d1 = new Document();
Document d2 = new Document("자바.txt");
Document d3 = new Document();
Document d4 = new Document();
}
}
----------------------------------------------------------------------------------------
class FactorialTest
{
public static void main(String[] args)
{
System.out.println(factorial(4));
}
static long factorial(int n){
long result = 0;
if (n == 1) {
result = 1;
} else result = n * factorial(n-1);
return result;
}
}
----------------------------------------------------------------------------------------
class MainTest
{
public static void main(String[] args)
{
main(null); //자기 자신을 다시 호출한다.
}
}
----------------------------------------------------------------------------------------
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(result1);
System.out.println(result2);
System.out.println(result3);
System.out.println(result4);
}
}
class MyMath{
long add(long a, long b){
return a + b; //int 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; }
}
----------------------------------------------------------------------------------------
class MyMath2 {
long a, b;
long add(){ return a + b; }
long substract(){ 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 substract(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.substract(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.substract());
System.out.println(mm.multiply());
System.out.println(mm.divide());
}
}
----------------------------------------------------------------------------------------
class OverloadTest
{
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(long a, int b){
System.out.print("long add(long 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, long b){
System.out.print("int 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;
}
}
----------------------------------------------------------------------------------------
class Data {
int x;
}
class ParameterTest
{
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("change() : x = " + x);
}
}
----------------------------------------------------------------------------------------
class Data {
int x;
}
class ParameterTest2
{
public static void main(String[] args)
{
Data d = new Data();
d.x = 10;
System.out.println("main() : x = " + d.x);
change(d);
System.out.println("After change(d.x)");
System.out.println("main() : x = " + d.x);
}
static void change(Data d){
d.x = 1000;
System.out.println("change() : x = " + d.x);
}
}
----------------------------------------------------------------------------------------
class Data {
int x;
}
class ParameterTest3
{
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]);
}
}
----------------------------------------------------------------------------------------
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){
//return (n == 1) ? x : x * power(x, n-1);
if (n == 1) {
return x;
} else { return x * power(x, n-1); }
}
}
----------------------------------------------------------------------------------------
class Product{
static int count = 0;
int serialNo;
{
count++;
serialNo = count;
}
}
class ProductTest
{
public static void main(String[] args)
{
Product p1 = new Product();
Product p2 = new Product();
Product p3 = new Product();
System.out.println("p1의 제품번호(serial no)는 " + p1.serialNo);
System.out.println("p2의 제품번호(serial no)는 " + p2.serialNo);
System.out.println("p3의 제품번호(serial no)는 " + p3.serialNo);
System.out.println("생산된 제품의 수는 모두 " + Product.count + "개 입니다.");
}
}
----------------------------------------------------------------------------------------
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 StaticBlockTest
{
static int[] arr = new int[10];
static {
for (int i=0; i<arr.length; i++) {
arr[i] = (int)(Math.random()*10)+1;
}
}
public static void main(String[] args)
{
for (int i=0; i<arr.length; i++) {
System.out.println("arr[" + i + "] :" + arr[i]);
}
}
}
----------------------------------------------------------------------------------------
class Tv{
// Tv의 속성 (맴버변수)
String color; // 색상
boolean power; // 전원상태(on/off)
int channel; //채널
// Tv의 기능 (메서드)
void power() { power = !power; }
void channelUp() { ++channel; }
void channelDown() { --channel; }
}
class TvTest
{
public static void main(String[] args)
{
Tv t = new Tv(); // Tv 인스턴스를 참조하기 위한 참조변수 t선언 후 Tv 인스턴스 생성한다.
t.channel = 7; // Tv 인스턴스의 멤버변수 channel의 값을 7로 한다.
t.channelDown(); // Tv 인스턴스의 메서드 channelDown()을 호출한다.
System.out.println("현재 채널은 " + t.channel + "입니다."); //
}
}
----------------------------------------------------------------------------------------
class Tv{
// Tv의 속성 (맴버변수)
String color; // 색상
boolean power; // 전원상태(on/off)
int channel; //채널
// Tv의 기능 (메서드)
void power() { power = !power; }
void channelUp() { ++channel; }
void channelDown() { --channel; }
}
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값을 " + t1.channel + "로 변경하였습니다.");
System.out.println("t1의 channel값은 " + t1.channel + "입니다.");
System.out.println("t2의 channel값은 " + t2.channel + "입니다.");
}
}
----------------------------------------------------------------------------------------
class Tv{
// Tv의 속성 (맴버변수)
String color; // 색상
boolean power; // 전원상태(on/off)
int channel; //채널
// Tv의 기능 (메서드)
void power() { power = !power; }
void channelUp() { ++channel; }
void channelDown() { --channel; }
}
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값을 " + t1.channel + "로 변경하였습니다.");
System.out.println("t1의 channel값은 " + t1.channel + "입니다.");
System.out.println("t2의 channel값은 " + t2.channel + "입니다.");
}
}
----------------------------------------------------------------------------------------
'자기개발 > Java' 카테고리의 다른 글
Chapter 8 예외처리(Exception Handling) (0) | 2012.01.21 |
---|---|
Chapter 7 객체지향 프로그래밍 2 (0) | 2012.01.21 |
Chapter 5 배열(Array) (0) | 2012.01.21 |
Chapter 4 조건문과 반복문 (0) | 2012.01.12 |
Chapter 3 연산자 (0) | 2012.01.12 |