Java-Constructor and If Statement Help

newprog

Beta member
Messages
4
I am very new to Java and I am having trouble with this:
Create a class named CheckingAccount with data fields for an account number and a balance. Include a constructor that takes arguments for each field. The constructor sets the balance to 0 if it is below the required 200.00 mininum for an account. Also include a method that displays account details, including an explanation if the balance was reduced to 0. Write an application named TestCheckingAccount in which you instantiate two CheckingAccount objects, prompt the user for values for the account number and balance, and display the values of both accounts.

This is what I have for my CheckingAccount class:

Code:
public class CheckingAccount
{
    int accountNumber;
    double accountBalance;
    public CheckingAccount(int num, double bal)
    {
    accountNumber = num;
    accountBalance = bal;
    }
    public void showAccount()
    {
      System.out.println("Account number: " + accountNumber);
      System.out.println("Balance: " + accountBalance);
      System.out.println("If your balance is under $200, then your balance is set to zero.");
    }
}

This is what I have for my TestAccount application:

Code:
import java.util.Scanner;
public class TestAccount
{
    public static void main(String[] args)
    {
      int n;
      double b;
      Scanner keyboard = new Scanner(System.in);
      System.out.print("Enter your account number ");
      n = keyboard.nextInt();
      System.out.print("Enter your account balance ");
      b = keyboard.nextDouble();
      CheckingAccount account1 = new CheckingAccount(n, b);
      CheckingAccount account2 = new CheckingAccount();
      account1.showAccount();
      account2.showAccount();
    }
}

The first problem I am having is that I have no idea where the if statement should be written. I know that the balance should be set to 0, but only if the balance is less than $200 by way of user input. Also, I am not sure about the instantiation of my objects in the TestAccount app. I don't think that I am understanding constructors correctly. Can anyone offer any guidance as to what I am doing wrong with these?
 
the constructor sets a default value for accountBalance which in this case it wants it to be 0. This means that it has to be set before that value is used anywhere else so before your first method.
Code:
{
accountBalance = 0;
}

I used set/get methods in the checking account class.
the if statement should be in the checking account class in the setBalance() method.
 
Back
Top Bottom