Java Constructor Help PLEASE

PokerDegenerate

Daemon Poster
Messages
1,465
OK I am learning Java and am stuck...:(

Here is the problem...

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.

OK first here is what I have for the constructors Please tell me if they are correct it compiles but I am not sure if they are correct for the question.

public class CheckingAccount
{
private int accountNumber;
private double balance;


public int getNumber()
{
return accountNumber;
}

public double getbalance()
{
return balance;
}

public void setNumber(int accountNmb)
{
accountNumber = accountNmb;
}
public void setBalance(double blnce)
{
if(balance < 200.0)
blnce = 0;
else
balance = blnce;
}
public void display()
{
if(balance == 0)
System.out.println("If you have less than $200.00 in your account the balance is set to zero");
else
System.out.println("The balance for account number " + accountNumber + " is " + balance);
}
}

If this is good I am stuck on how to write the application to instantiate the objects.

I have this but I can't seem to get it right no matter what I add or change:

import java.util.Scanner;
public class testCheckingAccount
{
public static void main(String[] args)
{
int accountNmb;
double blnce;
CheckingAccount CustomerAccount1 = new CheckingAccount();
CheckingAccount CustomerAccount2 = new CheckingAccount();
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter your account number");
accountNmb = keyboard.nextInt();
keyboard.nextLine();
System.out.println("Please enter your account number");
blnce = keyboard.nextDouble();
}

EDIT*** sorry for the lack of whitespace I don't know how to put code up with whitespace....
 
OK I figured it out on my own... I was trying to do too much with it and got lost along the way..... :(

I think I misunderstood exactly what the question was asking... :D
 
Just for clarity (and for future reference in case anyone else sees this thread)

The things you're calling "constructors" in the class above aren't constructors at all, they're methods. A constructor would look like this:

Code:
public CheckingAccount(int accountNumber, double balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}

Note the difference between the two - the constructor is always called the name of the class and has no return type. The code that you've got there sets and gets the fields after instantiation and those types of methods are perfectly valid (if they're required) but they're not constructors since they don't set the fields on the object's creation.

Hope that makes sense, feel free to ask if you've got any questions :)
 
Yes berry that makes perfect sense. That is where I think I went wrong because there was a constructor needed which would set the balance to $0 but it was only to set it "if" the balance entered was below $200. I was really not exactly sure how to use a constructor and got them mixed up with the get() and set() methods. all that I really had to do was use a constructor to give balance a default value of 0, and then use a setbalance() to set the balance to the amount given by a user if the balance was above 200.00

Thank you for the explanation of a constructor though because it makes more sense the way you put it. :)
 
I am also having trouble with the same problem that you mentioned in your first post. The question states to create a constructor that accepts arguments for both the account number and balance. So, this is not right? I only need a constructor that sets the default to 0? Also, are there any get and set methods in the CheckingAccount class or are these in the TestCheckingAccount class? I am so confused, but I really want to write the classes on my own. Can you offer anymore help on this question without writing the applications out? Thanks.
 
It's best to start a new thread posting full details of your problem, this one is rather old...
 
It's the same question so I see your logic. But just from the original question and your post I couldn't really understand what you're asking. It'll be a lot clearer if you start afresh, posting the same question but showing the things that you've tried and why you're stuck.
 
Back
Top Bottom