# calling method in coffee program

Page 1 of 1

## 2 Replies - 1085 Views - Last Post: 29 April 2012 - 03:50 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=276296&amp;s=024bd75ff30ccd8051f17d1f8244693e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 himraj

Reputation: 0
• Posts: 5
• Joined: 18-April 12

# calling method in coffee program

Posted 22 April 2012 - 12:14 PM

Bill's Coffee Shop sells coffee on the Internet for \$20.00 per pound, plus the cost of shipping. Each order ships for \$1.00 per pound, plus \$5.00 fixed cost for overhead. Write a program that calculates the cost of an order, given the number of pounds of coffee purchased. The program should follow the structured chart in fig#1.The main method calls three methods, getPounds: to obtain the pounds of coffee the user wants, calCost: to calculate the cost of the order and display: to display the cost of an order. An example run would look like this:

Welcome to “Bill’s Coffee” on the Internet:
How many pounds would you like? 5
Your total cost is \$58.30 for 5 lbs
Thank you!

Here's what i did so far..

```import java.util.Scanner;
public class Assignment3 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
float cost = 0;
String prompt = "";
System.out.println("Welcome to “Bill's Coffee” on the Internet:");
getPounds();
calCost(cost);
display(prompt);
System.out.println("Thank you!");
}
public static void getPounds() {
Scanner input = new Scanner (System.in);
String prompt;
prompt = "How many pounds would you like?";

System.out.print(prompt);
float pounds = input.nextInt();

}

public static float calCost( float pounds) {

float cost;
cost =(pounds * 20) + (pounds * 1) + 5;
return cost;
}
public static String display(String prompt) {
float cost = 0;
prompt = "Your total cost is \$";
calCost(cost);
System.out.printf("%s%.2f for 5 lbs\n",prompt,cost);
return prompt;
}

}
```

Is This A Good Question/Topic? 0

## Replies To: calling method in coffee program

### #2 Ryano121

• D.I.C Lover

Reputation: 1444
• Posts: 3,246
• Joined: 30-January 11

## Re: calling method in coffee program

Posted 22 April 2012 - 12:27 PM

I think you are having some variable scoping problems. For example in the getPounds method the variable pounds then cannot be used elsewhere. An improved structure -

```main()
{
float pounds = getPounds() // get input
float result = calcost(pounds) // calculate the result
display(result) // display the result
}
```

You will also need to modify the method a little. For example getPounds returns the input from the user, not void. Display will take as a parameter a float, not a String and there is no need to return the String as it already prints it out.

Good luck!

### #3 himraj

Reputation: 0
• Posts: 5
• Joined: 18-April 12

## Re: calling method in coffee program

Posted 29 April 2012 - 03:50 PM

Ryano121, on 22 April 2012 - 12:27 PM, said:

I think you are having some variable scoping problems. For example in the getPounds method the variable pounds then cannot be used elsewhere. An improved structure -

```main()
{
float pounds = getPounds() // get input
float result = calcost(pounds) // calculate the result
display(result) // display the result
}
```

You will also need to modify the method a little. For example getPounds returns the input from the user, not void. Display will take as a parameter a float, not a String and there is no need to return the String as it already prints it out.

Good luck!

hey thanks man..I finally got it to work correctly..my problem was knowing how to call the other methods in the main method..but u helped me.so thanks.