java_exercise_1
java_exercise_1
1. HelloWorld.java
I. Simply print a word “Hello World” on the screen.
II. Issue: How to capture output stream, means how to place chs on the screen.
Proposed solution: System.out.print(“Hello World”);
III. Key the code
Question: what is meant by system.out.print(), and how it differs from system.out.println()?
2. FizzBuzz.java
I. This program plays the game “fizzBuzz”. It counts to 100, replacing each multiple of 5 with the word
“fizz”, and each multiple of 7 with “buzz”, and each multiple of both with the word “fizzbuzz”.
II. Issue: how to identify that a number is divisible by 5 or 7.
Proposed solution: using modulo operator(%)
III. Key the code.
3. Fibonacci.java
I. Print a sequence of numbers like 1, 1, 2, 3, 5, 8. 13, 21, 34, 55…..
II. Issue: how to get first 10 terms
Proposed solution: c = a + b; a = b; b = c;
III. Key the code.
4. Commandline.java
I. Send some parameters into your program through command line and print them on screen
II. Issue: 1) where these strings will get collected?
2) how we will determine the number of strings(parameters) passed?
Proposed solution: 1) main(String a[]) // all the strings get collected here.
2) a.length returns the size of array. Its is the property of the array.
III. Key the code.
5. ReverseCommand.java
I. This program echos the command –line arguments backwards.
II. Issue: how to get length of oa string? And how to get a character from a string?
Proposed solution: str.length() // it is a function and not a property
str.charAt()
eg. East or west java is the best.
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
a.length = 7; a[6].length = 4.
III. Key the code.
6. Factorial.java
7. Refact.java
I. It should be able to compute at least factorial of 20.
II. Issue: which data type can store that a big number?
Proposed solution: take long data type.
8. SortNumber.java
I. Issue: how declare an array and how to initialize this array with different values every time?
Proposed solution: double nums[] = new double[10]; & Simply use Math.random()
Eg. For(int I = 0, i< nums.length; i++)
Nums[i] = Math.Random()* 100;
Now apply selection, bubble or insertion sort techniques
9. Prime.java
10. BigFact.java
I. Compute factorial greater than 20.
II. Issue: how to get represent a number that goes beyond the range of long.
Proposed solution: use class java.math.BigInteger!!!
Exercise 2 - Applet
1) Print Applet Life Cycle methods and identify when they actually get called.
Hint – no hint required
2) Getting an Applet Parameter from an html page and display it on the applet.
Hint - <param> tag and getParameter(“paramName”) method
3) Making the Browser Visit a URL as it loaded your applet
Hint - getAppletContext().showDocument(url)
4) Scroll a Message in the Browser's Status Bar.
Hint – cut the first char and add it to last, place this into a loop with some sleep time.
5) Loading and Playing Audio.
Hint - AudioClip ac = new AudioClip(url);
ac.play();
6) Loading and Drawing an Image in an Applet.
Hint – use Image image = getImage(getDocumentBase(), url);
g.drawImage(image, 0, 0, this);
Exercise 3 – Utility Classes
1. You have a record string - name:age:sex:education:experience:domain , parse the record element from
this string, store into corresponding variables and print them.
Hint – use StringTokenizer class
2. Generate random numbers.
Hint – use Random class or Math.random()
3. List all available locales (language resource bundle).
Hint – use Locale.getAvailableLocales();
4. Get the Current Time – Avoid using Date class
Hint - use Calendar cal = new GregorianCalendar();
// Get the components of the time
int hour12 = cal.get(Calendar.HOUR); // 0..11
int min = cal.get(Calendar.MINUTE); // 0..59
int sec = cal.get(Calendar.SECOND); // 0..59
int ampm = cal.get(Calendar.AM_PM); // 0=AM, 1=PM
4. Get the current Date
Hint - Look into Calendar class again.
5. Best way to compare the dates?
Hint – look Calendar class method getTimeInMillis()
6. how to load and store key value pair information from a file.
Hint – use Properties class
7. Create an Address Book program in which you can store and retrieve your friends
information.
Hint – use HashMap, ObjectInputStream and ObjectOutputStream for better performance
8. what do you mean by thread safe collections? List all thread safe collection.
9. differentiate bw Collection and Collections
10. explain List, Set and Map interfaces.
My Title X
Text Area 2
Events –
1. When I am typing in text area1 same thing should be typed in text area2, simultaneously.
2. on pressing concate button both text area contents must be concatenated.
3. on pressing clear button both textarea must be cleared.
4. on pressing notepad button, notepad should be opened with the java code of your current
program.
5. on pressing frame cross button, frame should be closed.
Exercise 5 – Thread
Exercise 6 – io