• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

My program wont work.

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am supposed to design a class that stores the number of units sold and has a method that returns the total cost of the purchse. Can anyone lead me in the right direction

 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use a meaningful subject line.

Also, please post your code inside code tags. It makes things much easier to read .

Anyway, what specifically are you having issues with? Is the program not functioning the way you believe it should? What is happening when you run the program as opposed to what you believe should be happening?

Also, if I'm not mistaken, you should probably use the BigDecimal class for financial calculations.
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For starters, do not use the keyword "class" in a constructor, and also you need to specify the type of each parameter individually. e.g.




also, calculate is spelled wrong. also in your example cost1=c1 did not end with a semicolon.

p.s. I agree with everything W. Joe Smith said.
 
Dominic Griffin
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I place codes inside of code tags?
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use Code Tags.
 
Dominic Griffin
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I figured out the code tags. Thank you all for the quick replies. I am a beggining programmer and I really want to become efficient in Java programming. I am actually taking a Java programming class online. Don't ask me why. I have been searching for tutors and a Java expert refered me to this site. Bear with me while I embark upon my programming journey.
 
Dominic Griffin
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believed I was at a point in my program where it would execute. When I compile it I get 2 error messages

')' expected - line 100
<identifier> expected - line 99

I was expecting the dialog boxes to appear and begin asking for information to be enterd.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just an observation

in your setDiscount method, if you reversed the order of your statements, you wouldn't need compund conditions, eg.

if( units >= 100 ) do this;
else if( units >= 50) do that;
else if( units >= 10) do something else;
etc. if( units >0 ) do nothing;
else( <code to deal with invalid # of units> )

or something like that. See how it's simpler?
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dominic Griffin wrote:I believed I was at a point in my program where it would execute. When I compile it I get 2 error messages

')' expected - line 100
<identifier> expected - line 99

I was expecting the dialog boxes to appear and begin asking for information to be enterd.





You have a mystery period after totalCost. I am playing around with this program now, but that is causing a problem. Depending what else I figure out, I will post again.

Also, you have a call to a method called totalCost(), but no such method exists.
 
Dominic Griffin
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



I have made the suggested adjustments and I also added DecimalFormat. Now I get two errors.

<identifier> expected line 86
illegal start of type line 21

Also how can I tell if my code is set up to where it can store the unitsSold variable.
 
Dominic Griffin
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean when you say no method exists for totalCost ()?
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't rule out google, for example, I just googled java illegal start of type and turned up all sorts of explanations.

anyways you should not have that last comma in your constructor after c1 that might be it.

public SoftwareSales(double c, double s, double d, double c1,)
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dominic Griffin wrote:What do you mean when you say no method exists for totalCost ()?



You had a method call, totalCost(), but there was no totalCost() method in your program. I believe you now have the same problem with setTotalCost().

Also, how are you compiling this program, and with what version of the JDK? I just compiled it on 1.6.0_11 on the command prompt and had 38 errors.

EDIT: I dropped it to 6 once I removed the comma last comma here:



The comma after c1 shouldn't be there.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

W. Joe Smith wrote:

Also, how are you compiling this program, and with what version of the JDK? I just compiled it on 1.6.0_11 on the command prompt and had 38 errors.

EDIT: I dropped it to 6 once I removed the comma last comma here:



The comma after c1 shouldn't be there.



Mr Smith touches on a useful aspect of debugging. 38 errors does not necessarily mean 38 mistakes. Just fix the first mistake you find, then recompile. Keep doing that till you have no errors left. It's a good strategy until you gain more experience at identifying problems.

Anyways, I'll let Mr Smith deal with the details from here on in. good luck.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The single most important piece of advice I give new programmers is this:

WHATEVER YOU DO, no matter how simple, easy, or trivial your program is, DO NOT WRITE MORE THAN 5 LINES OF CODE BEFORE YOU COMPILE/TEST/DEBUG.

Heck, I'd say the LESS you write before going through this cycle, the better. If I assume the original 100 lines of code you posted were all written before you ever compiled anything, you're in for a world of self-induced pain.

If you only write one line and re-compile, you know exactly which line has the error. if you only write one line and test, and find a new bug, you know something about the one line is causing (or revealing) the bug.

for your own sanity, get in the habit of compiling as often as you can NOW.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:The single most important piece of advice I give new programmers is this:

WHATEVER YOU DO, no matter how simple, easy, or trivial your program is, DO NOT WRITE MORE THAN 5 LINES OF CODE BEFORE YOU COMPILE/TEST/DEBUG.

Heck, I'd say the LESS you write before going through this cycle, the better. If I assume the original 100 lines of code you posted were all written before you ever compiled anything, you're in for a world of self-induced pain.

If you only write one line and re-compile, you know exactly which line has the error. if you only write one line and test, and find a new bug, you know something about the one line is causing (or revealing) the bug.

for your own sanity, get in the habit of compiling as often as you can NOW.



That's very good advice, and I'll take it a step further by saying that often I will compile a class knowing full well that it contains errors. For example I may have a main() method that contains a bunch of calls to other methods, but the other methods aren't yet written. If all my compile errors are because of method not found, then I know that all is as it should be, and I can then focus on writing my methods.
 
Dominic Griffin
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the advice. I will surely put it into practice.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic