Skip to main content

Java Objects - Basics!!!!

How to create and "use" an object in Java 

In the following tutorial, I will explain how to create an Object in JAVA, and how you can "use" this object from a "tester" program...

The object described is a cashRegistrer System in a Supermarket. Multiple instances of cashRegisters exist, each one of them has its own balance (money inside the machine).  This is an instance variable.
The only operation the machines perform is to put money inside (and increase the balance).
This operation is called payAmount and needs two inputs :
        1) the amount of money the customer needs to pay, for example: 5.75 (amountPayable)
        2) the amount of money that the customer gives to the cashier, for example: 10  (amountGiven)
the same operation returns a value, which is the change that the cashier should give to the customer.
if the amount of money the customer gives to the cashier  (amountGiven)  is less than the required amount (amountPayable) the operation should return a negative number that represents how much more money the customer should pay.

A "global" static variable should keep tract of the money inside ALL instances of cashRegisters (totalBalance). This is called a class variable.
_________________________________________________________________________________
After the object is created a TesterProgram will instantiate 3 cashRegisters: A, B and C.
We will use the following scenario for our tester:

A: starts with 100 euros in the register.
B: starts with 250 euros in the register.
C: starts with 0 euros in the register.

  • Someone has to pay in cashRegister A the amount  7,86 and gives a 20 euro bill. Print the change the cashier should give.
  • Someone has to pay in cashRegister B the amount  22 and gives a 50 euro bill. Print the change the cashier should give.
  • Someone has to pay in cashRegister C the amount 18 and gives a 20 euro bill. Print the change the cashier should give... (be careful,  the object code needs modification... the case that the register does not have change is not covered)


At the end display the balance of each cash register and the totalBalance of all cashRegisters.

The full program source follows:

Enjoy!!!!



public class cashRegister {

  private double balance; /*this attribute is an instance variable, so each and every cashRegister will have its own balance */  
  
  private static double totalBalance=0; /* this attribute belongs to the cashRegister class, shared among cashRegister instances */
                            
/* A constructor method executes every time an object is instantiated.. look at the following line of the UseCashRegisters  program : 
cashRegister A = new cashRegister(100);  --> the single parameter constructor is called       
       or   
cashRegister C = new cashRegister();    --> the no-args constructor is called */
  
  //no-args constructor
    public cashRegister () {
        balance=0;
        }
    //constructor with parameters
    public cashRegister (double InitialMoney) {
        balance = InitialMoney;
        totalBalance += balance;
        }


    public double getBalance() {
        return balance;
    }

    public static double getTotalBalance() {
        return totalBalance;
    }

    public double payAmount (double amountPayable, double amountGiven) {
        if (amountGiven < amountPayable) {
            return amountPayable - amountGiven;
            }
        else {
            balance += amountPayable;
            totalBalance += amountPayable;
            return amountGiven - amountPayable;
            }
        }
}

***************************************************************
public class UseCashRegisters {
    public static void main(String[] args) {
        cashRegister A = new cashRegister(100);
        cashRegister B = new cashRegister(250);
        
        System.out.println(A.getBalance());
        System.out.println(B.getBalance());
        
        System.out.println("Change: " + A.payAmount(7.86, 20));
        System.out.println("Change: " + B.payAmount(22, 50));
        System.out.println(cashRegister.getTotalBalance());
    }
}

Comments

Popular posts from this blog

How to create your Java Executable...

What is a package? A package is a collection of related classes... (physically in your hard-disk is a directory/folder) all files under the package will be compressed together in a jar file...(a java archive like zip) Your java application may contain one or more packages to logically group functionality. When you deploy your application you create one jar file per project . In this jar file you must specify which is the java class file that contains the main class (the starter class) of your project. Follow the instructions on the screen-cam to create your java "executable".
How to Fix Divi 3 Color-Picker not showing HEX colour textbox I installed Divi   (3.0.40)  template on my Wordpress  (4.9.5)  site  yesterday to test it... and I immediately run into a problem... Choosing a colour from a colour palette did not display the textbox to type in the colour hex code...   So go and edit  /wp-content /  themes  /  Divi  /  css  / theme-customizer-controls-styles.css around line 69 find wp-picker-container and add the text in red: .wp-picker-container input[type=text].wp-color-picker, .wp-core-ui .wp-picker-default, .wp-core-ui .wp-picker-clear { width: 45% !important; vertical-align: bottom; margin-top: 10px; display:inline !important ; }