Sie sind auf Seite 1von 3

Read Me:-

i. Before going through below exercises please visit the


link given below, where you can experience the coding standard
that each and every developer should follow.
ii. This Code Conventions for the Java Programming Language document
contains the standard conventions that Sun follow and recommend that
we should follow. It covers filenames, file organization, indentation,
comments, declarations, statements, white space, naming conventions,
programming practices and includes a code example.
iii. LINK -
http://www.oracle.com/technetwork/java/javase/documentation/cod
econventions-135099.html
OOPS LAB EXERCISE STAGE-2
1. Array with Objects…
i. Suppose There are 20 students in a College . All Students
have their unique rollNumber and name, and collegeName is
same for all the Students.
ii. Write a program (which will make your program efficient) for
the above scenario.
iii. Initialize the properties rollNumber and name of each student
and college name with corresponding values.
2. Experience Encapsulation…
i. Write a program to create ‘StudentInfo’ class having instance
variables as follows
rollNo is a Integer
name is a String
course is a String
dateOfBirth is a String
mobileNo is a String
contactNO is a String
ii. Visibility of all the above variables should be private.
iii. Create getter and setter methods for each instance variable like.
iv. For rollNo instance variable getters and setters are follows
public int getRollNo(){
return this.rollNO;
}
public void setRollNo(int rollNo){
this.rollNO = rollNo;
}
Copyright © 2013 Computronics System India pvt ltd. All rights reserved.
v. Create another class in which create an array of StudentInfo class
having size 5.Put some appropriate values inside them using
setter methods of those and print these values in console using
getter methods.
3. A Class that Demonstrates Shadowing…
i. Write a program to create a class ‘ShadowApp’ having a static
variable ‘x’ of int type.
ii. This class implements main () method. Inside main method do the
following.
a. First initialize the static variable ‘x’ with value 5.
b. Then print the value of x with some appropriate message.
c. Now declare another variable with same name ‘x’ inside main
method.
d. Now assign value 10 to the variable x.
e. Then print the value of x with some appropriate message.
f. Now the print the value of ShadowApp.x in the console with
some appropriate message.
Expected output :
x=5
x = 10
ShadowApp.x = 5
4. OOPS implementation…
i. Implement a public class HdfcBank which inherits another public
class GenericBank.
ii. Create one more public class Test.java which has the main
method which is used for creating reference variables and calling
member methods on the instance variables.
iii. See if you can declare a reference variable of super class which
references to the subclass object, like
GenericBank hb = new HdfcBank()
iv. Also declare one more instance variable
as GenericBank hb1 = new GenericBank()
v. Add a method public void welcome (which prints Welcome) in the
GenericBank class.
vi. Are you able to call the super class method using the hb and hb1
reference variables? What happens at runtime?
vii. Add the method public void welcome (prints Welcome to HDFC
Bank) in the HdfcBank class. Call the method Welcome using the
hb and hb1 instance variables.
viii. Have you now got an idea of how runtime polymorphism works?

Copyright © 2013 Computronics System India pvt ltd. All rights reserved..
ix. Create one more class SocialBank, try to inherit the HdfcBank
class from GenericBank and SocialBank classes.
x. Why that is not allowed. Is it the diamond problem?
xi. Add two interfaces WithdrawContract and DepositContract. Are
you able to implement these interfaces from HdfcBank class?
xii. Add an instance variable bankbalanceto the GenericBankclass.
Will you make that variable private, protected, default or public?
xiii. Add a method public void deposit (int amount) in the
GenericBank class. Call this method using the hb instance
variable.
xiv. Add a method public void withdraw (int amount) in the
GenericBank class. Call this method using the hb instance
variable.
xv. Add a method public void checkbalance (int amount) in the
GenericBank class. Call this method using the hb instance
variable.
xvi. Add more versions of these methods as listed below as the amount
can be in float numbers also:
public void deposit (double amount) ,public
void withdraw (double amount) ,public
void checkbalance (double amount)
xvii. Is it also a form of polymorphism? What is the name given to such
methods?
xviii. Can you guess the benefit of adding the methods deposit,
withdraw and checkbalance? You could also have made the
bankbalance instance variable as public and performed various
arithmetic operations by accessing that variable from anywhere? Is
this data hiding?
xix. The Test class simulates the customer of the bank. Can you think of
a situation where the customers have to manage the workflow of the
bank?
xx. Is the use of HdfcBank class for maintaining balance and providing
operations on the account provides an abstraction for the customer?
(The code in HdfcBank could also have been written in the Test
class itself).

ALL THE BEST

Copyright © 2018 Computronics System India pvt ltd. All rights reserved.

Das könnte Ihnen auch gefallen