Oop Prac 1-16
Oop Prac 1-16
PRACTICAL-1
AIM: Write a Program that displays Welcome to Java, Learning Java Now and
Programming is fun.
TOOL/Language: JDK/JAVA
Program :
class Prac1 {
public static void main(String[] args) {
System.out.println("Welcome to Java, Learning Java Now and Programming is fun.");
}
}
Output:
1
PEN:230840131023
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 )
TOOL/Language: JDK/JAVA
Program:
class Prac2 {
public static void main(String[] args) {
double a1 = 3.4, b1 = 50.2, c1 = 44.5, a2 = 2.1, b2 = .55, c2 = 5.9;
double d = a1 * b2 - b1 * a2;
System.out.println(x);
System.out.println(y);
}
}
Output:
2
PEN:230840131023
PRACTICAL-3
AIM: Write a program that reads a number in meters, converts it to feet, and
displays the result.
TOOL/Laguage : JDK/Java
Program:
import java.util.*;
class Prac3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter value in meter: ");
double meter = scanner.nextDouble();
double feet = 3.281 * meter;
System.out.println(feet);
}
}
Output:
3
PEN:230840131023
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.
TOOL/Language : JDK/JAVA
Program:
import java.util.*;
class Prac4 {
static double convertInchToMeter(double inch) {
return inch * 0.0254;
}
4
PEN:230840131023
5
PEN:230840131023
PRACTICAL-5
AIM: Write a program that prompts the user to enter three integers and display
the integers in decreasing order.
TOOL/Language : JDK/JAVA
Program:
import java.util.Arrays;
import java.util.Scanner;
class Prac5 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter three integers:");
displayDescending(sortedNumbers);
}
6
PEN:230840131023
Output:
7
PEN:230840131023
PRACTICAL-6
AIM : Write a program that prompts the user to enter a letter and check whether
a letter is a vowel or consonant.
TOOL/Language: JDK/JAVA
Program:
import java.util.Scanner;
class Prac6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
if (Character.isLetter(letter)) {
if ("aeiou".indexOf(letter) != -1) {
System.out.println(letter + " is a vowel.");
} else {
System.out.println(letter + " is a consonant.");
}
} else {
System.out.println("Invalid input. Please enter a valid letter.");
}
scanner.close();
}
}
OUTPUT:
8
PEN:230840131023
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.
TOOL/Language: JDK/JAVA
Program:
import java.util.Random;
class Prac7 {
public static int randomInt() {
Random r = new Random();
return r.nextInt(26);
}
System.out.println(numberPlate);
}
}
OUTPUT:
9
PEN:230840131023
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.
TOOL/Language: JDK/JAVA
Program:
import java.util.Scanner;
class Prac8 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
scanner.close();
10
PEN:230840131023
PRACTICAL-9
AIM: 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.
TOOL/Language: JDK/JAVA
Program:
import java.util.Scanner;
class Prac9 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
scanner.close();
}
while (num2 != 0) {
int temp = num2;
num2 = num1 % num2;
num1 = temp;
}
return num1;
}
}
11
PEN:230840131023
OUTPUT:
12
PEN:230840131023
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.
TOOL/Language: JDK/JAVA
Program:
import java.util.Scanner;
class Prac10 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] numbers = new int[10];
reverseNumbers(numbers);
System.out.println("Reversed numbers:");
left++;
right--;
}
13
PEN:230840131023
}
}
OUTPUT:
14
PEN:230840131023
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.
TOOL/Language: JDK/JAVA
Program:
import java.util.Random;
class Prac11 {
public static void main(String[] args) {
int[][] matrix = generateMatrix(6, 6);
displayMatrix(matrix);
boolean isOddRowColumn = checkOddRowsAndColumns(matrix);
System.out.println("Every row and column has an odd number of 1's: " +
isOddRowColumn);
}
15
PEN:230840131023
16
PEN:230840131023
if (colOnesCount % 2 == 0) {
return false;
}
}
return true;
}
}
OUTPUT:
17
PEN:230840131023
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.
TOOL/Language: JDK/JAVA
Program:
import java.util.Random;
class Prac12 {
public static void main(String[] args) {
Random random = new Random(1000);
Output:
18
PEN:230840131023
19
PEN:230840131023
PRACTICAL-13
Aim: Write 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.
TOOL/Language: JDK/JAVA
Program:
import java.util.*;
Lexer(String input) {
this.input = input;
this.position = 0;
}
20
PEN:230840131023
Token getNextToken() {
if (position >= input.length()) {
return new Token(Token.Type.EOF, "");
}
if (Character.isDigit(currentChar)) {
StringBuilder number = new StringBuilder();
while (position < input.length() && Character.isDigit(input.charAt(position))) {
number.append(input.charAt(position));
position++;
}
return new Token(Token.Type.NUMBER, number.toString());
}
if (currentChar == '(') {
position++;
return new Token(Token.Type.LPAREN, "(");
}
if (currentChar == ')') {
position++;
return new Token(Token.Type.RPAREN, ")");
}
21
PEN:230840131023
OperandNode(double value) {
this.value = value;
}
@Override
double evaluate() {
return value;
}
}
22
PEN:230840131023
@Override
double evaluate() {
double leftValue = left.evaluate();
double rightValue = right.evaluate();
switch (operator) {
case '+':
return leftValue + rightValue;
case '-':
return leftValue - rightValue;
case '*':
return leftValue * rightValue;
case '/':
if (rightValue == 0) {
throw new ArithmeticException("Division by zero");
}
return leftValue / rightValue;
default:
throw new IllegalArgumentException("Invalid operator: " + operator);
}
23
PEN:230840131023
}
}
Parser(Lexer lexer) {
this.lexer = lexer;
this.currentToken = lexer.getNextToken();
}
Node factor() {
if (currentToken.type == Token.Type.NUMBER) {
double value = Double.parseDouble(currentToken.value);
consume(Token.Type.NUMBER);
return new OperandNode(value);
} else if (currentToken.type == Token.Type.LPAREN) {
consume(Token.Type.LPAREN);
Node node = expr();
consume(Token.Type.RPAREN);
24
PEN:230840131023
return node;
} else {
throw new IllegalArgumentException("Unexpected token: " + currentToken.value);
}
}
Node term() {
Node node = factor();
while (currentToken.type == Token.Type.OPERATOR &&
(currentToken.value.equals("*") || currentToken.value.equals("/"))) {
Token operatorToken = currentToken;
consume(Token.Type.OPERATOR);
node = new OperatorNode(operatorToken.value.charAt(0), node, factor());
}
return node;
}
Node expr() {
Node node = term();
while (currentToken.type == Token.Type.OPERATOR &&
(currentToken.value.equals("+") || currentToken.value.equals("-"))) {
Token operatorToken = currentToken;
consume(Token.Type.OPERATOR);
node = new OperatorNode(operatorToken.value.charAt(0), node, term());
}
return node;
}
Node parse() {
return expr();
}
25
PEN:230840131023
class Prac13 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an expression: ");
String input = scanner.nextLine();
Output:
26
PEN:230840131023
PRACTICAL-14
Aim: Write 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.
TOOL/Language: JDK/JAVA
Program :
import java.util.ArrayList;
import java.util.Date;
class Loan {
private double amount;
@Override
public String toString() {
return "Loan amount: " + amount;
}
}
class Circle {
private double radius;
@Override
27
PEN:230840131023
class Prac14 {
public static void main(String[] args) {
ArrayList<Object> list = new ArrayList<>();
list.add(new Loan(1000.0));
list.add(new Date());
list.add("Hello, World!");
list.add(new Circle(5.0));
Output:
28
PEN:230840131023
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.
TOOL/Language: JDK/JAVA
Program :
import java.util.Scanner;
class BinaryConverter {
public static int bin2Dec(String binaryString) {
if (!binaryString.matches("[01]+")) {
throw new NumberFormatException("Input is not a binary string");
}
int decimal = 0;
int power = 0;
for (int i = binaryString.length() - 1; i >= 0; i--) {
if (binaryString.charAt(i) == '1') {
decimal += Math.pow(2, power);
}
power++;
}
return decimal;
}
}
class Prac15 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
29
PEN:230840131023
try {
int decimalNumber = BinaryConverter.bin2Dec(binaryString);
System.out.println("Binary: " + binaryString + " Decimal: " + decimalNumber);
} catch (NumberFormatException e) {
System.out.println(e.getMessage());
}
}
}
Output:
30
PEN:230840131023
PRACTICAL-16
Aim: 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.
TOOL/Language: JDK/JAVA
Program :
import java.util.Scanner;
class Prac16 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
class DecimalFractionConverter {
public String convertToFraction(String decimalStr) {
31
PEN:230840131023
numerator /= gcd;
denominator /= gcd;
Output:
32