OOP Practical List
OOP Practical List
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;
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);
result = mtr*feet;
}
}
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);
weightInPounds = weight*0.45359237;
heightInMeter = height*0.254;
bodyMassIndex = weightInPounds/(heightInMeter*heightInMeter);
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");
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);
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);
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]);
}
}
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();
}
}
}
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;
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+");
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);
}
}
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;
}
class Circle{
private double radius;
Circle(double radius){
this.radius = radius;
}
arrList.add(date);
arrList.add(loan);
arrList.add(name);
arrList.add(cr);
}
}
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;
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);
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;
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);
}
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)
Output :
Up Key Pressed Down Key Pressed
Page | 21
IT Dept. Year 2023 3140705(OOP)
Page | 22