Intro Programming: Computing Total Cost

In summary: 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?The cost of 6 tacos and 4 drinks would be "totalCost= 6*3+ 4*2".
  • #1
rawxlimits
1
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
  • #2
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?
 
  • #3
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;
   }
}
 
  • #4
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;
 
  • #5
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.
 
  • #6
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. ;)
 
  • #7
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?
 
  • #8
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. ;)
 
  • #9
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?
 

FAQ: Intro Programming: Computing Total Cost

What is intro programming?

Intro programming, also known as introductory programming, is the process of learning the fundamentals of computer programming. It involves understanding basic concepts such as variables, data types, control structures, and algorithms.

What does "computing total cost" mean?

"Computing total cost" refers to the process of calculating the total cost of a product or service. This involves considering all the individual costs associated with the product or service, including materials, labor, and overhead expenses.

Why is computing total cost important?

Computing total cost is important because it allows businesses to determine the true cost of their products or services. This information can then be used to set prices, make decisions about production and operations, and plan for future growth and profitability.

What are some common programming languages used for computing total cost?

There are many programming languages that can be used for computing total cost, including Java, Python, C++, and Ruby. The choice of language often depends on the specific needs and preferences of the programmer or company.

Is programming experience required for computing total cost?

No, programming experience is not always required for computing total cost. There are many user-friendly software programs and tools available that can handle the calculations and analysis without the need for extensive programming knowledge. However, having some programming skills may be helpful for customizing and fine-tuning the process.

Similar threads

Back
Top