Professional Documents
Culture Documents
LABORATORY MANUAL
OBJECT ORIENTED PROGRAMMING WITH JAVA (1330304)
COMPUTER ENGINEERING
(APPROVED BY AICTE)
District Mehsana-Gujarat
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.
2
INDEX
Sr. Page
No. Experiment Title No. Date Sign
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.
Write a Program that reads an integer and displays all its smallest 14
8 factors in increasing order.
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 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.
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 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.
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:
5
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();
double x = ((e*d)-(b*f))/((a*d)-(b*c));
double y = ((a*f)-(e*c))/((a*d)-(b*c));
}
}
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
6
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);
Output:
Enter the values in meters:
44
44.0 meters=144.3569feets
7
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);
}
}
8
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
9
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();
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
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
GTU practical
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:
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
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;
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;
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());
}
});
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;
c.setStroke(Color.WHITE); StackPane
}
}
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:
32