Can someone help me with this. I am brand new to programming and this makes no s
ID: 3669552 • Letter: C
Question
Can someone help me with this. I am brand new to programming and this makes no sense to me. I am very lost and have watched hours upon hours of youtube tutorials but still dont understand. I would be extremely grateful.
Include a private variable of type int to represent the account balance. Provide a constructor that has a parameter for initial balance and uses it to initialize the account balance. Provide 3 member functions: credit - adds an amount passed as an argument to the current balance. debit - withdraws an amount passed as an argument from the current balance. getBalance - returns the balance amount. Create a program (AccountTest.java) that creates two Account objects and validate the methods of the class Account. (Call the methods passing the appropriate values to make sure they produce the expected results.)
Explanation / Answer
ACcout.java :
public class Account {
private double balance;
public Account(double initialBalance ) {
if( initialBalance > 0.0 )
balance = initialBalance;
}
public void credit( double amount ){
balance = balance + amount;
}
public void withdraw( double withdrawAmount)
{
if(withdrawAmount <= balance ){
balance = balance-withdrawAmount;
}
else
System.out.println("No sufficient funds, you have only $ "+balance);
}
public double getBalance()
{
return balance ;
}
}
AccountTest.java
import java.util.Scanner;
import java.io.*;
public class AccountTest{
public static void main( String args[] ) {
Account account1 = new Account( 1990.00 );
Account account2 = new Account(34.80 );
System.out.printf( "account1 balance: $%.2f ",account1.getBalance() );
System.out.printf("account2 balance: $%.2f ", account2.getBalance() );
Scanner input = new Scanner( System.in);
double withdrawalAmount;
System.out.println("Enter withdrawal amount for account1: ");
withdrawalAmount = input.nextDouble();
System.out.printf( " subtracting %.2f from account1 balance ",withdrawalAmount);
account1.withdraw(withdrawalAmount);
System.out.printf( "account1 balance: $%.2f ", account1.getBalance() );
System.out.printf("account2 balance: $%.2f ",account2.getBalance() );
System.out.println("Enter withdrawal amount for account2: ");
withdrawalAmount = input.nextDouble();
System.out.printf(" subtracting %.2f from account2 balance ", withdrawalAmount );
account2.withdraw(withdrawalAmount);
System.out.printf( "account1 balance: $%.2f ", account1.getBalance() );
System.out.printf("account2 balance: $%.2f ",account2.getBalance() );
}
}
Exceution pattern:
D:>javac Accout.java
D:> javac AccountTest.java
AccountTest.java has the main method of the program
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.