ShaikhInamulHasan Java Day5
ShaikhInamulHasan Java Day5
1) Java Interfaces:
a) Create an interface Vehicle which contains the methods brake() and run()
b) Create another interface Fuel which contains the following methods:
- fill(String type, int quantity)
- pay(int quantity, int price)
c) Create a subclass Car which implements both the interfaces Vehicle and
Fuel
- Here brake() method should print "Car Applies Power brake"
- run() method should print "Car is running on 4 wheels"
d) Similarly fill() method can print the the type and quantity of the fuel filled
- For example: 10 Litres of Petrol
e) pay() method can be used to print the price to be paid
- For example: Pay Rs.640
f) Create another subclass Bike which again implements both the
interfaces Vehicle and Fuel
- Here brake() method should print "Bike Applies hand brake"
- run() method should print "Bike is running on 2 wheels"
g) Next, implement the fill() and pay() methods as explained earlier
h) Finally create a Demo class containing the main() method to verify the
results.
Code:
Output:
2) Static Variables:
a) Design a class CarService to represent a Car Service Station
b) This class should contain variables to represent the following details
- Name of the Service Station
- Car make, model and regno - which are in for service
- No. of Cars in for Service
c) Identify the instance variables and static variables
d) Declare them using suitable keywords
e) Define a constructor to initialise the values for Car make, model and regno
f) Define a method show() to print the values of all the variables
g) Also create a Demo class containing the main method to verify the results
- Create a few objects of CarService
- Invoke the show() method using these objects
- Also access the static variables directly using the class name.
Code:
Output:
3) Static Methods:
a) Design a class CarService to represent a Car Service Station
b) This class should contain variables to represent the following:
- Name of the Service Station
- Car make, model, regno and status
- No. of Cars in for Service
- No. of Cars out after Service
c) Define a method service(Car c) which updates the status to "out" and
accordingly modify the values for
- No. of Cars in for Service
- No. of Cars out after Service
d) Define a method show() to print the values of all the variables
e) Identify the Instance variables and Static variables
f) Identify the Instance methods and Static methods
g) Define a constructor to initialise the values for Car make, model, regno and
status
h) Also create a Demo class containing the main method
- Create a few objects of CarService
- Invoke the service() method on some of them
- Invoke the show() method using all the objects and verify the results.
Code:
Output:
4) Static Blocks:
a) Design a class CarService to represent a Car Service Station
b) This class should contain variables to represent the following:
- Name of the Service Station
- Car make, model, regno and status
- No. of Cars in for Service
- No. of Cars out after Service
c) Identify the instance variables and static variables
d) Define a constructor to initialize the values for instance variables
e) Define a static block to initialize the values of static variables
f) Also create a Demo class containing the main method
- Create a few objects of CarService
- Invoke the show() method using all the objects and verify the results.
Code:
Output:
5) Exception Handling:
a) Learn about another Runtime Exception called NullPointerException
b) Refer to the java program named Demo.java provided below
c) An exception will be raised when you run this code
d) Identify the code which is responsible for the exception
e) Rectify it using a try-catch block
Demo.java
public class Demo {
public static void main(String[] args) {
String s=null;
int k;
k=s.length();
System.out.println(k);
System.out.println("I am here");
}
}
Code:
Output:
Code:
Output:
6) Custom Exceptions:
Code:
Output: