Go Back   Computer Forums > General Computing > Programming
Join Computer forums Today

Thread Tools Search this Thread Display Modes
Old 01-28-2011, 11:57 AM   #1
Beta Member
Join Date: Jan 2011
Posts: 4
Default Java-Constructor and If Statement Help

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:

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:

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();
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?

newprog is offline   Reply With Quote
Old 01-28-2011, 06:38 PM   #2
Daemon Poster
PokerDegenerate's Avatar
Join Date: Dec 2008
Posts: 1,465
Default Re: Java-Constructor and If Statement Help

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.
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.
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
PokerDegenerate is offline   Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

All times are GMT -5. The time now is 01:43 PM.

Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2016, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0