Comparable & Comparator Interface

PersonStudentInterface

/////////////////////////////////

import java.util.*;

class Person implements Comparable<Person>
{
private static int count = 0; // static (class) variables

protected String name; // instance variables
protected int age; // instance variables

public Person()
{
//System.out.println(“Person Constructor”); // this(“”, 0); error: call to this must be first statemenht in constructor
this(“”, 0);
}

public Person(String name, int age)
{
count++;
this.name = name;
this.age = age;
}

public Person(Person other)
{
this(other.name, other.age);
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public int getAge()
{
return age;
}

public void setAge(int age)
{
this.age = age;
}

@Override
public boolean equals(Object other) // Object.equals overriding
{
if (other instanceof Person) {
Person that = (Person) other;
return that.canEqual(this) && this.getName().equals(that.getName()) && this.getAge() == that.getAge();
}
return false;
}

@Override
public int hashCode() {
return (41 * getName().hashCode() + getAge());
}

public boolean canEqual(Object other) {
return (other instanceof Person);
}

@Override
public String toString() // Object.toString() overriding
{
return “Person Name: ” + name + ” Age: ” + age;
}

public void print() // instance methods
{
System.out.println(“Person Name: ” + name + ” Age: ” + age);
}

public void set(String name, int age)
{
this.name = name;
this.age = age;
}

public void set(Person other)
{
this.name = other.name;
this.age = other.age;
}

public Person clone()
{
Person p = new Person(this.name, this.age);
return p;
}

public static void printCount() // static (class) methods
{
System.out.println(“Person Count: ” + count);
}

public static int getCount() { return count; } // static (class) methods
public static void setCount(int value) { count = value; } // static (class) methods

public int compareTo(Person other)
{
String thisName = this.getName().toUpperCase();
String otherName = ((Person)other).getName().toUpperCase();

//ascending order
return thisName.compareTo(otherName);
//descending order
//return otherName.compareTo(thisName);
}

public static Comparator<Person> AgeComparator
= new Comparator<Person>() {

public int compare(Person p1, Person p2) {

int age1 = p1.getAge();
int age2 = p2.getAge();

//ascending order
return age1 – age2;

//descending order
//return age2 – age1;
}

};

}

///////////////////////////////////////////////////////////////////////////////////////

class Student extends Person
{
private static int count = 0; // static (class) variables
protected int id;

public Student()
{
id = 5208;
}

public Student(String name, int age, int id)
{
super(name, age);
this.id = id;
count++;
}

public int getID()
{
return id;
}

public void setID(int id)
{
this.id = id;
}

@Override
public boolean equals(Object other) // Object.equals overriding
{
if (other instanceof Student) {
Student that = (Student) other;
return that.canEqual(this) && this.getName().equals(that.getName()) && this.getAge() == that.getAge() && this.getID() == that.getID();
}
return false;
}

@Override
public int hashCode() {
return (41 * super.hashCode() + getID());
}

public boolean canEqual(Object other) {
return (other instanceof Student);
}

@Override
public String toString() // Object.toString() overriding
{
return “Student Name: ” + name + ” Age: ” + age + ” ID: ” + id;
}

public void superPrint()
{
super.print();
}

public void print() // Person class print() method overriding
{
System.out.println(“Student Name: ” + name + ” Age: ” + age + ” ID: ” + id);
}

public void set(String name, int age, int id)
{
super.set(name, age);
this.id = id;
}

public void set(String name, int age)
{
super.set(name, age);
}

public void set(Student other)
{
this.set(other.name, other.age, other.id);
}

public void set(Person other)
{
if (other instanceof Person)
super.set(other);
else
this.set((Student)other);
}

public Student clone()
{
Student s = new Student(this.name, this.age, this.id);
return s;
}

public static void printCount() // static (class) methods
{
System.out.println(“Student Count: ” + count);
}

public static int getCount() { return count; } // static (class) methods
public static void setCount(int value) { count = value; } // static (class) methods

public int compareTo(Student other)
{
String thisName = this.getName().toUpperCase();
String otherName = ((Student)other).getName().toUpperCase();

//ascending order
return thisName.compareTo(otherName);
//descending order
//return otherName.compareTo(thisName);
}

public static Comparator<Student> AgeComparator
= new Comparator<Student>() {
public int compare(Student p1, Student p2) {
int age1 = p1.getAge();
int age2 = p2.getAge();

//ascending order
return age1 – age2;

//descending order
//return age2 – age1;
}

};

public static Comparator<Student> IDComparator
= new Comparator<Student>() {
public int compare(Student p1, Student p2) {
int id1 = p1.getID();
int id2 = p2.getID();

//ascending order
return id1 – id2;

//descending order
//return id2 – id1;
}

};
}

 

class PersonStudentTest
{

public static void print(Object[] array) {
for(Object o : array) {
System.out.println(o);
}
}

public static void main(String[] args)
{

///////////////////////////////////////////////////////////////////////

Student[] sList = new Student[3];
sList[0] = new Student(“Kevin”, 0, 222);
sList[1] = new Student(“Jason”, 1, 333);
sList[2] = new Student(“John”, 2, 111);

System.out.println(“STUDENT SORT BY NAME (DEFAULT)!!!”);
Arrays.sort(sList);
print(sList);

System.out.println(“STUDENT SORT by AGE!!!”);
Arrays.sort(sList, Student.AgeComparator);
print(sList);

System.out.println(“STUDENT SORT by ID!!!”);
Arrays.sort(sList, Student.IDComparator);
print(sList);

///////////////////////////////////////////////////////////////////////
Student[] sList2 = new Student[3];
sList2[0] = new Student(“Kevin”, 0, 222);
sList2[1] = new Student(“Jason”, 1, 333);
sList2[2] = new Student(“John”, 2, 111);

System.out.println(“STUDENT SORT BY NAME (anonymous method)!!!”);
Arrays.sort(sList2, new Comparator<Student>() {
public int compare(Student s1, Student s2) {
return s1.getName().toUpperCase().compareTo(s2.getName().toUpperCase());
}
});
print(sList2);

System.out.println(“STUDENT SORT by AGE (anonymous method)!!!”);
Arrays.sort(sList2, new Comparator<Student>() {
public int compare(Student s1, Student s2) {
return s1.getAge() – s2.getAge();
}
});
print(sList2);

System.out.println(“STUDENT SORT by ID (anonymous method)!!!”);
Arrays.sort(sList2, new Comparator<Student>() {
public int compare(Student s1, Student s2) {
return s1.getID() – s2.getID();
}
});
print(sList2);

///////////////////////////////////////////////////////////////////////
Student[] sList3 = sList2;

System.out.println(“STUDENT SORT BY NAME (lambda)!!!”);
Arrays.sort(sList3, (Student ss1, Student ss2) ->
ss1.getName().compareTo(ss2.getName())
);
Arrays.stream(sList3).forEach((s) -> System.out.println(s));

System.out.println(“STUDENT SORT by AGE (lambda)!!!”);
Arrays.sort(sList3, (Student ss1, Student ss2) ->
Integer.compare(ss1.getAge(), ss2.getAge())
);
Arrays.stream(sList3).forEach((s) -> System.out.println(s));

System.out.println(“STUDENT SORT by ID (lambda)!!!”);
Arrays.sort(sList3, (Student ss1, Student ss2) ->
Integer.compare(ss1.getID(), ss2.getID())
);
Arrays.stream(sList3).forEach((s) -> System.out.println(s));

///////////////////////////////////////////////////////////////////////
List<Student> sList4 = new ArrayList<Student>();
sList4.add(new Student(“Kevin”, 0, 222));
sList4.add(new Student(“Jason”, 1, 333));
sList4.add(new Student(“John”, 2, 111));

System.out.println(“STUDENTLIST SORT BY NAME (lambda)!!!”);
sList4.sort((Student ss1, Student ss2) ->
ss1.getName().compareTo(ss2.getName())
);
sList4.forEach((s) -> System.out.println(s));

System.out.println(“STUDENTLIST SORT by AGE (lambda)!!!”);
sList4.sort((Student ss1, Student ss2) ->
ss1.getAge() – ss2.getAge()
);
sList4.forEach((s) -> System.out.println(s));

System.out.println(“STUDENTLIST SORT by ID (lambda)!!!”);
sList4.sort((Student ss1, Student ss2) ->
ss1.getID() – ss2.getID()
);
sList4.forEach((s) -> System.out.println(s));

}

}

Midterm Exam

중간고사

시험범위: 처음부터 – 배운데까지 (강의노트, 실습, 수업블로그)

주요내용: 기본문법, enum, array, class, abstract class, interface, parameter passing (comparable & comparator & equals는 안나옴)

일시 및 장소: 2017년 10월 23일 (월) 1:00 – 2:30 (2공학관 524호)

java16-midterm-answer

Person Student Class

// Person

class Person
{
private static int count = 0; // static (class) variables
protected String name; // instance variables
protected int age; // instance variables
public Person() { // default constructor
this(“”, 0);
}
public Person(String name, int age) {
count++;
this.name = name;
this.age = age;
}
public String getName(){
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void set(String name, int age){
this.name = name;
this.age = age;
}
public void set(Person other){
this.set(other.name, other.age);
}
public Person clone(){
return new Person(this.name, this.age);
}
public String toString() { // Object.toString() overriding
return “Person Name: ” + name + ” Age: ” + age;
}
public void print() { // instance method & virtual method
System.out.println(“Person Name: ” + name + ” Age: ” + age);
}
public static void printCount(){ // static (class) methods
System.out.println(“Person Count: ” + count);
}
public static int getCount() { return count; } // static (class) methods
public static void setCount(int value) { count = value; } // static (class) methods
}

// Student

class Student extends Person
{
private static int count = 0; // static (class) variables
protected int id;
public Student() {
id = 5208;
}
public Student(String name, int age, int id) {
super(name, age);
this.id = id;
count++;
}
public int getID() {
return id;
}
public void setID(int id) {
this.id = id;
}
public void set(String name, int age, int id){
super.set(name, age);
this.id = id;
}
public void set(String name, int age){
super.set(name, age);
}
public void set(Student other){
this.set(other.name, other.age, other.id);
}
public void set(Person other){
if (other instanceof Student)
this.set((Student)other);
else
super.set(other);
}
public Student clone(){
return new Student(this.name, this.age, this.id);
}
public String toString() { // Object.toString() overriding
return “Student Name: ” + name + ” Age: ” + age + ” ID: ” + id;
}
public void print() { // Person class print() method overriding
System.out.println(“Student Name: ” + name + ” Age: ” + age + ” ID: ” + id);
}
public void superPrint() {
super.print();
}
public static void printCount() { // static (class) methods
System.out.println(“Student Count: ” + count);
}
public static int getCount() { return count; } // static (class) methods
public static void setCount(int value) { count = value; } // static (class) methods
}

// PersonStudentTest

class PersonStudentTest
{
static void print(Object o) {
System.out.println(o);
}
static void print(Person[] arr) {
for (Person p : arr)
System.out.println(p);
}
public static void main(String[] args)
{
Person h1 = new Person(“JAVA”, 2016);
h1.print();
print(h1);
System.out.println(h1);

Person h2 = new Student(); // 기본생성자를 불렀을때 자기 count를 증가시키지 않는다
//h2.print();
// print(h2);
//System.out.println(h2);

Object o = h2; // upcasting
print(o); // dynamic binding
((Student)o).print();
((Person)o).print(); // o=> Person type => Person.print() => dynamic binding Student.print()

Person.printCount(); // calls Person printCount()
Student.printCount(); // calls Student printCount()
System.out.println();
}
}

// PersonStudentTest

class PersonStudentTest
{
static void print(Object o) {
System.out.println(o);
}
static void print(Person[] arr) {
for (Person p : arr)
System.out.println(p);
}
public static void main(String[] args)
{
Person h1 = new Person(“JAVA”, 2016);
h1.print();
print(h1);
System.out.println(h1);

Person h2 = new Student(); // 기본생성자를 불렀을때 자기 count를 증가시키지 않는다
//h2.print();
// print(h2);
//System.out.println(h2);

Object o = h2; // upcasting
print(o); // dynamic binding
((Student)o).print();
((Person)o).print(); // o=> Person type => Person.print() => dynamic binding Student.print()

Person.printCount(); // calls Person printCount()
Student.printCount(); // calls Student printCount()
System.out.println();

Person[] pList = new Person[5];
pList[0] = new Person(“JAVA1”, 1);
pList[0].print();
pList[1] = pList[0];
pList[1].print();
pList[2] = new Student(); // default constructor count증가 안함
pList[2].print();
pList[3] = new Student(“JAVA2”, 2, 222); // Student.count++
pList[3].print();
pList[4] = pList[3];
pList[4].print();

System.out.println(“AFTER SET”);
pList[0].set(“JAVA3”, 3); // Person.set(string, int)
pList[4].set(“JAVA4”, 4); // Person.set(string, int)
print(pList);

System.out.println(“AFTER SET2”);
Student s = (Student)pList[4]; // down casting
s.set(“JAVA5”, 5, 555); // Student.set(string, int, int)
print(pList);

System.out.println(“AFTER SET3”);
pList[0].set(pList[4]); // Person.set(Person)
print(pList);

System.out.println(“AFTER SET4”);
((Student)pList[2]).set(pList[4]); // Student.set(Person)
//((Student)pList[2]).set((Student)pList[4]); // Student.set(Student)
print(pList);

System.out.println(“AFTER SET5”);
((Student)pList[2]).set(new Person(“JAVA6”, 6)); // Student.set(Person)
//((Student)pList[2]).set((Student)pList[4]); // Student.set(Student)
print(pList);

Person.printCount(); // calls Person printCount()
Student.printCount(); // calls Student printCount()
System.out.println();
}
}

 

Car Sedan Class

public class Car  {

// member field
private boolean disel; // private은 파생 클래스에서 사용하지 못함
protected boolean gasoline; // protected은 파생 클래스에서 사용 가능하나 외부(다른 패키지)에서는 호출하지 못함
protected int wheel = 4;

// constructor
protected Car() { disel = true; gasoline = true; }
protected Car(int wheel) { this.wheel = wheel; disel = false; gasoline = false; }

// method
protected void Move()
{
if (disel)
System.out.println(“Disel Car”);
System.out.println(“Move wheel=” + wheel);
}

}

public class Sedan extends Car  {

// member field
private boolean gasoline; // 파생 클래스에서 기반클래스에 멤버필드명이 같다면 default는 자신의 멤버부터 호출 & 
this.gasoline을 사용함 

// constructor
public Sedan() { gasoline = false; } // 내부적으로 super() 호출
public Sedan(int wheel)  { super(wheel); gasoline = true; } //
super(int wheel)를 호출 – protected 생성자는 파생클래스에서 호출 가능

// method
public void sedanMove()
{

// base의 gasoline과 this의 gasoline을 구분해야하는 경우
if (super.gasoline)
System.out.print(“Gasoline Car “);
if (this.gasoline)
System.out.print(“Gasoline SedanCar “);

System.out.println(“move wheel=” + wheel);

}

static void Main(string[] args)
{

Car myCar = new Car(); // protected 생성자 같은 패키지 내에 사용가능.
myCar.move(); // protected 메소드 호출 가능
System.out.println(“myCar wheel=” +  myCar.wheel); // protected 필드 호출 가능

// 바퀴 4개 디젤 자동차
Sedan myCar1 = new Sedan(); // public 생성자 호출 가능
myCar1.move(); // protected 메소드 호출 가능
myCar1.sedanMove(); // public 메소드 호출가능

 

// 바퀴 6개 가솔린 자동차
Sedan myCar2 = new Sedan(6); // public 생성자 호출 가능
myCar2.move(); // protected 메소드 호출 가능
myCar2.sedanMove(); // public 메소드 호출가능

}

}

BankAccount Class

class BankAccount
{

double balance; // instance field
string name; // instace field
static double interest; // static field

public BankAccount() // default constructor
{

this(1000, “JAVA17”) ;
}

public BankAccount(double balance, string name)
{

this.balance = balance;
this.name = name;
BankAccount.interest = 0.7; // 0.7%

}

public void deposit(double amount) // instance method
{

balance += amount;

}

public void withdrawal(double amount) // instance method
{

balance -= amount;

}

public void print() // instance method
{

double currentBalance = balance + balance * interest * 0.01;

System.out.printf(“%s의 계좌 잔고는 %f\n”, name, currentBalance);

}

public static void setInterestRate(double interest) // static method
{

BankAccount.interest = interest;

}

}

 

class BankAccountTest {

public static void main(String[] args) {

BankAccount b = new BankAccount(5000, “HCI222222222222”);
b.print();

b.deposit(1000);
b.print();

b.withdrawal(500);
b.print();

BankAccount.setInterestRate(1.5); // 1.5%
b.print();

BankAccount b2 = new BankAccount();
b2.print();

}

}

Java swap pass-by-value

static void swap(int p, int q) // Java uses pass-by-value
{
int t = p;
p = q;
q = t;
}

static void swap2(int[] p, int[] q)  // Java uses pass-by-value
{
int t = p[0];
p[0] = q[0];
q[0] = t;
}

static void swap(int[] p, int[] q)  // Java uses pass-by-value
{
int[] t = p;
p = q;
q = t;
}

static void swap2(int[][] p, int[][] q)  // Java uses pass-by-value
{
int[] t = p[0];
p[0] = q[0];
q[0] = t;
}

static void swap(Point p, Point q)        // Java uses pass-by-value
{
Point t = p;
p = q;
q = t;
}

static void swap2(Point p, Point q)        // Java uses pass-by-value
{
double[] t = p.get(); // T t = p
p.set(q.get());  // p = q
q.set(t);  // q = t
}

////////////////////////////////////////

public static void main(String[] args)
{
Point p1 = new Point(10.5, 10.7);
Point p2 = new Point(1.5, 1.7);

swap(p1, p2);
System.out.println(“After swap p1=” + p1 + ” p2 ” + p2); // p1=(10.5, 10.7) p2=(1.5, 1.7)
swap2(p1, p2);
System.out.println(“After swap2 p1=” + p1 + ” p2 ” + p2); // p1=(1.5, 1.7) p2=(10.5, 10.7)

int[] arr1 = { 1, 2, 3 };
int[] arr2 = { 4, 5, 6, 7, 8 };
swap(arr1, arr2);
System.out.print(“arr1: “);
System.out.println(Arrays.toString(arr1)); // arr1: [1, 2, 3]
System.out.print(“arr2: “);
System.out.println(Arrays.toString(arr2)); // arr2: [4, 5, 6, 7, 8]

int[][] array1 = { new int[] { 1, 2, 3 } };
int[][] array2 = { new int[] { 4, 5, 6, 7, 8 } };
swap2(array1, array2);
System.out.print(“array1: “);
System.out.println(Arrays.toString(array1[0])); // array1: [4, 5, 6, 7, 8]
System.out.print(“array2: “);
System.out.println(Arrays.toString(array2[0])); // array2: [1, 2, 3]

int a = 10;
int b = 20;
swap(a, b);
System.out.println(“a=” + a); // a=10
System.out.println(“b=” + b); // b=20

int[] a2 = { 10 };
int[] b2 = { 20 };
swap2(a2, b2);
System.out.println(“a2=” + a2[0]); // a2 = 20
System.out.println(“b2=” + b2[0]); // b2 = 10
}

 

Java Parameter Passing: Pass-by-value only

//pass value type by value (값형식을 값에 의한 전달) – copy of value 전달

static void square1(int x) {
x *= x;
System.out.printf(“The value inside square1: %d\n”, x);
}
public static void main(String[] args)
{
int i = 5;
System.out.println(“Before i=” + i);
square1(i);
System.out.println(“After i=” + i + “\n\n”);
}
//Before i=5
//The value inside square1: 25
//After i=5

//pass reference type by value (참조형식을 값에 의한 전달) – copy of reference 전달

static void square2(IntValue value) {
value.x *= value.x;
System.out.printf(“The value inside square2: %d\n”, value.x);
}
public static void main(String[] args)
{
int i = 5;
IntValue value = new IntValue(i);
System.out.println(“Before value.x=” +value.xi);
square2(value);
System.out.println(“After value.x=” + value.x + “\n\n”);
}
//Before value.x=5
//The value inside square2: 25
//After value.x=25
//pass reference type by value (참조형식을 값에 의한 전달) – copy of reference 전달

static void changeArray1(int[] arr)
{

arr[0]=888; // arr -> myArray이므로 원본 배열의 첫번째 값은 888로 변경
System.out.println(“changeArray1 arr=” + Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.joining(“,”)));
arr = new int[] {-3, -1, -2, -3, -4}; // local 변수로 새롭게 할당하여 지정 그러나 원본 배열 변경 안됨
System.out.println(“changeArray1 arr=” + Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.joining(“,”)));
}
 
public static void main(String[] args)
{
int[] myArray = {1, 4, 5};
System.out.println(“Before myArray=” + Arrays.stream(myArray).mapToObj(String::valueOf).collect(Collectors.joining(“,”)));
changeArray1(myArray);
System.out.println(“After changeArray1 myArray=” + Arrays.stream(myArray).mapToObj(String::valueOf).collect(Collectors.joining(“,”)));
}
//Before myArray=1, 4, 5
//changeArray1 arr=888, 4, 5
//changeArray1 arr=-3, -1, -2, -3, -4
//After myArray=888, 4, 5

 

//pass reference type by value (참조형식을 값에 의한 전달) – copy of reference 전달

 

static void changeArray2(IntValue[] arr)
{
arr[0].x=888; // 원본 배열의 첫번째 값은 888로 변경
System.out.println(“changeArray2 arr=” + Arrays.stream(arr).map(Object::toString).collect(Collectors.joining(“,”)));
arr = new IntValue[] { new IntValue(-3), new IntValue(-1), new IntValue(-2), new IntValue(-3), new IntValue(-4)};  // local 변수로 새롭게 할당하여 지정 그러나 원본 배열 변경 안됨
System.out.println(“changeArray2 arr=” + Arrays.stream(arr).map(Object::toString).collect(Collectors.joining(“,”)));
}
public static void main(String[] args)
{
IntValue[] myArray2 = { new IntValue(1), new IntValue(4), new IntValue(5)};
System.out.println(“Before myArray2=” + Arrays.stream(myArray2).map(Object::toString).collect(Collectors.joining(“,”)));
changeArray2(myArray2);
System.out.println(“After myArray2=” + Arrays.stream(myArray2).map(Object::toString).collect(Collectors.joining(“,”)));
}
// Before myArray2=1, 4, 5
// changeArray2 arr=888, 4, 5
// changeArray2 arr=-3, -1, -2, -3, -4
// After myArray2=888,-4,5

 

 

Instance vs Static Method Overriding

PersonStudentClass

/// Person

class Person
{
static int count = 0; // static (class) variables

String name; // instance variables
int age; // instance variables

public Person()
{
//System.out.println(“Person Constructor”); // this(“”, 0); error: call to this must be first statement in constructor
this(“”, 0);
}

public Person(String name, int age)
{
count++;
this.name = name;
this.age = age;
}

public static void printCount() // static (class) method
{
System.out.println(“Person Count: ” + count);
}

public static int getCount() { return count; } // static (class) methods

 

public String toString() // Object.toString() overriding
{
return “Person Name: ” + name + ” Age: ” + age;
}

public void print() // instance methods
{
System.out.println(“Person Name: ” + name + ” Age: ” + age);
}
}

/// Student

class Student extends Person
{
static int scount = 0; // static (class) variables
int id;

public Student()
{
this(“”, 0, 5208);
}

public Student(String name, int age, int id)
{
super(name, age);
this.id = id;
scount++;
}

public static void printCount() // static (class) method overriding
{
System.out.println(“Student Count: ” + scount);
}

public String toString() // Object.toString() overriding
{
return “Student Name: ” + name + ” Age: ” + age + ” ID: ” + id;
}

public void print() // Person class print() method overriding
{
System.out.println(“Student Name: ” + name + ” Age: ” + age + ” ID: ” + id);
}

}

 

class PersonStudentTest
{
public static void main(String[] args)
{
Person p1 = new Student(“Kyoung”, 22, 1207);
p1.print(); // dynamic binding Student Name: Kyoung Age: 22 ID: 1207
p1.name = “JAVA”; // default (package private)
p1.age = 1; // default (package private)
//p1.id = 2016; // default (package private) cannnot call id coz Person System.out.println(p1); // dynamic binding Student Name: JAVA Age: 1 ID: 1207
p1.printCount(); // calls Person p1 printCount() 1
Person.printCount(); // calls Person printCount() 1
Student.printCount(); // calls Student printCount() 1
System.out.println();

Student s1 = (Student)p1;
s1.name = “JAVA”; // default (package private)
s1.age = 2; // default (package private)
s1.id = 2016; // default (package private)
s1.print(); // Student Name: JAVA Age: 2 ID: 2016
s1.printCount(); // calls Student s1 printCount() 1
Person.printCount(); // calls Person printCount() 1
Student.printCount(); // calls Student printCount() 1
System.out.println();

Student s2 = new Student(“Shin”, 20, 1207);
s2.print(); // Student Name: Shin Age: 20 ID: 1217
s2.printCount(); // calls Student s2 printCount() 2
Person.printCount(); // calls Person printCount() 2
Student.printCount(); // calls Student printCount() 2
System.out.println();

Person p2 = new Person(“Park”, 10);
p2.printCount(); // calls Person p2 printCount() 3
Person.printCount(); // calls Person printCount() 3
Student.printCount(); // calls Student printCount() 2
System.out.println();

Person p3 = new Person();
p3.printCount(); // calls Person p3 printCount() 4
Person.printCount(); // calls Person printCount() 4
Student.printCount(); // calls Student printCount() 2
System.out.println();

System.out.println(“Number of Person: ” + Person.getCount()); // 4
}
}