Java Lab Cycle 3 (1)
Java Lab Cycle 3 (1)
Objective: Understand the concept of inheritance and how to create a subclass from a
superclass.
Problem Statement:
Expected Output:
Brand: Toyota
Speed: 180 km/h
Number of Doors: 4
Objective: Learn how to use the super keyword to invoke superclass methods and
constructors.
Problem Statement:
Expected Output:
Objective: Understand method overriding and the role of super in accessing overridden
methods.
Problem Statement:
1. Create a superclass Animal with a method void sound() that prints "Animal makes a
sound".
2. Create a subclass Dog that overrides the sound() method to print "Dog barks".
3. In the Dog class, create a method void makeSound() that:
○ Calls the sound() method of the superclass using the super keyword.
○ Calls the overridden sound() method of the Dog class.
4. In the main method, create an object of the Dog class and call the makeSound()
method.
Expected Output:
Problem Statement:
1. Create a superclass Employee with the following properties:
○ String name
○ double salary
2. The class should have:
○ A constructor that initializes these properties.
○ A method void display() to display the employee's information.
3. Create a subclass Manager that has:
○ An additional property int teamSize.
○ A constructor that uses the super keyword to initialize name and salary, and
initializes teamSize.
○ Override the display() method to include the team size.
4. In the main method, create an object of the Manager class and use it to display the
manager's details.
Expected Output:
Problem Statement:
Expected Output:
Brand: Samsung
Power: 1500 W
Capacity: 7 kg
Brand: LG
Power: 500 W
Volume: 350 liters
-------------------------------------------------------------------------------------------------------------------------------
Problem Statement:
Expected Output:
Problem Statement:
1. Create a superclass Shape with a method void draw() that prints "Drawing a
shape".
2. Create a subclass Circle that inherits from Shape and overrides the draw() method
to print "Drawing a circle".
3. Create another subclass Rectangle that also inherits from Shape and overrides the
draw() method to print "Drawing a rectangle".
4. In the main method, create objects of Circle and Rectangle and call the draw()
method on each.
Expected Output:
Drawing a circle
Drawing a rectangle
Problem Statement:
1. Create a superclass Vehicle with a method void start() that prints "Vehicle is
starting".
2. Create a subclass Car that overrides the start() method to print "Car is
starting", but also calls the superclass's start() method using super.
3. In the main method, create an object of the Car class and call the start() method.
Expected Output:
Vehicle is starting
Car is starting
Objective: Understand the use of the final keyword with variables, methods, and classes.
Problem Statement:
1. Create a class Constants with a final variable PI (value: 3.14159) and a final
method getPI() that returns the value of PI.
2. Attempt to create a subclass MathConstants that tries to override the getPI()
method. Observe the compilation error and discuss why overriding is not allowed.
3. Create a final class ImmutableClass with a final method showMessage() that
prints "This is an immutable class".
4. Attempt to create a subclass ExtendedClass that tries to extend ImmutableClass.
Observe the compilation error and discuss why inheritance is not allowed.
5. In the main method, demonstrate that PI cannot be changed by attempting to reassign
its value. Discuss the immutability enforced by final.
Expected Observations:
Objective: Apply the concepts of multilevel inheritance, method overriding, and the final
keyword in a real-world scenario.
Problem Statement:
Expected Output:
Problem Statement:
Expected Output:
Objective: Learn how to use abstract classes to create a template for subclasses.
Problem Statement:
Expected Output:
Objective: Understand the use of abstract classes with both abstract and concrete methods.
Problem Statement:
Expected Output:
Problem Statement:
Expected Output:
Problem Statement:
Printing document...
Scanning document...
Problem Statement:
1. Create an abstract class Appliance with a concrete method void powerOn() that
prints "Appliance is powered on" and an abstract method void operate().
2. Create an interface RemoteControl with a method void turnOn().
3. Create a class TV that inherits from Appliance and implements RemoteControl. The
turnOn() method should print "TV is turned on with remote", and the
operate() method should print "TV is operating".
4. In the main method, create an object of TV, call the turnOn(), powerOn(), and
operate() methods, and discuss the differences between abstract classes and
interfaces based on this example.
Expected Output:
Problem Statement:
1. Write a program that accepts two integers from the user and performs division.
2. Implement exception handling to manage the following cases:
○ Division by zero (ArithmeticException)
○ Input mismatch when the user enters non-integer values
(InputMismatchException)
3. In the main method, display appropriate error messages if exceptions are caught, and
ensure the program continues to run.
Expected Output:
Objective: Learn how to use nested try-catch blocks for handling multiple exceptions.
Problem Statement:
Expected Output:
Problem Statement:
1. Write a program that asks the user to input their name, age, and favorite color, and then
displays this information.
2. Use Scanner for input and System.out.println for output.
Expected Output:
Problem Statement:
1. Write a program that reads a list of names from a file called input.txt and writes
these names to another file called output.txt, but in uppercase.
2. Implement proper exception handling for file-related errors, such as
FileNotFoundException and IOException.
Expected Output:
input.txt (Content):
John
Jane
Bob
output.txt (Content):
JOHN
JANE
BOB
Problem Statement:
1. Write a program that appends a new line of text to an existing file called notes.txt.
2. Ensure that if the file does not exist, it is created.
3. Implement exception handling for file-related errors.
Expected Output:
● Before appending:
Meeting at 10 AM
○
● After appending:
notes.txt:
Meeting at 10 AM
Don't forget the reports.
Problem Statement:
1. Write a program that reads an integer from a file called data.txt and performs division
with another integer entered by the user.
2. Handle the following exceptions:
○ FileNotFoundException if the file does not exist.
○ NumberFormatException if the file content is not a valid integer.
○ ArithmeticException for division by zero.
3. Implement a finally block to close the file resource if it was successfully opened.
Expected Output:
Problem Statement:
Expected Output:
Serialized Object:
Name: Alice, Age: 30
Deserialized Object:
● Deserialized Age: 30