0% found this document useful (0 votes)
2 views25 pages

OOP Practical File

The document contains a series of Java programming practicals aimed at teaching various programming concepts. Each practical includes an aim, code, and expected output, covering topics such as basic input/output, mathematical calculations, data structures, and exception handling. The practicals are designed for students to gain hands-on experience in Java programming.

Uploaded by

ritikashewale24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views25 pages

OOP Practical File

The document contains a series of Java programming practicals aimed at teaching various programming concepts. Each practical includes an aim, code, and expected output, covering topics such as basic input/output, mathematical calculations, data structures, and exception handling. The practicals are designed for students to gain hands-on experience in Java programming.

Uploaded by

ritikashewale24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

OOP[3140705] Enrollment No:-230090107126

Practical: 1

AIM :Write a Program that displays Welcome to Java, Learning Java Now
and Programming is fun.

CODE:

import java.io.*;
public class p1
{
public static void main(String args[])
{
System.out.println("Welcome to Java");
System.out.println("Learning Java Now");
System.out.println("Programming is fun");
}
}

OUTPUT:

CKPCET/BE/CE PAGE 1
OOP[3140705] Enrollment No:-230090107126

Practical: 2

AIM :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 )

CODE:

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

System.out.println("Values from equestion-1:");


System.out.println("Enter value of a:");
double a=input.nextDouble();
System.out.println("Enter value of b:");
double b=input.nextDouble();
System.out.println("Enter value of e:");
double e=input.nextDouble();

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


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

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

CKPCET/BE/CE PAGE 2
OOP[3140705] Enrollment No:-230090107126

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

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

OUTPUT:

CKPCET/BE/CE PAGE 3
OOP[3140705] Enrollment No:-230090107126

Practical: 3

AIM:-Write a program that reads a number in meters, converts it to feet, and


displays the result.

CODE:-

import java.util.Scanner;
public class p3
{

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 = meters*3.28084;
System.out.println("Value in feet"+feet);
}
}

OUTPUT:-

CKPCET/BE/CE PAGE 4
OOP[3140705] Enrollment No:-230090107126

Practical: 4

AIM:-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.

CODE:-
import java.util.Scanner;
public class p4
{
public static void main(String[] Strings)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the weight in pound:");
double weight = input.nextDouble();

System.out.println("Enter the height in inche:");


double height = input.nextDouble();

double BMI = weight*0.45359237 /(height*0.0254*height*0.0254);


System.out.println("BMI Is :"+BMI);
}
}

OUTPUT :-

CKPCET/BE/CE PAGE 5
OOP[3140705] Enrollment No:-230090107126

Practical: 5

AIM:- Write a program that prompts the user to enter three


integers and display the integers in
decreasing order.

CODE :-

import java.util.Scanner;

public class p5
{
public static void main(String[] args)
{
Scanner kbd = new Scanner(System.in);

System.out.println("Enter the first number:");


int n1 = kbd.nextInt();

System.out.println("Enter the second number:");


int n2 = kbd.nextInt();

System.out.println("Enter the third number:");


int n3 = kbd.nextInt();

largeSmall(n1, n2, n3);


}

public static void largeSmall(int num1, int num2, int num3) {


if (num1 >= num2 && num1 >= num3) {
if (num2 >= num3) {
System.out.println(num1 + " " + num2 + " " + num3);
} else {

CKPCET/BE/CE PAGE 6
OOP[3140705] Enrollment No:-230090107126

System.out.println(num1 + " " + num3 + " " + num2);


}
} else if (num2 >= num1 && num2 >= num3) {
if (num1 >= num3) {
System.out.println(num2 + " " + num1 + " " + num3);
} else {
System.out.println(num2 + " " + num3 + " " + num1);
}
} else {
if (num1 >= num2) {
System.out.println(num3 + " " + num1 + " " + num2);
} else {
System.out.println(num3 + " " + num2 + " " + num1);
}
}
}
}

OUTPUT :-

CKPCET/BE/CE PAGE 7
OOP[3140705] Enrollment No:-230090107126

Practical: 6

AIM:- Write a program that prompts the user to enter a letter and check
whether a letter is a vowel or
constant.

CODE :-

import java.util.Scanner;
public class p6
{
public static void main(String[] args)
{

Scanner scanner = new Scanner(System.in);


System.out.print("Enter a letter: ");
char ch = scanner.next().charAt(0);
if (ch=='a'||ch =='e'||ch =='i'||ch=='o'|| ch == 'u'|| ch == 'A'|| ch == 'E'||ch==
'I'||ch== 'O'||ch== 'U')
{
System.out.println(ch + "It's vowel");
}
else
{
System.out.println(ch + " Its's consonant");
}
}
}

CKPCET/BE/CE PAGE 8
OOP[3140705] Enrollment No:-230090107126

OUTPUT :-

CKPCET/BE/CE PAGE 9
OOP[3140705] Enrollment No:-230090107126

Practical: 7

AIM:- Assume a vehicle plate number consists of three uppercase letters


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

CODE:-

import java.util.Random;
public class p7
{
public static void main(String[] args)
{
Random random = new Random();
StringBuilder plateNumber = new StringBuilder();
for (int i = 0; i < 3; i++) {
char letter = (char) ('A' + random.nextInt(26));

plateNumber.append(letter);
}
for (int i = 0; i < 4; i++) {
int digit = random.nextInt(10);
plateNumber.append(digit);
}

System.out.println("Generated Plate Number: " + plateNumber);


}
}

OUTPUT :-

CKPCET/BE/CE PAGE 10
OOP[3140705] Enrollment No:-230090107126

Practical : 8

AIM:- 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 p8
{
public static void main(String args[])
{
int div=2;
Scanner input=new Scanner(System.in);
System.out.print("Enter the integer value:");
int n=input.nextInt();
while(n>1){
if(n%div==0){
System.out.print(div+",");
n=n/div;
}
else{
div++;
}
}
}
}
OUTPUT:-

CKPCET/BE/CE PAGE 11
OOP[3140705] Enrollment No:-230090107126

Practical : 9

AIM:-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 integers.

CODE:-
import java.util.Scanner;
public class p9{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(&quot;Enter the first integer: &quot;);
int num1 = scanner.nextInt();
System.out.print(&quot;Enter the second integer: &quot;);
int num2 = scanner.nextInt();
int result = gcd(num1, num2);
System.out.println(&quot;The GCD of &quot; + num1 + &quot; and &quot; +
num2 + &quot; is: &quot; + result);
}
public static int gcd(int num1, int num2) {
while (num2 != 0) {
int temp = num2;
num2 = num1 % num2;
num1 = temp;
}
return num1;
}
}
OUTPUT:-

CKPCET/BE/CE PAGE 12
OOP[3140705] Enrollment No:-230090107126

Practical : 10

AIM:-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;

public class P10 {


public static int gcd(int num1, int num2) {
while (num2 != 0) {
int temp = num2;
num2 = num1 % num2;
num1 = temp;
}
return num1;
}
public static void reverseArray(int[] numbers) {
int left = 0, right = numbers.length - 1;
while (left < right) {
int temp = numbers[left];
numbers[left] = numbers[right];
numbers[right] = temp;
left++;
right--;
}
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

int[] numbers = new int[10];

CKPCET/BE/CE PAGE 13
OOP[3140705] Enrollment No:-230090107126

System.out.println("Enter ten integers:");


for (int i = 0; i < 10; i++) {
numbers[i] = scanner.nextInt();
}

reverseArray(numbers);

System.out.println("Reversed numbers:");
for (int num : numbers) {
System.out.print(num + " ");
}

scanner.close();
}
}

OUTPUT:-

CKPCET/BE/CE PAGE 14
OOP[3140705] Enrollment No:-230090107126

Practical : 11

AIM:-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.Random;

public class p11{


public static void main(String[] args) {
int[][] matrix = new int[6][6];
Random random = new Random();

for (int i = 0; i < 6; i++) {


for (int j = 0; j < 6; j++) {
matrix[i][j] = random.nextInt(2);
}
}

System.out.println("Generated 6x6 Matrix:");


for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}

boolean isValid = true;


for (int i = 0; i < 6; i++) {
int rowCount = 0, colCount = 0;
for (int j = 0; j < 6; j++) {
rowCount += matrix[i][j];

CKPCET/BE/CE PAGE 15
OOP[3140705] Enrollment No:-230090107126

colCount += matrix[j][i];
}
if (rowCount % 2 == 0 || colCount % 2 == 0) {
isValid = false;
break;
}
}

if (isValid) {
System.out.println("Every row and column have an odd number of 1’s.");
} else {
System.out.println("Not every row and column have an odd number of
1’s.");
}
}
}

OUTPUT:-

CKPCET/BE/CE PAGE 16
OOP[3140705] Enrollment No:-230090107126

Practical : 12

AIM:-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;
public class p12 {
public static void main(String[] args) {
Random random = new Random(1000);

System.out.println("First 100 random integers between 1 and 49:");


for (int i = 0; i < 100; i++) {
System.out.print((random.nextInt(49) + 1) + " ");
if ((i + 1) % 10 == 0) {
System.out.println();
}
}
}
}

OUTPUT :-

CKPCET/BE/CE PAGE 17
OOP[3140705] Enrollment No:-230090107126

Practical : 13

AIM:-Write a program for calculators 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;

public class p13 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter an expression (e.g., 3+4 or 3 + 4): ");


String input = scanner.nextLine();

input = input.replaceAll("\s+", "");


char operator = ' ';
int index = -1;

if (input.contains("+")) {
operator = '+';
index = input.indexOf("+");
} else if (input.contains("-")) {
operator = '-';
index = input.indexOf("-");
} else if (input.contains("*")) {
operator = '*';
index = input.indexOf("*");
} else if (input.contains("/")) {
operator = '/';
index = input.indexOf("/");
} else {
System.out.println("Invalid expression.");

CKPCET/BE/CE PAGE 18
OOP[3140705] Enrollment No:-230090107126

scanner.close();
return;
}

int num1 = Integer.parseInt(input.substring(0, index));


int num2 = Integer.parseInt(input.substring(index + 1));
int result = 0;

switch (operator) {
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/':
if (num2 == 0) {
System.out.println("Error: Division by zero.");
scanner.close();
return;
}
result = num1 / num2;
break;
}

System.out.println("Result: " + result);


scanner.close();
}
}

OUTPUT:-

CKPCET/BE/CE PAGE 19
OOP[3140705] Enrollment No:-230090107126

Practical : 15

AIM:-Write 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:-
import java.util.Scanner;

public class p15


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

CKPCET/BE/CE PAGE 20
OOP[3140705] Enrollment No:-230090107126

System.out.println("Value = " + bin2Dec(str));


}
catch(NumberFormatException e)
{
System.out.println(e);
}
}
}

OUTPUT:-

CKPCET/BE/CE PAGE 21
OOP[3140705] Enrollment No:-230090107126

Practical : 16

AIM:-Write a program to demonstrate user defined exceptions.

CODE:-
class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}

public class p16 {

public static void validateAge(int age) throws InvalidAgeException {


if (age < 18 ) {
throw new InvalidAgeException("Age must be 18.");
}
System.out.println("Your age is valid: " + age);
}

public static void main(String[] args) {


try {
java.util.Scanner scanner = new java.util.Scanner(System.in);
System.out.print("Enter your age: ");
int age = scanner.nextInt();
validateAge(age);
} catch (InvalidAgeException e) {
System.out.println("Error: " + e.getMessage());
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input! Please enter a valid number.");
}
}
}

CKPCET/BE/CE PAGE 22
OOP[3140705] Enrollment No:-230090107126

OUTPUT:-

CKPCET/BE/CE PAGE 23
OOP[3140705] Enrollment No:-230090107126

Practical : 17

AIM:-Write a program that prompts the user to enter a decimal number and
displays the number in a fraction.

CODE:-
import java.util.Scanner;
public class p17 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a decimal number: ");
String input = scanner.nextLine();
if (input.contains(".")) {
String[] parts = input.split("\\.");
String integerPart = parts[0];
String fractionalPart = parts[1];
int denominator = (int) Math.pow(10, fractionalPart.length());
int numerator = Integer.parseInt(integerPart) * denominator +
Integer.parseInt(fractionalPart);
System.out.println("The number " + input + " as a fraction is: " +
numerator + "/" + denominator);
} else {
System.out.println(input + " is already an integer. Fraction: " + input +
"/1");
}
scanner.close();
}
}

OUTPUT :-

CKPCET/BE/CE PAGE 24
OOP[3140705] Enrollment No:-230090107126

Practical : 18

AIM:-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:-

CKPCET/BE/CE PAGE 25

You might also like