Core Java – Assignments (JavaSE)
Session Name Assignment Details
Introduction to Classes and Members Part-I: Assignment-I
Create a new Class Customer
Create 2 Class Members - custId and
custName
Add new method print()
Create instance of the class and
invoke print method
Assignment-II
Create total 4 member variables for
Customer class
Assign public, private, protected and
default access modifiers respectively
to these variables
Try to access these variable from
other classes
Introduction to Classes and Members Part-II Assignment-I
Define a static variable custCount.
This contains total count of customers
Create 2 instances of Customer object
Increment the custCount static
variable and print its value after every
increment
Access custCount using class name as
well as object name
Assignment-II
Define a final variable
MAXPHONES. This contains
maximum allowable phones for a
customer
Initialize MAXPHONES
Try to initialize in the constructor
Access it using class name and object
name
Introduction to Methods Assignment-I
Define a getter method for empId
variable of Employee class
Define setter method for empID
variable taking int parameter
Define a print method which prints
the empID
Assignment-II
Define a static variable empCount for
Employee class
Define a static method to increment
the empCount variable
Invoke increment() method using
class name
Invoke increment() method using
object name
Assignment-III
Define a print method with variable
length argument list of type String
Print the contents of the argument list
using for loop
Constructors Assignment-I
Create Employee class
Create instance of Employee class
using default constructor. Does it
execute?
Now define default constructor, print
a message inside it and execute the
program
Try to add one more constructor
taking empID and empName as
parameters. Create another object of
Employee using this second
constructor. Does the program
execute?
Remove the default constructor
definition. Does the program execute?
Assignment-II
Define a private constructor for
Employee class
Define a protected constructor for
Employee class
Invoke both private and protected
constructors from the main method.
Does the program execute?
Invoke both private and protected
constructors from the subclass say
ContractEmp. Does the program
execute?
Remove the public constructor for
Employee class. How will another
class create instance of Employee
class?
Inheritance Part-I Assignment-I
Create a subclass Contractor for base
class Employee
Define a constructor for Contractor.
Can you initialize all variables
defined in the base class?
Create instance of subclass
Contractor. Are base class
constructors accessible?
Invoke Contractor's constructor.
Which all constructors are getting
invoked?
Define a variable in Contractor
matching its definition in the base
class Employee. Is that allowed?
Inheritance Part-II Assignment-I
Implement equals method in
Employee class
Validate that the parameter to equals
method is instance of Employee
Perform comparison on empName
Test that equals method returns true
for two different objects having same
empName
Polymorphism Part-I Assignment-I
Create base class Customer and
subclasses SilverCustomer and
GoldCustomer
Define discount() method in
Customer class whcih returns 20%
discount
Overload discount method in the
subclasses and return different
discount value
Define base class variable as
"Customer cust"
Assign different objects of Customer,
SilverCustomer and GoldCustomer to
variable cust one after other and
invoke discount method each time.
What is the discount % returned each
time?
Assignment-II
In the earlier assignment, how will
you access the discount() method of
Customer class from SilverCustomer
and GoldCustomer classes?
Polymorphism Part-II Assignment-I
Continue earlier assignment in
Polymorphism Part-I
Define another discount method in
Customer class taking String location
as parameter. Can it be invoked?
Can this method be invoked from
SilverCustomer or GoldCustomer
classes?
Try to overload this discount(String)
method in SilverCustomer class. Try
to invoke it. Does the program run?
Assignment-II
Continue earlier assignment
Define a new method getCustomer.
This should return instance of
Customer or SilverCustomer or
GoldCustomer class randomly.
Main method will call getCustomer
and then invoke the discount()
method on the object received.
Execute and observe the results.
Now create a new PlatinumCustomer
class extending Customer class.
Implement discount() method in this
class
How do you invoke discount method
of PlatinumCustomer class without
changing main method?
Abstract Classes Assignment-I
Create abstract class Car
Define an abstract method ignition()
Define a non-abstract/normal method
changeGear(). Is this allowed?
Create concrete classe Sedan.
Overload ignition methods
Create instance of Sedan and invoke
ignition() and changeGear() methods.
Does the program execute?
Define a variable noOfWheels in Car
class. Can it be accessed in Sedan
class?
Assignment-II
Continue earlier assignment
Create an abstract class SUV
extending Car class. Do you need to
implement all abstract methods of Car
class here?
Define method 4WheelDrive() in
SUV class
Create a concrete class Safari
extending SUV class. Which methods
must be implemented here?
Interfaces Assignment-I
Create a new interface to develop a
simple Calculator
Define two methods sum and divide
in the interface. Can you implement
the methods inside the interface?
Create a new class which implements
above interface. Do you need to
implement both the methods here?
Define a variable inside calculator
interface? Can it be invoked from
calculator implementation?
Now define one more interface
ScientificCalculator and add couple
of methods
Can you implementation class
implement this new interface as well?
Try it.
Assignment-II
Continue earlier assignment
Define a variable whose type is an
interface for the calculator
Assign an object of calculator
implementation
Invoke methods defined by the
interface using the interface reference.
Does the program execute?
*Sample Programs to get Concept over the core java