• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

boolean always returned as false

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys just wondering if you could give me a pointer as to how I return the cost of a job as getting out put with false if it is set up like this?

The constuctors
----------------------------------------------------------------------
public Job(int ref, int nCopy, boolean doubleSided, int nPages, String staffMember, Time time, Date date)
{
refNo = ref;
numCopy = nCopy;
numPages = nPages;
doubleSided = false; " I think this is bits the trouble "
staffMem = staffMember;
time = new Time(00,00);
date = new Date (12,12,2006);
}
-----------------------------------------------------------------------
The method
-----------------------------------------------------------------------
/** Returns the cost of the job
*/
public int getCost()
{
if (doubleSided == true)
return numPages * numCopy * 2;
else
return numPages * numCopy * 3;
}
------------------------------------------------------------------------
refNo = 20102 Staff Name = Micky Mouse Number of Pages = 27 Number of copies = 2 Double sided = false



refNo = 10101 Staff Name = Mucky Mouse Number of Pages = 12 Number of copies = 1 Double sided = false



refNo = 30103 Staff Name = Murry Mouse Number of Pages = 38 Number of copies = 3 Double sided = false
[ January 29, 2006: Message edited by: Iain Linton ]
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Iain Linton:

public Job(int ref, int nCopy, boolean doubleSided, int nPages, String staffMember, Time time, Date date)
{
doubleSided = false;
}


I don't think this line does what you think it does. This says that the input parameter should be set to false, but since this is a primitive, this does not set any values on the object nor is the result saved by the calling of the constructor.

If you want to save the value on the Job object, you need to use 'this.doubleSided = doubleSided' assuming the Job defines a doubleSided variable.

If you want to save the value from the call such as 'new Job(x)' where x is set to 'false' in the constructor, then you need to pass it another way. Java passes by reference (or copy of reference for you neat freaks out there) so that you cannot change the object a reference points to, whether it is an object being passed nor a primitive.
 
Iain Linton
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you that worked just fine, but now I am having probelms with returning the cost within aGetString method.

+ cost produces Null in the getAsString method
------------------------------------------------------------------
[ January 29, 2006: Message edited by: Iain Linton ]
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well you're not actually calling getCost() in your getAsString() method so I'm not sure why you posted the code for it. Also, cost is an int which cannot be null (defaults to 0 when not set) so I think your null error might be something else. Please post the output message you get.

... re-reading your code ... why do you have a string called getAsString? That's a bad name for a variable and may be the cause of your problem: variables should be names starting with lower case, the 'get' word should be in methods, not variables, and the fact that you have a string and a method with the same name is dangerous to say the least.
[ January 25, 2006: Message edited by: Scott Selikoff ]
 
Iain Linton
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the output I get

refNo = 15987 Staff Name = Morbid Mouse Number of Pages = 95 Number of copies = 22 Double sided = true Submitted Date = Date@1113622 Cost 0



refNo = 10101 Staff Name = Mucky Mouse Number of Pages = 12 Number of copies = 1 Double sided = false Submitted Date = Date@f8f7db Cost 0



refNo = 30103 Staff Name = Murry Mouse Number of Pages = 38 Number of copies = 3 Double sided = false Submitted Date = Date@15856a5 Cost 0



Originally posted by Scott Selikoff:
Well you're not actually calling getCost() in your getAsString() method so I'm not sure why you posted the code for it. Also, cost is an int which cannot be null (defaults to 0 when not set) so I think your null error might be something else. Please post the output message you get.

... re-reading your code ... why do you have a string called getAsString? That's a bad name for a variable and may be the cause of your problem: variables should be names starting with lower case, the 'get' word should be in methods, not variables, and the fact that you have a string and a method with the same name is dangerous to say the least.

[ January 25, 2006: Message edited by: Scott Selikoff ]

 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whats the problem? If you are referring to the date formatting, its outputting the converted object id of the object. You need to use a date formatter to view dates properly.
 
Iain Linton
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Scott Selikoff:

I will post all the code from the class.

then you can see where I am going wrong.

Whats the problem? If you are referring to the date formatting, its outputting the converted object id of the object. You need to use a date formatter to view dates properly.


[ January 29, 2006: Message edited by: Iain Linton ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks to me like you are printing out the value of the cost variable. However, since its value is 0, it is likely that the variable has never been given a meaningful value. For primitive instance variables, the value 0 is the default. So what line of code sets this value to something else before you try to print it?

Layne
reply
    Bookmark Topic Watch Topic
  • New Topic