about exceptions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • miss time
    New Member
    • Jun 2007
    • 16

    #1

    about exceptions

    I try to this solve this question and I have some problem.Can you please tell where the wrong and explan to me.
    package bank;
    public class BankAccount {
    private double balance;

    public BankAccount(dou ble balance) {
    this.balance = balance;
    }

    public double getBalance() {
    return balance;
    }

    public double withdraw(double amount){
    balance = balance - amount;
    return balance;
    }

    public double deposit(double amount){
    balance = balance + amount;
    return balance;
    }
    }
    1-Add to the BankAccount class a new method called getAmount() that read a value from the input (using the Scanner class). This method should return a double value.
    and this my answare:
    public double getAmount(Strin g r){
    double d;
    d = Double.parseDou ble(r);
    Scanner s = new Scanner(System. in);
    System.out.prin tln("Enter a value ");
    double x = s.nextDouble();
    return d;
    }

    " my problem in Scanner method and I try to read more and understand from net,So can you explan to in easy way about this method because i think that i do not answar the question correct "
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Code:
    public double getAmount(String r){
    	double d;
    	d = Double.parseDouble(r);	
    	Scanner s = new Scanner(System.in);
    	System.out.println("Enter a value ");
    	double x  = s.nextDouble();
    	return d;
    }
    Base on the method above, you converted the value of the parameter String into double...

    You implemented a scanner that would capture the input from the user... ( s )
    you have stored the captured value in double data type ( x ), but it is no use...

    Since you return d that wasn't came from the user's input....


    Sukatoa.

    Comment

    Working...