Practice Question
Practice Question
Note: You can find the solution of these questions on google easily, but our goal is to practice them not
copy from google because such type of questions helps in java lab and course paper also these questions
built you logic if you can try by yourself not from google. At last you can compare your solution with
google solution just to make sure that your solution is correct.
2.
Create a class named 'Student' with String variable 'name' and integer variable 'roll_no'. Assign the value of
roll_no as '2' and that of name as "John" by creating an object of the class Student.
3.
Assign and print the roll number, phone number and address of two students having names "Sam" and "John"
respectively by creating two objects of class 'Student'.
4.
Write a program to print the area and perimeter of a triangle having sides of 3, 4 and 5 units by creating a class
named 'Triangle' without any parameter in its constructor.
5.
Write a program to print the area and perimeter of a triangle having sides of 3, 4 and 5 units by creating a class
named 'Triangle' with constructor having the three sides as its parameters.
6.
Write a program to print the area of two rectangles having sides (4,5) and (5,8) respectively by creating a class
named 'Rectangle' with a method named 'Area' which returns the area and length and breadth passed as
parameters to its constructor.
7.
Write a program to print the area of a rectangle by creating a class named 'Area' taking the values of its length
and breadth as parameters of its constructor and having a method named 'returnArea' which returns the area of
the rectangle. Length and breadth of rectangle are entered through keyboard.
8.
Print the average of three numbers entered by user by creating a class named 'Average' having a method to
calculate and print the average.
9.
Print the sum, difference and product of two complex numbers by creating a class named 'Complex' with
separate methods for each operation whose real and imaginary parts are entered by user.
10.
Write a program that would print the information (name, year of joining, salary, address) of three employees
by creating a class named 'Employee'. The output should be as follows:
Name Year of joining Address
Robert 1994 64C- WallsStreat
Sam 2000 68D- WallsStreat
John 1999 26B- WallsStreat
11.
2.
Create a class called 'Matrix' containing constructor that initializes the number
of rows and number of columns of a new Matrix object. The Matrix class has the
following information:
1 - number of rows of matrix
2 - number of columns of matrix
3 - elements of matrix in the form of 2D array
3.
4.
Write a program to print the area and perimeter of a triangle having sides of 3, 4 and 5units by
creating a class named 'Triangle' without any parameter in its constructor.
5.
Print the average of three numbers entered by user by creating a class named 'Average'having a
method to calculate and print the average.
6.
Print the sum, difference and product of two complex numbers by creating a classnamed
'Complex' with separate methods for each operation whose real and imaginaryparts are entered
by user.
7.
Write a program by creating an 'Employee' class having the following methods and printthe final
salary. 1 - 'getInfo()' which takes the salary, number of hours of work per day of employee
asparameter
2 - 'AddSal()' which adds $10 to salary of the employee if it is less than
$500.3 - 'AddWork()' which adds $5 to salary of employee if the number of hours of work
perday is more than 6 hour
8.
Write a program that has variables to store Car data like; CarModel, CarName, CarPrice
and CarOwner. The program should include functions to assign user defined values to
the above mentioned variable and a display function to show the values. Write a mai
Write a program that has variables to store Car data like; CarModel, CarName, CarPriceand
CarOwner. The program should include functions to assign user defined values tothe above
mentioned variable and a display function to show the values. Write a main that calls these
functions. Now write another runner class that declares three Carobjects and displays
the data of all three
9.
A Student is an object in a university management System. Analyze the concept andidentify the
data members that a Student class should have. Also analyze thebehavior of student
in a university management System and identify the methodsthat should be included in
Student class.
10.
Create a class circle class with radius as data member. Create two constructors (noargument,
and two arguments) and a method to calculate Circumference.
Activity 2:
11.
Create a class Account class with balance as data member.
Create twoconstructors (no argument, and two arguments) and methods to withdraw and
depositbalance.
Create a class “Distance” with two constructors (no argument, and two argument), twodata
Create a class “Distance” with two constructors (no argument, and two argument), twodata
12.
Write a class Time with three data members to store hr, min and seconds. Create
two constructors and apply checks to set valid time. Also create display function which
displays all data members.
11. Create a class “ Distance” with two constructors (no argument, and two argument), two
data
11. Implement Vehicle as outer and owner as the inner class, the vehicle class contains
vehicle name, engine cc, model as data members. The inner class data members are
owners name, CNIC number and phone contact of the owner.
Write down proper setters/ getters and constructors for both the classes.
1: Override the method of a class using anonymous inner class.
2: Pass an anonymous inner class as a method argument.
3: Implement the inner class as static first and then as non static nested class
Write a class Time with three data members to store hr, min and seconds. Createtwo
constructors and apply checks to set valid time. Also create display function whichdisplays all
data members.
13.
Implement Vehicle as outer and owner as the inner class, the vehicle class containsvehicle
name, engine cc, model as data members. The inner class data members areowners name, CNIC
number and phone contact of the owner. Write down proper setters/ getters and constructors for
both the classes. 1: Override the method of a class using anonymous inner class. 2: Pass an
anonymous inner class as a method argument. 3: Implement the inner class as static first and then
as non static nested class
14.
Create an Abstract class Student that contains a method take exam, implement themethod in the
child classes PhdStudent and GradStudent in which PhdStudent takesexam by giving his
final defense presentation while the graduate student gives a written paper
15.
Define a class called Fraction. This class is used to represent a ratio of two integers.Create two
constructors, set, get and display function. Include an additional method,equals, that takes as
input another Fraction and returns true if the two fractions areidentical and false if they are not
16.
Create a SavingsAccount class. Use a static data member annualInterestRate to
store the annual interest rate for each of the savers. Each member of the class contains
a private data member savingsBalance indicating the amount the saver currently has on
deposit. Provide member function calculateMonthlyInterest that calculates the monthly
interest by multiplying the balance by annualInterestRate divided by 12; this interest
should be added to savingsBalance. Provide a static member function
modifyInterestRate that sets the static annualInterestRate to a new value. Write a driver
program to test class SavingsAccount. Instantiate two different objects of class
SavingsAccount, saver1 and saver2, with balances of $2000.00 and $3000.00,
respectively. Set the annualInterestRate to 3 percent. Then calculate the monthly
interest and print the new balances for each of the savers. Then set the
annualInterestRate to 4 percent, calculate the next month's interest and print the new
balances for each of the savers.
Create a SavingsAccount class. Use a static data member annualInterestRate tostore the
annual interest rate for each of the savers. Each member of the class containsa private data
member savingsBalance indicating the amount the saver currently has ondeposit. Provide
member function calculateMonthlyInterest that calculates the monthlyinterest by multiplying the
balance by annualInterestRate divided by 12; this interestshould be added to
savingsBalance. Provide a static member functionmodifyInterestRate that sets the static
annualInterestRate to a new value. Write a driverprogram to test class SavingsAccount.
Instantiate two different objects of classSavingsAccount, saver1 and saver2, with
balances of $2000.00 and $3000.00,respectively. Set the annualInterestRate to 3
percent. Then calculate the monthlyinterest and print the new balances for each of
the savers. Then set theannualInterestRate to 4 percent, calculate the next month's interest
and print the newbalances for each of the savers.
17.
Create a class named Pizza that stores information about a single pizza. It should containthe
following:
Private instance variables to store the size of the pizza (either small, medium,or large),
the number of cheese toppings, the number of pepperoni toppings, andthe number of ham
toppings.
Constructor(s) that set all of the instance variables.
Public methods to get and set the instance variables.
A public method named calcCost( ) that returns a double that is the cost of the pizza.Pizza cost is
determined by:
Small: $10 + $2 per topping
Medium: $12 + $2 per topping
Large: $14 + $2 per topping
public method named getDescription( ) that returns a String containing the pizzasize, quantity of
each topping.
Write test code to create several pizzas and output their descriptions. For example, alarge pizza
with one cheese, one pepperoni and two ham toppings should cost a total of$22. Now Create a
PizzaOrder class that allows up to three pizzas to be saved in an order.Each pizza saved should
be a Pizza object. Create a method calcTotal() that returns thecost of order. In the runner order
two pizzas and return the total cost
18.
Write a Java program to find the longest word in a text file.
19.
Write a Java program to read the first 3 lines of a file.