Monday, August 22, 2011

Try Runnable


package com.evs.objava33.class14;

public class TryRunnable implements Runnable {

private String firstName;
private String lastName;
private Long delay;

public TryRunnable(String firstName, String lastName, Long delay) {
this.firstName = firstName;
this.lastName = lastName;
this.delay = delay;
}

public void run() {
while (true) {
System.out.println(firstName);
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(lastName);
}
}

public static void main(String[] args) {
Thread first = new Thread(new TryRunnable("Asif", "Sajjad", 2000L),
"First");
Thread second = new Thread(new TryRunnable("Shahzad", "Masud", 3000L),
"Second");
Thread third = new Thread(new TryThread("Kamran", "Ahmad", 5000L),
"Third");

//first.setDaemon(true);
first.start();
second.start();
third.start();
}
}

Try Thread


package com.evs.objava33.class14;

public class TryThread extends Thread {

private String firstName;
private String lastName;
private Long delay;

public TryThread(String firstName, String lastName, Long delay) {
this.firstName = firstName;
this.lastName = lastName;
this.delay = delay;
}

public void run() {
while (true) {
System.out.println(firstName);
try {
sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(lastName);
}
}

public static void main(String[] args) {
Thread first = new TryThread("Asif", "Sajjad", 2000L);
Thread second = new TryThread("Shahzad", "Masud", 3000L);
Thread third = new TryThread("Kamran", "Ahmad", 5000L);

first.start();
second.start();
third.start();
}
}

Test Collections


package com.evs.objava33.class13;

import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Vector;

public class TestCollections {

public static void main(String[] args) {
// Sorted Duplicate Ordered
// Set No Yes No
// Sequence / List No/Yes Yes Yes
// Map Yes No Yes

// Set -> HashSet, LinkedHashSet, TreeSet, EnumSet
// List -> Vector, Stack, LinkedList, ArrayList, Queue
// Map -> Hashtable, HashMap, LinkedHashMap, WeakHashMap
// IdentityHashMap, TreeMap

Vector<Person> vec = new Vector<Person>();
vec.add(new Person(30, "Shahzad", "shahzad@gmail.com"));
vec.add(new Person(21, "Ahmad", "zeeshan@gmail.com"));
vec.add(new Person(11, "Mahmood", "mahmood@gmail.com"));

System.out.println(vec);
// System.out.println(vec.indexOf(new Person(21, "", "")));
// System.out.println(vec.contains(new Person(21, "", "")));
Collections.sort(vec);
System.out.println(vec);
Collections.sort(vec, new Comparator<Person>() {
public int compare(Person o1, Person o2) {
return o1.getEmail().compareTo(o2.getEmail());
}
});
System.out.println(vec);

// Date
java.sql.Date date = new java.sql.Date(System.currentTimeMillis());

// Time
Time time = new Time(System.currentTimeMillis());

// Timestamp
Timestamp timestamp = new Timestamp(System.currentTimeMillis());

// Calendar
Calendar cal = Calendar.getInstance();
// cal.add(Calendar.DAY_OF_MONTH, 15);
// java.sql.Date dt = new java.sql.Date(cal.getTimeInMillis());
// cal.add(Calendar.DAY_OF_WEEK, -2);
// cal.get(Calendar.DAY_OF_WEEK);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.add(Calendar.MONTH, -3);
java.sql.Date begin = new java.sql.Date(cal.getTimeInMillis());
cal.add(Calendar.MONTH, 4);
cal.add(Calendar.DAY_OF_MONTH, -1);

}

private static class Person implements Comparable<Person> {
private Integer id;
private String name;
private String email;

public int compareTo(Person o) {
// Small = -1
// Equal = 0
// Greater = 1
int idx = getId().compareTo(o.getId());
return idx == 0 ? getName().compareTo(o.getName()) : idx;
}

public boolean equals(Object obj) {
// return super.equals(obj);
Person p = (Person) obj;
return p.getId().intValue() == getId().intValue();
}

public Person(int id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}

/**
* @return the id
*/
public Integer getId() {
return id;
}

/**
* @param id
*            the id to set
*/
public void setId(Integer id) {
this.id = id;
}

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name
*            the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @return the email
*/
public String getEmail() {
return email;
}

/**
* @param email
*            the email to set
*/
public void setEmail(String email) {
this.email = email;
}

/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", email=" + email
+ "]";
}
}
}

Sunday, August 7, 2011

Assignment - Week 6



Address Book
1. Name, Email, Phone, Address, Semester

1. Add / Update / Delete / View Person
2. Search (Name, Email, Phone, Address, Semester)
3. Pagination (5 per page)
4. Load on startup, Save on exit

Constraints:
- Object Serialization
- Composition / Inheritance / Abstraction
- GUI is optional
- Program should be Platform independent (work on Linux + windows)

PS: Please start working on your project ideas, and share it in next class.

Pair (Generics)


package com.evs.objava33.class12;

public class Pair<KeyType, ValueType> {

private KeyType key;
private ValueType value;

public Pair(KeyType key, ValueType value) {
this.key = key;
this.value = value;
}

/**
* @return the key
*/
public KeyType getKey() {
return key;
}

/**
* @param key
*            the key to set
*/
public void setKey(KeyType key) {
this.key = key;
}

/**
* @return the value
*/
public ValueType getValue() {
return value;
}

/**
* @param value
*            the value to set
*/
public void setValue(ValueType value) {
this.value = value;
}

public String toString() {
return "Pair [key=" + key + ", value=" + value + "]";
}
}

Try LinkedList


package com.evs.objava33.class12;

import com.evs.objava33.class7.PalmTree;
import com.evs.objava33.class7.Tree;

public class TryLinkedList {

public static void main(String[] args) {
LinkedList<String> list = new LinkedList<String>();
list.addItem("One");
list.addItem("Two");
list.addItem("Three");

System.out.println(list);

LinkedList<Integer> intList = new LinkedList<Integer>();
LinkedList<PalmTree> palmList = new LinkedList<PalmTree>();

LinkedList<LinkedList<LinkedList<String>>> lst = new LinkedList<LinkedList<LinkedList<String>>>();
LinkedList<Pair<String, Integer>> newList = new LinkedList<Pair<String, Integer>>();
// newList.getFirst().getKey()
// newList.getFirst().getValue()

LinkedList<?>[] listArray = { list, intList, palmList, lst, newList };

print(list);
print(intList);
}

private static void print(LinkedList<?> list) {
StringBuilder buf = new StringBuilder();
buf.append("Linked List: ");
for (Object item = list.getFirst(); item != null; item = list.getNext()) {
buf.append(item);
buf.append(",");
}
System.out.println(buf.toString());
}
}

Linked List (Generics)


package com.evs.objava33.class12;

public class LinkedList<T> {

private ListItem start, end, current;

public LinkedList() {
start = end = current = null;
}

public LinkedList(T item) {
addItem(item);
}

public T getFirst() {
current = start;
return start == null ? null : start.item;
}

public T getNext() {
if (current != null) {
current = current.next;
}
return current == null ? null : current.item;
}

public void addItem(T[] items) {
if (items != null) {
for (T s : items) {
addItem(s);
}
}
}

public String toString() {
StringBuilder buf = new StringBuilder();
buf.append("Linked List: ");
for (T item = getFirst(); item != null; item = getNext()) {
buf.append(item);
buf.append(",");
}
return buf.toString();
}

public void addItem(T item) {
ListItem newItem = new ListItem(item);
if (start == null) {
start = end = newItem;
} else {
end.next = newItem;
end = newItem;
}
}

private class ListItem {
T item;
ListItem next;

public ListItem(T item) {
this.item = item;
}

@Override
public String toString() {
return "ListItem [item=" + item + "]";
}
}
}