Sei sulla pagina 1di 2

TCP1311 Object Oriented Programming

Lab 3
Week:4
Topic: Java Classes

In this lab session, you will develop the complete code for a Java program. The lab
exercises given below are based on the lecture notes on Introduction to Java, and are
intended to familiarize you with the concepts of class, objects, methods and access
modifiers.

1. Creating a class
Create a class Account with the following variables and methods:
Variables:

balance: Balance amount in an account (Type: double)


number: An account number (Type: int)
name: The name of the account holder (Type: String)

Constructor: Define a constructor to initialize the above variables with its arguments.
Methods:

credit(amount):

Adds amount to balance.

withdraw(amount):

Subtracts amount from balance.

printBalance(): Prints the balance amount with account number.


Compile this program. Note that this code is just a class definition, and is neither a Java
application nor an applet, and hence is not runnable.

2. Creating an application and a set of objects


Write a Java application Myprog to create a set of objects a1,a2,a3of the class
Account. For each object, define initial values for name, number and balance through the
class constructor. Call the various methods of class Account on these objects of the
class. Compile and run the program to verify the output.

3. Need for Restricted Access to Variables


Now, add the following lines to Myprog (inside main):
a3 = a1;
a3.balance=10000;

//***

a1.printBalance();

//print balance of

a1

(1) Even though the above code has apparently changed the balance amount of account
a3, you will notice that the balance amount of a1 is changed to 10000. Explain
why.
(2) How will you disallow direct modification of the balance amount such as that given
in line *** above?

4. Static Variables
Define a variable k inside the class Account which should contain the total number
of accounts (Account objects) created so far. Use this variable inside Myprog to print
the total number of accounts created.

5. Access Modifier for Constructor


Set the access to the constructor of Account to private. Compile Account.java and
Myprog.java.
1. Run Myprog, and notice that when the constructor is defined as private, you cannot
create any objects of the class.
2. Change the keyword for the constructor to static.

Note the compile-time error (A

constructor cannot be declared static).


New Question:
a)

Write an Emp class that has the following:


Two attributes called fname and lname of type String.
One attribute called sal of type int for salary.
One attributes called date of type Date that gives start date.
One default constructor that initialize the fname , lname with "unknown" and the
sal with 0.
5. One constructor that initializes the fname, lname, and sal with the argument
values.
6. A method setFirstName that sets the fname.
7. A method getFirstName that returns fname.
8. A method setLastName that sets lname.
9. A method getLastName that returns lname.
10. A method setSalary that sets the sal.
11. A method getSalary that returns the sal.
12. A method getStartDate that returns the start date.
13. A print method that prints the fname, lname, sal, and start.
b) Write an application class MyApp that creates two objects of the Emp class and prints
the results.
1.
2.
3.
4.

Potrebbero piacerti anche