You are on page 1of 36

A

Lab Manual
of
“OBJECT ORIENTED PROGRAMING-I”
SUBJECT CODE: 3140705

Semester: 4

Department of Computer Engineering & Information Technology


Summer 2021

S.P.B. Patel Engineering College, Linch


Saffrony Institute of Technology
Gujarat Technological University, Ahmedabad
Enrollment-No : 190390107043

CERTIFICATE

This is to certify that Mr/Ms.


Enrolment No. of 4th Semester Degree Course
in CE/IT Engineering has satisfactorily completed and submitted his/her
Laboratory work in the subject of Object Oriented Programing-I
(3140705) within four walls of the Laboratory of the Institute during
summer 2021.

Subject In-charge Head of Department

Date: / /

~2~
Enrollment-No : 190390107043

INDEX

Sr. Title Date


No. Performed Sign

Write a Program that displays Welcome to Java, Learning


1 Java Now and Programming is fun.
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
2 (Assume Cramer’s rule to solve equation ax+by=e
x=ed-bf/ad-bc cx+dy=f y=af-ec/ad-bc )
Write a program that reads a number in meters, converts it to
3 feet, and displays the result.
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
4 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.
5 Write a program that prompts the user to enter three
* integers and display the integers in decreasing order.
Write a program that prompts the user to enter a letter and
6 check whether a letter is a vowel or constant.
Write a program that prompts the user to enter two
7 integers and display the maximum using method.
Write a method with following method header.
8 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.
Write a test program that prompts the user to enter ten
9 numbers, invoke a method to reverse the numbers,
display the numbers.
Write a program that creates a Random object with seed
10 1000 and displays the first 100 random integers between
1 and 49 using the NextInt (49) method.
Write a program that generate 6*6 two-dimensional matrix,
11 filled with 0’s and 1’s , display the matrix, check every raw
and column have an odd number’s of 1’s.
12* Write a program that will use object as function argument.
Write a program that will get information (CarName,
13 ModelNo, color) and display it. Create proper getter and
setter methods for it.
Write a program that will explain concept of function
14
overloading.

~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

❖ How to set the PATH variable to the java directory?

1. From the desktop, right click the Computer icon.


2. Choose Properties from the context menu.
3. Click the Advanced system settings link.
4. Click Environment Variables. In the section System Variables, find the
PATH environment variable and select it. Click Edit. If the PATH
environment variable does not exist, click New.
5. In the Edit System Variable (or New System Variable) window, specify the
value of the PATH environment variable (For Ex: C:\Program
Files\Java\jdk1.8.0_202\bin C:\Program Files\Java\jdk1.8.0_202\bin). Click
OK. Close all remaining windows by clicking OK.
6. Reopen Command prompt window, and run your java code.

❖ Compiling and running a Java program: (after JDK installation)

o Open a command prompt window and go to the directory where you saved
the java program (WelcomeToJava.java). Assume it's C:\

o Type “javac WelcomeToJava.java” and press enter to compile your code. If


there are no errors in your code, the command prompt will take you to the
next-line.
o Now, type “java WelcomeToJava” to run your program.
o You will be able to see the result printed on the window.

~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;

public class LM2 {


public static void main(String[] args) {
// Create a new scanner
double x,y; // Declaring x,y as double variable
double a1,a2,b1,b2; // Declaring coefficient of x and y for two equation
double c1,c2; // Declaring constants for two equation
Scanner sc = new Scanner(System.in);

// Taking user input for equation 2


System.out.println("For equation a1X + b1Y = c1. \nEnter the value of
a1,b1,c1 : ");
System.out.print("a1 = ");
a1 = sc.nextDouble();
System.out.print("b1 = ");
b1 = sc.nextDouble();
System.out.print("c1 = ");
c1 = sc.nextDouble();

// Taking user input for equation 1


System.out.println("For equation a2X + b2Y = c2. \nEnter the value of
a1,b1,c1 : ");
System.out.print("a2 = ");
a2 = sc.nextDouble();
System.out.print("b2 = ");
b2 = sc.nextDouble();
System.out.print("c2 = ");
c2 = sc.nextDouble();
x = ((c1 * b2) - (b1 * c2))/((a1 * b2)-(b1 * a2)); // Using cramer's rule
y = ((a1 * c2) - (c1 * a2))/((a1 * b2)-(b1 * a2)); // Using cramer's rule
System.out.println("From equations: \n 1) " + a1 + "X + " + b1 + "Y = " + c1
+ "\n 2) " + a2 + "X + " + b2 + "Y = " + c2 );
System.out.println(" The value of X = " + x + "\n The Value of Y = " + y);
}
}

~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;

public class LM3 {


public static void main(String[] args) {
double meter,feet;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the distance in meters: ");
meter = sc.nextDouble();
feet = meter * 3.28084;
System.out.println(meter + "m = " + feet + "ft as 1m = 3.28084ft");
}
}

~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;

public class LM4 {


public static void main(String[] args) {
double BMI;
double height,weight;
Scanner sc = new Scanner(System.in);

// Taking height
System.out.print("Enter your height(m.) : ");
height = sc.nextDouble();

// Taking weight
System.out.print("\nEnter your weight(kg.) : ");
weight = sc.nextDouble();

BMI = weight / (Math.pow(height,2));


System.out.println("Your Body Mass Index(BMI) is " + BMI);

}
}

~ 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;

public class LM5 {


public static void main (String[] args) {
int a,b,c;
int max,mid,min;
max = mid = min = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter three integer numbers :");
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();

if(a > b && a > c){


max = a;
mid = Math.max(b, c);
min = Math.min(b, c);
System.out.println(max + "," + mid + "," + min);
}else if (b > a && b > c){
max = b;
mid = Math.max(a, c);
min = Math.min(a, c);
System.out.println(max + "," + mid + "," + min);
}else if (c > b && c > a){
max = c;
mid = Math.max(a, b);
min = Math.min(a, b);
System.out.println(max + "," + mid + "," + min);
}
}
}

~ 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;

public class LM6 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print(“Enter a letter to check if it is vowel or consonant: ”)
String letter = sc.next();
if(letter == "a" || letter == "e" || letter == "i" || letter == "o" ||
letter == "u" || letter == "A" || letter == "E" || letter == "I" || letter == "O"
|| letter == "U"){
System.out.println("The letter is Vowel");
}else {
System.out.println("The letter is Consonant");
}
}
}

~ 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;

public class LM7 {


public static double Maximum(double a,double b){
return Math.max(a,b);
}
public static void main(String[] args) {
double num1,num2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers:");
System.out.print("num1 = ");
num1 = sc.nextDouble();
System.out.print("num2 = ");
num2 = sc.nextDouble();
System.out.println("Maximum = " + Maximum(num1,num2));
}
}

~ 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;

invoke a method to reverse the numbers, display the numbers.


public class LM9 {
public static void reverse(int []arr){
for (int i = arr.length-1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}

public static void main(String[] args) {


int []arr = new int[10];
Scanner sc = new Scanner(System.in);
System.out.println("Enter ten numbers:");
for (int i = 0; i < 10; i++) {
System.out.print("Number " + (i+1) + ":");
arr[i] = sc.nextInt();
}
reverse(arr);
}
}

~ 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;

public class LM10 {


public static void main(String[] args) {
Random rand = new Random();
for (int i = 0; i <= 100 ; i++) {
System.out.println(rand.nextInt(48)+ 1);
}
}
}

~ 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 ~

You might also like