Value-returning Method
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Value-returning Method

  1. #1
    Join Date
    Jul 2012
    Posts
    3

    Value-returning Method

    Code:
    public static void main(String[] args) {
    		
    		
    		double cups, ounces;
    		
    		System.out.println("Enter number of cups");
    	        cups = getCups(); :wave:
    		ounces = CupsToOunces(cups);
    		displayResults(cups,ounces);
    		
    		System.exit(0);
    		
    	}
    		
    	
    
    		
    		
    		public static double getCups(double numberOfCups)
    		
    		{
    			
    		
    			
    			Scanner input = new Scanner(System.in);
    			
    			numberOfCups = input.nextDouble();
    			
    			return numberOfCups;


    Can you guys advise on the method call getCups();.

  2. #2
    Join Date
    Aug 2011
    Location
    West Yorkshire, U.K.
    Posts
    54

    Re: Value-returning Method

    You do not need to pass in the argument (double numberOfCups) to getCups. You should declare numberOfCups as a double within the method.
    You could make the method more resilient. What would happen if the user entered a string (e.g. "Hello") or a negative number?
    You could pass the question string "Enter number of cups" as a String argument, check that the user enters a sensible value and return the value if it is, otherwise provide some guidance and ask the question again.

  3. #3
    Join Date
    Jul 2012
    Posts
    3

    Re: Value-returning Method

    Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center