ASSESSMENT – 1

Scenario #1:
Expected Understanding: Access Modifiers, Single Inheritance, getter methods, Constructor Overloading


1) Create a Class named “Trainer”.
– Have default instance variables String dept, institute
– Have private instance variable int salary
– Assign 10000 as value for salary.
– Create getter method for salary.
– Assign values – “Java”, “Payilagam” to them
– 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

Trainer Class:

public class Trainer
{

String dept = "Java";
String institute = "Payilagam";
private int salary = 10000;

Trainer()
{
}
	
Trainer(String course, String center)
{
dept = course;
institute = center;
}

public static void main(String[] args)
{
Trainer trainerKumar = new Trainer("CSE", "payilagam");
}

public int getSalary()
{
return this.salary;
}

public void training()
{
System.out.println("Online Training");
}

}

SQLTrainer:

public class SQLTrainer extends Trainer
{

SQLTrainer()
{
}

SQLTrainer(String a, String b)
{
super("CSE","Payilagam");
}

public static void main(String[] args)
{

SQLTrainer ram = new SQLTrainer();
SQLTrainer ramu = new SQLTrainer("SQL", "PAYILAGAM");

System.out.println("Trainer Dept: " + ram.dept);
System.out.println("Trainer Institute: " + ram.institute);

int earn = ram.getSalary();
System.out.println("Trainer Salary: " + earn);

ram.training();
}

}

Output:

Trainer Dept: Java
Trainer Institute: Payilagam
Trainer Salary: 10000
Online Training


Other way for above scenario:

Trainer Class:

public class Trainer
{

String dept = "Java";
String institute = "Payilagam";
private int salary = 10000;
	
Trainer(String course, String center)
{
dept = course;
institute = center;
}

public static void main(String[] args)
{
Trainer trainerKumar = new Trainer("CSE", "payilagam");
}

public int getSalary()
{
return this.salary;
}

public void training()
{
System.out.println("Online Training");
}

}

SQLTrainer Class:

public class SQLTrainer extends Trainer
{

SQLTrainer()
{
super("CSE","Payilagam");
}

public static void main(String[] args)
{

SQLTrainer ram = new SQLTrainer();

System.out.println("Trainer Dept: " + ram.dept);
System.out.println("Trainer Institute: " + ram.institute);

int earn = ram.getSalary();
System.out.println("Trainer Salary: " + earn);

ram.training();
}

}

Output:

Trainer Dept: CSE
Trainer Institute: Payilagam
Trainer Salary: 10000
Online Training

Code Explanation:

  • In Trainer class, we have created the trainerKumar object with two arguments.
    • And created the constructor for that.
  • In SQLTrainer class, we have created the ram object with zero argument.
    • Manually, we created the zero argument constructor for that.
    • Inside the zero arg constructor, we declared the superOf keyword with two String inputs.
    • superOf keyword called the parent class (Trainer Class) parameterized constructor.

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

Actor Interface:

public interface Actor
{

boolean makeupRequired = true;
String address = "Chennai";

void act();
void dance();
void sing();

}

ActorSivakumar Implementing Class:

public class ActorSivakumar implements Actor
{

static String address = "Coimbatore";

ActorSivakumar()
{
}

ActorSivakumar(int age, String car)
{
}

public static void main(String[] args)
{
ActorSivakumar as = new ActorSivakumar(65, "Audi Car");
as.act();
as.dance();
as.sing();
as.speaking();
System.out.println("Sivakumar living in: " + address);

Actor ac = new ActorSivakumar();
ac.act();
ac.dance();
ac.sing();
System.out.println("Actor living in: " + ac.address);
}

public void act()
{
System.out.println("Acting");
}

public void dance()
{
System.out.println("Dancing");
}

public void sing()
{
System.out.println("Singing");
}

public void speaking()
{
System.out.println("Sivakumar is speaking about Ramayanam");
}

}

Output:

Acting
Dancing
Singing
Sivakumar is speaking about Ramayanam
Sivakumar living in: Coimbatore
Acting
Dancing
Singing
Actor living in: Chennai

Code Explanation:

  • ActorSivakumar as = new ActorSivakumar(65, “Audi Car”);
    • Above code, object reference & class type both are same.
    • ActorSivakumar class implemented the Actor interface.
    • So, as object reference can access Actor interface own methods & ActorSivakumar own methods.
  • Actor ac = new ActorSivakumar();
    • Above code, Actor interface reference is pointing to ActorSivakumar implementing class.
    • Interface reference can only access the implemented methods those are abstract in interface.
    • Can’t access implementing class own methods.

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.

Smartphone Abstract Class:

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");
}

}

FactoryDemo Abstract Sub Class:

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");
}

}

Samsung Class:

class Samsung extends FactoryDemo
{
 
static int price = 5000;

public static void main(String[] args)
{
Samsung sam = new Samsung();
System.out.println("Price: " + sam.price);
sam.browse();
sam.call(10);
sam.sendMessage();
sam.receiveCall();
sam.verifyFingerPrint();
sam.providePattern();
}

int call(int seconds)
{
seconds = 10;
System.out.println("Outgoing calls 10 secs");
return seconds;
}

void sendMessage()
{
System.out.println("Message sent successfully");
}

void receiveCall()
{
System.out.println("Have received 10 calls");
}

void verifyFingerPrint()
{
System.out.println("Finger Print Verified");
}

void providePattern()
{
System.out.println("Lockpattern Provided");
}

}

Output:

Smartphone under development
Price: 5000
Factory Demo Browsing
Outgoing calls 10 secs
Message sent successfully
Have received 10 calls
Finger Print Verified
Lockpattern Provided

Code Explanation:

  • Samsung class is the child class of FactoryDemo class & grandchild of Smartphone class.
  • Samsung class provides the definitions for all abstract methods of FactoryDemo & Smartphone Class.
  • So, Samsung class object can access the property of FactoryDemo & Smartphone class.
  • FactoryDemo & Samsung both are have price variable. Samsung object access it’s own object variable. It overwrites the FactoryDemo price variable.

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.

India class:

abstract class India
{

static String capital = "New Delhi";

public India(String PrimeMinister)
{
System.out.println("Our Prime Minister is: " + PrimeMinister);
}

abstract void speakLanguage();
abstract void eat();
abstract void dress();

}

SouthIndia Class:

abstract class SouthIndia extends India
{

public SouthIndia()
{
super("Modi");
}

void cultivate()
{
System.out.println("Rice & Wheat Cultivation");
}

void livingStyle()
{
System.out.println("Average Development Inside");
}

}

TamilNadu Class:

class TamilNadu extends SouthIndia
{

static String capital = "Chennai";

public static void main(String[] args)
{
System.out.println("Capital of India: " + India.capital);
System.out.println("Capital of TamilNadu: " + TamilNadu.capital);

SouthIndia si = new TamilNadu();
System.out.println("South India can access this capital: " + si.capital);
si.speakLanguage();
si.eat();
si.dress();
si.cultivate();
si.livingStyle();
}

void speakLanguage()
{
System.out.println("Tamizh is a speaking language");
}

void eat()
{
System.out.println("Healthy vegetable food");
}

void dress()
{
System.out.println("Traditional Dress");
}

void cultivate()
{
System.out.println("Rice & Sugar cane Cultivation");
}

void livingStyle()
{
System.out.println("Above Average Development Inside");
}

}

Output:

Capital of India: New Delhi
Capital of TamilNadu: Chennai
Our Prime Minister is: Modi
South India can access this capital: New Delhi
Tamizh is a speaking language
Healthy vegetable food
Traditional Dress
Rice & Sugar cane Cultivation
Above Average Development Inside

Code Explanation:

  • India – Parent Abstract Class, SouthIndia – Child Abstract Class, TamilNadu – GrandChild Class.
  • TamilNadu class provided the implementation for all abstract methods of SouthIndia & India Class.
  • SouthIndia si = new TamilNadu();
    • SouthIndia object reference is pointing to TamilNadu Class.
    • Inside tha TamilNadu Class,
      • SouthIndia Class obj reference can access the TamilNadu class Methods which are overriding (cultivate & livingStyle methods).
      • Can’t access TamilNadu class own methods.
      • And also SouthIndia obj reference access India Class & South India class abstract methods which are implemented by TamilNadu class.

Doubt:

TamilNadu Class Program : 12th line

SouthIndia si = new TamilNadu();
System.out.println("South India can access this capital: " + si.capital);
  • India & TamilNadu both class have capital variable.
  • In output, SouthIndia object reference(si) access the India’s Capital variable.
  • In object creation, SouthIndia object reference(si) is pointing to TamilNadu Class.

Scenario #5:
Expected Understanding: Interface, access modifiers, Method Overriding


1) Create a package called tamilnadu.chennai
2) Create an interface ‘TrafficRulesChennai’ 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.

TrafficRulesChennai Interface:

package tamilnadu.chennai;
 
public interface TrafficRulesChennai
{
 
String trafficCommisioner = "Kavin";
 
void goByDieselVehicle();
void goByBicycle();
 
}

CommonManInChennai Implementing Class:

package tamilnadu.chennai;
 
public class CommonManInChennai implements TrafficRulesChennai
{
 
public static void main(String[] args)
{
CommonManInChennai chennaiMan = new CommonManInChennai();
chennaiMan.goByDieselVehicle();
chennaiMan.goByBicycle();   
}
 
public void goByDieselVehicle()
{
System.out.println("Go By Diesel Vehicle");
}
     
public void goByBicycle()
{
System.out.println("Go By Bicycle");
}
 
}

Output:

Go By Diesel Vehicle
Go By Bicycle

TrafficRulesDelhi Interface:

package india.newDelhi;
 
interface TrafficRulesDelhi
 
{
 
String trafficCommisioner = "Navin";
 
void dontGoByDieselVehicle();
void goByBicycle();
 
}

CommonManInDelhi Implementing Class:

package india.newDelhi;
import tamilnadu.chennai.TrafficRulesChennai;
class CommonManInDelhi implements TrafficRulesDelhi, TrafficRulesChennai
{
 
public static void main(String[] args)
{
CommonManInDelhi delhiMan = new CommonManInDelhi();
delhiMan.dontGoByDieselVehicle();
delhiMan.goByBicycle();
delhiMan.goByDieselVehicle();
System.out.println(TrafficRulesDelhi.trafficCommisioner);
System.out.println(TrafficRulesChennai.trafficCommisioner);
}
 
public void dontGoByDieselVehicle()
{
System.out.println("Don't Go By Diesel Vehicle");
}
 
public void goByBicycle()
{
System.out.println("Go By Bicycle");
}
 
public void goByDieselVehicle()
{
System.out.println("Go By Diesel Vehicle");
}
 
}

Output:

Don’t Go By Diesel Vehicle
Go By Bicycle
Go By Diesel Vehicle
Navin
Kavin

Code Explanation:

  • By default, interface methods are public & abstract.
  • We do not need to mention it in interface.
  • Inside the class, we must declare the public keyword in implemented methods signature.

Leave a comment

Design a site like this with WordPress.com
Get started