Cycle Questions
Cycle Questions
2. Define a class called Rectangle with member variables length and width. Use appropriate
member functions to calculate the perimeter and area of the rectangle. Define another member
function int sameArea(Rectangle) that has one parameter of type Rectangle. sameArea returns 1 if
the two Rectangles have the same area, and returns 0 if they don't. Use appropriate constructors to
initialize the member variables(Use both default and parameterized constructor)
Write a main function to create two rectangle objects and display its area and perimeter. Check
whether the two Rectangles have the same area and print a message indicating the result. (Use the
concept of this pointer too)
3. Write the definition for a class called Complex that has floating point data members for storing
real and imaginary parts. Define a function Complex sum(Complex) to add two complex numbers
& return complex number. Write main function to create three complex number objects. Set the
value in two objects and call sum() to calculate sum and assign it in third object. Display all
complex numbers. (Use the concept of this pointer too.)
4. Define a class called Time that has hours and minutes as integer. The class has the following
member function: Time sum(Time) to sum two time object & return time
a. Use the concept of constructor overloading to initialize the time
b. Write the definitions for each of the above member functions.
c. Write main function to create three time objects. Set the value in two objects and call sum() to
calculate sum and assign it in third object. Display all time objects. (Use the concept of this pointer
too)
5. Create a class ‘Account’ with two overloaded constructors. The first constructor is used for
initializing the name of account holder, the account number and the initial amount in the account.
The second constructor is used for initializing the name of the account holder, the account number,
the addresses, the type of account and the current balance. The Account class is having methods
Deposit (), Withdraw (), and Get_Balance(). Make the necessary assumption for data members
and return types of the methods. Create objects of Account class and use them.
2. Write two Java classes Employee and Engineer. Engineer should inherit from Employee class.
Employee class to have two methods display() and calcSalary(). Write a program to display the
engineer salary and to display from Employee class using a single object instantiation (i.e., only
one object creation is allowed).
• display() only prints the name of the class and does not return any value. Ex. “ Name of
class is Employee.”
• calcSalary() in Employee displays “Salary of employee is 10000” and calcSalary() in
Engineer displays “Salary of employee is 20000.”
3. Write a Java program to implement the following level of inheritance.
4. Write a java program to create an abstract class named Shape that contains an empty
method named numberOfSides(). Provide three classes named Rectangle, Triangle and
Hexagon such that each one of the classes extends the class Shape. Each one of the classes
contains only the method numberOfSides( ) that shows the number of sides in the given
geometrical structures.
5. write a program to represent geometric shapes and some operations that can be performed
on them. The idea here is that shapes in higher dimensions inherit data from lower
dimensional shapes. For example a cube is a three dimensional square. A sphere is a three
dimensional circle and a glome is a four dimensional circle. A cylinder is another kind of
three dimensional circle. The circle, sphere, cylinder, and glome all share the attribute
radius. The square and cube share the attribute side length. There are various ways to use
inheritance to relate these shapes but please follow the inheritance described in the table
below.
Specification:
Your program will consist of the following classes: Shape, Circle, Square, Cube,
Sphere, Cylinder, and Glome and two interfaces Area and Volume
Your classes may only have the class variable specified in the table below and the
methods defined in the two interfaces Area and Volume. You will implement the
methods specified in the Area and Volume interfaces and have them return the
appropriate value for each shape. Class Shape will have a single public method called
getName that returns a string.
Cycle 4
Input-Output, File Management and exception handling
1. Write a Java Program to merge data from two files into a third file. (Handle all file related
exceptions)
2. Write a Java Program to perform file merge operation where merging should be done by line
by line alternatively. (Handle all file related exceptions)
3. Write a Java program that reads a set of real numbers from a file and displays the minimum,
maximum, average, and range of the numbers in the file. The user should be able to enter the name
of the input file from the keyboard.
4. Write a program that reads the contents of a file and creates an exact copy of the file, except
that each line is numbered. For example, if the input file contains the following text:
The user should be able to enter the names of the input and output files from the keyboard.
5. Write a Java program that reads a line of integers, and then displays each integer, and the sum
of all the integers. (Use StringTokenizer class of java.util)
6. Write a Java program that displays the number of characters, lines and words in a text file.
7. Create a class Student with attributes roll no, name, age and course (use user inputs).If age of
student is not in between 15 and 21 then generate an exception to handle it.
8. Write a Java program to define a class salesman with the attributes name, salesman code, sales
amount and commission(use user inputs). The Company calculates the commission of a salesman
according to the following formula:
(i) 8% if sales <2000
(ii) 10% sales if sales>=2000 and but <=5000
(iii) 12% if sales exceeds 5000
Create salesman objects and find the commission of sales. Generate and handle exceptions if
sales amount is less than 0.
Cycle 5: Multithreading
1) Write a Java program to create two threads: One for displaying all odd number between 1 and
100 and second thread for displaying all even numbers between 1 and 100. Create a
multithreaded program by creating a subclass of Thread and then creating, initializing, and
starting two Thread objects from your class. The threads will execute concurrently Main
thread should wait until all the other thread terminates its execution(using join()).
2) Write a Java program that set thread priorities and display the priority.
3) Write a java program that implements a multi-thread application that has three threads. The
first thread generates random integer every 1 second and if the value is even, second thread
computes the square of the number and prints. If the value is odd, the third thread will print
the value of cube of the number
4) Write a program to illustrate creation of threads using runnable interface. (start method start
each of the newly created thread. Inside the run method there is sleep() for suspend the thread
for 500 milliseconds). Main thread should wait until all the other thread terminates its
execution (using join()).
5) Write a java program showing a typical invocation of banking operations via multiple threads.
Create three threads and 2 methods deposit and withdraw methods to add the amount to the
account and withdraw an amount from the account respectively. As the threads concurrently
run the method, avoid the unpredictable behavior. (Use synchronization).
2. Write a Java program that implements Quick sort algorithm for sorting a list of names in
ascending order.