Scenario #1:
Expected Understanding: Access Modifiers, Single Inheritance, getter methods, Constructor Overloading
1) Create a Class named “Trainer” with main method. – Have default instance variables String dept, institute – Assign values – “Java”, “Payilagam” to them – Have private instance variable int salary – Assign 10000 as value for salary. – Create getter method for salary. – Have instance method training() with void as return data type – Add a print statement inside training() method – Have instance named as ‘trainerKumar’ and pass “CSE”, “payilagam” as arguments to it. – Handle above line with matching Constructor.
2) Create a sub class “SQLTrainer” under “Trainer”. – Have main method in it. – Create instance ram for this class – Handle with proper super class constructor – Access parent class instance variables – Call parent class instance method training() – Access salary using getter method in parent class
class Trainer
{
static String name = "Trainer Kumar";
String dept = "Java";
String institute = "Payilagam";
private int Salary = 1000;
int getSalary()
{
return this.Salary;
}
Trainer(String name)
{
this.name=name;
System.out.println("Name of the trainer is "+this.name);
}
Trainer(String dept,String institute)
{
this(name);
this.dept=dept;
this.institute=institute;
System.out.println("Name of the department is "+this.dept);
System.out.println("Name of the institute is "+this.institute);
}
public static void main(String[] args)
{
//Trainer trainerKumar1 = new Trainer("trainerKumar");
Trainer trainerKumar = new Trainer("CSE","Payilagam");
trainerKumar.training();
}
public void training()
{
System.out.println("Name of the method is "+"trainer method");
}
}
Output:
Name of the trainer is Trainer Kumar
Name of the department is CSE
Name of the institute is Payilagam
Name of the method is trainer method
public class SQLTrainer extends Trainer
{
static String name = "Trainer ram";
String dept = "SQL";
String institute = "Payilagam";
//public SQLTrainer(){
//super(name);
//this.name=name;
//this.dept=dept;
//this.institute=institute;
//System.out.println("Name of the trainer is"+this.name);
//System.out.println("Name of the department is"+this.dept);
//System.out.println("Name of the institute is"+this.institute);
//}
public SQLTrainer(){
super(name);
this.name=name;
this.dept=dept;
this.institute=institute;
System.out.println("Name of the trainer is"+this.name);
System.out.println("Name of the department is"+this.dept);
System.out.println("Name of the institute is"+this.institute);
}
public static void main(String[] args)
{
SQLTrainer ram = new SQLTrainer();
ram.training();
int Salary = ram.getSalary();
System.out.println(Salary);
}
}
Output:
Name of the trainer is Trainer ram
Name of the trainer isTrainer ram
Name of the department isSQL
Name of the institute isPayilagam
Name of the method is trainer method
1000
Scenario #2:
Expected Understanding: Interface, Class, static variables, dynamic binding
1) Create an interface called ‘Actor’ – Have variables boolean makeUpRequired – Assign ‘true’ value for ‘makeUpRequired’ – Have variable String address. – Assign value as “Chennai” to address. – Have void methods act(), dance(), sing()
2) Create class named as ActorSivakumar with main method – implement interface ‘Actor’ to this class. – Give your own definitions for methods from interface – Have static variable String address. – Assign value to address as “Coimbatore”. – Have instance method ‘speaking()’ with void return data type. – Create instance for ActorSivakumar as below ActorSivakumar as = new ActorSivakumar(65, “Audi Car”) – Handle with proper Constructor – Access all the methods from ActorSivakumar class – Access variable address and print the value – Create another instance of interface ‘Actor’ using dynamic binding approach Actor ac = new Sivakumar(); – Handle with proper Constructor – Access methods in ActorSivakumar class. – Access variable address using ‘ac’ intance and print the value – Observe and note down the difference between two instances
interface Actor
{
String address = "Chennai";
boolean makeupRequried = true;
void act();
void dance();
void sing();
//if(makeupRequried)
//{
//System.out.println("Makeup is requried");
//}
}
public class ActorSivakumar implements Actor
{
static String address = "Coimbatore";
int model;
String name;
ActorSivakumar(int model,String name)
{
this.model=model;
this.name=name;
System.out.println("ActorSivakumar model is "+ this.model);
System.out.println("ActorSivakumar name is "+ this.name);
}
ActorSivakumar()
{
System.out.println("constructor for dynamic binding");
}
public static void main(String[] args)
{
ActorSivakumar as = new ActorSivakumar(65, "Audi Car");
Actor ac = new ActorSivakumar();
System.out.println("ActorSivakumar address is "+ address);
as.speaking();
as.act();
as.dance();
as.sing();
//ac.speaking();
ac.act();
ac.dance();
ac.sing();
System.out.println("Actor address is "+ac.address);
}
public void speaking()
{
System.out.println("He is a speaker");
}
public void act()
{
System.out.println("He is a actor");
}
public void dance()
{
System.out.println("He is a dance");
}
public void sing()
{
System.out.println("He is a singer");
}
}
Output:
ActorSivakumar model is 65
ActorSivakumar name is Audi Car
constructor for dynamic binding
ActorSivakumar address is Coimbatore
He is a speaker
He is a actor
He is a dance
He is a singer
He is a actor
He is a dance
He is a singer
Actor address is Chennai
Scenario #3:
Expected Understanding: Abstraction, Inheritance, return keyword, Method Arguments, Constructor
1) Create an abstract class named ‘SmartPhone’ – Add the below abstract methods – int call(int seconds) – void sendMessage() – void receiveCall() – Add non abstract method void browse() – print ‘SmartPhone browsing’ inside browse() method definition – Have constructor as below.
public SmartPhone() { System.out.println(“Smartphone under development”); }
2) Create class called ‘FactoryDemo’ as abstract subclass of SmartPhone – Add the below abstract methods – void verifyFingerPrint() – void providePattern() – Add non abstract method void browse() – print ‘Factory Demo browsing’ inside browse() method definition – Add variable boolean isOriginalPiece and assign ‘false’ as value. – Add static variable int price and set value as 0.
3) Create class called ‘Samsung’ with main method as sub class of FactoryDemo. – Add unimplemented methods – Add static variable int price and set value as 5000. – Create instance for Samsung class called sam – Access browse() method using sam instance. – Access price variable using sam instance. – Observe which method is called and note down.
abstract class SmartPhone
{
public SmartPhone()
{
System.out.println("Smartphone under development");
}
abstract int call(int seconds);
abstract void sendMessage();
abstract void receiveCall();
void browse(){
System.out.println("SmartPhone browsing");
}
abstract class FactoryDemo extends SmartPhone
{
boolean OriginalPiece = false;
static int price = 0;
abstract void verifyFingerPrint();
abstract void providePattern();
void browse()
{
System.out.println("Factory Demo browsing");
}
}
class Samsung extends FactoryDemo
{
static int price = 5000;
public static void main(String[] args){
Samsung sam = new Samsung();
System.out.println("Samsung price is "+sam.price);
sam.browse();
sam.sendMessage();
sam.receiveCall();
sam.verifyFingerPrint();
sam.providePattern();
int result= sam.call(30);
System.out.println(result);
}
int call(int seconds)
{
//int seconds1=2,seconds2=2;
//return seconds1+seconds2;
return(seconds);
}
void sendMessage(){
System.out.println("sendMessage");
}
void receiveCall(){
System.out.println("receiveCall");
}
void verifyFingerPrint(){
System.out.println("verifyFingerPrint");
}
void providePattern(){
System.out.println("providePattern");
}
}
Output:
Smartphone under development
Samsung price is 5000
Factory Demo browsing
sendMessage
receiveCall
verifyFingerPrint
providePattern
30
Scenario #4:
Expected Understanding: Abstraction, Inheritance, Dynamic Binding, Polymorphism (Overriding), Constructor Overloading
1) Create an abstract class called ‘India’ – Have below abstract methods – void speakLanguage() – void eat() – void dress() – Have static variable String capital = “New Delhi” – Have below Constructor public India(String primeMinister) { System.out.println(“our Prime Minister is” + primeMinister); }
2) Create an abstract class called ‘SouthIndia’ – Make this class as sub class of ‘India’ – Add below non abstract methods – void cultivate() – Print ‘Rice and Wheat cultivation’ inside this method – void livingStyle() – Print ‘Average development’ inside this method
3) Create a class called ‘TamilNadu’ with main method as sub class of ‘South India’. – Add unimplemented methods – Provide your own definitions wherever necessary. – Have static variable String capital = “Chennai” – Add below non abstract methods – void cultivate() – Print ‘Rice and Sugar cane cultivation’ inside this method – void livingStyle() – Print ‘Above Average development’ inside this method – Using class name “India” – access variable ‘capital’ and print the value – Using class name “TamilNadu” – access variable ‘capital’ and print the value. – Create instance for “SouthIndia” as below SouthIndia si = new TamilNadu() – Observe which methods and variables can be accessed using ‘si’ and note down.
abstract class India
{
static String capital = "New Delhi";
public India(String primeMinister)
{
System.out.println("our Prime Minister is" + primeMinister);
}
public India()
{
System.out.println("Prime Minister");
}
abstract void speakLanguage();
abstract void eat();
abstract void dress();
}
abstract class SouthIndia extends India
{
void cultivate()
{
System.out.println("Rice and Wheat cultivation");
}
void livingStyle()
{
System.out.println("Average development");
}
}
class TamilNadu extends SouthIndia
{
static String capital = "Chennai";
public static void main(String[] args){
SouthIndia si = new TamilNadu();
si.cultivate();
si.livingStyle();
si.speakLanguage();
si.eat();
si.dress();
System.out.println("Capital of India is"+India.capital);
System.out.println("Capital of TamilNadu is"+TamilNadu.capital);
}
void cultivate(){
System.out.println("Rice and Sugar cane cultivation");
}
void livingStyle(){
System.out.println("Above Average development");
}
void speakLanguage()
{
System.out.println("Language is tamil");
}
void eat()
{
System.out.println("Traditionally eat at bannana leaf");
}
void dress(){
System.out.println("Traditional dress is dhothi and sarees");
}
}
Output:
Prime Minister
Rice and Sugar cane cultivation
Above Average development
Language is tamil
Traditionally eat at bannana leaf
Traditional dress is dhothi and sarees
Capital of India is New Delhi
Capital of TamilNadu is Chennai
Scenario #5:
Expected Understanding: Interface, access modifiers, Method Overriding
1) Create a package called tamilnadu.chennai
2) Create an interface ‘TrafficRules’ under this package
3) Make sure this interface is public interface – Add variable String trafficCommisssioner = “Kavin”; – Add below methods – void goByDieselVehicle(); – void goByBicycle();
4) Create class called ‘CommonManInChennai’ with main method in the same package tamilnadu.chennai – Implement interface ‘TrafficRulesChennai’ – Create instance for this class and access all the methods
5) Now, create another package called ‘india.newDelhi’
6) Create an interface ‘TrafficRulesDelhi’ under this package
7) Make sure this interface is not public interface – it should be default interface – Add variable String trafficCommisssioner = “Navin”; – Add below methods – void dontGoByDieselVehicle(); – void goByBicycle();
8) Create class called ‘CommonManInDelhi’ with main method in the same package india.newDelhi – Implement interface ‘TrafficRulesDelhi’ – Create instance for this class and access all the methods – Now, implement interface ‘TrafficRulesChennai’ also. – Add unimplemented methods – Access all the methods and observe the difference.
package tamilnadu.chennai;
public interface TrafficRules
{
String trafficCommisssioner = "Kavin";
void goByDieselVehicle();
void goByBicycle();
}
package tamilnadu.chennai;
public class CommonManInChennai implements TrafficRules
{
public static void main(String[] args){
CommonManInChennai cmc = new CommonManInChennai();
cmc.goByDieselVehicle();
cmc.goByBicycle();
}
public void goByDieselVehicle(){
System.out.println("goByDieselVehicle");
}
public void goByBicycle(){
System.out.println("goByBicycle");
}
}
Output:
goByDieselVehicle
goByBicycle
package india.newDelhi;
interface TrafficRulesDelhi
{
String trafficCommisssioner = "Navin";
void dontGoByDieselVehicle();
void goByBicycle1();
}
package india.newDelhi;
import tamilnadu.chennai.TrafficRules;
public class CommonManInDelhi implements TrafficRulesDelhi,TrafficRules
{
public static void main(String[] args){
CommonManInDelhi cmd = new CommonManInDelhi();
cmd.dontGoByDieselVehicle();
cmd.goByBicycle();
cmd.goByDieselVehicle();
cmd.goByBicycle1();
}
public void dontGoByDieselVehicle()
{
System.out.println("dontGoByDieselVehicle");
}
public void goByBicycle()
{
System.out.println("goByBicycle");
}
public void goByDieselVehicle(){
System.out.println("goByDieselVehicle1");
}
public void goByBicycle1(){
System.out.println("goByBicycle1");
}
}
Output:
dontGoByDieselVehicle
goByBicycle
goByDieselVehicle1
goByBicycle1
Leave a comment