473,324 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Java acting wierd?

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:
Expand|Select|Wrap|Line Numbers
  1. public static void main(String[] args) {
  2.      Sterling c1 = new Sterling(1,2,3);
  3.      Sterling c2 = new Sterling(4,5,6);
  4.  
  5.  
  6.      int c1Pound = c1.getPound();
  7.      int c1Shilling = c1.getShilling();  
  8.      int c1Pence = c1.getPence();
  9.  
  10.  
  11.      int c2Pound = c2.getPound();
  12.      int c2Shilling = c2.getShilling();
  13.      int c2Pence = c2.getPence();
  14.  
  15.  
  16.      System.out.println("Sterling object 1 with " + c1Pound + " pounds and "      +     c1Shilling + " shillings and " + c1Pence+" pence");
  17.  
  18.      System.out.println("Sterling object 2 with " + c2Pound + " pounds and " +      c2Shilling + " shillings and " + c2Pence +" pence");
  19. }
  20.  
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
Expand|Select|Wrap|Line Numbers
  1. //Sterling c2 = new Sterling(4,5,6); 
  2. //int c2Pound = c2.getPound();
  3. //int c2Shilling = c2.getShilling();
  4. //int c2Pence = c2.getPence();
  5. //System.out.println("Sterling object 2 with " + c2Pound + " pounds and " + c2Shilling + " shillings and " + c2Pence +" pence");
  6.  
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?
Nov 24 '08 #1
7 1679
JosAH
11,448 Expert 8TB
Without seeing your code I guess that the pound, shilling and pence variables in your Stirling class are static member variables.

kind regards,

Jos
Nov 24 '08 #2
the pound, shilling and pence attributes are not static in my class. Should they be?
Nov 24 '08 #3
JosAH
11,448 Expert 8TB
@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
Nov 24 '08 #4
Expand|Select|Wrap|Line Numbers
  1. class Sterling
  2. {
  3.     private static int pound;        //creating private attributes
  4.     private static int shilling;
  5.     private static int pence;
  6.  
  7.  
  8.     public Sterling()         //default constructor that takes all values and initializes to 0
  9.     {
  10.         pound = 0;
  11.         shilling = 0;
  12.         pence = 0;
  13.  
  14.     }
  15.     public Sterling(int p, int sh, int pe)    //overloaded constructor that takes all values and initializes to user defined
  16.     {
  17.         pound = p;
  18.         shilling = sh;
  19.         pence = pe;
  20.  
  21.         if(pound<0)                        //verifying condition when initialized
  22.         {
  23.             System.out.println("Sorry, you have entered an incorrect value");
  24.             pound = 0;
  25.         }
  26.         else
  27.         {
  28.             pound = p;
  29.         }
  30.  
  31.         if(shilling<0)    //verifying condition when initialized
  32.         {    
  33.             System.out.println("Sorry, you have entered an incorrect shilling amount");
  34.             shilling = 0;
  35.         }
  36.         else
  37.         {
  38.             shilling = sh;
  39.         }
  40.  
  41.         if(pence<0)            //verifying condition when initialized
  42.         {
  43.             System.out.println("Sorry, you have entered an incorrect pence amount");
  44.             pence = 0;
  45.         }
  46.         else
  47.         {
  48.             pence = pe;
  49.         }
  50.     }//closes public Sterling (int lb, int sh, int p)
  51.  
  52.     //******************************************************************************
  53.     //begins my accessor methods
  54.  
  55.     public int getPound()        //accessor method will return pound
  56.     {
  57.         return pound;
  58.     }
  59.  
  60.     public int getShilling()    //accessor method will return shilling
  61.     {
  62.         return shilling;
  63.     }
  64.  
  65.     public int getPence()        //accessor method will return pence
  66.     {
  67.         return pence;
  68.     }
  69.  
  70.     //begins my mutator methods
  71.  
  72.     public void setPound(int p)            //mutator method will set to given pound
  73.     {
  74.         if(pound<0)
  75.         {
  76.             System.out.println("Sorry, you have entered an incorrect value");
  77.             pound = 0;
  78.         }
  79.         else
  80.         {
  81.             pound = p;
  82.         }
  83.  
  84.     }
  85.  
  86.     public void setShilling(int sh)    //mutator method will set to given shilling
  87.     {
  88.         if(shilling<0)
  89.         {    
  90.             System.out.println("Sorry, you have entered an incorrect shilling amount");
  91.             shilling = 0;
  92.         }
  93.         else
  94.         {
  95.             shilling = sh;
  96.         }
  97.     }
  98.  
  99.     public void setPence(int pe)        //mutator method will set to given shilling
  100.     {
  101.         if(pence<0)
  102.         {
  103.             System.out.println("Sorry, you have entered an incorrect pence amount");
  104.             pence = 0;
  105.         }
  106.         else
  107.         {
  108.             pence = pe;
  109.         }
  110.     }
  111.  
  112.  
  113.  
  114.  
  115.     public boolean equals(Sterling c)                                //method to compare 2 sterling objects
  116.     {
  117.         if(pound==(pound) && shilling==(shilling) && pence==(pence))
  118.         {
  119.             return true;
  120.         }
  121.         else
  122.         {
  123.             return false;
  124.         }
  125.     }
  126.  
  127.  
  128.     public int compareTo(Sterling b)        //methods to compare 2 sterling methods and return integer values
  129.     {
  130.         if((this.getPound()< b.getPound())&& (this.getShilling()< b.getShilling() )&& (this.getPence()< b.getPence()))
  131.         {
  132.             return -1;
  133.         }
  134.         else if(this.getPound()== b.getPound()&& this.getShilling()== b.getShilling()&& this.getPence()== b.getPence())
  135.         {
  136.             return 0;
  137.         }
  138.         else//(this.getPound()> this.getPound()&& this.getShilling()> b.getShilling()&& this.getPence()> b.getPence())
  139.         {
  140.             return 1;
  141.         }
  142.     }
  143.  
  144.  
  145.     public String toString()             //will convert all the values to string when this method is invoked
  146.     {
  147.         return ("pound = " + pound + " shilling = " + shilling + " pence = " + pence);
  148.     }
  149.  
  150.  
  151.     public void showInfo()                //will show all attributes
  152.     {
  153.         System.out.println("This Sterling object has " + this.getPound() + " pounds, " + this.getShilling() + " shillings, " + this.getPence() + " pence." );
  154.     }
  155.  
  156. /*
  157.     public static int toPence(Sterling a)
  158.     {
  159.         double convertedPence;
  160.  
  161.         double poundToPence = a.getPound();                            will get number of pounds
  162.         poundToPence*=240;                                                    //will convert to number of pence
  163.  
  164.         double shillingToPence = a.getShilling();                            //will get number of shilling
  165.         shillingToPence*=12;                                                //will convert to pence
  166.  
  167.         double penceToPence = a.getPence();                                    //will get number of pence
  168.  
  169.         convertedPence = poundToPence + shillingToPence + penceToPence;        //will hold the total number of pence
  170.  
  171.         double tax = convertedPence*(0.08);                                    //will calculate 8% tax
  172.  
  173.         convertedPence = (convertedPence + tax);                            //adds tax to pence value
  174.  
  175.  
  176.  
  177.         return ((int)convertedPence);                                        //returns pence value with tax
  178.  
  179.     }
  180.  
  181.  
  182.     */
  183.  
  184.  
  185.     public static int add(Sterling a, Sterling b)
  186.     {
  187.  
  188.         double aConvertedPence;
  189.  
  190.         double aPoundToPence = a.getPound();                                    //will get number of pounds
  191.         aPoundToPence*=240;                                                        //will convert to number of pence
  192.  
  193.         double aShillingToPence = a.getShilling();                                //will get number of shilling
  194.         aShillingToPence*=12;                                                    //will convert to pence
  195.  
  196.         double aPenceToPence = a.getPence();                                    //will get number of pence
  197.  
  198.         aConvertedPence = aPoundToPence + aShillingToPence + aPenceToPence;        //will hold the total number of pence
  199.  
  200.         double aTax = aConvertedPence*(0.08);                                    //will calculate 8% tax
  201.  
  202.         aConvertedPence = (aConvertedPence + aTax);                                //adds tax to pence value
  203.  
  204.  
  205.  
  206.         double bConvertedPence;
  207.  
  208.         double bPoundToPence = b.getPound();                                    //will get number of pounds
  209.         bPoundToPence*=240;                                                        //will convert to number of pence
  210.  
  211.         double bShillingToPence = b.getShilling();                                //will get number of shilling
  212.         bShillingToPence*=12;                                                    //will convert to pence
  213.  
  214.         double bPenceToPence = b.getPence();                                    //will get number of pence
  215.  
  216.         bConvertedPence = bPoundToPence + bShillingToPence + bPenceToPence;        //will hold the total number of pence
  217.  
  218.         double bTax = bConvertedPence*(0.08);                                    //will calculate 8% tax
  219.  
  220.         bConvertedPence = (bConvertedPence + bTax);                                //adds tax to pence value
  221.  
  222.  
  223.  
  224.  
  225.         int totalSterling = ((int)(aConvertedPence +bConvertedPence ));
  226.  
  227.  
  228.         return totalSterling;                                        //returns pence value with tax
  229.  
  230.     }
  231.  
  232.     //need same method for subtract
  233.  
  234.  
  235.     public static int sub(Sterling a, Sterling b)
  236.     {
  237.  
  238.         double aConvertedPence;
  239.  
  240.         double aPoundToPence = a.getPound();                                    //will get number of pounds
  241.         aPoundToPence*=240;                                                        //will convert to number of pence
  242.  
  243.         double aShillingToPence = a.getShilling();                                //will get number of shilling
  244.         aShillingToPence*=12;                                                    //will convert to pence
  245.  
  246.         double aPenceToPence = a.getPence();                                    //will get number of pence
  247.  
  248.         aConvertedPence = aPoundToPence + aShillingToPence + aPenceToPence;        //will hold the total number of pence
  249.  
  250.         double aTax = aConvertedPence*(0.08);                                    //will calculate 8% tax
  251.  
  252.         aConvertedPence = (aConvertedPence + aTax);                                //adds tax to pence value
  253.  
  254.  
  255.  
  256.         double bConvertedPence;
  257.  
  258.         double bPoundToPence = b.getPound();                                    //will get number of pounds
  259.         bPoundToPence*=240;                                                        //will convert to number of pence
  260.  
  261.         double bShillingToPence = b.getShilling();                                //will get number of shilling
  262.         bShillingToPence*=12;                                                    //will convert to pence
  263.  
  264.         double bPenceToPence = b.getPence();                                    //will get number of pence
  265.  
  266.         bConvertedPence = bPoundToPence + bShillingToPence + bPenceToPence;        //will hold the total number of pence
  267.  
  268.         double bTax = bConvertedPence*(0.08);                                    //will calculate 8% tax
  269.  
  270.         bConvertedPence = (bConvertedPence + bTax);                                //adds tax to pence value
  271.  
  272.  
  273.  
  274.  
  275.         int totalSterling = ((int)(aConvertedPence -bConvertedPence ));
  276.  
  277.         if((aConvertedPence -bConvertedPence <=0))
  278.         {
  279.             return totalSterling = 0;
  280.         }
  281.         else
  282.         {
  283.             return totalSterling;    
  284.         }
  285.  
  286.     }
  287.  
  288.  
  289.     public static int toPence(int a)
  290.     {
  291.         int pence = a%12;        //will give the remainder
  292.         return pence;
  293.     }
  294.  
  295.     public static int toShilling(int a)
  296.     {
  297.         int shillingValue = a/12;
  298.         shillingValue%=20;
  299.         return shillingValue;
  300.     }
  301.  
  302.     public static int toPound(int a)
  303.     {
  304.         int shillingValue = a/12;
  305.         int poundValue = shillingValue/20;
  306.         return poundValue;
  307.     }
  308.  
  309.  
  310.  
  311. }// end of class sterling
  312.  
  313.  
  314. public class English_Currency {
  315.  
  316.  
  317.     public static void main(String[] args) {
  318.  
  319.         Sterling c1 = new Sterling(1,2,3);
  320.         Sterling c2 = new Sterling(4,5,6);
  321.  
  322.  
  323.         int c1Pound = c1.getPound();
  324.         int c1Shilling = c1.getShilling();
  325.         int c1Pence = c1.getPence();
  326.  
  327.  
  328.         int c2Pound = c2.getPound();
  329.         int c2Shilling = c2.getShilling();
  330.         int c2Pence = c2.getPence();
  331.  
  332.  
  333.         System.out.println("Sterling object 1 with " + c1Pound + " pounds and " + c1Shilling + " shillings and " + c1Pence+" pence");
  334.  
  335.         System.out.println("Sterling object 2 with " + c2Pound + " pounds and " + c2Shilling + " shillings and " + c2Pence +" pence");
  336.  
  337.  
  338.     }
  339.  
  340. }
  341.  
Thanks Jos for all your help, i hope the code will be clear enough to find the problem
Nov 24 '08 #5
JosAH
11,448 Expert 8TB
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
Nov 24 '08 #6
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
Nov 24 '08 #7
JosAH
11,448 Expert 8TB
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
Nov 24 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

114
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
3
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...
4
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...
3
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...
7
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...
16
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...
15
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...
5
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...
2
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
1
isladogs
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...
0
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...
1
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)...
1
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...
1
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....
0
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
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.