0% found this document useful (0 votes)
30 views

Polymorphism Abstraction&Interfaces

Uploaded by

Subhajit Kundu
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Polymorphism Abstraction&Interfaces

Uploaded by

Subhajit Kundu
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

1: Write code to determine if the string is a palindrome.

Input String : Madam

Output : Madam is a Palindrome


ans:
*/
import java.util.Scanner;

public class PalindromeChecker {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Input String: ");
String input = scanner.nextLine().toLowerCase(); // Convert the input to
lowercase
scanner.close();

if (isPalindrome(input)) {
System.out.println(input + " is a Palindrome");
} else {
System.out.println(input + " is not a Palindrome");
}
}

// Method to check if a string is palindrome


static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j) {
if (str.charAt(i) != str.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
}

/*This program prompts the user to input a string, converts it to lowercase, and
then checks if it is a palindrome using the isPalindrome method. If the input
string is the same forwards and backwards (ignoring case), it is considered a
palindrome.

2: You need to find and print all the unique characters in a given string.

Input string : java

Output : jv

ans:
*/
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class UniqueCharacters {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Input string: ");
String input = scanner.nextLine();
scanner.close();

Set<Character> uniqueChars = new HashSet<>();

// Iterate through each character in the string


for (char ch : input.toCharArray()) {
// Add the character to the set
uniqueChars.add(ch);
}

// Print the unique characters


System.out.print("Output: ");
for (char ch : uniqueChars) {
System.out.print(ch);
}
}
}

/*1:This program prompts the user to input a string, iterates through each
character, and adds it to a HashSet. Since a HashSet does not allow duplicate
elements, only unique characters will be added. Finally, it prints out the unique
characters found in the input string.

ans:Here's a Java program implementing the superclass HillStations and its


subclasses Manali, Mussoorie, and Gulmarg, with overridden methods as described:

java
*/
// Superclass
class HillStations {
// Method to get location
void location() {
System.out.println("Location: Hill Station");
}

// Method to get what it is famous for


void famousFor() {
System.out.println("Famous for its scenic beauty.");
}
}

// Subclass Manali
class Manali extends HillStations {
// Overriding location method
void location() {
System.out.println("Location: Manali");
}

// Overriding famousFor method


void famousFor() {
System.out.println("Famous for its snow-capped mountains and adventure
sports.");
}
}

// Subclass Mussoorie
class Mussoorie extends HillStations {
// Overriding location method
void location() {
System.out.println("Location: Mussoorie");
}

// Overriding famousFor method


void famousFor() {
System.out.println("Famous for its lush green hills and waterfalls.");
}
}

// Subclass Gulmarg
class Gulmarg extends HillStations {
// Overriding location method
void location() {
System.out.println("Location: Gulmarg");
}

// Overriding famousFor method


void famousFor() {
System.out.println("Famous for its ski resorts and beautiful landscapes.");
}
}

public class Main {


public static void main(String[] args) {
// Create objects of each subclass
HillStations hillStation = new HillStations();
Manali manali = new Manali();
Mussoorie mussoorie = new Mussoorie();
Gulmarg gulmarg = new Gulmarg();

// Call location and famousFor methods by the parent class


hillStation.location();
hillStation.famousFor();
System.out.println();

// Call location and famousFor methods by the subclasses


manali.location();
manali.famousFor();
System.out.println();

mussoorie.location();
mussoorie.famousFor();
System.out.println();

gulmarg.location();
gulmarg.famousFor();
}
}

/*This program demonstrates multilevel inheritance with a superclass HillStations


and its three subclasses Manali, Mussoorie, and Gulmarg. Each subclass overrides
the location() and famousFor() methods inherited from the superclass. The main
method creates objects of each subclass and calls both superclass and subclass
methods to print out the location and what each hill station is famous for.

2:Write a Java program that demonstrates method overriding by creating a superclass


called Animal and two subclasses called Dog and Cat. ● The Animal class should have
a method called makeSound(), which simply prints "The animal makes a sound." ● The
Dog and Cat classes should override this method to print "TheCat/The dog
meows/barks" respectively. ● The program should allow the user to create and
display objects of each class.

ans:: */
// Superclass Animal
class Animal {
// Method to make sound
void makeSound() {
System.out.println("The animal makes a sound.");
}
}

// Subclass Dog
class Dog extends Animal {
// Override makeSound method
@Override
void makeSound() {
System.out.println("The dog barks.");
}
}

// Subclass Cat
class Cat extends Animal {
// Override makeSound method
@Override
void makeSound() {
System.out.println("The cat meows.");
}
}

public class Main {


public static void main(String[] args) {
// Create objects of each class
Animal animal = new Animal();
Dog dog = new Dog();
Cat cat = new Cat();

// Display the sound of each animal


System.out.print("Animal: ");
animal.makeSound();

System.out.print("Dog: ");
dog.makeSound();

System.out.print("Cat: ");
cat.makeSound();
}
}

/*[ The Animal class serves as the superclass with a method makeSound() that
prints "The animal makes a sound."
The Dog and Cat classes are subclasses of Animal. They override the makeSound()
method to print "The dog barks." and "The cat meows." respectively.
In the main method, objects of each class are created, and the makeSound()
method of each object is called to display the sound each animal makes.]

3: Create abstract class vaccine.Create two variables


age(int),nationality(String).create 2 concrete methods firstDose() and
secondDose(). Scenario 1:user can take the first dose if the user is Indian and age
is 18.After vaccination the user has to pay 250rs(which will be displayed on the
console). Scenario 2: Users are eligible to take the second dose only after
completing the first dose. Scenario 3: create abstract method boosterDose() in
abstract class Vaccine.Create one implementation class vaccinationSuccessful, where
implement boosterDose() method. Create main class vaccination and invoke all
methods accordingly. [Hint:Create constructor to initialize variables age and
nationality,Use flow control(Ifelse) to check condition]

ans:
*/
// Abstract class Vaccine
abstract class Vaccine {
// Variables
int age;
String nationality;

// Constructor
public Vaccine(int age, String nationality) {
this.age = age;
this.nationality = nationality;
}

// Concrete method for first dose


public void firstDose() {
if (nationality.equalsIgnoreCase("Indian") && age >= 18) {
System.out.println("First dose administered successfully. Please pay
250 Rs.");
} else {
System.out.println("Sorry, you are not eligible for the first dose.");
}
}

// Concrete method for second dose


public void secondDose() {
System.out.println("Second dose administered successfully.");
}

// Abstract method for booster dose


abstract void boosterDose();
}

// Implementation class VaccinationSuccessful


class VaccinationSuccessful extends Vaccine {
// Constructor
public VaccinationSuccessful(int age, String nationality) {
super(age, nationality);
}

// Implementation of abstract method


void boosterDose() {
System.out.println("Booster dose administered successfully.");
}
}

// Main class Vaccination


public class Vaccination {
public static void main(String[] args) {
// Create object of implementation class
Vaccine user = new VaccinationSuccessful(20, "Indian");

// Invoke first dose method


System.out.println("Scenario 1:");
user.firstDose();

// Invoke second dose method


System.out.println("\nScenario 2:");
user.secondDose();

// Invoke booster dose method


System.out.println("\nScenario 3:");
user.boosterDose();
}
}

/*note[ Vaccine is an abstract class with variables age and nationality, and
concrete methods firstDose() and secondDose().
The VaccinationSuccessful class extends Vaccine and implements the abstract
method boosterDose().
In the main method of the Vaccination class, an object of the
VaccinationSuccessful class is created and its methods are invoked according to the
scenarios provided.]
*/

/*Manali, Mussoorie, Gulmarg. Subclasses extend the superclass and override its
location() and famousFor() method.

i.call the location() and famousFor() method by the Parent class’, i.e.
Hillstations class. As it refers to the base class object and the base class method
overrides the superclass method; the base class method is invoked at runtime.

ii.call the location() and famousFor() method by the all subclass’,and print
accordingly.

ans:: implementing the superclass HillStations and its subclasses Manali,


Mussoorie, and Gulmarg */

// Superclass HillStations
class HillStations {
// Method to get location
void location() {
System.out.println("Location: Hill Station");
}

// Method to get famous for


void famousFor() {
System.out.println("Famous for: Scenic beauty and cool climate");
}
}

// Subclass Manali
class Manali extends HillStations {
// Override method for location
@Override
void location() {
System.out.println("Location: Manali, Himachal Pradesh");
}

// Override method for famousFor


@Override
void famousFor() {
System.out.println("Famous for: Snow-capped mountains and adventure
sports");
}
}

// Subclass Mussoorie
class Mussoorie extends HillStations {
// Override method for location
@Override
void location() {
System.out.println("Location: Mussoorie, Uttarakhand");
}

// Override method for famousFor


@Override
void famousFor() {
System.out.println("Famous for: Lush green hills and waterfalls");
}
}

// Subclass Gulmarg
class Gulmarg extends HillStations {
// Override method for location
@Override
void location() {
System.out.println("Location: Gulmarg, Jammu and Kashmir");
}

// Override method for famousFor


@Override
void famousFor() {
System.out.println("Famous for: Skiing and snowboarding");
}
}

// Main class to test the implementation


public class Main {
public static void main(String[] args) {
// Creating objects of each subclass
HillStations hillStations = new HillStations();
Manali manali = new Manali();
Mussoorie mussoorie = new Mussoorie();
Gulmarg gulmarg = new Gulmarg();

// Calling location and famousFor methods using superclass object


System.out.println("Calling location and famousFor methods using superclass
object:");
hillStations.location();
hillStations.famousFor();

// Calling location and famousFor methods using subclass objects


System.out.println("\nCalling location and famousFor methods using subclass
objects:");
manali.location();
manali.famousFor();

mussoorie.location();
mussoorie.famousFor();

gulmarg.location();
gulmarg.famousFor();
}
}

output::

Calling location and famousFor methods using superclass object:


Location: Hill Station
Famous for: Scenic beauty and cool climate

Calling location and famousFor methods using subclass objects:


Location: Manali, Himachal Pradesh
Famous for: Snow-capped mountains and adventure sports
Location: Mussoorie, Uttarakhand
Famous for: Lush green hills and waterfalls
Location: Gulmarg, Jammu and Kashmir
Famous for: Skiing and snowboarding

/* note HillStations is the superclass with methods location() and famousFor().


Subclasses Manali, Mussoorie, and Gulmarg override these methods to provide
specific details about their location and what they are famous for.
In the main method, both superclass and subclass objects are created and their
methods are called to demonstrate polymorphism
*/

/*
Write a Java program that demonstrates method overriding by creating a superclass
called Animal and two subclasses called Dog and Cat.

● The Animal class should have a method called makeSound(), which simply prints
"The animal makes a sound."

● The Dog and Cat classes should override this method to print "TheCat/The dog
meows/barks" respectively.

● The program should allow the user to create and display objects of each class.

[Hint:Use multilevel inheritance]

*/
/*Here's the Java program demonstrating method overriding using superclass Animal
and subclasses Dog and Cat:*/

// Superclass Animal
class Animal {
// Method to make sound
void makeSound() {
System.out.println("The animal makes a sound.");
}
}

// Subclass Dog
class Dog extends Animal {
// Override method for makeSound
@Override
void makeSound() {
System.out.println("The dog barks.");
}
}

// Subclass Cat
class Cat extends Animal {
// Override method for makeSound
@Override
void makeSound() {
System.out.println("The cat meows.");
}
}

// Main class to test the implementation


public class Main {
public static void main(String[] args) {
// Creating objects of each class
Animal animal = new Animal();
Dog dog = new Dog();
Cat cat = new Cat();

// Calling makeSound method for each object


System.out.print("Animal: ");
animal.makeSound();

System.out.print("Dog: ");
dog.makeSound();

System.out.print("Cat: ");
cat.makeSound();
}
}

OUTPUT:
Animal: The animal makes a sound.
Dog: The dog barks.
Cat: The cat meows.

/* Animal is the superclass with a method makeSound() which prints "The animal
makes a sound."
Subclasses Dog and Cat override the makeSound() method to provide specific
sounds for each animal.
In the main method, objects of each class are created and their makeSound()
methods are called to demonstrate method overriding.

*/

You might also like