Note that, System.out.printf and System.out.format is exactly the same, the Java engineer come out with System.out.printf may be want to make C Programmer happy.
Now, I am going to use this System.out.printf to format my money, in account book, for example, there are different type of formatting. I now tied up with my code, and the output let you to see what happen in this.
import java.util.*; import java.text.*;
class MoneyForm { private double amount; private int type; public MoneyForm(double amount, int type) { this.amount = amount; this.type = type; }
public void generateForm() { switch(type) { case 0: System.out.printf(" >$%1$-+,(15.3f< ", amount); break; case 1: System.out.printf(" >$%1$0+,(15.3f< ", amount); break; case 2: System.out.printf(" >$%1$0+(15.3f< ", amount); break; case 3: System.out.printf(" >$%1$+(15.3f< ", amount); break; case 4: System.out.printf(" >$%1$+,(15.3f< ", amount); break; }
System.out.println(); } }
class Money { public static void main(String args[]) { for(int i=0; i<5; i++) printMoney(i); }
public static void printMoney(int type) { System.out.println("Money Form of type: " + type); for(int i=-3; i<=3; i++) new MoneyForm(i * 1530.21, type).generateForm();
It will return "TRUE". Why? Why show two different results, one return false the other return true. But how come, it is same concept!!!
Confusing?
The answer is, in order to save memory, two instances of the following wrapper objects will always be == when their primitive value are the same.
Boolean
Byte
Character from \u0000 to \u007f (7f is 127 in decimal)
Short and Integer from -128 to 127.
Yes, I know it, in order to save memory, so it become like that. So why first time I see it I will get confused, is it the book talk wrong thing? Not! I try in my machine, it showed the same result.
If I not see this, if the Sun Exam come out this question, I definite will… So I need to be very very careful about this. Since Java 5 the creator of Java is giving programmer more convenient way to construct code, so it may be some things not following logic, so as a testtaker we need to be very careful about this.
This is the demonstration on how to use java.util.Comparable and java.util.Comparator to sort data. In this example, I would using some example of student data, to sort it using both ways.
Comparable is an interface under java.util, the class of the student data must implements it, in order using Collections.sort() to sort it. The programmer need to implements the compareTo() method in order to achieve it.
Comparator is another interface under java.util, it provide more flexibility in sorting the data in many ways. With using Comparator, the programmer is required to make another class which implements Comparator but no implements on class of student data. The benefits are we need not to modify the class of data being sorted, and also we can create as more as possible the Comparator class, provide many sorting method. Lastly, the programmer need to implements the compare() method in order to achieve it.
Here I using the Comparable to sort the Student Data. Below is the code and the output.
import java.util.*;
// Demonstrate using Comparable to sort the student data based on ID, Name, Age
class Student implements Comparable<Student> { private int ID; private String name; private int age;
public Student(int i, String s, int a) { ID = i; name = s; age = a; }
public int getID() {return ID;} public String getName() {return name;} public int getAge() {return age;}
public int compareTo(Student stu) { // sort based on ID return this.ID – stu.ID; }
public String toString() { return ID + "\t" + name + "\t" + age; } }
public class StudentData { public static void main(String args[]) { List<Student> list = new ArrayList<Student>(); list.add(new Student(3,"Mohan",50)); list.add(new Student(2,"fyhao",20)); list.add(new Student(5,"Ahmin",35)); list.add(new Student(6,"Abang",32)); list.add(new Student(1,"Meili",13));
System.out.println("Before Sort:\nID\tName\tAge"); for(Student stu : list) { System.out.println(stu); }
Where below is the implementation of the code using Comparator to sort it. I demonstrated it using three different sorting method, you all can check it out from below the code and the output.
import java.util.*;
// Demonstrate using Comparator to sort the student data based on ID, Name, Age
class Student { private int ID; private String name; private int age;
public Student(int i, String s, int a) { ID = i; name = s; age = a; }
public int getID() {return ID;} public String getName() {return name;} public int getAge() {return age;}
public String toString() { return ID + "\t" + name + "\t" + age; } }
class reverseAge implements Comparator<Student> { public int compare(Student s1, Student s2) { return s2.getAge() – s1.getAge(); } }
class reverseAlphabetical implements Comparator<Student> { public int compare(Student s1, Student s2) { return s2.getName().compareTo(s1.getName()); } }
class naturalID implements Comparator<Student> { public int compare(Student s1, Student s2) { return s1.getID() – s2.getID(); } }
public class StudentData { public static void main(String args[]) { List<Student> list = new ArrayList<Student>(); list.add(new Student(3,"Mohan",50)); list.add(new Student(2,"fyhao",20)); list.add(new Student(5,"Ahmin",35)); list.add(new Student(6,"Abang",32)); list.add(new Student(1,"Meili",13));
System.out.println("Before Sort:\nID\tName\tAge"); for(Student stu : list) { System.out.println(stu); }
I currently doing some sample or experiments about generic collection, a new thing added in Java SE 5. It is very interesting but a bit complex, so I need to deep study about it.
Here is my code and the interface shown.
import java.util.*;
class Some { public <T> T getCode(T n) { return n; } }
abstract class Animal<E> { abstract void eat(E x); }
class Fish<E extends Animal> extends Animal<E> { public void eat(E x) { System.out.println("Fish: " + x); } }
class Cat<E extends Fish> extends Animal<E> { public void eat(E x) { System.out.println("Cat: " + x); } }
class Dog<E extends Animal> extends Animal<E> { public void eat(E x) { System.out.println("Dog: " + x); } }
class MyEatList { public void addFish(List<? super Fish> list) { list.add(new Fish()); }
public void addDog(List<? super Dog> list) { list.add(new Dog()); }
}
class GenericClass { public static void main(String args[]) { Some s = new Some(); System.out.println(s.getCode(1)); System.out.println(s.getCode("2")); System.out.println((s.getCode(1) instanceof Integer)); System.out.println(s.getCode("2") instanceof String);
Cat<Fish> c = new Cat<Fish>(); c.eat(new Fish());
Dog<Cat> d = new Dog<Cat>(); d.eat(new Cat());
List<Animal> eatList = new ArrayList<Animal>(); MyEatList menu = new MyEatList(); menu.addFish(eatList); menu.addDog(eatList); menu.addFish(eatList); menu.addDog(eatList); System.out.println(eatList); } }
This tutorial demonstrated how to use comparator to sort Collection, for this example the ArrayList of List.
Now, I used the java.util.Collections static utility method, Collections.sort() method to sort the List, how I sort the list? I first sort the list in alphabetically, and otherwise I am using a user-defined comparator to sort in reverse order. Here is my code:
import java.util.*;
public class SortSort {
public static void main(String args[]) { List<String> name = new ArrayList<String>(); name.add("Mohan"); name.add("Ali"); name.add("Hao"); name.add("Some"); System.out.println("Before: " + name); Collections.sort(name); System.out.println("After Sort: " + name); Collections.sort(name, new reverseOrder()); System.out.println("After Reverse Sort: " + name); } }
class reverseOrder implements Comparator<String> { public int compare(String one, String two) { return two.compareTo(one); } }
Actually Sunday is the end of Sun Certification Training, meant that the end of the chapter, today still got revision with my lecturer. Just now I doing the Test he given me, 120++ questions taken about 90 minutes to complete. And I saw that my performance is quite good today.
Today, my that training lecturer, Mohan, talked me last week that I today supposed to find our FOCIT head of department, Goh Poh Kim, asked him about Sun Certification Exam about buy the exam voucher stuff, but he talked me I supposed to do own.
Some I now come back and I will go to Mohan’s house later, to discuss and ask him how the procedure.
And just now when I came back to hostel, I first searching through the Google, and finally I searching to Google Map. I decided to find the location of the Menara Citybank, that Sun Microsystem located. I think I will go tomorrow to buy the exam voucher if possible. Then, I may be will contact one company in Seremban, to schedule the time and venue of the exam, and may be next week I need go Seremban to take the exam.
According to Vincent Tan, President of Centior, he taught me how to get to the Menara Citybank. First, I need to take the KTM to KL Central. Then I take the transit of Rapid KL to get to Ampang Park Station. When I arrived at there, then when I come out, the Menara Citybank will be at front of me, very nearby.
And I hope my partners specially Wuliaoren, or Wuming can go with me. However, I will discuss with Mohan see how going on.
I have made some operation on Set and List in Java, using addAll, removeAll, retainAll functionality. These functionality act like the mathematical set functions.
Now I proceed with the Java coding, you all can learn from here. Below the code will be the DOS-based output.
import java.util.*;
class Test8 { public static void main(String args[]) { Set setA = new HashSet(); setA.add(1); setA.add(2); setA.add(3); setA.add(4);
Set setB = new HashSet(); setB.add(2); setB.add(4); setB.add(6); setB.add(8);
List addAllList = new ArrayList(listA); addAllList.addAll(listB); System.out.println("Add All Operation: " + addAllList);
List removeAllList = new ArrayList(listA); removeAllList.removeAll(listB); System.out.println("Remove All Operation: " + removeAllList);
List retainAllList = new ArrayList(listA); retainAllList.retainAll(listB); System.out.println("Retain All Operation: " + retainAllList); } }
Please note that, Set is unordered, and having unique elements, meant non-duplicate. List is ordered, and having redundant elements, meant duplicate is allowed.
Now my world is full of assignments. I currently still having HCI Assignment 2 and Business Assignment 1 that to be survived.
Yesterday, I still working on HCI Assignment 2, I try to draw the storyboard myself in Flash. I find some pictures online, referenced from there, put into the Flash, but use the line and pen to draw the exactly cute picture based on this. It costs me 4 hours to draw the completed 3 screens. There are total of 9 screens for stories, and 1 screen for main page. I am supposed to draw a story board for a web based system for kids. My desired topic is Introduction to Occupation, and I decided to introduce nine different occupation for the kid. In my web based system, I would use attractive cute pictures, attractive and interesting animations, such as stories around the occupation, and also some texts below the animation. I will not use long text because the kid very not like it. They prefer colorful pictures and animation. In this system, also got a sound button, the kid can click the sound button to play the voice, which saying the text to the kid. However, I am not supposed to create a real system but only drawing story board only. But if it is required to create one, I think that should not be a problem for me, because I have such experience to create that before.
Today, I will more concentrated on Business Assignment. For me, I thought that is tough and hard for me to complete this assignment. First, my essay structure I could not be able to design well, and also I could not have many points to write. Fortunately my partner is very very very good in these, he gave me some business books when last ehosting gathering. However it will not help throughout the assignment, but it will also the big help for me. Somehow I have some problems I will ask him, but currently I seldom to ask him because he is now very busy in ehosting stuff. I could not help him because I currently stuck in many assignments and projects, and the most important thing is Sun Certification Exam. Now, I think I can complete tonight for the assignment, which began to do it last last last weeks, a long time ago. I will try to re-design the structure, to make it well. I will try my best to make the assignment better. It is because, this is my job, throughout the learning curves.
Hence, it is better for me to go for bath and lunch, and later I need to go for revision for Sun Certification Exam, and also 4pm to 6pm I got the Sun Certification Training. Bye for my friends, and I will continue to update my blog to let you know where I am, how I am.
I have going through the Dreamweaver second lesson today.
Today, Miss Liew taught us the Form concept in Dreamweaver. Later she taught us how to use CSS to control the layout of the form elements.
Later on, we are taught to insert several type of media elements, including sound, SWF, FLV, and movie file inside the web page. There are different ways of inserting each type of media elements into the web page.
Second is about the media element lesson: http://fyhao.com/hci/dwlab2/media.html (It took me about long time to upload, it is about 15mb, during the very very very fast Internet speed provided by EBB)
Please going to the link to review, if you all got any doubt need ask me, I can help you to solve your problem here. Please do not hesitate to tell me your problem when you have encountered.
I am very happy today because my PTPTN problem solved. When this problem solved, the burden of my family has been reduced, and I can more concentrate on my university works. Today, I started from 2pm to now, reading the Sun Certification the biggest one textbook, I have learnt a lot from there, it is quite useful for me. However, I will thanks Qxinnet, having the emotional support towards me. Thanks all.