Date: / /
S.NO. Program Name
1 WAP in java to print “Hello
World.” Code:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Output:
Hello World
WAP in java to print the values of various primitive data.
Primitive data types - includes byte , short , int , long , float , double , boolean and char. Code:
public class Primitive {
public static void main(String[] args) {
byte byteValue = 12;
short shortValue = 32;
int intValue = 21;
long longValue = 9267L;
float floatValue = 3;
double doubleValue = 33;
char charValue = 'A';
boolean booleanValue = true;
System.out.println("byteValue: " + byteValue);
System.out.println("shortValue: " + shortValue);
System.out.println("intValue: " + intValue);
System.out.println("longValue: " + longValue);
System.out.println("floatValue: " + floatValue);
System.out.println("doubleValue: " + doubleValue);
System.out.println("charValue: " + charValue);
System.out.println("booleanValue: " + booleanValue);
}
}
Output:
Date: / /
2. WAP in java to find sum of odd numbers and even numbers.
Code:
import java.util.Scanner;
public class Sum {
public static void main(String[] args) {
Scanner s = new
Scanner(System.in);
System.out.print("Enter Start and End point of range\
n"); System.out.print("Start: ");
int start = s.nextInt();
System.out.print("End : ");
int end = s.nextInt();
int evenSum =
0; int oddSum =
0;
for (int i = start; i <= end; i++)
{ if (i % 2 == 0) {
evenSum += i;
} else {
oddSum += i;
}
}
System.out.println("Sum of even numbers: " + evenSum);
System.out.println("Sum of odd numbers: " + oddSum);
}
}
Output:
Date: / /
3. WAP in java to find factorial of any given number using recursion.
Code:
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = s.nextInt();
int factorial = calculate(num);
System.out.println("Factorial of " + num + " is : " + factorial);
s.close();
}
public static int calculate(int num)
{ if (num == 0) {
return 1;
} else {
return num * calculate(num - 1);
}
}
}
Output:
Date: / /
4. WAP in java to read an integer value through Scanner class
Code:
import java.util.Scanner;
public class Read {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter an integer:
"); int num = s.nextInt();
System.out.println("You entered: " + num);
}
}
Output:
Find prime number within given number.
Code:
import java.util.Scanner;
public class Prime {
public static void main(String[] args) {
Scanner s = new
Scanner(System.in);
System.out.print("Enter lower limit : ");
int lowerLimit = s.nextInt();
System.out.print("Enter upper limit : ");
int upperLimit = s.nextInt();
System.out.println("Prime numbers :");
for (int i = lowerLimit; i <= upperLimit; i++) {
if (isPrime(i)) {
System.out.print(i + ", ");
}
}
}
public static boolean isPrime(int number) {
if (number <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
}
Date: / /
Output:
Date: / /
5. WAP in java that uses length property for displaying any number of command line arguments.
Code:
public class command {
public static void main(String[] args) {
System.out.println("Number of command line arguments: " + args.length);
System.out.println("Command line arguments:");
for (int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}
}
}
output:
Date: / /
6. WAP in java to sort n numbers using bubble sort, selection sort and insertion sort.
Code:
import java.util.Scanner;
public class Sorting {
public static void bubbleSort(int[] arr)
{ int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++)
{ if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] =
temp;
}
}
}
}
public static void selectionSort(int[] arr)
{ int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex])
{
minIndex = j;
}
}
int temp =
arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
public static void insertionSort(int[] arr)
{ int n = arr.length;
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
System.out.print("Enter number of elements: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter
elements:"); for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
Date: / /
}
System.out.println();
bubbleSort(arr);
System.out.println(" Bubble
Sort:"); for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
selectionSort(arr);
System.out.println(" Selection
Sort:"); for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
insertionSort(arr);
System.out.println(" Insertion
Sort:"); for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
Output:
Date: / /
7. WAP in java to find additions and multiplication of two Matrices.
Code:
import java.util.Scanner;
public class Matrix {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
System.out.print("=========== Matrix 1 =============\n ");
System.out.print("Enter row number: ");
int rows1 = sc.nextInt();
System.out.print("Enter column number: ");
int cols1 = sc.nextInt();
int[][] matrix1 = new int[rows1][cols1];
System.out.println("\nEnter the
elements:"); for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
matrix1[i][j] = sc.nextInt();
}
}
System.out.print("============ Matrix 2 ==============\n ");
System.out.print("Enter row number: ");
int rows2 = sc.nextInt();
System.out.print("Enter column number: ");
int cols2 = sc.nextInt();
if (cols1 != rows2) {
System.out.println("Matrix multiplication is not possible!");
return;
}
int[][] matrix2 = new int[rows2][cols2];
System.out.println("Enter the elements:
"); for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
matrix2[i][j] = sc.nextInt();
}
}
int[][] sum = new int[rows1]
[cols1]; for (int i = 0; i < rows1; i++)
{
for (int j = 0; j < cols1; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
int[][] product = new int[rows1]
[cols2]; for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++)
{ int sumProduct = 0;
for (int k = 0; k < cols1; k++) {
sumProduct += matrix1[i][k] * matrix2[k][j];
}
product[i][j] = sumProduct;
}
}
System.out.println("\n ");
System.out.println("Sum of matrix : ");
displayMatrix(sum);
System.out.println("Product of matrix :");
displayMatrix(product);
Date: / /
}
public static void displayMatrix(int[][] matrix) { int rows = matrix.length;
int cols = matrix[0].length;
for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
Output:
Date: / /
8. WAP in java to find largest among n numbers.
Code:
import java.util.Scanner;
public class LargestNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the array size: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter "+n+"
elements:"); for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int max = arr[0];
for (int i = 1; i < n; i++)
{ if (arr[i] > max) {
max = arr[i];
}
}
System.out.println("The largest number is " + max);
}
}
Output:
Date: / /
9. WAP in java to implement getter and setter method.
Code:
public class method
{
public static void main (String args[] )
{
A ob = new A();
ob.setmessage(" world
");
System.out.println("helo "+ob.getmessage() );
}
}
class A
{
private String message;
public void setmessage(String msg)
{
message=msg;
}
public String getmessage()
{
return message;
}
}
Output:
Date: / /
10. WAP in java to create constructor of a class and initialize values in it and later print them.
Code:
public class MyClass {
private int age;
private String name;
public MyClass(int age, String name) {
this.age = age;
this.name = name;
}
public void print() {
System.out.println("Name: " +
name); System.out.println("Age: " +
age);
public static void main(String[] args) {
MyClass obj = new MyClass(42,
"Rohit"); obj.print();
}
}
Output:
Date: / /
11. WAP in Java to implement the concept of abstract classes and interface.
Code:
// Define an interface for a
vehicle interface Vehicle {
void start();
void stop();
}
// Define an abstract class for a car that implements the Vehicle interface
abstract class Car implements Vehicle {
// Common properties for all
cars protected String make;
protected String
model; protected int
year; protected String
color;
// Constructor
public Car(String make, String model, int year, String color)
{ this.make = make;
this.model =
model; this.year =
year; this.color =
color;
}
// Common method for all
cars public void display() {
System.out.println("Make: " + make);
System.out.println("Model: " +
model); System.out.println("Year: " +
year); System.out.println("Color: " +
color);
}
// Abstract method to be implemented by
subclasses abstract void drive();
}
// A specific type of car that extends the abstract Car class
class SportsCar extends Car {
// Constructor
public SportsCar(String make, String model, int year, String color) {
super(make, model, year, color);
}
// Implementation of the abstract
method void drive() {
System.out.println("Driving a " + color + " " + make + " " + model + " sports car.");
}
// Implementation of the Vehicle
interface public void start() {
System.out.println("Starting the " + color + " " + make + " " + model + " sports car.");
}
public void stop() {
System.out.println("Stopping the " + color + " " + make + " " + model + " sports car.");
}
}
Date: / /
// Main program
public class AbstractClassInterfaceExample { public static void main(String[] args) {
SportsCar myCar = new SportsCar("Ferrari", "458 Italia", 2015, "Red"); myCar.display();
myCar.start(); myCar.drive(); myCar.stop();
}
}
Output:
Date: / /
12. WAP in Java to implement the concept of method overloading and constructor overloading
Code:
class Shape {
private int length;
private int width;
private int height;
// Constructor with no
arguments public Shape() {
length = 0;
width = 0;
height = 0;
}
// Constructor with one
argument public Shape(int
length) {
this.length = length;
width = 0;
height = 0;
}
// Constructor with two arguments
public Shape(int length, int width)
{
this.length = length;
this.width = width;
height = 0;
}
// Constructor with three arguments
public Shape(int length, int width, int height) {
this.length = length;
this.width = width;
this.height = height;
}
// Method with no
arguments public void area()
{
System.out.println("Area: " + (length * width));
}
// Method with one
argument public void area(int
side) {
System.out.println("Area: " + (side * side));
}
// Method with two arguments
public void area(int length, int width) {
System.out.println("Area: " + (length * width));
}
// Method with three arguments
public void area(int length, int width, int height) {
System.out.println("Surface area: " + (2 * (length * width + width * height + height * length)));
}
}
public class OverloadingExample {
Date: / /
public static void main(String[] args) { Shape shape1 = new Shape(); Shape shape2 = new Shape(5); Shape shape3 = new Shape(
shape1.area(); shape2.area(5); shape3.area(4, 6);
shape4.area(3, 4, 5);
}
}
Output:
Date: / /
13. WAP in Java to create a class Shape and override area() method to calculate area of rectangle
Square and circle
Code:
abstract class Shape {
// abstract method for calculating area
public abstract double area();
}
class Rectangle extends Shape
{ private double length;
private double width;
public Rectangle(double length, double width)
{ this.length = length;
this.width = width;
}
// Override the area method to calculate the area of the rectangle
@Override
public double area() {
return length *
width;
}
}
class Square extends Shape {
private double side;
public Square(double side)
{ this.side = side;
}
// Override the area method to calculate the area of the square
@Override
public double area()
{ return side *
side;
}
}
class Circle extends Shape
{ private double radius;
public Circle(double radius)
{ this.radius = radius;
}
// Override the area method to calculate the area of the circle
@Override
public double area() {
return Math.PI * radius * radius;
}
}
public class ShapeExample {
public static void main(String[] args) {
Shape shape1 = new Rectangle(5, 3);
Shape shape2 = new Square(4);
Shape shape3 = new Circle(2);
Date: / /
System.out.println("Area of rectangle: " + shape1.area()); System.out.println("Area of square: " + shape2.area()); System.out.pr
}
}
Output:
Date: / /
14. WAP in Java to demonstrate wrapper class, boxing auto boxing, unboxing auto unboxing .
Code:
public class WrapperExample {
public static void main(String[] args) {
// Wrapper class objects
Integer intObj = new Integer(10);
Double doubleObj = new
Double(3.14);
Boolean booleanObj = new Boolean(true);
// Boxing - converting primitive to wrapper class object
Integer intBoxed = Integer.valueOf(20);
Double doubleBoxed = Double.valueOf(6.28);
Boolean booleanBoxed =
Boolean.valueOf(false);
// Auto-boxing - implicit conversion from primitive to wrapper class object
Integer intAutoBoxed = 30;
Double doubleAutoBoxed = 9.42;
Boolean booleanAutoBoxed =
true;
// Unboxing - converting wrapper class object to
primitive int intUnboxed = intObj.intValue();
double doubleUnboxed = doubleObj.doubleValue();
boolean booleanUnboxed = booleanObj.booleanValue();
// Auto-unboxing - implicit conversion from wrapper class object to primitive
int intAutoUnboxed = intBoxed;
double doubleAutoUnboxed = doubleBoxed;
boolean booleanAutoUnboxed = booleanBoxed;
System.out.println("Wrapper class objects:");
System.out.println("Integer: " + intObj);
System.out.println("Double: " + doubleObj);
System.out.println("Boolean: " + booleanObj);
System.out.println("Boxed objects:");
System.out.println("Integer: " + intBoxed);
System.out.println("Double: " + doubleBoxed);
System.out.println("Boolean: " +
booleanBoxed);
System.out.println("Auto-boxed objects:");
System.out.println("Integer: " + intAutoBoxed);
System.out.println("Double: " + doubleAutoBoxed);
System.out.println("Boolean: " + booleanAutoBoxed);
System.out.println("Unboxed values:");
System.out.println("int: " + intUnboxed);
System.out.println("double: " + doubleUnboxed);
System.out.println("boolean: " + booleanUnboxed);
System.out.println("Auto-unboxed values:");
System.out.println("int: " + intAutoUnboxed);
System.out.println("double: " + doubleAutoUnboxed);
System.out.println("boolean: " + booleanAutoUnboxed);
}
}
Date: / /
Output:
Date: / /
15. WAP in Java to implement the concept of simple inheritance , multilevel inheritance ,and
hierarchical
Code:
// Class representing a Vehicle
class Vehicle {
protected String name;
protected int wheels;
public Vehicle(String name, int wheels)
{ this.name = name;
this.wheels = wheels;
}
public void drive() {
System.out.println(name + " is driving on " + wheels + " wheels");
}
}
// Class representing a Car, which is a subclass of Vehicle
class Car extends Vehicle {
protected String type;
public Car(String name, int wheels, String type) {
super(name, wheels);
this.type = type;
}
public void start() {
System.out.println(name + " is starting as a " + type + " car");
}
}
// Class representing a SportsCar, which is a subclass of Car
class SportsCar extends Car {
protected boolean isConvertible;
public SportsCar(String name, int wheels, String type, boolean isConvertible) {
super(name, wheels, type);
this.isConvertible = isConvertible;
}
public void race() {
System.out.println(name + " is racing as a " + type + " sports car with " + (isConvertible ? "a" : "no") + "
convertible top");
}
}
// Class representing a Bike, which is a subclass of Vehicle
class Bike extends Vehicle {
protected boolean isMotorized;
public Bike(String name, int wheels, boolean isMotorized)
{ super(name, wheels);
this.isMotorized = isMotorized;
}
public void pedal() {
Date: / /
System.out.println(name + " is pedaling with " + (isMotorized ? "a" : "no") + " motor");
}
}
// Class representing a Truck, which is a subclass of Vehicle
class Truck extends Vehicle {
protected int capacity;
public Truck(String name, int wheels, int capacity) {
super(name, wheels);
this.capacity = capacity;
}
public void load() {
System.out.println(name + " is loading with capacity of " + capacity + " tons");
}
}
public class InheritanceExample {
public static void main(String[] args) {
// Simple Inheritance example
System.out.println("Simple Inheritance Example:");
Bike myBike = new Bike("My Bike", 2, false);
myBike.drive();
myBike.pedal();
System.out.println();
// Multilevel Inheritance example
System.out.println("Multilevel Inheritance
Example:");
SportsCar mySportsCar = new SportsCar("My Sports Car", 4, "Sports", true);
mySportsCar.drive();
mySportsCar.start();
mySportsCar.race();
System.out.println()
;
// Hierarchical Inheritance example
System.out.println("Hierarchical Inheritance
Example:"); Truck myTruck = new Truck("My Truck", 6,
10); myTruck.drive();
myTruck.load();
System.out.println();
Bike anotherBike = new Bike("Another Bike", 2, true);
anotherBike.drive();
anotherBike.pedal();
}
}
Date: / /
Output:
Date: / /
16. WAP in Java to implement multiple inheritance using interface.
Code:
// Interface representing a Vehicle
interface Vehicle {
void drive();
}
// Interface representing a
MusicPlayer interface MusicPlayer {
void playMusic();
}
// Class representing a Car that implements both Vehicle and MusicPlayer interfaces
class Car implements Vehicle, MusicPlayer {
private String name;
public Car(String name) {
this.name = name;
}
@Override
public void drive() {
System.out.println(name + " is
driving");
}
@Override
public void playMusic() {
System.out.println(name + " is playing
music");
}
}
public class MultipleInheritanceExample {
public static void main(String[] args) {
Car myCar = new Car("My Car");
myCar.drive();
myCar.playMusic();
}
}
Output: