Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Is My Math Wrong?

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Is My Math Wrong?

    Hi everyone,

    This will be my first post in the forums! Have an assignment where user has to input number of people at a party, the amount of pizza slices per person and then how many pizzas will it require (large, medium or small pizzas each with 12, 6, and 4 slices respectively) to feed all the people WHILE keeping the number of left-over slices to a minimum of 3. Now, I got the whole program basically up and running but I think my math is wrong, because whenever I attempt to enter test data (number of people 17 and 4 slices per person) the answer come back with only 66 instead of 68 and say that i still have 2 "extra" slices, when in fact I dont. I am missing two. The correct answer should be 70 slices with 2 leftover (because 5 x 12 + 1x6 + 1x4 = 70).

    Here is the code:
    import java.util.Scanner; //Importing the Scanner class.
     
    public class A_Gray_PizzaPartyMildVers
    {
     
    	public static void main(String[] args)
    	{
    			//Display title of program and output to screen
    			System.out.println("Aaron's Pizza Party Planning Program!");
    			System.out.println("*************************************");
     
    			//Create Scanner object
    			Scanner keyinput = new Scanner(System.in);
     
    			//Declare variables of how many people will be at the party and how many slices they will want each.
    			int numberOfPeople;
    			int numberOfSlices;
     
    			//Prompt user to input the amount of people at the party that want pizza and how many slices they will each want.
    			System.out.print("\nHow many people at your party would like some pizza: ");
    			numberOfPeople = keyinput.nextInt();
     
    			System.out.print("How many slices are being bought per person: ");
    			numberOfSlices = keyinput.nextInt();
     
    			//Calculate total number of slices for the people at the party and store in a variable.
    			int slicesNeeded = numberOfPeople * numberOfSlices;
    			System.out.println("\nYou will need " + slicesNeeded + " slices of pizza to feed all these hungry party-goers!");
     
    			//Declare + initialize variables for the pizza prices
    			double smallPizzaPrice = 7.99;
    			double mediumPizzaPrice = 11.59;
    			double largePizzaPrice = 19.35;
     
    			//Declare variables for the number of pizzas per size and the total number of slices of the order
    			int numberOfSmallPizzas;
    			int numberOfMediumPizzas;
    			int numberOfLargePizzas;
    			int totalSlices = slicesNeeded; //Will be used to calculate extra slices by modulus division.
     
    			double orderCost = 0.00; //Declare + initialize variable of the total cost of the pizza order
    			int temp = 0; //Declare + initialize placeholder variable.
     
     
    			//Create nested-if structure to determine how many pizzas of each size will be needed to fulfill slicesNeeded.
    			//Also will calculate costs per total of pizza size (how many of each size of pizza will need to be bought).
     
    			if (slicesNeeded > 12)
    			{
    				temp = totalSlices % 12; 
    				numberOfLargePizzas = ( (totalSlices - temp) / 12);
    				System.out.println("\t" + numberOfLargePizzas +" large pizza(s) at $" + largePizzaPrice);
    				totalSlices = temp;
    				orderCost += (numberOfLargePizzas * largePizzaPrice);
     
    						if (slicesNeeded >= 6)
    						{
    								temp = totalSlices % 6;
    								numberOfMediumPizzas = ( (totalSlices - temp) / 6);
    								System.out.println("\t" + numberOfMediumPizzas +"  medium pizza(s) at $" + mediumPizzaPrice);
    								totalSlices = temp;
    								orderCost += (numberOfMediumPizzas * mediumPizzaPrice);
     
    						//end medium pizza if
     
    									if (slicesNeeded >= 4)
    									{
    											temp = totalSlices % 4;
    											numberOfSmallPizzas = ( (totalSlices - temp) / 4);
    											System.out.println("\t" + numberOfSmallPizzas +"  small pizza(s) at $" + smallPizzaPrice);
    											totalSlices = temp;
    											orderCost += (numberOfSmallPizzas * smallPizzaPrice);
     
    									}//end small pizza if
    						}
    				}//end outer if (large pizza)
     
    			System.out.println("\nYour order will include " + slicesNeeded + " slices which means there will be " 
    												+ totalSlices + " left-over.");
     
    			System.out.println("\nYour order comes to $ " + orderCost);
    	}//end main
    }//end class
    If anyone can give me a hint, that'd be great! Thanks a lot!
    Last edited by helloworld922; February 12th, 2013 at 08:31 PM. Reason: please use [code] tags


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Is My Math Wrong?

    Quote Originally Posted by grayaa93 View Post
    Is My Math Wrong?
    If you have correct input and incorrect output, I would say that something is wrong. (Maybe math.)

    Wait a minute. I'll get my trusty #2 Ticonderoga and some paper. I'll go through things a step at a time and see what my math comes up with.

    Tick.

    Tick.

    Tick.

    OK! I'm back.

    I entered the intermediate results in my text editor and pasted them here:
    Number of people = 17, number of slices per person = 4.
    Total number of slices needed = 68.
     
    Number of Large  pizzas = 5.
      After large  calculations, we have 60 slices so far.
      Number of slices left = 8.
     
    Number of Medium pizzas = 1.
      After medium calculations, we have 66 slices so far.
      Number of slices left = 2.
     
    Number of small  pizzas = 1.
      After small  calculations, we have 70 slices so far.
      Number of slices left = 2.
     
    Bottom line:
      Your order will include 70 slices.
      That means that there will be 2 slices left over.


    Now if that seems right, why not put some print statements in your program at strategic locations so that you can see what the program is seeing and doing at each step of the way? (If it doesn't seem right, then do your own math with pencil and paper and compare with output from the extra print statements that you put into your program.)

    I mean, debugging is a learned skill, just as much as is writing code. Maybe more so.


    Cheers!

    Z

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is My Math Wrong?

    That's exactly the issue im having! When I write it out on paper the problem happens after in the if (slicesNeeded >= 4) statement im sure.
    Writing it all out starting with the first if statement:
    if (slicesNeeded > 12)
    {
    temp = 68 % 12 = 8
    numberOfLargePizzas = ( (68 - 8) / 12) = 5 so therefore 5 large pizzas
    }
    if (slicesNeeded >= 6)
    {
    temp = 8 % 6 = 2
    numberOfMediumPizzas = ( (8 - 2) / 6) = 1 so therefore 1 medium pizza
    }

    It's at this point that the program fails. There are obviously only 66 slices accounted for, and I still need two, but for some reason the slicesNeeded >= 4 isnt going through.
    Also, I did a stub code after the second if statement and the temp variable is holding 2.
    So following through the third if statement:
    if (slicesNeeded >= 4)
    {
    temp = 2 % 4 = 2
    numberOfSmallPizzas = ( (2 - 2) /4) !!! theres the problem you cant divide 0 by 4.
    ...
    }

    So, how could I fix that? Would I remove the modulus division from temp or will have to include a different mathematical expression in order to do it?

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Is My Math Wrong?

    Quote Originally Posted by grayaa93 View Post
    ...

    It's at this point that the program fails. There are obviously only 66 slices accounted for, and I still need two...
    OK, you have located the point of failure: After doing the medium calculations there are two slices still needed. Well, How many small pizzas are needed at this point?

    Your program obviously doesn't work, so go through it "by hand" with pencil and paper. Back away from the computer. Don't look at the incorrect program. Don't even think about how you are going to implement this yet.

    Write this stuff down. Really. Write it down on paper. Use a pencil (and your brain).

    How do you get the following answers with pencil and paper?

    How many small pizzas are needed if the number of slices still needed is, say, 1?

    How about if two slices are still needed?

    How about 3?

    How about 4?

    How about 5?

    You know that there won't be six or more still needed, right? ( How to you know that?)

    Bottom line: Look at the results of hand calculations. Can you figure out a way to determine the number of small pizzas required by using some arithmetic expression? Don't think about anything else that has gone before and don't try to warp any previous work into making it fit for this last little bit.

    I mean, you could do it with a switch statement or even a bunch of if{}else{} junk, but where's the fun in that? Everything else is so beautifully mathematical, carry it out to the end if you can.


    Cheers!

    Z

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is My Math Wrong?

    Just wanted to first off come back and let you know I updated the code entirely, just to make it easier to read and also to fix some poor coding style on my own part, but the problem still stands. I think I am halfway to getting it though but here is the new code:

    import java.util.Scanner; //Importing the Scanner class.
    import java.text.DecimalFormat; //Importing DecimalFormat class.

    public class A_Gray_PizzaPartyMild
    {

    public static void main(String[] args)
    {
    //Variable declaration and initialization block.

    //Pizza prices and pizza sizes variables:
    final double SMALL_PIZZA_PRICE = 7.99;
    final double MEDIUM_PIZZA_PRICE = 11.59;
    final double LARGE_PIZZA_PRICE = 19.35;

    final int NUMBER_OF_SLICES_SMALL = 4;
    final int NUMBER_OF_SLICES_MEDIUM = 6;
    final int NUMBER_OF_SLICES_LARGE = 12;

    final int MAX_EXTRA_SLICES = 3;

    //Variables for number of people/slices and the number of pizzas of each size:
    int numberOfPeople = 0;
    int numberOfSlices = 0;

    int numberOfSmallPizzas = 0;
    int numberOfMediumPizzas = 0;
    int numberOfLargePizzas = 0;

    double orderCost = 0.00; //Will hold the total cost of the order after price calculations are completed.

    //Introduce program to user.
    System.out.println("\t\t\t " + "|**Welcome to**|");
    System.out.println("\t\t " + "|Aaron's Pizza Party Planning Program!|");
    System.out.println("|***************************** *******************************************|");

    //Create the Scanner object: keyinput.
    Scanner keyinput = new Scanner(System.in);

    //Create the DecimalFormat object: formatter.
    DecimalFormat formatter = new DecimalFormat("#0.00"); //This object will be used to round and format the orderCost variable
    //to two places.

    //Prompt user to input the amount of people at the party that want pizza and how many slices they will each want.
    System.out.print("\nHow many people at your party would like some pizza: ");
    numberOfPeople = keyinput.nextInt();

    System.out.print("How many slices are being bought per person: ");
    numberOfSlices = keyinput.nextInt();

    //Output required slices to feed the people at the party that requested pizza.
    int requiredSlices = numberOfPeople * numberOfSlices; //Variable that will hold the required number of slices for the guests.
    System.out.println("\nYou will need " + requiredSlices + " slices of pizza to feed all these hungry party-goers!");

    /*Make a sequence of if statements that will run and determine how many pizzas of each size are needed
    *to fulfill the required number of slices. The if structure will also calculate the cost for the order.
    *This will be done by taking the required pizzas of a certain size and multiplying them by their price
    *and adding it to the orderCost variable.
    */

    int currentSlices = requiredSlices; //Declare and initialize a variable that will keep track of remaining slices after each sequential step.

    if(currentSlices >= NUMBER_OF_SLICES_LARGE)
    {
    numberOfLargePizzas = (currentSlices / NUMBER_OF_SLICES_LARGE);
    currentSlices %= NUMBER_OF_SLICES_LARGE; /*Use modulus division to find out how many pizza slices remain.
    *This will give us a remainder which we will use to see how many medium or small pizzas,
    *if any, will be required.
    */
    System.out.println("\t" + numberOfLargePizzas +" large pizza(s) at $" + LARGE_PIZZA_PRICE);
    orderCost += (numberOfLargePizzas * LARGE_PIZZA_PRICE); //Storing the total cost of the large pizzas into the overall orderCost.
    }//end if

    if(currentSlices >= NUMBER_OF_SLICES_MEDIUM)
    {
    numberOfMediumPizzas = (currentSlices / NUMBER_OF_SLICES_MEDIUM); //Add MAX_EXTRA_SLICES in here?
    currentSlices %= NUMBER_OF_SLICES_MEDIUM;
    System.out.println("\t" + numberOfMediumPizzas +" medium pizza(s) at $" + MEDIUM_PIZZA_PRICE);
    orderCost += (numberOfMediumPizzas * MEDIUM_PIZZA_PRICE);
    }//end if

    if(currentSlices <= NUMBER_OF_SLICES_SMALL)
    {
    numberOfSmallPizzas++;
    System.out.println("\t" + numberOfSmallPizzas +" small pizza(s) at $" + SMALL_PIZZA_PRICE);
    orderCost += (numberOfSmallPizzas * SMALL_PIZZA_PRICE);
    }//end if

    currentSlices = (numberOfLargePizzas * NUMBER_OF_SLICES_LARGE) +
    (numberOfMediumPizzas * NUMBER_OF_SLICES_MEDIUM) +
    (numberOfSmallPizzas * NUMBER_OF_SLICES_SMALL); //Add total number of slices by pizza size to totalSlices.

    int extraSlices = currentSlices - requiredSlices; /*Declare and initialize a variable to hold the extra slices.
    *Will equate to the currentSlices in the order subtracted by the requiredSlices
    *needed in order to satisfy the party guests.
    */

    //Output the results of the calculations for totalSlices and extraSlices.
    System.out.println("\nYour order will include " + currentSlices + " slices, which means there will be " + extraSlices + " left-over!");

    //Output the results of the orderCost calculations
    System.out.println("\nYour order will come to $ " + formatter.format(orderCost));

    }//end main
    }//end class

    The project states that the minimum amount of extra slices we can have per order is 3, so that is the reason for the MAX_EXTRA_SLICES constant. So I actually managed to fix the first issue that we were having getting the 70 slices to output with an extra of 2. That now works. However, I am using new test data of 17 people ordering 1 slice each. This almost always returns 1 large pizza + 1 small pizza (only 16) and gives me a left-over of -1. Obviously that left-over should be +1 (1 large + 1 medium). I have been trying to find a way in my if statements to get the MAX_EXTRA_SLICES to act as range but after the first large pizza calculation, I am left with 5 slices remaining which seems to kind of just disappear. It doesnt fall into the medium pizza range but, if I was to write a condition that said if ((currentSlices >= NUMBER_OF_MEDIUM_SLICES) - MAX_EXTRA_SLICES)) it will fall under the small pizza range (<= NUMBER_OF_SMALL_SLICES...3 is less than 4). So, I am completely lost with this one. I've been trying to get help from a friend of mine who knows more than I do and he said to add MAX_EXTRA_SLICES to update currentSlices by adding it into the if statements and the calculations for the number of pizzas for a given size. I am completely lost now, ive tried every combination of if statements and ive written out the code and just cant seem to get it to click. Im probably missing the most obvious thing, but I just cant seem to get what to do.

Similar Threads

  1. Math.E - what exactly is it?
    By RedCloudTsunami in forum Java Theory & Questions
    Replies: 2
    Last Post: July 10th, 2012, 12:24 AM
  2. [SOLVED] Help with Math.tan and Math.atan
    By Dr.Code in forum Algorithms & Recursion
    Replies: 6
    Last Post: July 2nd, 2012, 05:54 AM
  3. Math.pow
    By britton33 in forum What's Wrong With My Code?
    Replies: 30
    Last Post: July 1st, 2012, 04:42 PM
  4. Math Game? not sure whats wrong?
    By melinko928 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 7th, 2011, 09:35 AM
  5. Confusion with Math.toDegrees() and Math.toRadians(). Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 01:28 AM