You are on page 1of 10

Birla Institute of Technology

Extension Centre, Noida


Question Bank
Subject: CS566 - Lab on Java Programming
Basic Concepts:
1. Design a java program that inputs a single letter from user and prints out the
corresponding digit on the telephone. The letters and digits on the telephone are
grouped in the following way:-
2=abc; 3=def; 4=ghi; 5=jkl; 6=mno; 7=pqrs; 8=tuv; 9=wxyz.
You may take the letter at the command line and use the following expression to
extract it.
Char c=args[0].charAt(0);

2. Write a program that takes a person’s name, age in years, gender as m or f and
marital status as y or n and then the program displays the following:
If not married and gender is ‘f’ Miss<name>
If age > 18 and gender is ‘m’ Mr<name>
If married and gender is ‘f’ Ms<name>
If age < 18 and gender is ‘m’ Master<name>
3. Write a program that takes a number from user and displays the following o/p
a) b) c)
1 1 1234……….….n
12 22 123……...n-1
123 333 ……
….. … ……
…. … 12
1234……n nnn…nn 1

4. Write a program that accepts the day of 1st January(after, 1 for Monday) day,
month and year from the user and displays the day of the given date.
E.g. Let Jan 1st be a Sunday
Let given date is 21:05:2005 then the output should be “Sunday”.
5. Write a program that displays the following output

7/4 using integer division equals<result>


7/4 using modulo division equals<result>
6. Write a program that takes your name at the command prompt and displays the
following message.
<Your name>! Welcome to Java programming.
7. Write a program that converts temperature in Fahrenheit to Celsius, you may
assume some value of temperature in your program.

8. Write a program that takes an integer from the user at command line and print the
following :
(a). Fibonacci series 0,0,1,2,…..n
(b). Sum and product of digits that constitute n
(c). 1+2^2+3^3+4^4+………+N^n
(d). Sum of all integers up n->1+2+3+…….+n
(e). Display all the digits in n place values
if n=253
3*1=3
5*10=50
2*100=200
Hint: suppose your execute your program with the following command C:\>java
MyJavaProgram 4732 , then your program will extract values “4732” using the
Following expression
Int n=Integer.parseInt(args[0]);

9. Write a program that takes integer form user and print the following o/p
*
* *
* * *
* * * *
* * *
* *
*
10. Write a program to find the number of all integers and their sum that are>100 and
<200 and divisible by 9.

11. A set of 2 linear equation with two unknown variables x and y are as follows:
ax+by = m
cx + dy =n such that x = md – nb/ad –bc
y= na-mc/ad –bc
Write a program to take values of a,b,c,d,m,n from the user and display the values of
x and y using the above equations.
Admission to a professional course is subject to the following conditions
Marks in math>=60
In physics >=50
In chemistry >=40
Total marks in 3 subjects or total marks in math & physics >=150
Write a program that takes marks in 3 subjects and prints whether the candidate is
eigible for admission.

b)
1
01
101
0101
10101
14. Write a program to print the reversed digits of a given integer and print a message if
the number is palindrome or not.
15. Write a program that prints all prime numbers from 2 to 300.
16 Write a program that writes a table of values for bitwise logical operators for all
combinations of 0 and 1.
Classes and Objects:
17. Define a class TimeType and include the following members:
- fields to represent hours, minutes and seconds.
- Constructors-default, parameterized, copy.
- Method to add or subtract few minutes to time value.
- Method to add or subtract a parameter as QUARTER_HOUR,
HALF_AN_HOUR or HOUR.
- Method called equals() that compares one TimeType object with another one
and returns true/false value.
- Method called print() that prints value of TimeType object in hh:mm:ss
format.
18. Define a class called DateType with the following members:
- fields to represent day, month and year.
- Constructors.
- Method to find difference in n dates between 2 date values.
- Method equals() to compare 2 date values for equality and returns true/false.
- Method that adds or subtracts a period from a date value by taking parameters as
QUARTER, HALF_YEAR, YEAR.
- Method that adds or subtracts a month(1-12) & number of days.
- Method called print() that prints the value of DateType object in dd-mm-yyyy
format.

19. Define a class Math Utilities that defines a integer field and provides methods for the
following functionalities:
- IsEvenNumber().
- IsPrimeNumber().
- SumOfDigits().
- ProductOfDigits().
- SumUptoN().
- SumOfSquaresUptoN().
- FibonacciSeries().
- IsPalindrome().
- ReturnSquare().

20..define a class pattern that includes the methods to display the following patterns;-

(i) 1
2 2
3 3 3
4 4 4 4
……….
…………………………n
(ii) 1
1 2
1 2 3
…………………
1 2 3 ………………n

(iii) 1 2 3 …………..n
1 2 3 …….n-1
…………..
……….
1 2
1

(iv) 1
2 3
4 5 6
7 8 9 10
…………………
……………………….n

(v) 1
1 2 1
1 2 3 2 1
……………………………
1 (n-1) n (n-1) ……………………1.

21. Define a class Stack that can store n no. of integers if size n is given or 10 integers if
n is not given .include a method push( ) to store a value on stack ; pop ( ) to retrive a
value.. display appropriate error msgs when necessary.

22. Define a class saving account that represents a savings bank account. The class
should have the following instance fields:
-Name of the account holder
-Account number
-Balance amount
Provide the following methods:
-OpenAccount()
-DepositAmount()
-WithdrawAmount()
-CloseAccount()
The account should open with a maximum balance of Rs. 1000/- and maintain it.
on closing account, the entire balance amount must be returned. The account
number should be generated by the system.
23. Implement the following class hierarchy:
Bank Account Fields:
- Name of depositor
- Account Number
- Balance Amount
Methods:
- assign all initial values
- deposits
- withdraw
- display name and balance
Fixed deposit extends bank account
An amount is deposited and is available only at the end of the term-a month, 6
months, 1 year or more.
Recurring deposit extends Bank Account
A fixed amount is deposited periodically for over a term. Amount is available
after the term.
Saving Account extends Bank Account
An account is opened with some minimum amount. An amount can be deposited
or withdrawn anytime.

24. Implement the following class hierarchy:


Library class stores following information about a book:
- book identification number
- book title
- author of the book
Text Book extends Library Book
A book is issued to a person for a term- a semester or a trimester or a year.
Reference Book extends Library Book
A book is issued only to be read in the library on the same day.
Issue Book extends Library Book
A book is issued to a person for 3 days. A fine of Rs 1/- is collected for
every delayed day.

25. Implement the following class hierarchy:


Abstract class Shape -> class 2-D shape extends Shape -> classes Rectangle,
Circle, Triangle.
Design the above class hierarchy that describes special features of each class.

26. Implement the following class hierarchy:


Mammal -> Feline -> Lion, Tiger, Cat
Think of appropriate fields for each class and add appropriate methods to each class.
Packages:
27. Create packages for various classes that you had created so far. For e.g. Create a
package bank account for the class hierarchy of bank account system.

Interfaces:

28.Define an interface called Shape which defines a draw() method.Define two sub
interfaces to this one—Two D shape which defines abstract methods area() and
perimeter(); and Three D shape in which Surface Area(); Volume methods are
declared.Implement these interfaces in different classes such as rectangle,circle,cube,and
cylinder.

29.Define an interfaces called Int Stack with two methods


- Push(int x) and Pop(). Implement this interface to define a stack of
integers of fixed numbers(size) and a stack that grows in size by one items
at a time.

30.Define an interface Queue that defines methods to store and retrieve items. Derive
two queue from this one

- an integer queue IntQueue


- a queue of strings StringQueue

String Classes:

31.Write a program to extract a portion of string and print it.


32.Write a program which reads a paragraph and counts all occurrences of the articles in
it(a, an, the).
33.Write a program that reads ‘n’ number of strings and prints them in alphabetical order.
34.Write a method that count the number of occurrences of a given string.
35.Overloaded the above method to count the number of occurrences of a given substring
in a string.
36.Write a method to convert string containing decimal number in to a comma-punctured
number. For e.g. 1753734 return 1, 753, 734.
37.Overload the above method to return a comma-punctured number, based on Indian
number system . For e.g. 1753734 return 17, 53, 734.
38.Use the above method to convert a given number to a string that is word
representation of the number. For e.g. 1,753,734 return “one million seven fifty three
thousand seven hundred thirty four”.
39.Overload the above method for decimal numbers.
40.Overload the above method to convert a given number to a string that is word
representation of the number. For e.g. 17,53,734 return seventeen lakh fifty three
thousand seven hundred and thirty four.
41.Write a method that counts the number of occurrences of a given character in a string.
ARRAYS:
42)Write a program that takes n numbers from the users and does the following tasks:

-search for particular value


-display the contents in ascending order without changing the original array
-display the contents in descending order
-modify the content of an item

43) Write a program that two arrays A and B and merges them into single array C
such that common elements of A and B appear only once in C
44) Write a program that takes values for two matrices A and B and perform the
following tasks:
-prints transpose of A
-prints inverse of A
- prints A+B
-prints A-B
-printsAB

45) Write a method that reads an integer from the input and returns binary representation
of number .
46) Submit assignment on vector class that is defined in java.util. package.
47) Submit assignment on wrapper classes in java.

I/O STREAMS:

48) Create a pair of stream classes that encrypts bytes using any algorithm you choose.
for example XOR the bytes with some value for encrypting .your decryptstream class
should decrypt the encrypted input.

49) Write a program that reads the specified file and searches the specified word, printing
all the lines on which that word is found ,preceded by its line number.
50) Add a method to your vehicle class that writes the contents of an object to
DataOutput Stream and a constructor that will read the state from Data Input Stream.
51) Do the same for the BankAccount that you have designed.
52) Re-implement the Bank Account class such that all the accounts are stored in a file
and provide the functionality to add an account to the file ,modify details of an account )
such as deposit, withdraw which change balance field of account .use
RandomAccessFile class.
53) Write a method that, given a directory name, prints all the files in that directory.

Applets:

58) Develop an applet that reads an integer ‘n’ and a message from the user and displays
the massage in n times, each time in a different color. The delay between each display
must be 2 sec.
59) Deign an applet to draw the following shapes:-
-cone -cube -pyramid
-Cylinder -triangle - square inside a triangle

60) Design an applet to display a chess board.

61) Design an applet to implement traffic light signals

62) Design an applet to implement calculator.


63) Write an applet that shows a pair of dice. When the user clicks on the applet, the dice
should be rolled (that is, the dice should be assigned newly computed random values).
Each die should be drawn as a square showing from 1 to 6 dots. Since you have to draw
two dice, its a good idea to write a subroutine, "void drawDie(Graphics g, int val,
int x, int y)", to draw a die at the specified (x,y) coordinates. The second
parameter, val, specifes the value that is showing on the die. Assume that the size of the
applet is 100 by 100 pixels.
64) Improve your dice applet from the previous exercise so that it also responds to
keyboard input. When the applet has the input focus, it should be hilited with a colored
border, and the dice should be rolled whenever the user presses a key on the keyboard.
This is in addition to rolling them when the user clicks the mouse on the applet

65) write a checkerboard applet where the user can select a square by clicking on it.
Hilite the selected square by drawing a colored border around it. When the applet is first
created, no square is selected. When the user clicks on a square that is not currently
selected, it becomes selected. If the user clicks the square that is selected, it becomes
unselected. Assume that the size of the applet is 160 by 160 pixels, so that each square on
the checkerboard is 20 by 20 pixels.

66) Modify your bank account program to use GUI for various functions of bank
operations.

67) Design a GUI based program that maintains student’s records in a file. The items
stored are students roll number, name, marks in quizzes, of all students in class,
subject name, subject code. When required, the application must display total of best
2 of 4 quizzes for all students. The application must also be able to accept the roll
number from the user and display entire detail of that student.

Exceptions:-
68) Implement exceptions for above two questions.

Threads:-

69) Write a program that prints out the elapsed time from the start of execution with a
thread that prints a message every 15 seconds. Have the message printing thread be
notified by the time printing thread. Add another thread that prints a different
message every 7 seconds without modifying the time priority thread.

70) Write a class whose objects hold a current value and have a method that will add to
that value, printing a new value. Write a program that creates such an object, creates
multiple threads, and invokes the adding method repeatedly from each thread. Write the
class such that no addition can be lost.

Miscellaneous:

71. In the BankAccount class that you had created, includes a method issuedraft() that
takes an amount, to whom to pay, where to pay and prints the details of the draft as
follows:
Date :
Favoring :
Payable at :
Amount :
Amount in words :
Draft No. :

72. Write a program that reads a file with entries separated by line starting with %% and
creates a “table” file with the starting position of each such entry. Write a program that
prints entry using that table.

73 Write a program that copies its input to its output, translating once particular byte
value to a different byte value along the way. The TranslateByte program takes two
parameters: a “from” byte and a “to” byte. Byte that matches the value in the string
“from” is translated into the value in the string to.

74. Rewrite TranslateByte as a method that translates the content of a InputStream into an
OutputStream,in which the mapping and the streams are parameters. For each type of
InputStream and OutputStream, write a new main method that uses this method to
operate on the a stream of that type.

75 Rewrite TranslateByte as a filter.

76. Create a pair of filter stream classes that encrypt bytes using any algorithms you
choose- for eg.XOR the bytes with some value. With your Decrypt InputStream decrypt
the bytes.

77 Create a subclass of filter reader that will return one line of i/p at a time via a method
that blocks until a full line i/p is available.

78. Write a program that takes input in the form “name op value”, where name is one of
the three words of your choosing, op is +,- or =, and value is a number. Apply each
operator to the named value. When input is exhausted, print their values. For extra credit,
use HashMap class.

79. Using A filename filter or file filter. Write a program that takes a directory and suffix
as parameters and all files that have that suffix.

80.Write a program to implement a calculator that has all math functions as well as the
following basic operators +,-,*,/,&,%.

You might also like