// 하나의 소스파일에는 하나의 클래스만 작성하는 것이 바람직
public class Ex6_1 {} // 소스파일의 이름은 public class이름과 일치
class hello2{} // 하나의 소스파일에는 하나의 public class 만 허용
class hello3{} // public class가 없다면 이름이 일치하는게 하나만 있다면 상관 없음
public class Ex6_2 {
public static void main(String[] args) {
Tv t; // Tv 인스턴스를 참조하기 위한 변수 t를 선언.
t = new Tv(); // Tv인스턴스를 생성한다.
t.channel = 7; // Tv인스턴스의 멤버변수 channel의 값을 7로 한다.
t.channelDown(); // Tv인스턴스의 메서드 channelDown()을 호출한다.
System.out.println("현재 채널은" + t.channel + " 입니다. ");
}
}
class Tv {
// Tv의 속성(멤버 변수)
String color; // 색상
boolean power; // 전원
int channel; // 채널
// Tv의 기능(메서드)
void power() { power = !power; } // Tv의 전원을 키고 끄는 기능을 하는 메서드
void channelUp() { ++channel; } // Tv채널을 높이는 기능을 하는 메서드
void channelDown() { --channel; } // Tv채널을 낮추는 기능을 하는 메서드.
}
public class Ex6_3 {
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로 변경한다.");
t2 = t1;
System.out.println("t1의 channel 값은" + t1.channel + "입니다.");
System.out.println("t2의 channel 값은" + t2.channel + "입니다.");
}
}
public class Ex6_4 {
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 = "spade";
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 + ")");
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;
}
public class Ex6_5 {
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;
// return a + b; // 위의 두 줄을 이와 같이 한 줄로 간단히 할 수 있다.
}
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;
}
}