aspose file tools*
The moose likes Beginning Java and the fly likes My program wont work. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "My program wont work. " Watch "My program wont work. " New topic
Author

My program wont work.

Dominic Griffin
Greenhorn

Joined: Jun 10, 2009
Posts: 16
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

W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
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.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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

Joined: Jun 10, 2009
Posts: 16
How do I place codes inside of code tags?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Use Code Tags.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dominic Griffin
Greenhorn

Joined: Jun 10, 2009
Posts: 16
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

Joined: Jun 10, 2009
Posts: 16
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

Joined: May 13, 2009
Posts: 679
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

Joined: Feb 10, 2009
Posts: 710
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

Joined: Jun 10, 2009
Posts: 16



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

Joined: Jun 10, 2009
Posts: 16
What do you mean when you say no method exists for totalCost ()?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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

Joined: Feb 10, 2009
Posts: 710
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

Joined: May 13, 2009
Posts: 679
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.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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

Joined: Jun 10, 2009
Posts: 16
Thank you for the advice. I will surely put it into practice.
 
 
subject: My program wont work.