lecture8
Midterm Extra
MidtermExtra 이클립스프로젝트 디렉토리 안에 모든 파일(src/*.java & bin/*.class & .project)와 보고서 (10장미만)를 넣고 MidtermExtra_학번_이름.zip 압축한 후 e-learning (http://lms.dankook.ac.kr/index.jsp)으로 제출 (중간고사 extra +10점)
MidtermExtra – Midterm&Package
Package와 Midterm 시험문제 프로그램을 테스트한다.
보고서는 출력해서 수업시작 전에 제출한다.
보고서의 내용은 기존 코드 분석과 이해한 내용 그리고 본인이 추가한 코드내용을 적는다.
Lecture7
lecture7
java1-lecture7
HW Grading
이러닝으로 보고서, 소스코드, 프로젝트 파일 폴더 전체가 없음 (HW3까지는 -0) -7
소스코드 컴파일 에러 -7
코드 실행 에러 및 잘못된 결과 -1
소스 코드에 주석 없음 (HW3까지는 -0) -1
소스코드 첫부분에 Lab이름, 날짜, 본인이름 적을것
보고서표지에 Lab번호, 분반번호, 제출일, 학번, 이름을 적을것
보고서 제출안함 -3
보고서에 내용을 작성하지 않고 소스코드 사진캡쳐해서 붙여넣기 (HW3까지는 -1로 채점함) -3
보고서에 소스코드 내용을 작성하지 않고 다른 것 적기 (HW3까지는 -1로 채점함) -3
보고서에 실행결과창 없음 -1
‘두둑한 연봉을 위한’ 2017년에 배울 만한 10가지 프로그래밍 언어
http://www.itworld.co.kr/insight/103581
1. 자바(Java)
2. 자바스크립트(JavaScript)
3. 파이썬(Python)
4. C++
5. 루비(Ruby)
6. C
7. 스위프트(Swift)
8. C#
9. 어셈블리(Assembly)
10. PHP
https://www.tiobe.com/tiobe-index/
1. 자바(Java)
2. C
3. C++
4. C#
5. 파이썬(Python)
6. 자바스크립트(JavaScript)
7. PHP
8. Visual Basic .NET
9. 어셈블리(Assembly)
10. 루비(Ruby)
11. Delphi/Object Pascal
12. Perl
13. MATLAB
14. Scratch
15. R
16. 스위프트(Swift)
17. Objective-C
18. Visual Basic
19. PL/SQL
20. Go
Midterm Exam Answer
모두들 수고 하셨습니다. 중간고사 답안지입니다.
java17-2-midterm-answer
Equals & Contains
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 main(String[] args)
{
///////////////////////////////////////////////////////////////////////
Person jason1 = new Person(“Jason”, 10);
Person jason2 = new Person(“Jason”, 10);
Person jason3 = jason1;
Person jason4 = new Person(“Jason”, 20);
if (jason1 == jason2)
System.out.println(“jason1 == jason2”);
else
System.out.println(“jason1 != jason2”); //동일한 reference를 가리키지 않으므로 jason1 != jason2
if (jason1 == jason3)
System.out.println(“jason1 == jason3”); // 동일한 reference이므로 jason1 == jason3
else
System.out.println(“jason1 != jason3”);
if (jason1.equals(jason4))
System.out.println(“jason1 == jason4”);
else
System.out.println(“jason1 != jason4”); //동일한 reference를 가리키지 않으므로 jason1 != jason4
if (jason1.equals(jason2))
System.out.println(“jason1 equals to jason2”); // 내용이 같으므로 jason1 equals to jason2
else
System.out.println(“jason1 is not equal to jason2”);
if (jason1.equals(jason3))
System.out.println(“jason1 equals to jason3”); // 내용이 같으므로 jason1 equals to jason3
else
System.out.println(“jason1 is not equal to jason3”);
if (jason1.equals(jason4))
System.out.println(“jason1 equals jason4”);
else
System.out.println(“jason1 is not equal to jason4”); // 내용이 다르므로 jason1 is not equal to jason4
///////////////////////////////////////////////////////////////////////
Student john1 = new Student(“John”, 10, 100);
Student john2 = new Student(“John”, 10, 100);
Student john3 = john1;
Student john4 = new Student(“John”, 20, 100);
if (john1.equals(john2))
System.out.println(“john1 equals to john2”); // 내용이 같으므로 john1 equals to john2
else
System.out.println(“john1 is not equal to john2”);
if (john1.equals(john3))
System.out.println(“john1 equals to john3”); // 내용이 같으므로 jogn1 equals to john3
else
System.out.println(“john1 is not equal to john3”);
if (john1.equals(john4))
System.out.println(“john1 equals to john4”);
else
System.out.println(“john1 is not equal to john4”); // 내용이 다르므로 john1 is not equal to john4
Set<Student> sList5 = new HashSet<Student>();
sList5.add(john1);
sList5.add(john2);
sList5.add(john3);
sList5.add(john4);
sList5.forEach((s) -> System.out.println(s));
System.out.println(“sList5 contains john1 Student(\”John\”, 10, 100): ” + sList5.contains(john1));
System.out.println(“sList5 contains john2 Student(\”John\”, 10, 100): ” + sList5.contains(john2));
System.out.println(“sList5 contains john3 Student(\”John\”, 10, 100): ” + sList5.contains(john3));
System.out.println(“sList5 contains john4 Student(\”John\”, 20, 100):: ” + sList5.contains(john4));
System.out.println(“sList5 contains Student(\”John\”, 20, 100): ” + sList5.contains(new Student(“John”, 20, 100)));
System.out.println(“sList5 contains Student(\”John\”, 20, 200): ” + sList5.contains(new Student(“John”, 20, 200)));
Hashtable<Integer, Student> sList6 = new Hashtable<Integer, Student>();
sList6.put(1, john1);
sList6.put(2, john2);
sList6.put(3, john3);
sList6.put(4, john4);
sList6.forEach((i, s) -> System.out.println(s));
System.out.println(“sList5 contains john1 Student(\”John\”, 10, 100): ” + sList5.contains(john1));
System.out.println(“sList5 contains john2 Student(\”John\”, 10, 100): ” + sList5.contains(john2));
System.out.println(“sList5 contains john3 Student(\”John\”, 10, 100): ” + sList5.contains(john3));
System.out.println(“sList5 contains john4 Student(\”John\”, 20, 100):: ” + sList5.contains(john4));
System.out.println(“sList5 contains Student(\”John\”, 20, 100): ” + sList5.contains(new Student(“John”, 20, 100)));
System.out.println(“sList5 contains Student(\”John\”, 20, 200): ” + sList5.contains(new Student(“John”, 20, 200)));
}
}
Comparable & Comparator Interface
/////////////////////////////////
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));
}
}
Lecture6
lecture6
Midterm Exam
중간고사
시험범위: 처음부터 – 배운데까지 (강의노트, 실습, 수업블로그)
주요내용: 기본문법, enum, array, class, abstract class, interface, parameter passing (comparable & comparator & equals는 안나옴)
일시 및 장소: 2017년 10월 23일 (월) 1:00 – 2:30 (2공학관 524호)