Lab Assignment 041035
Lab Assignment 041035
Lab Assignment 041035
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
{
}
OUTPUT:
Enter any String: Calendar
Printing a string in a reverse order:
R
A
D
N
E
L
A
C
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
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.
int rows = 6;
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
❖ JAVA PROGRAMS
● Inheritance
● SINGLE INHERITANCE
// Create a superclass named Animal
class Animal
{
String name;
public Animal(String name)
{
this.name = name;
}
● 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.