자바실습 예제
다음 어프리케이션을 컴파일해서 화면에 전시하시오. class Box { int width; int height; int depth; } class TwoBox { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); int vol1, vol2; // 첫번째 박스객체 생성부 mybox1.width = 20; mybox1.height = 40; mybox1.depth = 15; // 두번째 박스객체 생성부 mybox2.width = 10; mybox2.height = 20 ; mybox2.depth = 30; vol1 = mybox1.width * mybox1.height * mybox1.depth; System.out.println(" 첫번째 박스의 부피는 " + vol1 + "입니다."); vol2 = mybox2.width * mybox2.height * mybox2.depth; System.out.println(" 두번째 박스의 부피는 " + vol2 + "입니다."); } } Bitwise.java class Bitwise { public static void main(String args[]) { byte bi1 = 0x31; // 0011 0001 byte bi2 = 0x22; // 0010 0010 int result1 = bi & bi2; // 0010 0000 -> 십진수로는 32 int result2 = bi | bi2; // 0011 0011 -> 십진수로는 51 int result3 = bi ^ bi2; // 0001 0011 -> 십진수로는 19 System.out.println(result1); System.out.println(result2); System.out.println(result3); } } ColorCar.java class Car { int velocity; // 속도를 정수형으로 선언 int wheelNum; // 바퀴의 수를 정수형으로 선언 String carName1; // 차의 이름을 String 클래스로 선언 // 디폴트 생성자 // 이것이 없으면 하위 클래스 생성시 에러 발생 public Car() { } // 생성자이다. 문자열을 String 클래스로 받아서 저장한다. public Car(String name) { carName = name; } // 초기값으로 속도를 주는 생성자. public Car(int speed) { velocity = speed; } // 초기값으로 속도와 이름을 주는 생성자. public Car(int speed,String name) { this(speed); carName = name; } // 속도를 높인다. // 멤버 변수인 velocity에 1을 더해주는 동작을 한다. public void speedUp1() { velocity = velocity + 1; } // 오버로딩된 메서드. // 주어진 스피드로 속도를 셋팅한다. public void speedUp(int speed) { velocity += speed; } // 속도를 낮춘다. public void speedDown() { velocity = velocity - 1; if(velocity < 0) velocity = 0; } // 멈춘다. public void stop1() { velocity = 0; } } class ColorCar extends Car { // 새로운 멤버변수의 추가 String carColor; public ColorCar(String c,String name,int speed) { carName = name1; velocity = speed; carColor = c; } public static void main(String args[]) { ColorCar cc; // 정의된 생성자를 호출한다. cc = new ColorCar("하얀색","소나타",100); // 제대로 메서드가 호출되었는지를 확인한다. System.out,println(cc.carName + " : " + cc.velocity + " : " + cc.carColor); } } AccessCar.java class Car { private int velocity; // 속도를 정수형으로 선언 private int wheelNum; // 바퀴의 수를 정수형으로 선언 private String carName; // 차의 이름을 String 클래스로 선언 // 디폴트 생성자 // 이것이 없으면 하위 클래스 생성시 에러 발생 public Car() { } // 생성자이다. 문자열을 String 클래스로 받아서 저장한다. public Car(String name) { carName = name; } // 초기값으로 속도를 주는 생성자. public Car(int speed) { velocity = speed; } // 초기값으로 속도와 이름을 주는 생성자. public Car(int speed,String name) { this(speed); carName = name; } // 속도를 높인다. // 멤버 변수인 velocity에 1을 더해주는 동작을 한다. public void speedUp() { velocity = velocity + 1; } // 오버로딩된 메서드. // 주어진 스피드로 속도를 셋팅한다. public void speedUp(int speed) { velocity += speed; } // 속도를 낮춘다. public void speedDown() { velocity = velocity - 1; if(velocity < 0) velocity = 0; } // 멈춘다. public void stop() { velocity = 0; } } class AccessCar { public static void main(String args[]) { Car myCar; myCar = new Car(100,"카렌스"); System.out.println(myCar.carName + " : " + myCar.velocity); } }