|
|
#1 |
|
Beta Member
Join Date: Jan 2011
Posts: 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.");
}
}
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();
}
}
|
|
|
|
|
|
#2 |
|
Daemon Poster
Join Date: Dec 2008
Posts: 1,465
|
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;
}
the if statement should be in the checking account class in the setBalance() method.
__________________
Raidmax Smilodon Extreme Black Case -|-AMD Phenom II x4 955 BE @ 3829.50 Mhz (18.5 x 207, vcore 1.424, NB 2600 mhz, HT link 2200 mhz) -|- Xigmatek Dark Knight S1283V -|- Gigabyte MA790XT-UD4P Mobo -|- G. Skill 6Gb (3 x 2Gb) DDR3 1333 RAM 7-7-7-18-2t -|- WD Caviar 7200 RPM 1 TB HDD -|- OCZ GameXtreme 700W -|- Radeon 4870 1Gb @ 780/1050 -|- Asus VW202NR 20" wide-screen -|- Windows 7 Professional 64 Bit -|- Saitek Cyborg Keyboard -|- Razer Lachesis Mouse |
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|