Skip to content

Commit

Permalink
add Test2, reformat all code
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Jul 11, 2024
1 parent 25ad9e4 commit a52510f
Show file tree
Hide file tree
Showing 171 changed files with 4,164 additions and 4,380 deletions.
93 changes: 44 additions & 49 deletions src/main/java/Advances/CalendarApi/demo1.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,73 @@

// https://www.youtube.com/watch?v=aPgJYCIc-fM&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=482


/** CalendarApi demo1 :
/**
* CalendarApi demo1 :
*
* Calendar class use class intro
* <p>Calendar class use class intro
*/

import java.util.Calendar;
import java.util.Date;
import org.junit.jupiter.api.Test;

/**
* 1) Calendar is an abstract class
* 1) Calendar is an abstract class
*
* 2) there are 2 ways to instantiate it
* <p>2) there are 2 ways to instantiate it
*
* - 2-1) create its sub-class' class (GregorianCalendar)
* - 2-2) call its static method : getInstance()
* <p>- 2-1) create its sub-class' class (GregorianCalendar) - 2-2) call its static method :
* getInstance()
*
* // these 2 methods are the same actually
* <p>// these 2 methods are the same actually
*
* 3) frequent used methods
* <p>3) frequent used methods
*
* - 3-1) get()
* - 3-2) set()
* - 3-3) add()
* - 3-4) getTime()
* - 3-5) setTime()
* <p>- 3-1) get() - 3-2) set() - 3-3) add() - 3-4) getTime() - 3-5) setTime()
*/
public class demo1 {

@Test
public void test1(){
// 2-2) call its static method : getInstance()
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getClass()); // still class java.util.GregorianCalendar
}
@Test
public void test1() {
// 2-2) call its static method : getInstance()
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getClass()); // still class java.util.GregorianCalendar
}

@Test
public void test2(){
Calendar calendar = Calendar.getInstance();
@Test
public void test2() {
Calendar calendar = Calendar.getInstance();

// get()
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
System.out.println(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
// get()
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
System.out.println(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));

System.out.println("--------------------------");
System.out.println("--------------------------");

// set() : change DAY_OF_MONTH or ... in Calendar
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.DAY_OF_MONTH, 1);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
// set() : change DAY_OF_MONTH or ... in Calendar
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.DAY_OF_MONTH, 1);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));

System.out.println("--------------------------");
System.out.println("--------------------------");

// add()
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
calendar.add(Calendar.DAY_OF_MONTH, 3);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
// add()
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
calendar.add(Calendar.DAY_OF_MONTH, 3);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));

System.out.println("--------------------------");
System.out.println("--------------------------");

// getTime() : Calendar dtype -> Date dtype
Date date = calendar.getTime();
System.out.println(date);
// getTime() : Calendar dtype -> Date dtype
Date date = calendar.getTime();
System.out.println(date);

System.out.println("--------------------------");
System.out.println("--------------------------");

// setTime() : Date dtype -> Calendar dtype
Date date1 = new Date();
calendar.setTime(date1); // set calendar from date1
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
}
// setTime() : Date dtype -> Calendar dtype
Date date1 = new Date();
calendar.setTime(date1); // set calendar from date1
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
}
}
5 changes: 1 addition & 4 deletions src/main/java/Advances/ClassLoader/demo1.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
// https://www.youtube.com/watch?v=rePjvHVWnQ0&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=643
// https://www.youtube.com/watch?v=bpP8CE98MhE&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=644

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Properties;
import org.junit.jupiter.api.Test;

public class demo1 {

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/Advances/CollectionDemo/IteratorDemo1.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
// https://www.youtube.com/watch?v=HA7LSr6-xls&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=525

/** Iterator demo 1 */
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import org.junit.jupiter.api.Test;

/**
* Iterator Demo1 -> collection iteration op, via Iterator interface
Expand All @@ -27,7 +26,7 @@ public void test1() {
col1.add(456);
col1.add(789);
col1.add("yooooo");
col1.add(new String("kate"));
col1.add("kate");
col1.add(new Person("kyo", 19));

Iterator iterator = col1.iterator();
Expand Down
64 changes: 35 additions & 29 deletions src/main/java/Advances/CollectionDemo/LinkedHashSetDemo1.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,48 @@
// https://www.youtube.com/watch?v=z8nlH3W1sBU&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=538

/** LinkedHashSet demo 1 */

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import org.junit.jupiter.api.Test;

public class LinkedHashSetDemo1 {

@Test
public void test1(){
/**
* LinkedHashSet demo 1
*
* 1) LinkedHashSet is HashSet's sub class
*
* 2) maintain 2 references when adding new element : pre element and next element
*
* 3) pros : good performance in looping, adding, deletion
*
* 4) cons :
*/

Set set = new LinkedHashSet();
set.add(456);
set.add(123);
set.add(123); // only storage one "123", since HashSet is non-duplicated
set.add("aa");
set.add("bb");
set.add(new Person("tim",11));
set.add(new Person("tim",11)); // NOTE : will storage 2 "Person("tim",11)" since we haven't overwrote "equals", "hashCode" methods in Person class
set.add(new User("ann",20));
set.add(new User("ann",20)); // NOTE : only storage 1 "User("ann",20)" if we overwrite "equals", "hashCode" methods in User class
@Test
public void test1() {
/**
* LinkedHashSet demo 1
*
* <p>1) LinkedHashSet is HashSet's sub class
*
* <p>2) maintain 2 references when adding new element : pre element and next element
*
* <p>3) pros : good performance in looping, adding, deletion
*
* <p>4) cons :
*/
Set set = new LinkedHashSet();
set.add(456);
set.add(123);
set.add(123); // only storage one "123", since HashSet is non-duplicated
set.add("aa");
set.add("bb");
set.add(new Person("tim", 11));
set.add(
new Person(
"tim",
11)); // NOTE : will storage 2 "Person("tim",11)" since we haven't overwrote "equals",
// "hashCode" methods in Person class
set.add(new User("ann", 20));
set.add(
new User(
"ann",
20)); // NOTE : only storage 1 "User("ann",20)" if we overwrite "equals", "hashCode"
// methods in User class

Iterator iterator = set.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
3 changes: 1 addition & 2 deletions src/main/java/Advances/CollectionDemo/ListDemo1.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
// https://www.youtube.com/watch?v=KptmLcdTECg&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=531

/** List demo 1 */
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;

/**
* List Demo1 |-- List interface : storage ordering, duplicated record -> "dynamic" array |---
Expand Down
127 changes: 62 additions & 65 deletions src/main/java/Advances/CollectionDemo/SetDemo1.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,85 +6,82 @@
// https://www.youtube.com/watch?v=fWaabv-UgCs&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=537

/** Set demo 1 */

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.junit.jupiter.api.Test;

/**
* Set Demo 1
* Set Demo 1
*
* 0) "Set" is an interface, we need to implement it before using.
* plz check below implemented classes
* <p>0) "Set" is an interface, we need to implement it before using. plz check below implemented
* classes
*
* 1) Collection framework:
* ....
* |--- Set interface : storage non-ordering, non-repeatable elements
* |--- HashSet, LinkedHastSet, TreeSet
* <p>1) Collection framework: .... |--- Set interface : storage non-ordering, non-repeatable
* elements |--- HashSet, LinkedHastSet, TreeSet
*
* 2) 3 implemented classes (Set)
* - HashSet : main implemented class (Set interface), thread Un-safety, can save `null` value
* - LinkedHastSet : "HashSet"'s sub class. hashset with linkedlist structure. make looping/add/delete elements more efficient
* - TreeSet : use "Red–black tree" in low level. can ordering input elements based on their properties
* <p>2) 3 implemented classes (Set) - HashSet : main implemented class (Set interface), thread
* Un-safety, can save `null` value - LinkedHastSet : "HashSet"'s sub class. hashset with linkedlist
* structure. make looping/add/delete elements more efficient - TreeSet : use "Red–black tree" in
* low level. can ordering input elements based on their properties
*
* 3) There are NO extra defined methods in Set -> all methods it (Set) has are as SAME as Collections's (methods)
* <p>3) There are NO extra defined methods in Set -> all methods it (Set) has are as SAME as
* Collections's (methods)
*
* 4) requirement :
* - when add element to HashSet -> we need to Overwrite hashCode() and equals() methods
* - Overwritten hashCode() and equals() methods should be "consistency" -> e.g. same elements SHOULD have SAME hash value
* <p>4) requirement : - when add element to HashSet -> we need to Overwrite hashCode() and equals()
* methods - Overwritten hashCode() and equals() methods should be "consistency" -> e.g. same
* elements SHOULD have SAME hash value
*/

public class SetDemo1 {

@Test
public void test1(){
/**
* Set : storage non-ordering, non-duplicated (HashSet)
*
* 0) HashSet : array + linked list (low level structure)
*
* 1) non-ordering :
* - non-ordering != randomness
* - ordering in storage space is NOT based on input ordering, BUT on record's hash value
*
* 2) non-duplicated
* - check duplicated or not (added elements) based on "equals()" method
* - Need to overwrite "equals", "hashCode" methods if user-defined class
* - can only add "one" same element into HashSet
* - plz check below "set.add(new User("ann",20))" example
*
* 3) adding element steps (HashSet)
* - step 1) when we add element a to HashSet
* - step 2) get its hash value via its class' hashCode() method
* - step 3) then get storage address (in storage space) via hash value above
* - step 4) check if there is already element on the address
* - if False : add element a OK
* - if True : compare element a and b's hash value (if there is already an element b on the address) //(case 1)
* - if False: add element a OK
* - if True: call class' equals() method, compare every part in element a and element b //(case 2)
* - if True (equals()): add element a Failed //(case 3)
* - if False (equals()): add element a OK
*
* - (for success case 2, case 3, element a will be saved as Linked list form)
* - (JDK 7 : put element a into array, point a -> original element)
* - (JDK 8 : point original element -> element a)
*/

Set set = new HashSet();
set.add(456);
set.add(123);
set.add(123); // only storage one "123", since HashSet is non-duplicated
set.add("aa");
set.add("bb");
set.add(new Person("tim",11));
set.add(new Person("tim",11)); // NOTE : will storage 2 "Person("tim",11)" since we haven't overwritten "equals", "hashCode" methods in Person class
set.add(new User("ann",20));
set.add(new User("ann",20)); // NOTE : only storage 1 "User("ann",20)" if we overwrite "equals", "hashCode" methods in User class
@Test
public void test1() {
/**
* Set : storage non-ordering, non-duplicated (HashSet)
*
* <p>0) HashSet : array + linked list (low level structure)
*
* <p>1) non-ordering : - non-ordering != randomness - ordering in storage space is NOT based on
* input ordering, BUT on record's hash value
*
* <p>2) non-duplicated - check duplicated or not (added elements) based on "equals()" method -
* Need to overwrite "equals", "hashCode" methods if user-defined class - can only add "one"
* same element into HashSet - plz check below "set.add(new User("ann",20))" example
*
* <p>3) adding element steps (HashSet) - step 1) when we add element a to HashSet - step 2) get
* its hash value via its class' hashCode() method - step 3) then get storage address (in
* storage space) via hash value above - step 4) check if there is already element on the
* address - if False : add element a OK - if True : compare element a and b's hash value (if
* there is already an element b on the address) //(case 1) - if False: add element a OK - if
* True: call class' equals() method, compare every part in element a and element b //(case 2) -
* if True (equals()): add element a Failed //(case 3) - if False (equals()): add element a OK
*
* <p>- (for success case 2, case 3, element a will be saved as Linked list form) - (JDK 7 : put
* element a into array, point a -> original element) - (JDK 8 : point original element ->
* element a)
*/
Set set = new HashSet();
set.add(456);
set.add(123);
set.add(123); // only storage one "123", since HashSet is non-duplicated
set.add("aa");
set.add("bb");
set.add(new Person("tim", 11));
set.add(
new Person(
"tim",
11)); // NOTE : will storage 2 "Person("tim",11)" since we haven't overwritten "equals",
// "hashCode" methods in Person class
set.add(new User("ann", 20));
set.add(
new User(
"ann",
20)); // NOTE : only storage 1 "User("ann",20)" if we overwrite "equals", "hashCode"
// methods in User class

Iterator iterator = set.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
Loading

0 comments on commit a52510f

Please sign in to comment.