Im working on an assignment where i need to create a class that defines old english currency. In this class i have 1 default constructor which sets my attributes, pound, shilling, and pence, to 0 and an overloaded constructor which allows me to create a sterling object with values. In my main, i have to create 2 Sterling objects which i will add together. To do this, i must take each object, convert to pence (pennies), add both of them together, and re-display pounds, shilling, and pence values separately. This part is absolutely NOT a problem. The problem comes where in my main, i am creating 2 Sterling objects: -
public static void main(String[] args) {
-
Sterling c1 = new Sterling(1,2,3);
-
Sterling c2 = new Sterling(4,5,6);
-
-
-
int c1Pound = c1.getPound();
-
int c1Shilling = c1.getShilling();
-
int c1Pence = c1.getPence();
-
-
-
int c2Pound = c2.getPound();
-
int c2Shilling = c2.getShilling();
-
int c2Pence = c2.getPence();
-
-
-
System.out.println("Sterling object 1 with " + c1Pound + " pounds and " + c1Shilling + " shillings and " + c1Pence+" pence");
-
-
System.out.println("Sterling object 2 with " + c2Pound + " pounds and " + c2Shilling + " shillings and " + c2Pence +" pence");
-
}
-
and this is my output:
Sterling object 1 with 4 pounds and 5 shillings and 6 pence
Sterling object 2 with 4 pounds and 5 shillings and 6 pence
The OBVIOUSLY, this is doing something wrong. BUT, if i comment out -
//Sterling c2 = new Sterling(4,5,6);
-
//int c2Pound = c2.getPound();
-
//int c2Shilling = c2.getShilling();
-
//int c2Pence = c2.getPence();
-
//System.out.println("Sterling object 2 with " + c2Pound + " pounds and " + c2Shilling + " shillings and " + c2Pence +" pence");
-
my output works perfectly:
Sterling object 1 with 1 pounds and 2 shillings and 3 pence
What's going on here? It seems like if there is more than 1 object that's created, Java forgets about the initialized attributes of the first object and just takes the attributes of the second object and applies it to the first object.
any suggestions? what am i doing wrong/forgetting/not seeing?
7 1679
Without seeing your code I guess that the pound, shilling and pence variables in your Stirling class are static member variables.
kind regards,
Jos
the pound, shilling and pence attributes are not static in my class. Should they be?
@rotaryfreak
No they shouldn't be, i.e. every Stirling object has its own pound etc. members but from what you described it looked as if they were static. We can't say much more without seeing the java code in that class.
kind regards,
Jos
- class Sterling
-
{
-
private static int pound; //creating private attributes
-
private static int shilling;
-
private static int pence;
-
-
-
public Sterling() //default constructor that takes all values and initializes to 0
-
{
-
pound = 0;
-
shilling = 0;
-
pence = 0;
-
-
}
-
public Sterling(int p, int sh, int pe) //overloaded constructor that takes all values and initializes to user defined
-
{
-
pound = p;
-
shilling = sh;
-
pence = pe;
-
-
if(pound<0) //verifying condition when initialized
-
{
-
System.out.println("Sorry, you have entered an incorrect value");
-
pound = 0;
-
}
-
else
-
{
-
pound = p;
-
}
-
-
if(shilling<0) //verifying condition when initialized
-
{
-
System.out.println("Sorry, you have entered an incorrect shilling amount");
-
shilling = 0;
-
}
-
else
-
{
-
shilling = sh;
-
}
-
-
if(pence<0) //verifying condition when initialized
-
{
-
System.out.println("Sorry, you have entered an incorrect pence amount");
-
pence = 0;
-
}
-
else
-
{
-
pence = pe;
-
}
-
}//closes public Sterling (int lb, int sh, int p)
-
-
//******************************************************************************
-
//begins my accessor methods
-
-
public int getPound() //accessor method will return pound
-
{
-
return pound;
-
}
-
-
public int getShilling() //accessor method will return shilling
-
{
-
return shilling;
-
}
-
-
public int getPence() //accessor method will return pence
-
{
-
return pence;
-
}
-
-
//begins my mutator methods
-
-
public void setPound(int p) //mutator method will set to given pound
-
{
-
if(pound<0)
-
{
-
System.out.println("Sorry, you have entered an incorrect value");
-
pound = 0;
-
}
-
else
-
{
-
pound = p;
-
}
-
-
}
-
-
public void setShilling(int sh) //mutator method will set to given shilling
-
{
-
if(shilling<0)
-
{
-
System.out.println("Sorry, you have entered an incorrect shilling amount");
-
shilling = 0;
-
}
-
else
-
{
-
shilling = sh;
-
}
-
}
-
-
public void setPence(int pe) //mutator method will set to given shilling
-
{
-
if(pence<0)
-
{
-
System.out.println("Sorry, you have entered an incorrect pence amount");
-
pence = 0;
-
}
-
else
-
{
-
pence = pe;
-
}
-
}
-
-
-
-
-
public boolean equals(Sterling c) //method to compare 2 sterling objects
-
{
-
if(pound==(pound) && shilling==(shilling) && pence==(pence))
-
{
-
return true;
-
}
-
else
-
{
-
return false;
-
}
-
}
-
-
-
public int compareTo(Sterling b) //methods to compare 2 sterling methods and return integer values
-
{
-
if((this.getPound()< b.getPound())&& (this.getShilling()< b.getShilling() )&& (this.getPence()< b.getPence()))
-
{
-
return -1;
-
}
-
else if(this.getPound()== b.getPound()&& this.getShilling()== b.getShilling()&& this.getPence()== b.getPence())
-
{
-
return 0;
-
}
-
else//(this.getPound()> this.getPound()&& this.getShilling()> b.getShilling()&& this.getPence()> b.getPence())
-
{
-
return 1;
-
}
-
}
-
-
-
public String toString() //will convert all the values to string when this method is invoked
-
{
-
return ("pound = " + pound + " shilling = " + shilling + " pence = " + pence);
-
}
-
-
-
public void showInfo() //will show all attributes
-
{
-
System.out.println("This Sterling object has " + this.getPound() + " pounds, " + this.getShilling() + " shillings, " + this.getPence() + " pence." );
-
}
-
-
/*
-
public static int toPence(Sterling a)
-
{
-
double convertedPence;
-
-
double poundToPence = a.getPound(); will get number of pounds
-
poundToPence*=240; //will convert to number of pence
-
-
double shillingToPence = a.getShilling(); //will get number of shilling
-
shillingToPence*=12; //will convert to pence
-
-
double penceToPence = a.getPence(); //will get number of pence
-
-
convertedPence = poundToPence + shillingToPence + penceToPence; //will hold the total number of pence
-
-
double tax = convertedPence*(0.08); //will calculate 8% tax
-
-
convertedPence = (convertedPence + tax); //adds tax to pence value
-
-
-
-
return ((int)convertedPence); //returns pence value with tax
-
-
}
-
-
-
*/
-
-
-
public static int add(Sterling a, Sterling b)
-
{
-
-
double aConvertedPence;
-
-
double aPoundToPence = a.getPound(); //will get number of pounds
-
aPoundToPence*=240; //will convert to number of pence
-
-
double aShillingToPence = a.getShilling(); //will get number of shilling
-
aShillingToPence*=12; //will convert to pence
-
-
double aPenceToPence = a.getPence(); //will get number of pence
-
-
aConvertedPence = aPoundToPence + aShillingToPence + aPenceToPence; //will hold the total number of pence
-
-
double aTax = aConvertedPence*(0.08); //will calculate 8% tax
-
-
aConvertedPence = (aConvertedPence + aTax); //adds tax to pence value
-
-
-
-
double bConvertedPence;
-
-
double bPoundToPence = b.getPound(); //will get number of pounds
-
bPoundToPence*=240; //will convert to number of pence
-
-
double bShillingToPence = b.getShilling(); //will get number of shilling
-
bShillingToPence*=12; //will convert to pence
-
-
double bPenceToPence = b.getPence(); //will get number of pence
-
-
bConvertedPence = bPoundToPence + bShillingToPence + bPenceToPence; //will hold the total number of pence
-
-
double bTax = bConvertedPence*(0.08); //will calculate 8% tax
-
-
bConvertedPence = (bConvertedPence + bTax); //adds tax to pence value
-
-
-
-
-
int totalSterling = ((int)(aConvertedPence +bConvertedPence ));
-
-
-
return totalSterling; //returns pence value with tax
-
-
}
-
-
//need same method for subtract
-
-
-
public static int sub(Sterling a, Sterling b)
-
{
-
-
double aConvertedPence;
-
-
double aPoundToPence = a.getPound(); //will get number of pounds
-
aPoundToPence*=240; //will convert to number of pence
-
-
double aShillingToPence = a.getShilling(); //will get number of shilling
-
aShillingToPence*=12; //will convert to pence
-
-
double aPenceToPence = a.getPence(); //will get number of pence
-
-
aConvertedPence = aPoundToPence + aShillingToPence + aPenceToPence; //will hold the total number of pence
-
-
double aTax = aConvertedPence*(0.08); //will calculate 8% tax
-
-
aConvertedPence = (aConvertedPence + aTax); //adds tax to pence value
-
-
-
-
double bConvertedPence;
-
-
double bPoundToPence = b.getPound(); //will get number of pounds
-
bPoundToPence*=240; //will convert to number of pence
-
-
double bShillingToPence = b.getShilling(); //will get number of shilling
-
bShillingToPence*=12; //will convert to pence
-
-
double bPenceToPence = b.getPence(); //will get number of pence
-
-
bConvertedPence = bPoundToPence + bShillingToPence + bPenceToPence; //will hold the total number of pence
-
-
double bTax = bConvertedPence*(0.08); //will calculate 8% tax
-
-
bConvertedPence = (bConvertedPence + bTax); //adds tax to pence value
-
-
-
-
-
int totalSterling = ((int)(aConvertedPence -bConvertedPence ));
-
-
if((aConvertedPence -bConvertedPence <=0))
-
{
-
return totalSterling = 0;
-
}
-
else
-
{
-
return totalSterling;
-
}
-
-
}
-
-
-
public static int toPence(int a)
-
{
-
int pence = a%12; //will give the remainder
-
return pence;
-
}
-
-
public static int toShilling(int a)
-
{
-
int shillingValue = a/12;
-
shillingValue%=20;
-
return shillingValue;
-
}
-
-
public static int toPound(int a)
-
{
-
int shillingValue = a/12;
-
int poundValue = shillingValue/20;
-
return poundValue;
-
}
-
-
-
-
}// end of class sterling
-
-
-
public class English_Currency {
-
-
-
public static void main(String[] args) {
-
-
Sterling c1 = new Sterling(1,2,3);
-
Sterling c2 = new Sterling(4,5,6);
-
-
-
int c1Pound = c1.getPound();
-
int c1Shilling = c1.getShilling();
-
int c1Pence = c1.getPence();
-
-
-
int c2Pound = c2.getPound();
-
int c2Shilling = c2.getShilling();
-
int c2Pence = c2.getPence();
-
-
-
System.out.println("Sterling object 1 with " + c1Pound + " pounds and " + c1Shilling + " shillings and " + c1Pence+" pence");
-
-
System.out.println("Sterling object 2 with " + c2Pound + " pounds and " + c2Shilling + " shillings and " + c2Pence +" pence");
-
-
-
}
-
-
}
-
Thanks Jos for all your help, i hope the code will be clear enough to find the problem
You wrote that those member variables aren't static but I can clearly see that they are static. As it is now that class sux big times. Remove the 'static' keyword from those three variables and see what happens.
kind regards,
Jos
talk about total embarrassment :s sorry Jos, i honestly thought i started out creating those members as non-static. Removing "static" makes the program work perfectly
Thank you so much for your help :)
Joe
You're welcome of course; always think whether or not a member belongs to an instance of your class; in your case those pounds/shillings/pence do belong to a certain Sterling object; they can differ per object. Don't make those things static. If a variables belongs to all instances, iow to the class, make them static.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Maurice LING |
last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic
typing, is there any other real reasons that Python is slower than Java?
maurice
|
by: Markus Fischer |
last post by:
Hi,
I'm experiencing a wierd problem with IE 6 in Windows with two
_slightly_ different Version.
Give the following HTMl-Code, ideally the output of offsetTop should be
"105"; a few pixel...
|
by: Rhino |
last post by:
Is it possible for a Java Stored Procedure in DB2 V7.2 (Windows) to pass a
Throwable back to the calling program as an OUT parameter?
If yes, what datatype should I use when registering the...
|
by: Sathyaish |
last post by:
A practice excercise from K&R. Kindly read the comments within the
program. I'd be very grateful to people who helped. Why is it that I
get the wierd face-like characters on the screen instead of...
|
by: Richard Phillips |
last post by:
Hello,
Does anyone else out there have a copy of this?
I'm working through my copy and have discovered that it has no chapter 11...
Pages 341-388 are just not there!
Anyone else able to check...
|
by: Amir Michail |
last post by:
Hi,
It seems to me that measuring productivity in a programming language
must take into account available tools and libraries.
Eclipse for example provides such an amazing IDE for java that it...
|
by: himilecyclist |
last post by:
My State government organization has written a PHP/MySQL application
which has been in production for about 6 months and has been highly
successful.
We are now embarking on a similar database...
|
by: desktop |
last post by:
I am confused about the use of the template parameter "E" in the below
class. Since when is it allowed to use these parameters like "E(1)" and
what does it mean (where can I read more about this...
|
by: jacksoncn |
last post by:
hello there everybody,
I am having problems with this code (or it could be the javascript associated with it) in ie7. Everything is peachy in Firefox so i'm assuming that ie7 is at fault here. The...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |