class JavaPractice {
static int[][] randomArr = { { 11, 12, 13, 14 }, { 21, 22, 23, 24 }, { 31, 32, 33, 34 }, { 41, 42, 43, 44 }, };
public static void main(String[] args) {
for (int i = 0; i < randomArr.length; i++) {
for (int j = 0; j < randomArr[i].length; j++) {
System.out.print(randomArr[i][j] + "\t");
}
System.out.println();
}
System.out.println("********************************");
for (int[] arr : randomArr) {
for (int num : arr) {
System.out.print(num + "\t");
}
System.out.println();
}
}
}
class A{}
class B extends A{}
class JavaPractice {
public static void main(String[] args) {
A a = new B();
// A a = (A) new B();
// 컴파일러가 자동적으로 형 변환을 해준다.
//B b = a; 는 컴파일 에러
B b = (B)a;
// 강제로 형 변환을 선언한다.
}
}
class Box {
public void simpleWrap() {
System.out.println("Simple Wrapping");
}
}
class PaperBox extends Box {
public void paperWrap() {
System.out.println("Paper Wrapping");
}
}
class GoldPaperBox extends PaperBox {
public void goldWrap() {
System.out.println("Gold Wrapping");
}
}
class JavaPractice {
public static void wrapBox(Box box) {
if (box instanceof GoldPaperBox) {
// GoldPaperBox aaa = (GoldPaperBox) box; 가능
((GoldPaperBox) box).goldWrap();
} else if (box instanceof PaperBox) {
// PaperBox aaa = (PaperBox) box; 가능
((PaperBox) box).paperWrap();
} else {
box.simpleWrap();
}
}
public static void main(String[] args) {
Box b1 = new Box();
PaperBox b2 = new PaperBox();
GoldPaperBox b3 = new GoldPaperBox();
wrapBox(b1);
wrapBox(b2);
wrapBox(b3);
}
}
[Console]
Simple Wrapping
Paper Wrapping
Gold Wrapping
class Friend {
protected String name;
protected String phone;
public Friend(String name, String phone) {
this.name = name;
this.phone = phone;
}
public void showInfo() {
System.out.println("이름 : " + name);
System.out.println("전화 : " + phone);
}
}
/*******************************************/
class CompFriend extends Friend {
private String department;
public CompFriend(String name, String department, String phone) {
super(name, phone);
this.department = department;
}
@Override
public void showInfo() {
super.showInfo();
System.out.println("부서 : " + department);
}
}
/*******************************************/
class UnivFriend extends Friend {
private String major;
public UnivFriend(String name, String major, String phone) {
super(name, phone);
this.major = major;
}
@Override
public void showInfo() {
super.showInfo();
System.out.println("전공 : " + major);
}
}
/*******************************************/
public class FriendsPractice {
public static void main(String[] args) {
Friend[] frns = new Friend[10];
int count = 0;
frns[count++] = new UnivFriend("LEE", "Computer", "010-333-555");
frns[count++] = new UnivFriend("SEO", "Electronics", "010-323-545");
frns[count++] = new CompFriend("YOON", "R&D 1", "02-321-999");
frns[count++] = new CompFriend("PARK", "R&D 2", "02-123-555");
for (int i = 0; i < count; i++) {
frns[i].showInfo(); // 오버라이딩이 적용된 자식 클래스가 출력된다.
System.out.println();
}
}
}
[Console]
이름 : LEE
전화 : 010-333-555
전공 : Computer
이름 : SEO
전화 : 010-323-545
전공 : Electronics
이름 : YOON
전화 : 02-321-999
부서 : R&D 1
이름 : PARK
전화 : 02-123-555
부서 : R&D 2
java.lang.Object
import java.util.Scanner;
class Course {
String[] name;
int[] num;
public Course(String[] name, int[] num) {
this.name = name;
this.num = num;
}
public void search(String a) {
boolean flag = false;
int score = 0;
for (int i = 0; i < name.length; i++) {
if (a.equals(name[i])) {
flag = true;
score = num[i];
}
}
if (flag == false) {
System.out.println("찾는 과목이 없습니다.");
} else {
System.out.println(a + "의 점수는 " + score);
}
}
}
class JavaTest {
public static void main(String[] args) {
String course[] = { "Java", "C++", "HTML5", "컴퓨터구조", "안드로이드" };
int score[] = { 95, 88, 76, 62, 55 };
Scanner sc = new Scanner(System.in);
while (true) {
Course c = new Course(course, score);
System.out.println("과목 입력 >> ");
String s = sc.nextLine();
if (s.equals("그만")) {
break;
} else {
c.search(s);
}
} // end of while
}
}
[Console]
과목 입력 >>
안드로이드
안드로이드의 점수는 55
과목 입력 >>
컴퓨터구조
컴퓨터구조의 점수는 62
과목 입력 >>
아이폰
찾는 과목이 없습니다.
과목 입력 >>
그만