Intro Programming: Computing Total Cost

  • Context: MHB 
  • Thread starter Thread starter rawxlimits
  • Start date Start date
  • Tags Tags
    Intro Programming
Click For Summary

Discussion Overview

The discussion revolves around a programming assignment that requires calculating the total cost of drinks and tacos based on their quantities and prices. Participants explore different approaches to writing the code in Java, focusing on variable usage and coding practices.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant seeks assistance in completing a Java program to calculate total cost based on given quantities of drinks and tacos.
  • Another participant suggests defining variables for the cost of each item and asks for a method to compute the total cost, not necessarily in code form.
  • A follow-up response outlines the formula to compute total cost using the defined costs and quantities, providing a complete code example.
  • Some participants emphasize that the solution can be simplified to a single line of code without changing or adding variables.
  • There is a discussion about the merits of using variables versus hard-coded values in expressions, with some preferring the former for maintainability.
  • A participant expresses concern about the original poster's need for help, prompting a discussion about how one would calculate total cost manually without programming.

Areas of Agreement / Disagreement

Participants generally agree on the formula for calculating total cost, but there is disagreement on the best coding practices, particularly regarding the use of hard-coded values versus variables. The discussion remains unresolved regarding which approach is superior.

Contextual Notes

Some participants highlight the importance of defining constants for prices to facilitate easier updates in larger programs, while others focus on the immediate solution to the assignment without considering broader programming practices.

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?
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
3
Views
3K
Replies
1
Views
8K
  • · Replies 4 ·
Replies
4
Views
3K