Professional Documents
Culture Documents
Lab Manual
of
“OBJECT ORIENTED PROGRAMING-I”
SUBJECT CODE: 3140705
Semester: 4
CERTIFICATE
Date: / /
~2~
Enrollment-No : 190390107043
INDEX
~3~
Enrollment-No : 190390107043
15 Write a program that will use static data member within class.
Create a class Account. It has three data fields accountID,
name and balance. Define methods to assign values and
16* display values. Write a test program that prompts the user to
enter details and display the same details.
Design a class named Rectangle to represent a rectangle.
The class contains:
1. Two double data fields named width and height
that specify the width and height of the rectangle.
The default values are 1 for both width and height.
2. A no-arg constructor that creates a default rectangle.
3. A constructor that creates a rectangle with the
specified width and height.
4. A method named getArea() that returns the
area of this rectangle.
17* 5. A method named getPerimeter() that returns the
perimeter. Write a test program that creates two Rectangle
objects—one with width 4 and height 40 and the other
with width 3.5 and height 35.9. Display the width, height,
area, and perimeter of each rectangle in this order.
18 Write a program that will explain concept of array of objects.
19* Write a program to demonstrate use of this keyword.
20 Write java Program for single level and Multilevel inheritance.
21 Write a java program for method Overriding.
22 Write a java program to demonstrate the use of super keyword.
23 Write java program for Interface.
Describe an abstract class called Shape which has three
subclasses say Triangle, Rectangle and Circle. Define one
24* method area() in the abstract class and override this area()
in these three subclasses to calculate area for specific
class’ object.
Write a program that illustrates interface inheritance.
Interface A is extended by A1 and A2. Interface A12
inherits from both A1 and A2. Each interface declares one
25
constant and one method. Class B implements A12.
Instantiate B and invoke each of its methods. Each method
displays one of the constants.
26 Write a Java program that will use try catch and Finally block.
Write a program that moves a circle up, down, left or right
27 using arrow keys.
Write a JAVA Program to read file and display content on
28*
screen.
Write an application that executes two threads. One thread
29 displays “A” every 1,000 milliseconds, and the other
displays “B” every 3,000
~4~
Enrollment-No : 190390107043
o Open a command prompt window and go to the directory where you saved
the java program (WelcomeToJava.java). Assume it's C:\
~5~
Enrollment-No : 190390107043
Practical - 1
Write a Program that displays Welcome to Java, Learning Java Now and Programming
is fun.
public class LM1 {
public static void main(String[] args) {
System.out.println("Welcome to Java");
System.out.println("Learning Java Now");
System.out.println("Programing is Fun");
}
}
~6~
Enrollment-No : 190390107043
Practical - 2
Write a program that solves the following equation and displays the value x and y:
1) 3.4x+50.2y=44.5 2) 2.1x+.55y=5.9 (Assume Cramer’s rule to solve equation ax+by=e
x=ed-bf/ad-bc cx+dy=f y=af-ec/ad-bc )
import java.util.Scanner;
~7~
Enrollment-No : 190390107043
~8~
Enrollment-No : 190390107043
Practical - 3
Write a program that reads a number in meters, converts it to feet, and displays the
result.
import java.util.Scanner;
~9~
Enrollment-No : 190390107043
Practical - 4*
Body Mass Index (BMI) is a measure of health on weight. It can be calculated by taking
your weight in kilograms and dividing by the square of your height in meters. Write a
program that prompts the user to enter a weight in pounds and height in inches and
displays the BMI. Note:- 1 pound=.45359237 Kg and 1 inch=.0254 meters.
import java.util.Scanner;
import java.lang.Math;
// Taking height
System.out.print("Enter your height(m.) : ");
height = sc.nextDouble();
// Taking weight
System.out.print("\nEnter your weight(kg.) : ");
weight = sc.nextDouble();
}
}
~ 10 ~
Enrollment-No : 190390107043
Practical - 5*
Write a program that prompts the user to enter three integers and display the integers
in decreasing order.
import java.util.Scanner;
import java.lang.Math;
~ 11 ~
Enrollment-No : 190390107043
~ 12 ~
Enrollment-No : 190390107043
Practical - 6
Write a program that prompts the user to enter a letter and check whether a letter is a
vowel or constant.
import java.util.Scanner;
~ 13 ~
Enrollment-No : 190390107043
Practical - 7
Write a program that prompts the user to enter two integers and display the maximum
using method.
import java.util.Scanner;
import java.lang.Math;
~ 14 ~
Enrollment-No : 190390107043
Practical - 8*
Write a method with the following method header.
public static int gcd(int num1, int num2) Write a program that prompts the user to
enter two integers and compute the gcd of two.
public static int gcd(int num1,int num2){
int maximum = Math.max(num1,num2); // Maximum of two numbers
int minimum = Math.min(num1,num2); // Minimum of two numbers
if(maximum % minimum == 0){ // GCD If minimum can divide maximum return it
return minimum;
}else {
for (int i = Math.min(num1,num2); i > 0; i--) {
if(num1 % i == 0 && num2 % i == 0){
return i; // GCD
}
}
}
return 0;
}
~ 15 ~
Enrollment-No : 190390107043
Practical - 9
Write a test program that prompts the user to enter ten numbers, invoke a method to
reverse the numbers, display the numbers.
import java.util.Scanner;
~ 16 ~
Enrollment-No : 190390107043
Practical - 10
Write a program that creates a Random object with seed 1000 and displays the first 100
random integers between 1 and 49 using the NextInt (49) method.
import java.util.Random;
~ 17 ~
Enrollment-No : 190390107043
Practical - 11
Write a program that generate 6*6 two-dimensional matrix, filled with 0’s and 1’s ,
display the matrix, check every raw and column have an odd number’s of 1’s.
~ 18 ~
Enrollment-No : 190390107043
Practical - 12*
Write a program that will use object as function argument.
~ 19 ~
Enrollment-No : 190390107043
Practical - 13
Write a program that will get information (CarName, ModelNo, color) and display it.
Create proper getter and setter methods for it.
~ 20 ~
Enrollment-No : 190390107043
Practical - 14
Write a program that will explain concept of function overloading.
~ 21 ~
Enrollment-No : 190390107043
Practical - 15
Write a program that will use static data member within class.
~ 22 ~
Enrollment-No : 190390107043
Practical - 16*
Create a class Account. It has three data fields accountID, name and balance. Define
methods to assign values and display values. Write a test program that prompts the user
to enter details and display the same details.
~ 23 ~
Enrollment-No : 190390107043
Practical - 17*
Design a class named Rectangle to represent a rectangle. The class contains:
1. Two double data fields named width and height that specify the width and height of
the rectangle. The default values are 1 for both width and height.
2. A no-arg constructor that creates a default rectangle.
3. A constructor that creates a rectangle with the specified width and height.
4. A method named getArea() that returns the area of this rectangle.
5. A method named getPerimeter() that returns the perimeter. Write a test program
that creates two Rectangle objects—one with width 4 and height 40 and the other with
width 3.5 and height 35.9. Display the width, height, area, and perimeter of each
rectangle in this order.
~ 24 ~
Enrollment-No : 190390107043
Practical - 18
Write a program that will explain concept of array of objects.
~ 25 ~
Enrollment-No : 190390107043
Practical - 19*
Write a program to demonstrate use of this keyword.
~ 26 ~
Enrollment-No : 190390107043
Practical - 20
Write java Program for single level and Multilevel inheritance.
~ 27 ~
Enrollment-No : 190390107043
Practical - 21
Write java program for method Overriding.
~ 28 ~
Enrollment-No : 190390107043
Practical - 22
Write java program to demonstrate the use of super keyword.
~ 29 ~
Enrollment-No : 190390107043
Practical - 23
Write java program for Interface.
~ 30 ~
Enrollment-No : 190390107043
Practical - 24*
Describe abstract class called Shape which has three subclasses say Triangle, Rectangle
and Circle. Define one method area() in the abstract class and override this area() in
these three subclasses to calculate area for specific class’ object.
~ 31 ~
Enrollment-No : 190390107043
Practical - 25
Write a program that illustrates interface inheritance. Interface A is extended by A1
and A2. Interface A12 inherits from both A1 and A2. Each interface declares one
constant and one method. Class B implements A12. Instantiate B and invoke each of its
methods. Each method displays one of the constants.
~ 32 ~
Enrollment-No : 190390107043
Practical - 26
Write a Java program that will use try catch and Finally block.
~ 33 ~
Enrollment-No : 190390107043
Practical - 27
Write a program that moves a circle up, down, left or right using arrow keys.
~ 34 ~
Enrollment-No : 190390107043
Practical - 28*
Write a JAVA Program to read file and display content on screen.
~ 35 ~
Enrollment-No : 190390107043
Practical - 29
Write an application that executes two threads. One thread displays “A” every 1,000
milliseconds, and the other displays “B” every 3,000
~ 36 ~