JAVA PROGRAMS
2(c). JAVA program that demonstrates how to use StringBuffer to delete and remove characters.
public class StringBufferExample {
public static void main(String[] args) {
// Create a StringBuffer object
StringBuffer sb = new StringBuffer("Hello, World!");
System.out.println("Original StringBuffer content: " + sb);
// Delete characters from index 5 to 7
sb.delete(5, 7);
System.out.println("After deleting characters from index 5 to 7: " + sb);
// Delete a character at index 5
sb.deleteCharAt(5);
System.out.println("After deleting character at index 5: " + sb);
}
}
This program:
1. Creates a StringBuffer object with the string "Hello, World!".
2. Prints the original content of the StringBuffer.
3. Uses the delete(int start, int end) method to remove characters from index 5 to 7.
4. Uses the deleteCharAt(int index) method to remove a character at index 5.
Output:
Original StringBuffer content: Hello, World!
After deleting characters from index 5 to 7: HelloWorld!
After deleting character at index 5: Helloorld!
5(a). JAVA program that demonstrates how to use the super keyword.
// Base class
class Vehicle {
String brand;
Vehicle(String brand) {
this.brand = brand;
}
void displayDetails() {
System.out.println("Brand: " + brand);
}
}
// Derived class
class Car extends Vehicle {
String model;
Car(String brand, String model) {
super(brand); // Calling Vehicle class constructor
this.model = model;
}
void displayDetails() {
super.displayDetails(); // Calling Vehicle class method
System.out.println("Model: " + model);
}
}
public class SuperKeywordExample {
public static void main(String[] args) {
Car car = new Car("Toyota", "Camry");
car.displayDetails();
}
}
This program:
1. Defines a base class Vehicle with a field brand and a method displayDetails().
2. Defines a derived class Car that extends Vehicle.
3. In the Car class constructor, uses super(brand) to call the Vehicle class constructor.
4. In the Car class displayDetails() method, uses super.displayDetails() to call the Vehicle class method.
The super keyword is used to:
- Access the parent class constructor.
- Access the parent class methods.
Output:
Brand: Toyota
Model: Camry
5(b). JAVA program that demonstrates how to implement an interface.
// Interface
interface Shape {
void draw();
double getArea();
}
// Class implementing the interface
class Circle implements Shape {
private double radius;
Circle(double radius) {
this.radius = radius;
}
@Override
public void draw() {
System.out.println("Drawing a circle.");
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
// Another class implementing the interface
class Rectangle implements Shape {
private double width;
private double height;
Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public void draw() {
System.out.println("Drawing a rectangle.");
}
@Override
public double getArea() {
return width * height;
}
}
public class InterfaceExample {
public static void main(String[] args) {
Shape circle = new Circle(5.0);
Shape rectangle = new Rectangle(4.0, 6.0);
circle.draw();
System.out.println("Circle area: " + circle.getArea());
rectangle.draw();
System.out.println("Rectangle area: " + rectangle.getArea());
}
}
This program:
1. Defines an interface Shape with two methods: draw() and getArea().
2. Creates classes Circle and Rectangle that implement the Shape interface.
3. Implements the interface methods in each class.
Inheritance Achieved
Implementing interfaces achieves multiple inheritance in JAVA.
A class can implement multiple interfaces.
// Example
interface A {
void methodA();
}
interface B {
void methodB();
}
class C implements A, B {
@Override
public void methodA() {
System.out.println("Implementing methodA.");
}
@Override
public void methodB() {
System.out.println("Implementing methodB.");
}
}
Multiple inheritance can be achieved using interfaces.
Output:
Drawing a circle.
Circle area: 78.53981633974483
Drawing a rectangle.
Rectangle area: 24.0
JAVA program that demonstrates runtime polymorphism (Overriding).
// Base class
class Shape {
void draw() {
System.out.println("Drawing a shape.");
}
double getArea() {
System.out.println("Shape area is not defined.");
return 0.0;
}
}
// Derived class
class Circle extends Shape {
private double radius;
Circle(double radius) {
this.radius = radius;
}
@Override
void draw() {
System.out.println("Drawing a circle.");
}
@Override
double getArea() {
return Math.PI * radius * radius;
}
}
// Another derived class
class Rectangle extends Shape {
private double width;
private double height;
Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
void draw() {
System.out.println("Drawing a rectangle.");
}
@Override
double getArea() {
return width * height;
}
}
public class RuntimePolymorphismExample {
public static void main(String[] args) {
Shape shape1 = new Circle(5.0);
Shape shape2 = new Rectangle(4.0, 6.0);
shape1.draw();
System.out.println("Circle area: " + shape1.getArea());
shape2.draw();
System.out.println("Rectangle area: " + shape2.getArea());
}
}
This program:
1. Defines a base class Shape with methods draw() and getArea().
2. Creates derived classes Circle and Rectangle that extend Shape and override the methods.
3. In the main() method, creates objects of Circle and Rectangle but refers to them using Shape type variables.
4. Calls the overridden methods.
Runtime polymorphism is achieved using:
- Method overriding: Derived classes provide specific implementations for methods declared in the base class.
- Base class reference: Using a base class reference to call methods on derived class objects.
The JVM determines which method to call at runtime based on the actual object type.
Output:
Drawing a circle.
Circle area: 78.53981633974483
Drawing a rectangle.
Rectangle area: 24.0
One Program which covers almost all the topics covered till now.
// Interface
interface SocialMediaActions {
void postUpdate(String message);
void followUser(String username);
}
// Base class
class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void displayInfo() {
System.out.println("Name: " + name);
}
// Static method
public static void printWelcomeMessage() {
System.out.println("Welcome to our social media platform!");
}
// Overloaded method
public void updateProfile(String newName) {
this.name = newName;
System.out.println("Profile updated with new name: " + name);
}
// Overloaded method
public void updateProfile(String newName, String newEmail) {
this.name = newName;
System.out.println("Profile updated with new name: " + name + " and new email: " + newEmail);
}
}
// Intermediate class
class SocialMediaUser extends User implements SocialMediaActions {
private String platform;
public SocialMediaUser(String name, String platform) {
super(name); // Calling User class constructor
this.platform = platform;
}
public String getPlatform() {
return platform;
}
@Override
public void displayInfo() {
super.displayInfo(); // Calling User class method
System.out.println("Platform: " + platform);
}
// Implementing interface methods
@Override
public void postUpdate(String message) {
System.out.println("Posted update on " + platform + ": " + message);
}
@Override
public void followUser(String username) {
System.out.println("Followed user " + username + " on " + platform);
}
// Static method
public static void printPlatformInfo() {
System.out.println("This is a social media platform.");
}
}
// Derived class
class Influencer extends SocialMediaUser {
private int followers;
public Influencer(String name, String platform, int followers) {
super(name, platform); // Calling SocialMediaUser class constructor
this.followers = followers;
}
public int getFollowers() {
return followers;
}
@Override
public void displayInfo() {
super.displayInfo(); // Calling SocialMediaUser class method
System.out.println("Followers: " + followers);
}
public static void main(String[] args) {
User.printWelcomeMessage(); // Calling static method
SocialMediaUser.printPlatformInfo(); // Calling static method
Influencer influencer = new Influencer("John Doe", "Instagram", 10000);
influencer.displayInfo();
influencer.postUpdate("Hello, world!"); // Calling interface method
influencer.followUser("janeDoe"); // Calling interface method
influencer.updateProfile("Jane Doe"); // Calling overloaded method
influencer.updateProfile("Jane Doe", "jane@example.com"); // Calling overloaded method
}
}
This program includes:
1. Interface: The SocialMediaActions interface defines two methods: postUpdate and followUser.
2. Interface implementation: The SocialMediaUser class implements the SocialMediaActions interface.
The benefits of using interfaces include:
- Abstraction: Interfaces help to define a contract or a set of methods that must be implemented.
- Polymorphism: Interfaces enable polymorphism, allowing objects of different classes to be treated as instances of the same interface.
By implementing the SocialMediaActions interface, the SocialMediaUser class provides a specific implementation for the postUpdate and followUser methods.
Comments
Post a Comment