Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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:
Constructor: Define a constructor to initialize the above variables with its arguments.
Methods:
credit(amount):
withdraw(amount):
//***
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.