Sunteți pe pagina 1din 3

Java Assignments

Day 1
1)
2)
3)
4)
5)
6)

Write a class to display Hello world


Write a class to print N Fibonacci numbers. Assume any value for N.
Write a class to find the G.C.D to two numbers.
Write a class to print the first 10 prime numbers.
Write a class to reverse an array of characters of size 20.
Write a class to sort an array of integers of size 15.

Day 2
1) Create a class room which will hold the height, width and breadth of the room
in three variables. Create another class roomdemo which will use the earlier
class, create instances of rooms, set the values of the variables and would
calculate the volume of room. [Hint: Have main in the 2 nd class and save the file
in the 2nd class name.]
2) Write a program to implement a class student with the following members.
i. Name of the student
ii. Marks of the student
iii. Function to assign initial values
iv. Function to compute total average
v. Function to display the data
Write an appropriate main() to demonstrate the functioning of the above.
3) Declare a class bank account with the following:
i. Account Holders Name
ii. Account Number
iii. Account Type (S for savings and C for Current)
iv. Balance amount
Functions:
i. Initialize data
ii. Deposit money
iii.
Withdrawal of money minimum deposit Rs. 1,000/iv. To display all items
Write an appropriate main() to run it.
4) Create a class box whose constructor function is passed three double values,
which represent the length of the sides of the box. Have the box class compute the
volume and store the result in a double variable. Include a member function vol()
that displays the volume of the box.

Day 3
1) Create a generic base class called vehicle that stores number of wheels and speed.
Create the following derived classes:
Car that inherits vehicle and also stores number of passengers.
Truck that inherits vehicle and also stores the load limit.
Write a main() function to create objects of these classes and display all the information
about Car and Truck. Also, compare the speed of the two vehicles, Car and Truck and
display faster or slower if Car is faster or slower than Truck.
2) Create a base class called Building that stores the number of floors, the number of
rooms, and the total area in square feet.
Create a derived class called House that inherits Building and also stores
number of bedrooms bathrooms.
Create another derived class called office that inherits Building and also
stores the number of chairs, tables and telephones.
Write a main() function to declare one object of each of the classes mentioned and find i)
Area of the building and ii) which object has the smallest area.
3) Write a program that creates a base class called Number. This class holds an integer
value and contains a virtual function called displayNum(). Create two derived classes
called HexNum and OctalNum that inherit Number. Override displayNum() in the
derived classes so that it displays the value in Hexadecimal and Octal, respectively. Write
a main() function to create objects of type HexNum and OctalNum classes and
display the hexadecimal and octal form of the supplied integer value. Note: Use base
class object to call a function.
4) Write a program that creates a base class called Distance. It stores the distance between
two points in a double value variable and contain a virtual function called travelTime()
that outputs the time it takes to travel that distance, assuming that the distance is in miles
and the speed is 60 miles per hour. In a derived class called DistMKS, override
travelTime() so that it outputs the travel time assuming that the distance is in kilometers
and the speed is 100 kilometers per hour. Note: Use base class object to call a function.
5) Create a generic base class called 2Dfigure that holds two dimensions of a figure. It
also declares an abstract function called calculateArea() that, when overridden by derived
classes, returns the area of the type of 2D figure defined by the derived class. Create two
derived classes, Rectangle and Triangle that inherit 2Dfigure. Write a main()
function to create object of these classes and display the area of rectangle and triangle.
Note: Use base class object to call a function.
Day 4
1) Develop an abstract class GeometricObject which will have two variables colour and
weight.it would have constructor function for setting the colour as white and the weight
as 1.0 as default values. The class should have methods getColour() and getWeight() to
return the colour and weight values to the caller. The class should have two abstract
methods findArea() and findCircumference().

2) Write a subclass for GeometricObject called Triangle which will be able to calculate
area and circumference for a triangle.

Assignment on Java Exception Handling


1. Write a code segment in a try block where divide by zero occurs such as int x=0,
y=3/x; Also write corresponding catch block to catch the exception that occurs in
the try block. Print the origin of the exception caught.
2. Create an array of 10 integers and try to assign an integer in location 15 of the array.
Put it in a try-catch block. Print the appropriate message in the catch block.
3. Consider the previous assignment. Write two catch block, one to catch the exception
Exception another for exception ArrayIndexOutOfBoundsException. In the first
catch block re-throw the exception caught. In the second catch block print the origin
of the exception recursively.
4. Create a superclass MathException and two subclasses OverFlowException and
UnderFlowException from it. Write code segment that throws an
OverFlowException. Write three catch block, one for MathException, another for
OverFlowException and another for UnderFlowException. In the first catch block rethrow the exception caught. In the other two catch blocks write appropriate
statements to handle it. Observe the result.
5. Write a code segment that results a NullPointerException. Write necessary catch
block to catch it. Also write a finally block. Write appropriate statements in it.
6. Consider the assignment number 4. Instantiate an integer variable and initialize it
with some value. If the value is greater that 100, an OverFlowException is thrown,
otherwise an UnderFlowException is called. Write appropriate message in the catch
blocks and observe the result.

S-ar putea să vă placă și