Lab Assignment 041035

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

LAB ASSIGNMENT

COMPILED BY:
FAKEHA SAMI
AAMNA KHALIQ
ARIBA KHALIQ
BUSHRA KASHIF

❖ SIMPLE PROGRAMS
● Program to print a String in reverse order
package reverse;
import java.util.*;
public class ReverseOrder
{

public static void main(String[] args)


{
Scanner input=new Scanner(System.in);
System.out.println("Enter any string :");
String a=input.nextLine();
int b=a.length();
System.out.println("printing a string in a revere order :");

for(int i=b-1; i>=0; i--)


{
System.out.println(a.charAt(i));
}
}

}
OUTPUT:
Enter any String: Calendar
Printing a string in a reverse order:
R
A
D
N
E
L
A
C

● Program to find the Largest number


import java.util.Scanner;
Public class Greatest Number
{
public static void main(String[] args)

{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();

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


int num2 = scanner.nextInt();

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


int num3 = scanner.nextInt();

int largestNumber = Math.max(num1, Math.max(num2, num3));

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


}
}
OUTPUT:
Enter the first number:
10
Enter the second number:
20
Enter the third number:
30
The largest number is: 30

● Program to check a Palindrome


import java.util.Scanner;
public class PalindromeChecker
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string to check if it is a palindrome: ");
String inputString = scanner.nextLine();
String reversedString = "";
for (int i = inputString.length() - 1; i >= 0; i--)
{
reversedString += inputString.charAt(i);
}

if (inputString.equals(reversedString))
{
System.out.println(inputString + " is a palindrome.");
} else {
System.out.println(inputString + " is not a palindrome.");
}
}
}
OUTPUT:
Enter a string: madam
madam is a palindrome.

❖ PROGRAM WITH PATTERNS


● Program to print a star pattern
package pattern;
import java.util.*;
public class Pattern {

public static void main(String[] args) {


// TODO Auto-generated method stub
Syst em.out.println("\tPrint a Pattern:\t");
System.out.println();

int rows = 6;

for (int i= rows-1; i>=0 ; i--)


{
for (int j=0; j<=i; j++)
{
System.out.print("*" + " ");
}
System.out.println();
}
}
}
OUTPUT:
Print a Pattern
******
*****
****
***
**
*
❖ DATA STRUCTURE PROGRAMS
● Bubble Sort Program
public class BubbleSort
{
public static void main(String[] args)
{
int[] arr = {64, 34, 25, 12, 22, 11, 90};
System.out.println("Unsorted Array:");
printArray(arr);
bubbleSort(arr);
System.out.println("\nSorted Array:");
printArray(arr);
}

static void bubbleSort(int[] arr)


{
boolean isSorted = false;
int n = arr.length;
for (int i = 0; i < n && !isSorted; i++)
{
is Sorted = true;
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;

is Sorted = false;
}
}
}
}
static void printArray(int[] arr)
{
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
OUTPUT:
Unsorted Array:
64 34 25 12 22 11 90
Sorted Array:
11 12 22 25 34 64 90

● Selection Sort Program


public class SelectionSort
{
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;
}
}
// Swap the minimum element with the current element
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
public static void printArray(int[] arr){
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
System.out.println("Unsorted array:");
printArray(arr);
selectionSort(arr);
System.out.println("Sorted array:");
printArray(arr);
}
}
OUTPUT:
Unsorted array:
64 34 25 12 22 11 90
Sorted array:
11 12 22 25 34 64 90

❖ JAVA PROGRAMS
● Inheritance
● SINGLE INHERITANCE
// Create a superclass named Animal
class Animal
{
String name;
public Animal(String name)
{
this.name = name;
}

public void eat()


{
System.out.println(name + " is eating.");
}
}
// Create a subclass named Dog that inherits from the Animal class
class Dog extends Animal
{
public Dog(String name)
{
super(name); // Call the superclass constructor
}
public void bark()
{
System.out.println(name + " is barking.");
}
}

// Create a main method to test the code


public class SingleInheritanceExample
{
public static void main(String[] args)
{
Dog dog = new Dog("Max");
dog.eat(); // Call the eat() method from the Animal superclass
dog.bark ();// Call the bark() method from the Dog subclass
}
}
OUTPUT:
Max is eating.
Max is barking.

● MULTILEVEL INHERITANCE
class Animal
{
void eat()
{
System.out.println("Eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("Barking...");
}
}
class BabyDog extends Dog
{
void play()
{
System.out.println("Playing...");
}
}
public class Main
{
public static void main(String[] args)
{
BabyDog babyDog = new BabyDog();
babyDog.play();
babyDog.bark();
babyDog.eat();
}
}
This program creates a multilevel inheritance hierarchy with three classes:
Animal, Dog, and BabyDog. The BabyDog class inherits from the Dog class,
which inherits from the Animal class. This means that BabyDog objects have
access to all of the methods that are defined in the Animal and Dog classes.
The main() method creates a BabyDog object and calls its play(), bark(), and
eat() methods. The output of the program is:
OUTPUT:
Playing...
Barking...
Eating...

● Polymorphism
● COMPILE TIME POLYMORPHISM
class Shape
{
void draw()
{
System.out.println("Drawing a shape");
}
}
class Circle extends Shape
{
void draw()
{
System.out.println("Drawing a circle");
}
}
class Square extends Shape
{
void draw()
{
System.out.println("Drawing a square");
}
}
public class CompileTimePolymorphism
{
public static void main(String[] args)
{
Shape shape1 = new Circle();
shape1.draw(); // Output: Drawing a circle
Shape shape2 = new Square();
shape2.draw(); // Output: Drawing a square
}
}
OUTPUT:
Drawing a circle
Drawing a square
In this example, the draw() method is overloaded in the Circle and Square
classes. At compile time, the compiler determines which version of the draw()
method to call based on the type of the object being referenced. This is an
example of compile-time polymorphism.

● RUN TIME POLYMORPHISM


class Shape
{
void draw()
{
System.out.println("Drawing shape...");
}
}
class Rectangle extends Shape
{
@Override
void draw()
{
System.out.println("Drawing rectangle...");
}
}
class Circle extends Shape
{
@Override
void draw()
{
System.out.println("Drawing circle...");
}
}
class Triangle extends Shape
{
@Override
void draw()
{
System.out.println("Drawing triangle...");
}
}
class TestPolymorphism
{
public static void main(String[] args)
{
Shape s;
s = new Rectangle();
s.draw(); // Output: Drawing rectangle...
s = new Circle();
s.draw(); // Output: Drawing circle...
s = new Triangle();
s.draw(); // Output: Drawing triangle...
}
}
OUTPUT:
Drawing rectangle
Drawing circle
Drawing triangle

This program demonstrates runtime polymorphism by creating a Shape class that


has a draw() method. Two subclasses, Rectangle and Circle, extend the Shape
class and override the draw() method to provide their own implementations. In
the main() method, a Shape reference variable is declared and used to reference
objects of both the Rectangle and Circle classes. When the draw() method is
called, the overridden version of the method is executed, depending on the actual
type of the object being referenced. This is an example of runtime polymorphism,
as the method that is called is determined at runtime, based on the type of the
object being referenced.

You might also like