Skip to main content
eick-at moodle
  • Home
  • Calendar
  • More
You are currently using guest access
Log in
eick-at moodle
Home Calendar
Expand all Collapse all
  1. OOP with JAVA
  2. Basics of object-oriented programming with Java
  3. Creation of further objects

Creation of further objects

Completion requirements
Opened: Saturday, 4 May 2019, 12:00 AM
Due: Saturday, 4 May 2019, 12:00 AM
The following program should be extended by further objects:

 1 public class Account {                      // Definition of class
 2     private double balance;                  // encapsulated attripbute (private)
 3 
 4     public double getBalance() {             // public definitions of methods (accessibla from everywhere)
 5         return balance;
 6     }
 7     public double transaction(double amount) {
 8         if (balance + amount >= 0) {        // Transaction is possible
 9             balance += amount;
10             return balance;
11         }
12         else                                // transaction impossible
13             return -1;                      // return of an error code
14     }
15 }
 1 public class Main {
 2 
 3     public static void main(String[] args) {
 4 
 5         Account account1 = new Account();  // Creation of object account1 of class Account
 6         account1.transaction(100);         // Invokation of method transaction() of object account1
 7 
 8         System.out.println("Balance: " + account1.getBalance());
 9     }
10 }
Two additional accounts account2 and account3 are to be set up:


UML class diagram with associated object diagram

UML class diagram with associated object diagram

(In the object diagram, the methods are not drawn because all objects of the class have them and they have already been listed in the class diagram.)

You are currently using guest access (Log in)
Data retention summary
Get the mobile app
Powered by Moodle