Results 1 to 2 of 2
- 01-22-2011, 10:10 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
Help using a method from another class
So I've got three class files, each named: Saldo.java, Person.java and SaldoDemo.java
So I've created an Object from the Person class in SaldoDemo, but how do I then use a method called "withdrawal" from Saldo on this Object?
Person.java
Saldo.javaJava Code:package test; public class Person { private String name; private Saldo Saldot; private Saldo withdraw; public Person(String initialName, Saldo initialSaldo) { name = initialName; Saldot = initialSaldo; } public Person(Person original) { if (original == null) { System.out.println("Fatal error. 1"); System.exit(0); } name = original.name; } public String toString() { return (name + " has " + Saldot + " euro."); } public boolean equals(Person otherPerson, Saldo otherSaldo) { if (otherPerson == null) return false; else return (name.equals(otherPerson.name)); } public void setName(String newName) { name = newName; } }
SaldoDemo.javaJava Code:package test; public class Saldo { private double Saldot = 0; private double withdraw; private double inSet; public Saldo(double Saldot) { System.out.println("Your Saldo is currently: " + Saldot + " euros."); } public void set(double newSaldo) { Saldot = newSaldo; } public void inSet(double inSet) { Saldot = Saldot + inSet; System.out.println("Registrated. 1"); } public void withdrawal(double withdraw) { Saldot = Saldot - withdraw; System.out.println("Registrated. 2"); } public void setSaldo(double Saldot) { this.Saldot = 0; } @Override public String toString() { return ("" + Saldot); } public void set(Person newName, double newSaldo) { name = newName; Saldot = newSaldo; } }
Java Code:package test; import java.util.Scanner; public class SaldoDemo { public static void main(String[] args) { String enterName; double enterSaldo, enterWithdraw; Scanner scan = new Scanner(System.in); System.out.println("Type in your name, please."); enterName = scan.nextLine(); System.out.println("Type in how much money you have on your account."); enterSaldo = scan.nextDouble(); Person Test = new Person(enterName, new Saldo(enterSaldo)); System.out.println("The Person " + enterName + " has " + enterSaldo + " euro on his account."); enterWithdraw = scan.nextDouble(); Test.withdrawal(enterWithdraw); // Problem here. } }
-
You could either give Person a public getSaldo() method, and then call withdrawal on the Saldo object returned, or you could give Person a public withDrawFromSaldo(double amount) and then inside of this method call the private Saldo's withdrawal method. I favor the second solution as it only exposes what is necessary -- it does better at encapsulation.
Similar Threads
-
how call from inner class(anonymous or not), a method of parent class?
By lse123 in forum AWT / SwingReplies: 2Last Post: 05-01-2010, 08:59 AM -
Java class HashIt with a static recursive method and a static iterative method
By kezkez in forum New To JavaReplies: 3Last Post: 02-09-2010, 05:22 AM -
Child-Class Calling a Method in a Parent-Class
By Blah_ in forum New To JavaReplies: 5Last Post: 09-29-2009, 02:48 AM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
Calling a method on original class from created class
By kpedersen in forum Advanced JavaReplies: 4Last Post: 08-20-2008, 12:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks