Java

Identifier – naming conventions

Variables – local vs class variable, instance vs static variable

Data Type – primitive vs reference data type

Type Conversion – implicit vs explicit type conversion, int <-> String conversion, numeric data type conversion

Operator – arithmetic vs relational vs bitwise vs logical vs assignment operator, operator precedence, postfix vs prefix increment/decrement operator

Control Statement – if/else, switch, for/foreach, while/do-while, break/continue

Array – primitive vs object array, 2D array, returning an array vs passing an array as parameter

Enum – enum data type, enum Operation example, enum Operator, enum Gender

Method – parameter passing (pass-by value – primitive vs reference data type), swap, method overloading vs overriding

OOP(Encapsulation, Inheritance, Polymorphism)

Class – default/public/protected/private, instance vs static member, constructor usage guideline, singleton design pattern vs static class,

Inheritance – BankAccount SavingAccount CheckingAccount class(instance vs static member), Car Sedan class (default/public/protected/private), Person Student class (instance vs static method overriding)

Polymorphism – Polymorphism vs Method Overriding vs Method OverloadingAbstract Class & Polymorphism

Interface – Comparable & Comparator Interface, Shape Polymorphism with Interface

Swing – Swing Component, Layout Manager, 5 Ways to Implement Event Listener, Event Listener (ChangeListener, KeyListener, ActionListener), TemperatureConverter, MetricConverter, AridityIndex, BMICalculator, Key Mouse Motion Listener & Adapter, Custom Component, TripleCalculatorFrame

Collections – autoboxing and unboxingArray vs ArrayList,
Difference between Array and ArrayList,
Equals vs Contains== vs equals vs hashCode

 

 

기말고사

일시: 2018년 6월 12일(화) 밤 8:00

장소: 2공 105호

시험범위: 중간고사 포함 – 배운데까지 (OOP, Polymorphism, Collection, SWING)

 

Lab7 find


// find photo by fullPath and highlight it
public void find() {
	System.out.println("Find image");
	String[] options = photoManager.getPhotoFullPaths(); // get photoList's fullPaths (as String[])
	if (options == null) return; // if photoManager has no photo, then return here
	String fullPath = (String)JOptionPane.showInputDialog(null, "Find the image:", "Find", JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
	if (fullPath != null) {
		Photo found = photoManager.find(fullPath); // find photo by fullPath
		if (found == null) return; // if photo is not found, then return here
		found.print();
		// find ImageLabel component that has the same fullPath. If found, then highlight its border to red
		Component[] componentList = photoPanel.getComponents();
		for (Component c: componentList) {
			if (c instanceof ImageLabel) 
				((JLabel)c).setBorder(javax.swing.BorderFactory.createEmptyBorder());
		}
		for(Component c : componentList) {
			if (c instanceof ImageLabel) {
				if (((ImageLabel)c).getFullPath() == fullPath) { // find ImageLabel component that has the same fullPath
					Border border = BorderFactory.createLineBorder(Color.RED, 3); // highlight its border to red
					((JLabel)c).setBorder(border);
				}
			}
		}
	}
}

Lab7

Lab7 – Collection (Due by 6/7)

java1-lab7-PhotoManager

Lab7_template

Lab7 프로젝트 디렉토리 안에 모든 파일(src/*.java & bin/*.class)와 보고서 (장수 제한 없음)를 넣고 Lab7_학번_이름.zip 압축한 후 e-learning (http://lms.dankook.ac.kr/index.jsp)으로 제출

Photo 클래스는 이미지파일을 읽어서 이미지 버퍼(BufferedImage)로 저장
-Photo 클래스의 Comparable vs Comparator 인터페이스를 비교분석한다.
-Photo 클래스의 equals vs hashCode를 비교분석한다.
ImageLabel 클래스는 Photo로 썸네일이미지레이블을 만들어 주는 클래스 (fullPath로 탐색가능하게 해줌)
-MainFrame 클래스는 Add/Find/Remove/RemoveAll/Sort/Close 버튼을 누르면 PhotoManager를 이용하여 Photo 이미지레이블을 추가/탐색/제거/모두제거/정렬/종료하는 GUI 를 구현한다.
-PhotoManager는 List 관리자를 구현한다.
-Photo[] vs ArrayList<Photo>를 비교분석한다.
-ArrayList<Photo> vs HashMap<Photo, Integer> 를 비교분석한다.

보고서의 내용은 기존 코드 분석과 이해한 내용을 적는다.

Lab6 open


// File->Open MenuItem, O - Mnemonic
JMenuItem openMenuItem = new JMenuItem("Open", KeyEvent.VK_O);
fileMenu.add(openMenuItem);
openMenuItem.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        System.out.println("OPEN");
        File[] selectedFiles = openFileDialog();
        if (selectedFiles == null) return;
        List photoList = new ArrayList(); // photoList
        for (int i = 0; i < selectedFiles.length; i++) {
            try {
                String fullPath = selectedFiles[i].getCanonicalPath(); //String fullPath = file.getCanonicalPath().replace('\\', '/');
                System.out.println("OPEN fullPath=" + fullPath);
                Photo photo = new Photo(fullPath);
                if (photo.getImg() != null) { // if image is loaded
                    photoList.add(photo);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }	
	photoArray = new Photo[photoList.size()]; // create photoArray with photoList.size()
	photoArray = photoList.toArray(photoArray); // photoList => photoArray
	for (Photo p : photoArray) p.print(); // debug to print photoArray
	
	displayPanel.loadPhotos(photoArray); // reload photoArray in displayPanel
	displayPanel.repaint(); // repaint
    }
});

== vs equal vs hashCode vs contains

EqualsHashCodeCollectionContains


class EqualsHashCodeCollectionContainsTest {
    // getReference 
    public static String getReference(Object o){
        return o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o));
    }

    public static void main(String[] args)  {

	// primitive type (== 연산자는 값이 같으면 true)
	int i = 10;
	int j = 10;
	int k = i;
	System.out.println("i == j " + (i == j));
	System.out.println("i == k " + (i == k));
	System.out.println();

	Object o1 = i; // boxing
	Object o2 = j; // boxing
	Object o3 = o1;
	System.out.println(getReference(o1) + " o1=" + o1);
	System.out.println(getReference(o2) + " o2=" + o2);
	System.out.println(getReference(o3) + " o3=" + o3);
	System.out.println("o1 == o2 " + (o1 == o2));
	System.out.println("o1 == o3 " + (o1 == o3));
	System.out.println("o1 equals o2 " + o1.equals(o2));
	System.out.println("o1 equals o3 " + o1.equals(o3));
	System.out.println("o1.hashCode() == o2.hashCode() " + (o1.hashCode() == o2.hashCode()));
	System.out.println("o1.hashCode() == o3.hashCode() " + (o1.hashCode() == o3.hashCode()));
	System.out.println();

	// reference type (== 연산자는 reference가 같으면 true)
	Person p1 = new Person("P",10);
	Person p2 = new Person("P",10);
	Person p3 = p1;
	System.out.println(getReference(p1) + " p1=" + p1);
	System.out.println(getReference(p2) + " p2=" + p2);
	System.out.println(getReference(p3) + " p3=" + p3);
	System.out.println("p1 == p2 " + (p1 == p2));
	System.out.println("p1 == p3 " + (p1 == p3));
	System.out.println("p1 equals p2 " + p1.equals(p2));
	System.out.println("p1 equals p3 " + p1.equals(p3));
	System.out.println("p1.hashCode() == p2.hashCode() " + (p1.hashCode() == p2.hashCode()));
	System.out.println("p1.hashCode() == p3.hashCode() " + (p1.hashCode() == p3.hashCode()));
	System.out.println();

	// String type (== 연산자는 reference가 같으면 true)
	String s1 = "PP"; // String literal을 사용할 경우, pool에서 관리
	String s2 = "PP"; // String literal을 사용할 경우, pool에서 관리
	String s3 = s1;
	String s4 = "P" + "P"; // String literal을 사용할 경우, pool에서 관리
	String s5 = new String("PP");
	String s6 = "PP" + ""; // String literal을 사용할 경우, pool에서 관리
	String s7 = s1 + "";
	System.out.println(getReference(s1) + " s1=" + s1);
	System.out.println(getReference(s2) + " s2=" + s2);
	System.out.println(getReference(s3) + " s3=" + s3);
	System.out.println(getReference(s4) + " s4=" + s4);
	System.out.println(getReference(s5) + " s5=" + s5);
	System.out.println(getReference(s6) + " s6=" + s6);
	System.out.println(getReference(s7) + " s7=" + s7);

	System.out.println("s1 == s2 " + (s1 == s2));
	System.out.println("s1 == s3 " + (s1 == s3));
	System.out.println("s1 == s4 " + (s1 == s4));
	System.out.println("s1 == s5 " + (s1 == s5));
	System.out.println("s1 == s6 " + (s1 == s6));
	System.out.println("s1 == s7 " + (s1 == s7));
	System.out.println("s1 equals s2 " + s1.equals(s2));
	System.out.println("s1 equals s3 " + s1.equals(s3));
	System.out.println("s1 equals s4 " + s1.equals(s4));
	System.out.println("s1 equals s5 " + s1.equals(s5));
	System.out.println("s1 equals s6 " + s1.equals(s6));
	System.out.println("s1 equals s7 " + s1.equals(s7));
	System.out.println("s1.hashCode() == s2.hashCode() " + (s1.hashCode() == s2.hashCode()));
	System.out.println("s1.hashCode() == s3.hashCode() " + (s1.hashCode() == s3.hashCode()));
	System.out.println("s1.hashCode() == s4.hashCode() " + (s1.hashCode() == s4.hashCode()));
	System.out.println("s1.hashCode() == s5.hashCode() " + (s1.hashCode() == s5.hashCode()));
	System.out.println("s1.hashCode() == s6.hashCode() " + (s1.hashCode() == s6.hashCode()));
	System.out.println("s1.hashCode() == s7.hashCode() " + (s1.hashCode() == s7.hashCode()));
	System.out.println();

	// ArrayList
	System.out.println("pList");
	List<Person> pList = new ArrayList<Person>();
	pList.add(p1);
	pList.add(p2);
	pList.add(p3);
	pList.forEach((p) -> System.out.println(p));
	System.out.println("pList contains p1: " + pList.contains(p1));
	System.out.println("pList contains p2: " + pList.contains(p2));
	System.out.println("pList contains p3: " + pList.contains(p3));
	System.out.println("pList contains new Person: " + pList.contains(new Person("P",10)));
	System.out.println();

	// HashSet의 경우 hashCode가 일치하면 동일한 것으로 간주하여 replace함
	System.out.println("pSet");
	Set<Person> pSet = new HashSet();
	pSet.add(p1);
	pSet.add(p2);
	pSet.add(p3);
	pSet.forEach((p) -> System.out.println(p));
	System.out.println("pSet contains p1: " + pSet.contains(p1));
	System.out.println("pSet contains p2: " + pSet.contains(p2));
	System.out.println("pSet contains p3: " + pSet.contains(p3));
	System.out.println("pSet contains new Person: " + pSet.contains(new Person("P",10)));
	System.out.println();

	// HashMap의 경우 key 값은 hashCode가 일치하면 동일한 것으로 간주하여 replace함
	System.out.println("pMap");
	Map<Person, Integer> pMap = new HashMap<Person, Integer>();
	pMap.put(p1, 1);
	pMap.put(p2, 2);
	pMap.put(p3, 3);
	pMap.forEach((p, e) -> System.out.println(p + " " + e));
	System.out.println("pMap contains p1: " + pMap.containsKey(p1));
	System.out.println("pMap contains p2: " + pMap.containsKey(p2));
	System.out.println("pMap contains p3: " + pMap.containsKey(p3));
	System.out.println("pMap contains new Person: " + pMap.containsKey(new Person("P",10)));
	System.out.println();

	// Array
	System.out.println("pArray");
	Person[] pArray = new Person[3];
	pArray[0] = p1;
	pArray[1] = p2;
	pArray[2] = p3;
	Arrays.asList(pArray).forEach(p -> System.out.println(p));
	System.out.println("pArray contains p1: " + Arrays.asList(pArray).contains(p1));
	System.out.println("pArray contains p2: " + Arrays.asList(pArray).contains(p2));
	System.out.println("pArray contains p3: " + Arrays.asList(pArray).contains(p3));
	System.out.println("pArray contains new Person: " + Arrays.asList(pArray).contains(new Person("P",10)));
	System.out.println();
    }
}

Array vs ArrayList

ArrayArrayList

int[] integerArray = new int[3];

integerArray[0] = 1;

integerArray[1] = 2;

integerArray[2] = 3;

for (int i : integerArray) System.out.println(i);

for (int j=0; j<integerArray.length; j++) System.out.println(integerArray[ j ]);

int k = 0;

while (k < integerArray.length) System.out.println(integerArray[k++]);

 

ArrayList<Integer> integerList = new ArrayList<Integer>();

integerList.add(1); //cannot store primitive in ArrayList, instead autoboxing will convert int to Integer object

integerList.add(2); //cannot store primitive in ArrayList, instead autoboxing will convert int to Integer object

integerList.add(3); //cannot store primitive in ArrayList, instead autoboxing will convert int to Integer object

for (int m : integerList) System.out.println(m);

for (int n=0; n<integerList.size(); n++) System.out.println(integerList.get(n));

Iterator<Integer> itr = integerList.iterator();

while (itr.hasNext()) System.out.println(itr.next());