Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 07-24-2010, 02:06 PM   #1
Daemon Poster
 
PokerDegenerate's Avatar
 
Join Date: Dec 2008
Posts: 1,465
Default Java Constructor Help PLEASE

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.

Quote:
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:

Quote:
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....
__________________

__________________
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
Old 07-25-2010, 03:54 PM   #2
Daemon Poster
 
PokerDegenerate's Avatar
 
Join Date: Dec 2008
Posts: 1,465
Default Re: Java Constructor Help PLEASE

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...
__________________

__________________
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
Old 07-26-2010, 06:44 PM   #3
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java Constructor Help PLEASE

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
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 07-26-2010, 08:20 PM   #4
Daemon Poster
 
PokerDegenerate's Avatar
 
Join Date: Dec 2008
Posts: 1,465
Default Re: Java Constructor Help PLEASE

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.
__________________
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
Old 01-26-2011, 05:34 PM   #5
Beta Member
 
Join Date: Jan 2011
Posts: 4
Default Re: Java Constructor Help PLEASE

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.
newprog is offline   Reply With Quote
Old 01-26-2011, 06:23 PM   #6
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java Constructor Help PLEASE

It's best to start a new thread posting full details of your problem, this one is rather old...
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 01-28-2011, 12:48 PM   #7
Beta Member
 
Join Date: Jan 2011
Posts: 4
Default Re: Java Constructor Help PLEASE

Thank you. I will. I only posted here because I am working on the exact same question.
newprog is offline   Reply With Quote
Old 01-28-2011, 12:53 PM   #8
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java Constructor Help PLEASE

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.
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 01-28-2011, 01:03 PM   #9
Beta Member
 
Join Date: Jan 2011
Posts: 4
Default Re: Java Constructor Help PLEASE

Thanks berry. I have reposted in a new thread.
__________________

newprog is offline   Reply With Quote
Reply

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 05:27 AM.


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