0% found this document useful (0 votes)
4 views

java_exercise_1

Uploaded by

manoj kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

java_exercise_1

Uploaded by

manoj kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Exercise – 1 – General

Step 1: First, have a clear idea as what to do?


Step 2: Identify the different issues that we may have to face.
Step 3: Start coding!!! (Remember!!! Your code must be fully documented (commented) and proper alignment must
be there.

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!!!

Sumit Agarwal (MCA)


University Gold Medalist
Software Engineer

PDF Creator - PDF4Free v2.0 http://www.pdf4free.com


11. Create a generic stack (Stack as an template) that can be used for any type of data.
12. create a link list program in java.
Hint – for keeping the address of next node, store the refrence.
Class node{
int I;
node next; }
13. Create your own checked exception in stack program and throw them whenever exceptional situation is raised.

Exercise 2 - Applet

Note: -Follow the same approach as in Exercise 1

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.

Sumit Agarwal (MCA)


University Gold Medalist
Software Engineer

PDF Creator - PDF4Free v2.0 http://www.pdf4free.com


Exercise 4 - AWT
1. create a frame looks like

My Title X

Image Text area1

Text Area 2

Concate Clear Notepad

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.

2. Develop Tic Tac Toe


Hint – Use grid layout and simple event handling
x x
Add button on each grid and on press check
- isValidMove 0
- isWin
x

3. Create a TextEditor having the following functionality-


File – New, Save, SaveAs, Close, CloseAll, Print, Exit and Last opened file name
Edit – All options those are in Notepad Edit option
Format-font, line no.
Build – set classpath, set env, compile Java, run java, create jar
Database – connection, execute sql
Help-about

4. create the following look and feel


Hint – use GridbagLayout

Sumit Agarwal (MCA)


University Gold Medalist
Software Engineer

PDF Creator - PDF4Free v2.0 http://www.pdf4free.com


5. Make an icon on your desktop for your TextEditor, on clicking that icon your Editor should open.
Hint - create a executable jar file of your TextEditor and create a batch file having command
java –jar TextEditor.jar.

Exercise 5 – Thread

1. Solution of Producer Consumer Problem.


Hint – make the methods synchronized.
2. Make your TextEditor threaded so that when compilation of java program is going on we should be able to
do something else on editor.
Hint – put your code for compilation into a thread
3. Make a threaded applet on which it will be displaying two sequence of no. one in increasing and another in
decreasing.
4. Make a GUI application having two circles, one controlled by arrow keys and one by H J K N keys. On
pressing the respective key circle should move in that direction
5. list all threads running within jvm.
6. write a program that will ensure if parent thread is destroyed then all childen should also get destroyed.
7. Make your Stack program synchronized so if push() is going on pop() should wait.

Exercise 6 – io

1. Create a industry level file copy program.


Hint –
- Check all condition by using File class before taking handle of the actual files.
-source is not a dir
-if destination is a dir then use the same source file name
-at destination if file exist then ask for overwriting
-if destination not given copy the file in the same location with name “copy –of –filename”
- if every thing is fine then create the stream (take the handle)
- copy into chunk don’t use available()
- release the handles.(resources)

2. store your AddressBook in a file.


3. Convert your byte array into a stream object.

Sumit Agarwal (MCA)


University Gold Medalist
Software Engineer

PDF Creator - PDF4Free v2.0 http://www.pdf4free.com

You might also like