Kalpana Test11
Kalpana Test11
1. Building Blocks:
○ Create a class called Car with attributes like color, model, and year.
Define methods like startEngine, accelerate, and brake.
CODE:
package com.test;
String color;
String model;
int year, speed;
private boolean engineStarted;
CODE:
package com.test;
2. Inheritance:
○ Create a base class called Shape with an abstract method
calculateArea. Extend it to classes like Circle, Rectangle, and
Square, implementing the calculateArea method in each.
CODE:
package com.test;
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
@Override
public double calculateArea() {
return length * width;
}
}
@Override
public double calculateArea() {
return side * side;
}
}
class Animal {
private String name;
@Override
public void makeSound() {
System.out.println(getName() + " says Meow!");
}
}
@Override
public void makeSound() {
System.out.println(getName() + " sings Tweet!");
}
}
dog.makeSound();
cat.makeSound();
bird.makeSound();
}
}
3. Encapsulation:
○ Modify the Car class from exercise 1 to make the attributes private and provide
getter and setter methods to access and modify them.
CODE:
package com.test1;
○ Modify the Employee class from exercise 1 to make the salary attribute private
and create a method to provide a raise while ensuring it stays within a
reasonable range.
CODE:
package com.test1;
emp.giveRaise(10);
emp.displayDetails();
}
}
4. Polymorphism:
○ Create an interface called Drawable with a method draw. Extend it to classes
like Shape, Circle, and Rectangle, overriding the draw method in each to
display their specific representation.
CODE:
package com.test1;
interface Drawable {
void draw();
}
@Override
public void draw() {
System.out.println("Drawing a circle with radius " + radius);
}
}
class Rectangle implements Drawable {
private double length, width;
@Override
public void draw() {
System.out.println("Drawing a rectangle with length " + length + " and
width " + width);
}
}
shape.draw();
circle.draw();
rectangle.draw();
}
}
○ Create an abstract class called AnimalSound with an abstract method
makeSound. Extend it to specific animal classes like Dog, Cat, and Bird,
overriding the makeSound method to play the animal's sound (using
System.out.println for simplicity).
CODE:
package com.test1;
dog.makeSound();
cat.makeSound();
bird.makeSound();
}
}
Advanced:
5. Abstraction:
○ Create an abstract class called PaymentProcessor with an abstract
method processPayment. Extend it to concrete classes like
CreditCardPaymentProcessor and PayPalPaymentProcessor,
implementing the processPayment method for each specific payment
method.
@Override
public void processPayment(double amount) {
System.out.println("Processing credit card payment of $" + amount + "
with card number: " + creditCardNumber);
// Additional credit card payment processing logic
}
}
@Override
public void processPayment(double amount) {
System.out.println("Processing PayPal payment of $" + amount + " with
email: " + paypalEmail);
}
}
creditCardProcessor.processPayment(100.0);
paypalProcessor.processPayment(50.0);
}
}
○ Create an interface called ShapeFactory with a method getShape.
Implement it for different shapes, e
CODE: package com.test2;
interface Shape {
void draw();
}
interface ShapeFactory {
Shape getShape();
}
circle.draw();
rectangle.draw();
triangle.draw();
}
}
Create a Class:
2. Define Constructors:
● In the main method, prompt the user to enter the hour, minute, and second
values through the console using Scanner.
● Create a new Time object using either the no-argument constructor or the
parameterized constructor based on user input.
○ If the user chooses the parameterized constructor and enters invalid
values, handle the IllegalArgumentException and prompt the user
to re-enter the values.
● Display the created Time object by printing its hour, minute, and second in a
formatted way (e.g., "HH:MM:SS").
CODE:
package com.test2;
import java.util.Scanner;
public Time() {
this.hour = 0;
this.minute = 0;
this.second = 0;
}
try {
Time userTime = new Time(hour, minute, second);
userTime.displayTime();
} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
System.out.println("Please enter valid time values.");
} finally {
scanner.close();
}
}
}