0% found this document useful (0 votes)
4 views40 pages

Java Practical

The document contains a series of Java programs demonstrating various programming concepts, including arithmetic operations, finding the largest number, checking for prime numbers, and determining even or odd numbers. It also covers string manipulation, array creation, inheritance, method overloading, and abstraction using both abstract classes and interfaces. Each program is presented with its code and a brief description of its functionality.

Uploaded by

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

Java Practical

The document contains a series of Java programs demonstrating various programming concepts, including arithmetic operations, finding the largest number, checking for prime numbers, and determining even or odd numbers. It also covers string manipulation, array creation, inheritance, method overloading, and abstraction using both abstract classes and interfaces. Each program is presented with its code and a brief description of its functionality.

Uploaded by

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

Program 1

1.Write a program to execute arithmetic operations.

import java.util.Scanner;

public class Arithmetic{

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

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

double num1 = scanner.nextDouble();

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

double num2 = scanner.nextDouble();

double sum = num1 + num2;

double difference = num1 - num2;

double product = num1 * num2;

double quotient = (num2 != 0) ? num1 / num2 : Double.NaN;

System.out.println("Addition: " + num1 + " + " + num2 + " = " + sum);

System.out.println("Subtraction: " + num1 + " - " + num2 + " = " + difference);

System.out.println("Multiplication: " + num1 + " * " + num2 + " = " + product);

if (num2 != 0) {

System.out.println("Division: " + num1 + " / " + num2 + " = " + quotient);

} else {

System.out.println("Division: Cannot divide by zero");

scanner.close();

}
}

Output:
Program 2
2.Write a program to find the largest number.
import java.util.Scanner;

public class Largestnumber {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

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

int num1 = scanner.nextInt();

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

int num2 = scanner.nextInt();

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

int num3 = scanner.nextInt();

int largest = num1;

if (num2 > largest) {

largest = num2;

if (num3 > largest) {

largest = num3;

System.out.println("The largest number is: " + largest);

scanner.close();

}
Output:
Program 3
3.Write a program to check number is prime or not.
import java.util.Scanner;

public class Primenumbercheck {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter a number to check if it is prime:");

int num = scanner.nextInt();

if (num <= 1) {

System.out.println(num + " is not a prime number.");

} else {

boolean isPrime = true;

for (int i = 2; i <= Math.sqrt(num); i++) {

if (num % i == 0) {

isPrime = false;

break;

} }

if (isPrime) {

System.out.println(num + " is a prime number.");

} else {

System.out.println(num + " is not a prime number.");

scanner.close();

}
}

Output:
Program 4
4.Write a program to check number is even or odd.
import java.util.Scanner;

public class EvenOddcheck {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter a number to check if it is even or odd:");

int num = scanner.nextInt();

if (num % 2 == 0) {

System.out.println(num + " is an even number.");

} else {

System.out.println(num + " is an odd number.");

scanner.close();

Output:
Program 5
5.Write a program to find the factorial of a number.
import java.util.Scanner;

public class Factorial {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter a number to find its factorial:");

int num = scanner.nextInt();

long factorial = 1;

if (num < 0) {

System.out.println("Factorial is not defined for negative numbers.");

} else {

for (int i = 1; i <= num; i++) {

factorial *= i;

System.out.println("The factorial of " + num + " is: " + factorial);

scanner.close();

}
Output:
Program 6
6.Write a program to find the reverse of a number.
import java.util.Scanner;

public class Reversenumber {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter a number to reverse:");

int num = scanner.nextInt();

int reversed = 0;

while (num != 0) {

int digit = num % 10;

reversed = reversed * 10 + digit;

num /= 10;

System.out.println("The reversed number is: " + reversed);

scanner.close();

}
Output:
Program 7
7.Write a program to concatenate two strings.
import java.util.Scanner;

public class Concatenate {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

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

String str1 = scanner.nextLine();

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

String str2 = scanner.nextLine();

String result = str1+str2;

System.out.println("The concatenated string is: " + result);

scanner.close();

Output:
Program 8
8.Write a program to count no. of char in a string.
import java.util.Scanner;

public class CountCharacters {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter a string:");

String inputString = scanner.nextLine();

int length = inputString.length();

System.out.println("The number of characters in the string is: " + length);

scanner.close();

Output:
Program 9
9.Write a program to count no. of vowels and consonants in a string.
import java.util.Scanner;

public class Vowelconsonants {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter a string:");

String inputString = scanner.nextLine();

inputString = inputString.toLowerCase();

int vowelCount = 0;

int consonantCount = 0;

for (int i = 0; i < inputString.length(); i++) {

char ch = inputString.charAt(i);

if (Character.isLetter(ch)) {

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {

vowelCount++;

} else {

consonantCount++;

System.out.println("Number of vowels: " + vowelCount);

System.out.println("Number of consonants: " + consonantCount);

scanner.close();

}
}

Output:
Program 10
10. Write a program to reverse string.
import java.util.Scanner;

public class ReverseString {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter a string to reverse:");

String inputString = scanner.nextLine();

String reversedString = new StringBuilder(inputString).reverse().toString();

System.out.println("The reversed string is: " + reversedString);

scanner.close();

Output:
Program 11
11.Write a program to create an array.
public class Array{

public static void main(String[] args) {

int[] myArray = {1, 2, 3, 4, 5};

System.out.print("The array is: ");

for (int i = 0; i < myArray.length; i++) {

System.out.print(myArray[i] + " ");

Output:
Program 12
12.Write a program to make a left triangle star pattern.
public class star {

public static void main(String[] args) {

int rows = 5;

for (int i = 1; i <= rows; i++) {

for (int j = 1; j <= i; j++) {

System.out.print("* ");

} System.out.println();

Output:
Program 13
13.Write a program to print a number pattern in pyramid.
public class Pyramid {

public static void main(String[] args) {

int rows = 5;

for (int i = 1; i <= rows; i++) {

for (int j = i; j < rows; j++) {

System.out.print(" ");

for (int k = 1; k <= (2 * i - 1); k++) {

System.out.print(i);

System.out.println();

}
Output:
Program 14
14.Write a program to calculate the area of rectangle
using inner class.
public class rectangle {

private double length;

private double width;

public rectangle(double length, double width) {

this.length = length;

this.width = width;

class AreaCalculator {

public double calculateArea() {

return length * width;

public static void main(String[] args) {

rectangle rectangle = new rectangle(5.0, 3.0);

rectangle.AreaCalculator areaCalculator = rectangle.new AreaCalculator();

double area = areaCalculator.calculateArea();

System.out.println("The area of the rectangle is: " + area);

}
Output:
Program 15(a)
15(a).Write a program to perform a multilevel inheritance.
class Person {

String name;

int age;

public Person(String name, int age) {

this.name = name;

this.age = age;

public void displayPersonDetails() {

System.out.println("Name: " + name);

System.out.println("Age: " + age);

class Employee extends Person {

String employeeId;

public Employee(String name, int age, String employeeId) {

super(name, age);

this.employeeId = employeeId;

public void displayEmployeeDetails() {

displayPersonDetails();

System.out.println("Employee ID: " + employeeId);


}

public void work() {

System.out.println(name + " is working.");

class Manager extends Employee {

String department;

public Manager(String name, int age, String employeeId, String department) {

super(name, age, employeeId);

this.department = department;

public void displayManagerDetails() {

displayEmployeeDetails();

System.out.println("Department: " + department);

public void manage() {

System.out.println(name + " is managing the " + department + " department.");

public class MultilevelInheritanceExample {

public static void main(String[] args) {

Manager manager = new Manager("John Doe", 40, "E12345", "HR");

manager.displayManagerDetails();

manager.work();

manager.manage();
}

Output:
Program 15(b)
15(b).Write a program to perform a hierarchical
inheritance.
class Person {

String name;

int age;

public Person(String name, int age) {

this.name = name;

this.age = age;

public void displayDetails() {

System.out.println("Name: " + name);

System.out.println("Age: " + age);

class Student extends Person {

String studentId;

public Student(String name, int age, String studentId) {

super(name, age);

this.studentId = studentId;

public void displayStudentDetails() {

displayDetails();
System.out.println("Student ID: " + studentId);

public void study() {

System.out.println(name + " is studying."); } }

class Professor extends Person {

String department;

public Professor(String name, int age, String department) {

super(name, age);

this.department = department;

public void displayProfessorDetails() {

displayDetails();

System.out.println("Department: " + department);

public void teach() {

System.out.println(name + " is teaching in the " + department + " department.");

public class HierarchicalInheritanceExample {

public static void main(String[] args) {

Student student = new Student("Alice", 20, "S12345");

student.displayStudentDetails();

student.study();
System.out.println();

Professor professor = new Professor("Dr. Smith", 45, "Computer Science");

professor.displayProfessorDetails();

professor.teach(); } }

Output:
Program 15(d)
15(b).Write a program to perform multiple inheritance
using interface in java.
interface Person {

void name();

void age();

interface Student {

void study();

void attendClasses();

interface Professor {

void teach();

void gradePapers();

class UniversityMember implements Person, Student, Professor {

public void name() {

System.out.println("Name: John Doe");

public void age() {

System.out.println("Age: 22");

public void study() {

System.out.println("John is studying for exams.");


}

public void attendClasses() {

System.out.println("John is attending classes.");

public void teach() {

System.out.println("John is teaching a course.");

public void gradePapers() {

System.out.println("John is grading papers.");

public class Main {

public static void main(String[] args) {

UniversityMember member = new UniversityMember();

member.name();

member.age();

member.study();

member.attendClasses();

member.teach();

member.gradePapers();

}
Output:
Program 16
16.Write a program to implement method overloading.
class Calculator {

public int add(int a, int b) {

return a + b;

public int add(int a, int b, int c) {

return a + b + c;

public double add(double a, double b) {

return a + b;

public String add(String a, String b) {

return a + b;

public class Overloading {

public static void main(String[] args) {

Calculator calculator = new Calculator();

int sum1 = calculator.add(10, 20);

int sum2 = calculator.add(10, 20, 30);

double sum3 = calculator.add(10.5, 20.5);

String sum4 = calculator.add("Hello, ", "World!");

System.out.println("Sum of 10 and 20: " + sum1);


System.out.println("Sum of 10, 20, and 30: " + sum2);

System.out.println("Sum of 10.5 and 20.5: " + sum3);

System.out.println("Concatenation of strings: " + sum4);

Output:
Program 17
17.Write a program to implement concept of method overriding.
class Person {

public void introduce() {

System.out.println("I am a person.");

class Student extends Person {

@Override

public void introduce() {

System.out.println("I am a student. I am studying.");

class Teacher extends Person {

@Override

public void introduce() {

System.out.println("I am a teacher. I teach students.");

public class Overridding {

public static void main(String[] args) {

Person student = new Student();

Person teacher = new Teacher();


student.introduce();

teacher.introduce();

}}

Output:

Program 18
18.Write a program to show abstraction to show abstraction using
abstract classes in java.
abstract class Employee {

public void displayDetails() {

System.out.println("Employee Details:");

public abstract void work();

class Manager extends Employee {

@Override

public void work() {

System.out.println("Manager is managing the team.");

class Developer extends Employee {

@Override

public void work() {

System.out.println("Developer is writing code.");

public class Main1{

public static void main(String[] args) {

Employee manager = new Manager();

Employee developer = new Developer();

manager.displayDetails();
manager.work();

developer.displayDetails();

developer.work();

Output:

Program 19
19.Write a program to show abstraction class using
interfaces in java.
interface Shape {

double area();

class Circle implements Shape {

private double radius;

public Circle(double radius) {

this.radius = radius;

@Override

public double area() {

return Math.PI * radius * radius;

class Rectangle implements Shape {

private double length;

private double width;

public Rectangle(double length, double width) {

this.length = length;

this.width = width;

@Override

public double area() {


return length * width;

public class Main2 {

public static void main(String[] args) {

Shape circle = new Circle(5.0);

Shape rectangle = new Rectangle(4.0, 6.0);

System.out.println("Area of Circle: " + circle.area());

System.out.println("Area of Rectangle: " + rectangle.area());

Output:

Program 20
20.Write a program to implement multithreading in java.
class MyThread extends Thread {

@Override

public void run() {

System.out.println(Thread.currentThread().getId() + " is running.");

public class Main3 {

public static void main(String[] args) {

MyThread t1 = new MyThread();

MyThread t2 = new MyThread();

t1.start();

t2.start();

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy