2 Replies - 609 Views - Last Post: 29 April 2012 - 03:50 PM Rate Topic: -----

#1 himraj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • 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!
Was This Post Helpful? 1
  • +
  • -

#3 himraj  Icon User is offline

  • New D.I.C Head

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

Re: calling method in coffee program

Posted 29 April 2012 - 03:50 PM

View PostRyano121, 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.:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1