MHB Intro Programming: Computing Total Cost

  • Thread starter Thread starter rawxlimits
  • Start date Start date
  • Tags Tags
    Intro Programming
AI Thread Summary
The discussion focuses on calculating the total cost of drinks and tacos in a Java program. The cost per drink is set at 2 dollars and per taco at 3 dollars. To compute the total cost, the formula used is totalCost = numTacos * 3 + numDrinks * 2. Participants emphasize the importance of using variables for prices instead of hardcoding values, as it simplifies future adjustments. The conversation highlights the practical application of programming concepts in real-life scenarios, such as calculating costs manually.
rawxlimits
Messages
1
Reaction score
0
A drink costs 2 dollars. A taco costs 3 dollars. Given the number of each, compute total cost and assign to totalCost. Ex: 4 drinks and 6 tacos yields totalCost of 26.

Code:
import java.util.Scanner;

public class ComputingTotalCost {
   public static void main (String [] args) {
      int numDrinks = 0;
      int numTacos  = 0;
      int totalCost = 0;

      numDrinks = 4;
      numTacos  = 6;
      
       <____*MY section to fill out* 

      System.out.print("Total cost: ");
      System.out.println(totalCost);

      return;
   }
}
Can someone help me learn to write this i have multiple assigntments i don't undeerstand the question its asking me to write inside the field i posted above
 
Technology news on Phys.org
I would define variables that hold the cost per item for drinks and tacos:

Code:
      int costPerTaco = 3;
      int costPerDrink = 2;

Now, given the cost per each item and the number of each item sold, can you state the formula we must use to compute the total cost? Not necessarily a line of code, but just the method that we would use. For example, if you were going to compute the cost using a pocket calculator, how would you go about doing it?
 
Just to follow up, to find the total cost, we compute the product of the cost per item and the number of items (for each menu item) and sum each associated product. So, your finished code would look like:

Code:
import java.util.Scanner;

public class ComputingTotalCost {
   public static void main (String [] args) {
      int numDrinks = 0;
      int numTacos  = 0;
      int totalCost = 0;

      numDrinks = 4;
      numTacos  = 6;
      
      int costPerTaco = 3;
      int costPerDrink = 2;

      totalCost = costPerTaco*numTacos + costPerDrink*numDrinks;

      System.out.print("Total cost: ");
      System.out.println(totalCost);

      return;
   }
}
 
The answer to the question is actually easier than you think, you only need to put in one solution and can not change or add variables. The solution is as follows:

totalCost = numTacos * 3 + numDrinks * 2;
 
fbarreira said:
The answer to the question is actually easier than you think, you only need to put in one solution and can not change or add variables. The solution is as follows:

totalCost = numTacos * 3 + numDrinks * 2;

This is the right answer. thank you for the help.
 
olyala said:
This is the right answer. thank you for the help.

That's a right answer, not the right answer. Personally, I prefer using variables rather than hard coding values in expressions. ;)
 
MarkFL said:
That's a right answer, not the right answer. Personally, I prefer using variables rather than hard coding values in expressions. ;)

could you possibly tell me the difference?
 
olyala said:
could you possibly tell me the difference?

The difference is primarily if you need to change the prices, you only need to change the value of the variables, rather than change every expression that depends on those values. This can be a real time saver, especially for larger programs. It is a good practice to use in programming. ;)
 
olyala said:
could you possibly tell me the difference?
In other words, MarkFL would also define constants "TacoPrice" and "DrinkPrice" and set "TacoPrice= 3" and "DrinkPrice= 2" at the beginning of the problem. The total cost then would be "totalCost= TacoPrice*numTacos+ DrinkPrice*numDrinks".

The advantage is that if those costs changed, he would have only two lines to change rather than searching the program for every occurrence of "3" and "2" and changing those (and making sure that each "3" or "2" was actually a price and not some other use for those numbers).

I am a bit concerned that you felt you had to ask for help on the original problem. Suppose you knew that tacos were 3 dollars each and that drinks were 2 dollars each. If you were not programming a computer but actually need to find the total price using paper and pencil, how would you have found the cost of 6 tacos and 4 drinks?
 
Back
Top