Hi want these to be input. The program seems to output those rather than inputin
ID: 3549851 • Letter: H
Question
Hi want these to be input. The program seems to output those rather than inputing.
initial balance account#99- Input (567.25)
initial balance account$81- Input (1064.32)
Here is the code
Class Implementation
/**
* Account class presents a bank account.
*/
class Account
{
private double balance; // Account balance
private int id;
/**
* The constructor initializes the balance
* and id fields with the values
* passed to startBalance and id.
*/
public Account(int newId,double initialBalance)
{
balance = initialBalance;
id = newId;
}
/**
* The deposit method adds the parameter
* amount to the balance field.
*/
public void deposit(double amount)
{
balance += amount;
}
/**Sets id to data field*/
public void setID(int newID)
{
id = newID;
}
/**Returns id field value*/
public int getID()
{
return id;
}
/**
* The withdraw method subtracts the
* parameter amount from the balance
* field.
*/
public void withdraw(double amount)
{
balance -= amount;
}
/**
* The setBalance method sets the
* value to balance field.
*/
public void setBalance(double bal)
{
balance= bal;
}
/**
* The getBalance method returns the
* value in the balance field.
*/
public double getBalance()
{
return balance;
}
}
Driver program to test the code:
public class AccountTest
{
/**
* @param args
*/
public static void main(String[] args)
{
double amount;
// //Create a Scanner object for keyboard input
Scanner input = new Scanner(System.in);
Account Acc1 = new Account(99,567.5);
Account Acc2 = new Account(81,1064.32);
System.out.println("initial balance account#"+Acc1.getID()+"-"+Acc1.getBalance());
System.out.println("initial balance account#"+Acc2.getID()+"-"+Acc2.getBalance());
//Operating second account
System.out.println("Deposit in account "+Acc1.getID()+"?");
amount=input.nextDouble();
Acc1.deposit(amount);
System.out.println("Withdraw from account "+Acc1.getID()+"?");
amount=input.nextDouble();
Acc1.withdraw(amount);
System.out.println("Current account in account number " +Acc1.getID()+" is "+Acc1.getBalance());
//Operating second account
System.out.println("Deposit in account "+Acc1.getID()+"?");
amount=input.nextDouble();
Acc2.deposit(amount);
System.out.println("Withdraw from account "+Acc2.getID()+"?");
amount=input.nextDouble();
Acc2.withdraw(amount);
System.out.println("Current account in account number " +Acc2.getID()+" is "+Acc2.getBalance());
}
}
Explanation / Answer
Made changes in your AccountTest class
import java.util.Scanner;
public class AccountTest
{
/**
* @param args
*/
public static void main(String[] args){
double amount;
// //Create a Scanner object for keyboard input
Scanner input = new Scanner(System.in);
double initialAmount;
System.out.println("enter initial balance account#99");
initialAmount = input.nextDouble();
Account Acc1 = new Account(99,initialAmount);
System.out.println("enter initial balance account#81");
initialAmount = input.nextDouble();
Account Acc2 = new Account(81,initialAmount);
System.out.println("initial balance account#"+Acc1.getID()+"-"+Acc1.getBalance());
System.out.println("initial balance account#"+Acc2.getID()+"-"+Acc2.getBalance());
//Operating second account
System.out.println("Deposit in account "+Acc1.getID()+"?");
amount=input.nextDouble();
Acc1.deposit(amount);
System.out.println("Withdraw from account "+Acc1.getID()+"?");
amount=input.nextDouble();
Acc1.withdraw(amount);
System.out.println("Current account in account number " +Acc1.getID()+" is "+Acc1.getBalance());
//Operating second account
System.out.println("Deposit in account "+Acc1.getID()+"?");
amount=input.nextDouble();
Acc2.deposit(amount);
System.out.println("Withdraw from account "+Acc2.getID()+"?");
amount=input.nextDouble();
Acc2.withdraw(amount);
System.out.println("Current account in account number " +Acc2.getID()+" is "+Acc2.getBalance());
}
}
/*******************edit***************************/
DecimalFormat df = new DecimalFormat("#.00");
String val = df.format(doubleVal); //this can be helpful for you.
/***************after the edits , the Account class comes as follows********************/
package com.chegg;
import java.text.DecimalFormat;
/**
* Account class presents a bank account.
*/
class Account
{
private double balance; // Account balance
private int id;
/**
* The constructor initializes the balance
* and id fields with the values
* passed to startBalance and id.
*/
public Account(int newId,double initialBalance)
{
balance = initialBalance;
id = newId;
}
/**
* The deposit method adds the parameter
* amount to the balance field.
*/
public void deposit(double amount)
{
balance += amount;
}
/**Sets id to data field*/
public void setID(int newID)
{
id = newID;
}
/**Returns id field value*/
public int getID()
{
return id;
}
/**
* The withdraw method subtracts the
* parameter amount from the balance
* field.
*/
public void withdraw(double amount)
{
balance -= amount;
}
/**
* The setBalance method sets the
* value to balance field.
*/
public void setBalance(double bal)
{
balance= bal;
}
/**
* The getBalance method returns the
* value in the balance field.
*/
public String getBalance()
{
DecimalFormat df = new DecimalFormat("#.00");
return df.format(balance);
}
}
/**************note************************/
doubles and floats are not exactly real numbers, there are finite number of bits to represent them while there are infinite number of real numbers [in any range], so you cannot represent all real numbers - You are getting the closest number you can have with the floating point representation.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.