IT Dept.
Year 2023 3140705(OOP)
Practical 1
Aim : Write a Program that displays Welcome to Java, Learning Java Now and Programming is fun.
Program :
class DisplayText{
public static void main(String[] args) {
System.out.println("Welcome To Java, Learning Java Now and Programming
is Fun..!!");
}
}
Output :
Page | 1
IT Dept. Year 2023 3140705(OOP)
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 )
Program :
class LinearEquation{
public static void main(String[] args) {
// 3.4x+50.2y=44.5 2) 2.1x+.55y=5.9
double a = 3.4;
double b = 50.2;
double c = 2.1;
double d = 0.55;
double e = 44.5;
double f = 5.9;
double det = a*d-b*c;
if(det==0){
System.out.println("No unique solution exists..!!");
}
else{
double x = (e * d - b * f) / det;
double y = (a * f - e * c) / det;
System.out.println("Value of X : "+x+" Value of Y : "+y);
}
}
}
Output :
Page | 2
IT Dept. Year 2023 3140705(OOP)
Practical 3
Aim : Write a program that reads a number in meters, converts it to feet, and displays the result.
Program :
import java.util.*;
class MeterToFeet{
public static void main(String[] args) {
float mtr;
double result, feet = 3.28084;
Scanner sc = new Scanner(System.in);
System.out.println("Convert Meter Into Feet");
System.out.print("Enter a number : ");
mtr = sc.nextFloat();
result = mtr*feet;
System.out.println("Meter Into Feet : "+result);
}
}
Output :
Page | 3
IT Dept. Year 2023 3140705(OOP)
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.
Program :
import java.util.*;
class BodyMassIndex{
public static void main(String[] args) {
double weight, height, weightInPounds, heightInMeter, bodyMassIndex;
Scanner sc = new Scanner(System.in);
System.out.print("Enter your weight : ");
weight = sc.nextDouble();
System.out.print("Enter your height in inches : ");
height = sc.nextDouble();
weightInPounds = weight*0.45359237;
heightInMeter = height*0.254;
bodyMassIndex = weightInPounds/(heightInMeter*heightInMeter);
System.out.println("Your Body Mass Index : "+bodyMassIndex);
}
}
Output :
Page | 4
IT Dept. Year 2023 3140705(OOP)
Practical 5
Aim : Write a program that prompts the user to enter three integers and display the integers in
decreasing order.
Program :
import java.util.*;
class DecreasingOrder{
public static void main(String[] args) {
int a, b, c;
Scanner sc = new Scanner(System.in);
System.out.println("Decreasing Order");
System.out.print("Enter the Value of First Number : ");
a = sc.nextInt();
System.out.print("Enter the Value of Second Number : ");
b = sc.nextInt();
System.out.print("Enter the Value of Third Number : ");
c = sc.nextInt();
if(a>b){
if(a>c){
if(b>c){
System.out.println("Incresing Order : "+a+" "+b+" "+c);
}
else{
System.out.println("Incresing Order : "+a+" "+c+" "+b);
}
}
else{
System.out.println("Incresing Order : "+c+" "+a+" "+b);
}
}
else if(b>a){
if(b>c){
if(c>a){
System.out.println("Incresing Order : "+b+" "+c+" "+a);
}
else{
System.out.println("Incresing Order : "+b+" "+a+" "+c);
}
}
Page | 5
IT Dept. Year 2023 3140705(OOP)
else{
System.out.println("Incresing Order : "+c+" "+b+" "+a);
}
}
}
}
Output :
Page | 6
IT Dept. Year 2023 3140705(OOP)
Practical 6
Aim : Write a program that prompts the user to enter a letter and check whether a letter is a
vowel or constant.
Program :
import java.util.*;
class VowelConsonant{
public static void main(String[] args) {
System.out.println("Vowel or Consonant");
Scanner sc = new Scanner(System.in);
char ch;
System.out.print("Enter any character : ");
ch = sc.next().charAt(0);
ch = Character.toUpperCase(ch);
if(Character.isAlphabetic(ch)){
if(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'){
System.out.println(ch+" is vowels");
}
else{
System.out.println(ch+" is consonant");
}
}
else{
System.out.println("You entered wrong input");
}
}
}
Output:
Page | 7
IT Dept. Year 2023 3140705(OOP)
Practical 7
Aim : Write a program to print following pattern:
1
212
32123
43214321
Program :
import java.util.*;
class Triangle{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int rows;
System.out.print("Enter number of rows : ");
rows = sc.nextInt();
for (int i = 1; i <= rows; i++) { // traverse through 1 to rows
int count = i; // current i = 1
for (int j = rows; j >= 1; j--) { //j traverse through 5 to 1
if (j > i) {
System.out.print(" ");
}
else {
System.out.print(count);
count--;
}
}
count = 2;
for (int j = 1; j < i; j++) {
System.out.print(count);
count++;
}
System.out.println();
}
}
}
Output :
Page | 8
IT Dept. Year 2023 3140705(OOP)
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.
Program :
import java.util.*;
class SmallestFactor{
public static void main(String[] args) {
int number, factor = 2;
Scanner sc = new Scanner(System.in);
System.out.print("Enter any number : ");
number = sc.nextInt();
System.out.print("Smallest Factors of "+number+" are : ");
while(factor<=number){
if(number%factor==0){
System.out.print(factor+" ");
number = number/factor;
}
else{
factor++;
}
}
}
}
Output :
Page | 9
IT Dept. Year 2023 3140705(OOP)
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.
Program :
import java.util.*;
class gcdOfTwoNumber{
public static void main(String[] args) {
int n1, n2;
Scanner sc = new Scanner(System.in);
System.out.print("Enter First Number : ");
n1 = sc.nextInt();
System.out.print("Enter Second Number : ");
n2 = sc.nextInt();
int gcdOfTwoNumber = gcd(n1, n2);
System.out.print("GCD of "+n1+", "+n2+" is : "+gcdOfTwoNumber);
}
public static int gcd(int num1, int num2){
if (num2==0) {
return num1;
}
else{
return gcd(num2, num1%num2);
}
}
}
Output :
Page | 10
IT Dept. Year 2023 3140705(OOP)
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
Program :
import java.util.*;
class reverseTheNumber{
public static void main(String[] args) {
int arr[] = new int[10];
Scanner sc = new Scanner(System.in);
System.out.print("Enter 10 numbers : ");
for(int i=0; i<arr.length; i++){
arr[i] = sc.nextInt();
}
reverseTheNumber(arr);
System.out.print("Revesed 10 Numbers : ");
for(int i=0; i<arr.length; i++){
System.out.print(" "+arr[i]);
}
}
public static void reverseTheNumber(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--;
}
}
}
Output :
Page | 11
IT Dept. Year 2023 3140705(OOP)
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.
Program :
class matrixCreation {
public static void main(String[] args) {
int matrix[][] = new int[6][6];
int count1 = 0;
for(int i = 0; i < 6; i++){
for(int j = 0; j < 6; j++){
matrix[i][j] = (int)(Math.random()*2);
}
}
System.out.println();
for(int i = 0; i < 6; i++){
for(int j = 0; j < 6; j++){
System.out.print(" "+matrix[i][j]);
}
System.out.println();
}
for(int i = 0; i < 6; i++){
for(int j = 0; j < 6; j++){
if(matrix[i][j]==1){
count1++;
}
}
}
System.out.println();
System.out.println("Total Odd Element : "+count1);
}
}
Output :
Page | 12
IT Dept. Year 2023 3140705(OOP)
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.
Program :
import java.util.Random;
public class RandomGenerator {
public static void main(String[] args) {
Random rand = new Random(1000);
System.out.println();
for (int i = 0; i < 100; i++) {
int randomNum = rand.nextInt(49) + 1;
System.out.print(randomNum + " ");
}
System.out.println();
}
}
Output :
Page | 13
IT Dept. Year 2023 3140705(OOP)
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.
Program :
import java.util.Scanner;
class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter an Expression : ");
String expression = sc.nextLine();
String[] expEval = expression.split("\\s+");
int result = Integer.parseInt(expEval[0]);
for (int i = 1; i < expEval.length; i += 2) {
if (i + 1 >= expEval.length) {
System.out.println("Invalid Expression : " + expression);
System.exit(0);
}
char op = expEval[i].charAt(0);
int operand = Integer.parseInt(expEval[i + 1]);
switch (op) {
case '+':
result += operand;
break;
case '-':
result -= operand;
break;
case '*':
result *= operand;
break;
case '/':
result /= operand;
break;
default:
System.out.println("Invalid Operator : " + op);
System.exit(0);
}
}
System.out.println("Expression After Evaluation : " + result);
sc.close();
}
}
Page | 14
IT Dept. Year 2023 3140705(OOP)
Output :
Page | 15
IT Dept. Year 2023 3140705(OOP)
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 toString() method.
Program :
import java.time.LocalDate;
import java.util.ArrayList;
class Loan{
private double amount;
Loan(double amount){
this.amount = amount;
}
public String toString(){
return "Load amount : "+amount;
}
}
class Circle{
private double radius;
Circle(double radius){
this.radius = radius;
}
public String toString(){
return "Circle radius : "+radius;
}
}
public class ArrayListEx {
public static void main(String[] args) {
ArrayList<Object> arrList = new ArrayList<>();
LocalDate date = LocalDate.now();
Loan loan = new Loan(1000);
Circle cr = new Circle(7);
String name = "Karan Kumbhare";
arrList.add(date);
arrList.add(loan);
arrList.add(name);
arrList.add(cr);
for(Object obj : arrList){
System.out.println(" "+obj.toString());
}
Page | 16
IT Dept. Year 2023 3140705(OOP)
}
}
Output :
Page | 17
IT Dept. Year 2023 3140705(OOP)
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.
Program :
import java.util.Scanner;
public class BinaryToDecimal {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a binary number : ");
String binary = sc.nextLine();
int decimal = 0;
int power=0;
if (binary.contains("1") || binary.contains("0")){
for (int i = binary.length()-1; i>=0; i--) {
int bit = binary.charAt(i)-'0';
decimal = (int) (decimal + bit * Math.pow(2, power));
power++;
}
System.out.println("Decimal : " + decimal);
}
else{
throw new NumberFormatException(binary);
}
}
}
Output :
Page | 18
IT Dept. Year 2023 3140705(OOP)
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.
Program :
import java.util.Scanner;
import java.text.DecimalFormat;
class Fraction {
public static void main(String[] args) {
DecimalFormat precision = new DecimalFormat("0.00");
Scanner sc = new Scanner(System.in);
System.out.print("Enter any number : ");
double number = sc.nextDouble();
double fraction = number % 1;
System.out.println("The fraction is : " + precision.format(fraction));
sc.close();
}
}
Output :
Page | 19
IT Dept. Year 2023 3140705(OOP)
Practical 18
Aim : Write a program that moves a circle up, down, left or right using arrow keys.
Program :
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class circleMoves extends Jframe{
private final int circleSize = 50;
private int ircle;
private int ircle;
circleMoves(){
setTitle(“Circle Movement”);
setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
setSize(400,400);
setResizable(false);
ircle = (getWidth()-circleSize)/2;
ircle = (getHeight()-circleSize)/2;
addKeyListener(new MovementKeyListener());
setFocusable(true);
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLUE);
g.fillOval(ircle, ircle, circleSize, circleSize);
}
private class MovementKeyListener implements KeyListener {
public void keyPressed(KeyEvent e){
int ircle = e.getKeyCode();
switch(ircle){
case KeyEvent.VK_UP: ircle -= 10; break;
case KeyEvent.VK_DOWN: ircle += 10; break;
case KeyEvent.VK_LEFT: ircle -= 10; break;
case KeyEvent.VK_RIGHT: ircle += 10; break;
}
repaint();
}
Page | 20
IT Dept. Year 2023 3140705(OOP)
public void keyTyped(KeyEvent event) {}
public void keyReleased(KeyEvent event) {}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
circleMoves cr = new circleMoves();
cr.setVisible(true);
});
}
}
Output :
Up Key Pressed Down Key Pressed
Left Key Pressed Right Key Pressed
Page | 21
IT Dept. Year 2023 3140705(OOP)
Page | 22