Path: csiph.com!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: Wages not calculating correctly Date: Wed, 18 Apr 2012 23:35:14 -0700 Organization: albasani.net Lines: 115 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net 69DgRxrBLO6UWW0d3yv4Oird9Rvcce2tvpVFX/z5BSTHKK5rv2L0+gnvJJOexAj27WpCq+0qC7mI3jGSTF/+Q8fltobvIKI17MnmbQV4hdk3fU1kIArXg/bX/IlgmuR3 NNTP-Posting-Date: Thu, 19 Apr 2012 06:35:16 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="CM96YAaMWBHJBvMT7ycqSBItjvZMeSbweNySu6N0Bj6IdBu2ou5gLh5TlkhUoX1dI38ooIFpvFMCu0TIWPRogxDnNkO47M9cZH2DS0A5hCUpB8XYXhixC1Gn4vCWCXYf"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:11.0) Gecko/20120329 Thunderbird/11.0.1 In-Reply-To: Cancel-Lock: sha1:sTfU7lkH8stUvIwxH5idVQETryQ= Xref: csiph.com comp.lang.java.programmer:13656 Simfonika Island wrote: > My program is not calculating the wages correctly. Can somebody please > tell me why??? > > /* > * PayCheck Program > * This program computes an employee's wages for the week > */ > import java.util.Scanner; > import static java.lang.System.in; You don't need to specify the 'java.lang'. > import static java.lang.System.out; > > class mainPayCheckProg > { > public static void main(String args[]) > { > // create an instance of the class or object > payCheckMethods aWage = new payCheckMethods(); > > Scanner prompt_user = new Scanner(in); > > double payRate; > double hours; > double wages; > int empNum; > > double max_hours = 40.0; > double overtime = 1.5; > > out.print("Enter Employee Number: "); > empNum = prompt_user.nextInt(); > out.print("Enter Pay Rate: "); > payRate = prompt_user.nextDouble(); > out.print("Enter Hours Worked: "); > hours = prompt_user.nextDouble(); > > if (hours > max_hours) > { > wages = (max_hours * payRate) > + (hours - max_hours) * payRate * overtime; > } > else > { > wages = max_hours * payRate; > } > > out.print("Employee Number: "); > out.println(empNum); > out.print("Pay rate: "); > out.println(payRate); > out.print("Hours worked: "); > out.println(hours); > out.print("Wages: "); > out.println(wages); > } > } Others have highlighted nicely the areas you ought to investigate closely. The example apparently is an academic exercise, and as such the key word is "exercise", meaning you actually have to work it yourself, and that's why the responses were somewhat indirect. They are helping you. Rest assured that if you follow through on their hints you will see the source of the difficulty. This is the universal phenomenon of computer programming, and a skill you will be glad you've mastered - that of digging to the root cause of what you observe. In this case you will come to an understanding of how variables convey information, or more precisely, data, or more precisely still for Java in particular, pointers to data and some primitives. Gene Wirchenko even gave you a starting point. Roedy's advice to paper-fake the program is a foundational practice. There's also precision of how you report an anomaly. There are useful debugging strategies that move you quickly to enlightenment and good software. The first is to report all observations surrounding the anomaly. For example, you observed that the "program is not calculating the wages correctly". OK. That's not a lot of analysis yet. What exactly is the program calculating (copy and paste actual output)? What did you expect? This is the heart of program testing - at its simplest, a pairing-up ("map") of preconditions and results (inputs and outputs). You type in "blahblah", the program prints "bleepbleep". You expected "bloopbloop". That begins to be specific. At that point, you see from the code how that "bleepbleep" got calculated as you trace how the variables transform their referenced structures. Or in your case, primitive 'double's. By the way, you should declare the class 'public'. Lastly, there are coding conventions. They differ, even to being opposite, between computer languages. They cover naming, spelling, indentation, all that boring (not really) crap. For Java they start with (and for a lot of folks, end with) Some of the conventions, like declaring arrays with the brackets after the type, or the variable, but not both, are in the Java Language Specification (JLS) itself. 'We do not recommend "mixed notation" in an array variable declaration, where brackets appear on both the type and in declarators.' Generally you see the brackets on the type in Java code. -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg