Why are the math calculations not working correctly?

M

mara.aelene

I can't figure out why the mathematical calculations are not working
in this program. It should be taking the duracion (number of days)
times the cost of the unit. This is for a university class and I
could really use the help.... please!



import java.util.Scanner;

public class Prob2DOS
{

public static void main (String[] args)
{

String titlePage = "\tWelcome to Yellowstone National " +
"Park's Lodging Reservation System.\n\n" +
"To complete your reservation, pleae answer the following
questions.\n" +
"(After each entry, press the ENTER key. Enter only one value per
question.\n\n";

Scanner dosInput = new Scanner(System.in);

//Declaring Variables
String customerNameString;
String customerUnitString;
String customerDateString;
int customerDate = 99/99/9999;
String customerDuracionString;
int customerDuracion = 9;
String customerCostString;
double customerCost = 999;
String customerTotalString = "";
double customerTotal;
String continueString = "y";

//Produce the title information
System.out.println("\t\t\tYellowstone National Park Lodging
Reservation System" +
"\n\t\tThis program produced by: Mara Francis\n");
while (continueString.equalsIgnoreCase("y"))
{
//Prompt and get input from User
System.out.print("Your Name?");
customerNameString = dosInput.next();
System.out.print("Which unit do you wish to reserve?");
customerUnitString = dosInput.next();
System.out.print("Arrival date (format: MM/DD/YYYY)?");
customerDateString = dosInput.next();
System.out.print("What is the duracion of your stay?");
customerDuracionString = dosInput.next();
System.out.print("What is the cost of per-night rental?");
customerCostString = dosInput.next();

//Conversions
customerCost = Double.parseDouble(customerCostString);

//Calculations
customerTotal = Math.round(customerCost * customerDuracion);

//Output
System.out.print("\nReservation for " + customerNameString + "\nYou
have reserved " + customerUnitString + " for "
+ customerDuracionString + " beginning on " + customerDateString +
".");

System.out.print("\nYour total cost is: $" + customerTotal);

System.out.print("\nYour price is based on a per night rental of $"
+ customerCostString);

System.out.print("\nDo you wish to enter more data? (Y/N)");
continueString = dosInput.next();


}
}
}
 
B

Ben Phillips

Let me guess: it's multiplying the cost by nine no matter what number of
days is input?

You have a single line in "conversions" to parse the cost, but none to
parse the days or other numbers (date, etc.); you even had compiler
errors about these "may have been used uninitialized" and went and
initialized them to dummy values. (The "date" which will come out as the
integer value zero being especially odd. I think you want a
java.util.Date and the "conversions" section using DateFormat here.)

Add the other conversions (and remove the dummy values; let the compiler
alert you if you miss any of the conversions until you have them all)
and it should then work properly.
 
J

Joshua Cranmer

I can't figure out why the mathematical calculations are not working
in this program. It should be taking the duracion (number of days)
times the cost of the unit. This is for a university class and I
could really use the help.... please!

The English word is `duration', not `duracion'. Furthermore, refrain
from asking homework questions on Usenet.

import java.util.Scanner;

public class Prob2DOS
{

public static void main (String[] args)
{

Don't use tabs in Usenet posts.
String titlePage = "\tWelcome to Yellowstone National " +
"Park's Lodging Reservation System.\n\n" +
"To complete your reservation, pleae answer the following
questions.\n" +
"(After each entry, press the ENTER key. Enter only one value per
question.\n\n";

Try to watch what you post so that you do not hit GG's 80-character
line-width limit.
Scanner dosInput = new Scanner(System.in);

//Declaring Variables
String customerNameString;
String customerUnitString;
String customerDateString;
int customerDate = 99/99/9999;

I doubt that your initialization of customerDate is what you want it to
be. Its value will be 0.

In addition, it is generally preferred in the Java world to declare
variables at the point of initialization and not at the beginnings of
functions.
String customerDuracionString;
int customerDuracion = 9;
String customerCostString;
double customerCost = 999;
String customerTotalString = "";
double customerTotal;
String continueString = "y";

//Produce the title information
System.out.println("\t\t\tYellowstone National Park Lodging
Reservation System" +
"\n\t\tThis program produced by: Mara Francis\n");
while (continueString.equalsIgnoreCase("y"))
{
//Prompt and get input from User
System.out.print("Your Name?");
customerNameString = dosInput.next();
System.out.print("Which unit do you wish to reserve?");
customerUnitString = dosInput.next();
System.out.print("Arrival date (format: MM/DD/YYYY)?");
customerDateString = dosInput.next();
System.out.print("What is the duracion of your stay?");
customerDuracionString = dosInput.next();

java.util.Scanner provides nextDouble(), nextInt(), etc. methods to
immediately parse at input time to the correct data type. Prefer those
over running the {Type}.parse{Type}() methods later on.
System.out.print("What is the cost of per-night rental?");
customerCostString = dosInput.next();

//Conversions
customerCost = Double.parseDouble(customerCostString);

//Calculations
customerTotal = Math.round(customerCost * customerDuracion);

I find that people learn best when guided towards the answer and not
told the exact problem. This line contains the first error that I found.
You should be able to find this error by following one of the two points
I made above:

1. Do not declare your variables until they are initialized.
2. Use the nextDouble(), etc. methods instead of next() + parseDouble(),
etc.

Other methods to solve your error:
3. Don't pre-initialize your declared variables.
4. Step through each line of code with a debugger that displays the
value of every single local variable.

My personal preference is that you use both steps 1 + 2 to solve your
answer.
 
M

mara.aelene

Let me guess: it's multiplying the cost by nine no matter what number of
days is input?

You have a single line in "conversions" to parse the cost, but none to
parse the days or other numbers (date, etc.); you even had compiler
errors about these "may have been used uninitialized" and went and
initialized them to dummy values. (The "date" which will come out as the
integer value zero being especially odd. I think you want a
java.util.Date and the "conversions" section using DateFormat here.)

Add the other conversions (and remove the dummy values; let the compiler
alert you if you miss any of the conversions until you have them all)
and it should then work properly.


Thank you to you both, (although sorry but the second response was
much more helpful) I am trying to learn jav, and am two weeks into a
class. Appreciate the first response but doesn't seem to help me
learn... had no idea what you were talking about. :)

Ben - I thought "int" in the variable section was to declare an
integer, not an initialization... The dates didn't really need to be
parsed, I didn't think be cause I only need the duration multiplied by
the cost. And yes, it is coming up with an answer in multiple of
nine... but can't understand why at all.

Sorry for asking a "school" question here - but I thought it was a
forum to get help and I'm really trying to learn this stuff.
 
M

mara.aelene

Ok so, it's multiplying by 9 of course, because of the variable
section. :) I thought 9 could be used as a integer declaration, like
in COBOL. I take it, no it cant
 
B

Ben Phillips

Thank you to you both, (although sorry but the second response was
much more helpful) I am trying to learn jav, and am two weeks into a
class. Appreciate the first response but doesn't seem to help me
learn... had no idea what you were talking about. :)

Sorry -- hard sometimes to gauge someone's level of knowledge from one
post. Joshua makes some good points, although I feel he might have been
more diplomatic about some of them -- perhaps we both could.

Nonetheless, asking yourself simply how I knew it was multiplying by
nine might give an idea as to how one might go about investigating a
bug. :) (And no, I didn't compile and run it either.)
Ben - I thought "int" in the variable section was to declare an
integer, not an initialization...

It is. The initialization occurs if you put "int variableName = value;"
rather than just "int variableName;", or later if you put "variableName
= value;" for the first time.
> The dates didn't really need to be parsed, I didn't think be cause
> I only need the duration multiplied by the cost. And yes, it is
> coming up with an answer in multiple of nine... but can't
> understand why at all.

Study it until you do; trace through the code seeing how the numbers
travel and get to where they're going. Or start somewhere and work your
way backwards -- where do the two variables in the multiplication get
their values? Work upwards from the multiplication until you find each.

If the dates are just read and later printed back to the screen, and
don't need to be validated or otherwise interpreted by the code, I'd
just store them in a String and feed them back to the output whenever
needed, without trying to parse them into Date objects, integers, or
anything else.
 
M

mara.aelene

Sorry -- hard sometimes to gauge someone's level of knowledge from one
post. Joshua makes some good points, although I feel he might have been
more diplomatic about some of them -- perhaps we both could.

Nonetheless, asking yourself simply how I knew it was multiplying by
nine might give an idea as to how one might go about investigating a
bug. :) (And no, I didn't compile and run it either.)


It is. The initialization occurs if you put "int variableName = value;"
rather than just "int variableName;", or later if you put "variableName
= value;" for the first time.


Study it until you do; trace through the code seeing how the numbers
travel and get to where they're going. Or start somewhere and work your
way backwards -- where do the two variables in the multiplication get
their values? Work upwards from the multiplication until you find each.

If the dates are just read and later printed back to the screen, and
don't need to be validated or otherwise interpreted by the code, I'd
just store them in a String and feed them back to the output whenever
needed, without trying to parse them into Date objects, integers, or
anything else.

Well I've got it figured out and traced it back. But I don't
understand what value I can use in declaring the variables. Whatever
number I use, it's trying to multiply by that number, not what's
entered in from the user prompt. Don't understand what values are
used to "store"
 
M

Matt Humphrey

| (e-mail address removed) wrote:
| > I can't figure out why the mathematical calculations are not working
| > in this program. It should be taking the duracion (number of days)
| > times the cost of the unit. This is for a university class and I
| > could really use the help.... please!
|
| The English word is `duration', not `duracion'. Furthermore, refrain
| from asking homework questions on Usenet.

It is perfectly reasonable to ask homework questions on this group (although
comp.lang.java.help is a better choice) provided that the poster has not
simply posted the homework assignment or asked the group to do the homework
for him/her and has made an attempt at solving the problem.

http://mindprod.com/jgloss/homework.html
http://mindprod.com/jgloss/newsgroups.html#RESPONSES (homework section)

Matt Humphrey http://www.iviz.com/
 
R

Roedy Green

customerTotal = Math.round(customerCost * customerDuracion);

Here is a strategy to solve this.

1. Verify that the problem is truly where you think it is. Do this by
using System.out.println to dump the values of the input to the
calculation.

2. read up on the docs for any methods you use. Don't just assume
they behave the way they "should". See
http://mindprod.com/jgloss/round.html
Pay particular attention to the types of the parameters and the
results.

P.S. the spelling is "duration" not "duracion".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,979
Messages
2,570,184
Members
46,721
Latest member
DollieSawt

Latest Threads

Top