Java class add method to display field values

Description

Java class add method to display field values


class Account {/*from www  .j a va 2 s  .c  o m*/
    String name;
    String address;
    double balance;

    public void display() {
       System.out.print(name);
       System.out.print(" (");
       System.out.print(address);
       System.out.print(") has $");
       System.out.print(balance);
    }
}


public class Main {

    public static void main(String args[]) {
        Account myAccount = new Account();
        Account yourAccount = new Account();

        myAccount.name = "Java";
        myAccount.address = "222 Main Lane";
        myAccount.balance = 24.02;

        yourAccount.name = "Javascript";
        yourAccount.address = "111 Main Street";
        yourAccount.balance = 42.42;

        myAccount.display();
        System.out.println();
        yourAccount.display();
    }
}



PreviousNext

Related