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

ShaikhInamulHasan Java Day5

This document discusses various Java programming concepts across 6 sections: 1) Java interfaces and implementing interfaces in subclasses. 2) Static variables and accessing them using class names. 3) Static methods and identifying instance vs static methods. 4) Static blocks for initializing static variables. 5) Exception handling using try-catch blocks to handle NullPointerExceptions. 6) Creating a custom exception class and validating conditions to throw the exception.

Uploaded by

Ashish Patel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

ShaikhInamulHasan Java Day5

This document discusses various Java programming concepts across 6 sections: 1) Java interfaces and implementing interfaces in subclasses. 2) Static variables and accessing them using class names. 3) Static methods and identifying instance vs static methods. 4) Static blocks for initializing static variables. 5) Exception handling using try-catch blocks to handle NullPointerExceptions. 6) Creating a custom exception class and validating conditions to throw the exception.

Uploaded by

Ashish Patel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Spoken Tutorial Java Programming (Day 5)

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:

a) Create a custom exception class InvalidAgeException


b) Create another class Age and create a constructor to initialize the value of age
c) Create validate() method to throw the above exception if age < 18
d) Create objects inside main method and invoke the validate() method
e) Provide exception handling using try-catch blocks
f) Verify the custom exception class.

Code:
Output:

You might also like