You are on page 1of 32

Enrollment No: 221041303001

LABORATORY MANUAL
OBJECT ORIENTED PROGRAMMING WITH JAVA (1330304)

COMPUTER ENGINEERING

Integrated M.Sc. SEM. III (COMPUTER), Year 2023-24(ODD SEM)

DEPARTMENT OF COMPUTER ENGINEERING


GUJARAT POWER ENGINEERING AND RESEARCH INSTITUTE, MEHSANA

(APPROVED BY AICTE)

(Affiliated to Gujarat Technological University)

Near Toll Booth, Ahmedabad-Mehsana Express Way, Village-Mevad

District Mehsana-Gujarat

Contact No. 02762-292262/63

1
GUJARAT POWER ENGINEERING AND RESEARCH
INSTITUTE, MEHSANA
(A constituent of Gujarat Technological University)

This is certify that the work carried out by Mr./Ms. Rudra KrunalKumar
Makavana Enrollment No. 221041303001 student of Computer Engineering,
semester III of session 2023-2024 has satisfactorily completed at Gujarat Power
Engineering and Research Institute, Mehsana. This manual is record of his/her
own work of subject Object Oriented Programming with Java Carried out under
competent supervision and guidance.

Faculty In-charge Head Of Department

Prof. Vyom Patel Prof. Khyati Prajapati

2
INDEX

Sr. Page
No. Experiment Title No. Date Sign

Write a program that displays Welcome to java, learning java now 5


1 and programming is fun.

Write A program that solves the following equations and1q displays 6


2 the value of X and Y : (1)3.4X + 50.2Y=44.5 (2)2.1X + 55Y=5.9

Write a program that reads a number in meters and convert into feets, 7
3 and displays the result.

Calculate BMI. Write a program that prompts the user to enter the 8
4 weight in pounds and height in inches and displays the BMI.

write a program which prompts the user to enter three integers and 10
5 display three integers in decreasing order.

write a program that prompts the user to enter a letter and check 12
6 Whether a letter is vowel or consonant.

Assume a vehicle plate number consists of three uppercase letters 13


7 followed by four digits. Write a program to generate a plate number.

Write a Program that reads an integer and displays all its smallest 14
8 factors in increasing order.

Write a method with following method header. public static int 15


gcd(int num1, int num2) Write a program that prompts the user to
9
enter two integers and compute the gcd of two integers.

Write a test program that prompts the user to enter ten numbers, 16
10 invoke a method to reverse the numbers, display the numbers.

3
Write a program that generate 6*6 two-dimensional matrix, filled 18
with 0’s and 1’s , display the matrix, check every raw and column
11
have an odd number’s of 1’s.

Write a program that creates a Random object with seed 1000 and 20
displays the first 100 random integers between 1 and 49 using the
12
NextInt (49) method.

A program for calculator to accept an expression as a string in which 21


13 the operands and operator are separated by zero or more spaces.

A program that creates an Array List and adds a Loan object , a Date 22
object , a string, and a Circle object to the list, and use a loop to
14 display all elements in the list by invoking the object’s to String()
method.

The bin2Dec (string binary String) method to convert a binary string 24


into a decimal number. Implement the bin2Dec method to throw a
15
NumberFormatException if the string is not a binary string.

Write a program that prompts the user to enter a decimal number and 26
16 displays the number in a fraction.

Write a program that displays a tic-tac-toe board. A cell may be X, O,


or empty. What to display at each cell is randomly decided. The X
17
and O are images in the files X.gif and O.gif.

Write a program that moves a circle up, down, left or right using 28
18 arrow keys.

Write a program that displays the color of a circle as red when the 30
mouse button is pressed and as blue when the mouse button is
19
released.

Write a program to create a file name 123.txt, if it does not exist. 32


Append a new data to it if it already exist. Write 150 integers created
20 randomly into the file using Text I/O. Integers are separated by
space.

4
No. 1 : Write a program that displays Welcome to java, learing java now and programming is fun.
Code:
class HelloWorld {
public static void main(String[] args) {
System.out.println ("Welcome to java, Learing java now and programming is fun.");
}
}

Output:

Welcome to Java, Learning Java Now and Programming is fun


No. 2 : Write A program that solves the following equations and1q displays the value of X and Y :
(1)3.4X + 50.2Y=44.5 (2)2.1X + 55Y=5.9

Code:
import java.util.Scanner;
public class OOP_02
{ public static void main(String[]
args)
{
Scanner input = new Scanner(System.in);
System.out.println("Values from Equestion - 1 :");
System.out.print("Enter value of a : ");
double a = input.nextDouble();
System.out.print("Enter value of b : ");
double b = input.nextDouble();
System.out.print("Enter value of e : ");
double e = input.nextDouble();

System.out.println("Values from Equestion - 2 :");


System.out.print("Enter value of c : ");
double c = input.nextDouble();
System.out.print("Enter value of d : ");
double d = input.nextDouble();
System.out.print("Enter value of f : ");
double f = input.nextDouble();

double x = ((e*d)-(b*f))/((a*d)-(b*c));
double y = ((a*f)-(e*c))/((a*d)-(b*c));

System.out.print(" X = "+ x + " Y = " + y);

}
}
Output:
Values from Equation - 1 :
Enter value of a : 3.4
Enter value of b : 50.2
Enter value of e : 44.5
Values from Equation - 2 :
Enter value of c : 2.1
Enter value of d : .55
Enter value of f : 5.9
X = 2.623901496861419 Y = 0.7087397392563978


No. 3 : Write a program that reads a number in meters and convert into feets, and displays the
result.

Code:
import java.util.Scanner;
public class Main
{ public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

System.out.println("Enter the Value in Meters:");


Double meters =
input.nextDouble(); Double feet;
feet = meters*3.28084;
System.out.println(meters+" meters "+"="+feet+"feet");
}
}

Output:
Enter the values in meters:
44
44.0 meters=144.3569feets


No. 4 : Calculate BMI.
Write a program that prompts the user to enter the weight in pounds and height in inches and
displays the BMI.

Code:
import java.util.Scanner;
public class Main
{ public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("For Finding out your Body Mass Index(BMI)");
System.out.println("Enter the weight in pound");
Double w = input.nextDouble();
System.out.println("Enter the height in inch");
Double h = input.nextDouble();

Double kg;
Double meter;
Double BMI; kg =
w*0.45359237;
meter = h*0.0254;
BMI = kg/(meter*meter);
System.out.println("Hight in meter: "+meter);
System.out.println("Weight in Kilograms: "+kg);
System.out.println("Your BMI is : "+BMI);
}
}


Output:
For Finding out your Body Mass Index(BMI)
Enter the weight in pound
148
Enter the height in inch
84
Height in meter: 2.1336
Weight in Kilograms: 67.13167076
Your BMI is: 14.7469242894835


No. 5 : prompts the user to enter three integers and display three integers in decreasing order.
Code:
import java.util.Scanner;
public class Main
{ public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter Three
numbers"); int a = input.nextInt(); int b =
input.nextInt(); int c = input.nextInt();

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


System.out.println( a + " " + b + " " + c);
else if ( b > a && b > c && a > c)
System.out.println( b + " " + a + " " + c);
else if ( c > b && c > a && b > a)
System.out.println( c + " " + b + " " + a);
else if ( a > c && a > b && c > b)
System.out.println( a + " " + c + " " + b);
else if ( b > c && b > a && c > a)
System.out.println( b + " " + c + " " + a);
else if ( c > a && c > b && a > b)
System.out.println( c + " " + a + " " + b);
}
}

10
Output:
Enter three Numbers
12
23
34
34 23 12

11
No. 6 : prompts the user to enter a letter and check Whether a letter is vowel or consonant.

Code:

import java.util.Scanner;
public class Main{ public static void
main(String[] args) {
char character;
Scanner input = new Scanner(System.in);
System.out.println("Enter the Alphabet: ");
character = input.next().charAt(0);

if(character=='a'||character=='A'||character=='e'||character=='E'||character=='i'||character=='I'||character=='
o'||character=='O'||character=='u'||character=='U')
{
System.out.println("This Alphabet is Vowel");
}
else
{
System.out.println("This Alphabet is Consonant");
}
}
}
Output:
Enter the Alphabet:
H
This Alphabet is Vowel

12
No. 7 : Assume a vehicle plate number consists of three uppercase letters followed by four digits.
Write a program to generate a plate number.

Code:
public class Practical_7
{
public static void main(String[] args)
{ int alpha1 = 'A' + (int)(Math.random() * ('Z' -
'A')); int alpha2 = 'A' + (int)(Math.random() * ('Z'
- 'A')); int alpha3 = 'A' + (int)(Math.random() *
('Z' - 'A')); int digit1 = (int)(Math.random() * 10);
int digit2 = (int)(Math.random() * 10); int digit3 =
(int)(Math.random() * 10); int digit4 =
(int)(Math.random() * 10);
System.out.println("" + (char)(alpha1) + ((char)(alpha2)) +
((char)(alpha3)) + digit1 + digit2 + digit3 + digit4);
}
}

Output:
KQC2913

13
No. 8 :Write a Program that reads an integer and displays all its smallest factors in increasing
order.
For example if input number is 120, the output should be as follows: 2,2,2,3,5.
Code:
import java.util.Scanner;
public class Practical_8
{
public static void main(String[] args)
{ int
div=2;
Scanner input = new Scanner(System.in);
System.out.print ("Enter Integer Value :
"); int number = input.nextInt();
while(number > 1)
{
if (number%div==0)
{
System.out.print(div+",");
number=number/div;
}
else {
div++;
}
}
} }
}
Output:
Enter Integer Value: 550
2,5,5,11

14
No. 9 : Write a method with 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
integers.
Code:
import java.util.Scanner;
class Practical_9
{
public static int gcd(int num1, int num2)
{
while (num1 != num2)
{
if(num1 > num2)
{
num1 = num1 - num2;
}
else
{
num2 = num2 - num1;
} } return
num1; }
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter First Number : ");
int number1 = input.nextInt();
System.out.print("Enter Second Number : ");
int number2 = input.nextInt();
System.out.print("GCD of "+number1+" and "+number2+" = "+gcd(number1, number2));
}
}
Output:
Enter First Number :333
Enter Second Number :666
GCD of 333 and 666 = 333

15
No. 10 : Write a test program that prompts the user to enter ten numbers, invoke a method to
reverse the numbers, display the numbers.
Code:
import java.util.Scanner;
class Practical_10
{ public static void reverse(int
numbers[])
{ int
j=0,temp;
while(j<=numbers.length/2)
{ temp=numbers[j];
numbers[j]=numbers[numbers.length-1-
j]; numbers[numbers.length-1-j]=temp;
j++;
}
}
public static void main(String[] args)
{
int i=0;
int num_array[]=new int[5];
Scanner input = new Scanner(System.in);
for(i=0;i<5;i++)
{
System.out.print("Enter at Position "+ (i+1) + " : ");
num_array[i] = input.nextInt();
}
reverse(num_array);
System.out.println("After reversing numbers in an Array :");
for(i=0;i<5;i++)
{
System.out.println("Value at Position "+ (i+1) + " : "+num_array[i]);
}
}
}

16
Output:
Enter at Position 1 : 1
Enter at Position 2 : 2
Enter at Position 3 : 3
Enter at Position 4 : 4
Enter at Position 5 : 5
After reversing numbers in an Array :
Value at Position 1 : 5
Value at Position 2 : 4
Value at Position 3 : 3
Value at Position 4 : 2
Value at Position 5 : 1

17
No. 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.
Code:
import java.util.Scanner;
class Practical_11
{
public static int[][] create_fill_matrix()
{
int [][]matrix = new int[6][6];
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++)
{
matrix[i][j]=(int)((Math.random()*5)%2);
}}
return matrix;
}
public static void displayMatrix(int [][]matrix)
{
System.out.print("\nMatrix Values \n");
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++)
{
System.out.print(matrix[i][j]+ " ");
}
System.out.println();
}}
public static void main(String[] args)
{
int my_matrix[][];
int i,j,cnt;
my_matrix=create_fill_matrix();
displayMatrix(my_matrix);
System.out.println("\nRows Having ODD no of 1s");
for(i=0;i<6;i++)
{
cnt=0;
for(j=0;j<6;j++)
{
if(my_matrix[i][j]==1)

18
{ cnt++; }}
if(cnt%2!=0
)
{
System.out.println("Row - "+(i+1)+" have ODD no of 1s");
}}
System.out.println("\nColumns Having ODD no of 1s");
for(i=0;i<6;i++
) { cnt=0;
for(j=0;j<6;j++)
{
if(my_matrix[j][i]==1)
{ cnt++; }}
if(cnt%2!=0
)
{
System.out.println("Column - "+(i+1)+" have ODD no of 1s");
}}}}

Output:
Matrix Values
101010
01 0 1 0 1
11 0 0 1 1
00 1 1 0 0
11 1 0 0 0
000111

Rows Having ODD no of 1s


Row – 1 have ODD no of 1s
Row – 2 have ODD no of 1s
Row – 5 have ODD no of 1s
Row – 6 have ODD no of 1s

Columns Having ODD no of 1s


Column – 1 have ODD no of 1s
Column – 2 have ODD no of 1s
Column – 3 have ODD no of 1s
Column – 4 have ODD no of 1s
Column – 5 have ODD no of 1s
Column – 6 have ODD no of 1s

19
No. 12 : 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.

Code:

import java.util.Random;
class Practical_12
{
public static void main(String[] args)
{
Random random = new Random(1000);
for (int i = 0; i < 100; i++)
{
System.out.format("%5d",random.nextInt(49));
if((i+1)%20==0)
{
System.out.println();
}
}

Output:
26 35 34 15 23 34 16 13 12 18 19 17 14 25 36 31 32 65 10 20 50 45 48 15 49 46

23 34 24 37 38 36 18 15 42 26 24 02 40 19 03 35 32 12 23 34 20 46 14 25 36 31

32 65 10 20 50 45 48 15 26 35 34 15 23 34 16 13 12 18 19 17

24 02 40 19 03 35 32 12 23 34 20 46 49 46 23 34 24 37 38 36 18 15 42 26

32 65 10 20 50 45 48 15 26 35 34 15 24 02 40 19 03 35 32 12 23 49 46 23

20
No. 13 :A program for calculator to accept an expression as a string in which the operands and
operator are separated by zero or more spaces. For ex: 3+4 and 3 + 4 are acceptable expressions.

Code:

import java.util.Scanner;
class Practical_13
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter Equation : ");
String string = input.nextLine();
String a = string.replaceAll(" ","");
if (a.length() < 3) {
System.out.println(
"Minimum 2 Opearator and 1 Opearand Required");
System.exit(0);
} int result =
0; int i = 0;
while(a.charAt(i)!='+' && a.charAt(i)!='-' && a.charAt(i)!='*' && a.charAt(i)!='/')
{
i++;
}
switch (a.charAt(i)) { case '+' : result =
Integer.parseInt(a.substring(0,i))+Integer.parseInt(a.substring(i+1,a.length())); break;
case '-' : result = Integer.parseInt(a.substring(0,i))-
Integer.parseInt(a.substring(i+1,a.length())); break; case '*' : result =
Integer.parseInt(a.substring(0,i))*Integer.parseInt(a.substring(i+1,a.length())); break;
case '/' : result =
Integer.parseInt(a.substring(0,i))/Integer.parseInt(a.substring(i+1,a.length())); break;
}
System.out.println(a.substring(0,i) + ' ' + a.charAt(i) + ' ' + a.substring(i+1,a.length())
+ " = " + result);
}
}

Output:
Enter Equation: 4+4

4+4=8

21
No. 14 : A program that creates an Array List and adds a Loan object , a Date object , a string, and
a Circle object to the list, and use a loop to display all elements in the list by invoking the object’s to
String() method.

Code:

Import java.util.ArrayList
Import java.util.Date;
class Practical_14
{
public static void main(String[] args)
{
ArrayList<Object> arr_list = new ArrayList<Object>();
arr_list.add(new Loan(10000));
arr_list.add(new Date());
arr_list.add(new String("GTU Practical"));
arr_list.add(new Circle(3.45)); for (int i =
0; i < arr_list.size(); i++)
{
System.out.println((arr_list.get(i)).toString());
}
} } class Circle
{ double
radius;
Circle(double r)
{
this.radius=r;
}
public String toString()
{
return "Circle with Radius "+this.radius;
} } class Loan
{ double
amount;
Loan (double amt)
{
this.amount=amt; }
public String to
String() {
return "Loan with Amount "+this.amount;
}
}

22
Output:
Loan with amount 10000.0

Sun Dec 06 21:19:51 IST 2021

GTU practical

Circle with radius 25.48

23
No. 15 :the bin2Dec (string binary String) method to convert a binary string into a decimal
number. Implement the bin2Dec method to throw a NumberFormatException if the string is not a
binary string.

Code:

public static int bin2Dec(String binaryString) throws NumberFormatException {


int decimal = 0;
int strLength=binaryString.length();
for (int i = 0; i < strLength; i++)
{
if (binaryString.charAt(i) < '0' || binaryString.charAt(i) > '1')
{ throw new NumberFormatException("The Input String is not
Binary");
}
decimal += (binaryString.charAt(i)-'0') * Math.pow(2, strLength-1-i);
}
return decimal;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter Binary Value : ");
String str = input.nextLine();
try
{
System.out.println("Value = " + bin2Dec(str));
}
catch(NumberFormatException e)
{
System.out.println(e);
}
}
}

Output:
Enter binary value : 100

Value = 4

24
No. 16 : Write a program that prompts the user to enter a decimal number and displays the
number in a fraction.
Hint: Read the decimal number as a string, extract the integer part and fractional part from the string.

Code:

import java.util.Scanner;
class Fraction{ private
int real; private int
imaginary; Fraction(int
r,int img){
real=r;
imaginary=img;
}
public long gcm(long a, long b) {
return b == 0 ? a : gcm(b, a % b);
}
public String toString() { long gcm
= gcm(real, imaginary); return
real/gcm+"/"+imaginary/gcm;
}
}
public class Practical_16 { public
static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter a Decimal Number: ");
String decimal=sc.nextLine(); int indexOfDecimal =
decimal.indexOf("."); int
len=decimal.substring(indexOfDecimal).length(); int
imag_part=(int) Math.pow(10,len-1);
int real_part=(int)(Double.parseDouble(decimal)*imag_part);
Fraction fr=new Fraction(real_part,imag_part);
System.out.println("The Fraction Number is "+fr);
System.out.print("Enter a Decimal Number: ");
decimal=sc.nextLine();
indexOfDecimal = decimal.indexOf(".");
len=decimal.substring(indexOfDecimal).length();
imag_part=(int) Math.pow(10,len-1);
real_part=(int)(Double.parseDouble(decimal)*imag_part);
fr=new Fraction(real_part,imag_part);

25
System.out.println("The Fraction Number is "+fr);
}
}

Output:
Enter a decimal number : 135.566

The fraction number is 67783/500

Enter a decimal number : -0.000000567

The fraction number is -567/1000000000

26
No. 17 : Write a program that displays a tic-tac-toe board. A cell may be X, O, or empty. What to
display at each cell is randomly decided. The X and O are images in the files X.gif and O.gif.

Code:
import javafx.application.Application;
import javafx.scene.Scene; import
javafx.scene.layout.GridPane; import
javafx.scene.control.Label; import
javafx.scene.image.Image; import
javafx.scene.image.ImageView; import
javafx.stage.Stage;

public class OOP_17 extends Application


{ @Override
public void start(Stage primaryStage) {

GridPane pane = new GridPane();

for (int i = 0; i < 3; i++)


{ for (int j = 0; j < 3; j++)
{ int n = (int)(Math.random() * 3);
if (n == 0)
pane.add(new ImageView(new Image("image/x.gif")), j, i);
else if (n == 1)
pane.add(new ImageView(new Image("image/o.gif")), j, i);
else
continue;
}
}

Scene scene = new Scene(pane, 120, 130);


primaryStage.setTitle("OOP_17");
primaryStage.setScene(scene); primaryStage.show(); }

27
No. 18 : Write a program that moves a circle up, down, left or right using arrow keys.
Code:
import javafx.application.Application;
import javafx.scene.Scene; import
javafx.scene.shape.Circle; import
javafx.scene.layout.Pane; import
javafx.geometry.Insets; import
javafx.stage.Stage;

public class OOP_18 extends Application


{ @Override public void start(Stage
primaryStage) { Pane pane = new Pane();
pane.setPadding(new Insets(30, 30, 30, 30)); Circle
circle = new Circle(30, 30, 30);
pane.getChildren().add(circle);

pane.setOnKeyPressed(e -> {
switch (e.getCode()) { case UP :
circle.setCenterY(circle.getCenterY() >
circle.getRadius() ? circle.getCenterY() - 15 :
circle.getCenterY()); break;
case DOWN : circle.setCenterY(circle.getCenterY() <
pane.getHeight() - circle.getRadius() ?
circle.getCenterY() + 15 : circle.getCenterY()); break;
case LEFT : circle.setCenterX(circle.getCenterX() >
circle.getRadius() ? circle.getCenterX() - 15 : circle.getCenterX());
break;
case RIGHT : circle.setCenterX(circle.getCenterX() <
pane.getWidth() - circle.getRadius() ?
circle.getCenterX() + 15: circle.getCenterX());
}
});

Scene scene = new Scene(pane, 200, 200);


primaryStage.setTitle("OOP_18");
primaryStage.setScene(scene);
primaryStage.show(); pane.requestFocus();
}
}

28
Output:

29
No. 19 : Write a program that displays the color of a circle as red when the mouse button is pressed
and as blue when the mouse button is released.
Code:
import javafx.application.Application;
import javafx.scene.Scene; import
javafx.scene.layout.StackPane; import
javafx.scene.paint.Color; import
javafx.scene.shape.Circle; import
javafx.stage.Stage;

public class OOP_19 extends Application


{
@Override public void start(Stage
primaryStage)
{ double width = 450;
double height = 450;
Circle c = new Circle(width / 2, height / 2, Math.min(width, height) / 10, Color.BLUE);

c.setStroke(Color.WHITE); StackPane

pane = new StackPane(c);

primaryStage.setScene(new Scene(pane, width, height)); pane.setOnMousePressed(e


-> c.setFill(Color.RED)); pane.setOnMouseReleased(e -> c.setFill(Color.BLUE));
primaryStage.setTitle("Click circle..");
primaryStage.show();
} public static void main(String[] args) {
Application.launch(args);

}
}

30
Output:

31
No. 20 : program to create a file name 123.txt, if it does not exist. Append a new data to it if it already
exist. Write 150 integers created randomly into the file using Text I/O. Integers are separated by
space.

Code:

import java.io.*; import


java.util.Scanner; class
Practical_21
{
public static void
main(String[] args)
{
try
(
PrintWriter pw = new PrintWriter(new FileOutputStream(new File("123.txt"), true));
){
for (int i = 0; i < 150; i++)
{
pw.print((int)(Math.random() * 150) + " ");
}
}
catch (FileNotFoundException fnfe)
{
System.out.println("Cannot create the file.");
fnfe.printStackTrace();
}
}
}

Output:It is create a txt file which name is 123.txt

32

You might also like