Results 1 to 4 of 4
  1. #1
    Hypnos is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Use methods to perform the repeated statements

    Hey, I'm working on an a homework problem, and am having an issue with this question.

    The ProceduralExer class converts Fahrenheit to Celsius and miles to kilometers, and prints out the converted values for -40F, 32F, 68F, 212F, 1 mile, 0.62137 miles, 62.137 miles and 100 miles. The code to convert Fahrenheit to Celsius, and miles to kilometers is repeated. The program can be made smaller by creating methods to perform the two conversions and print out the result. Arrays or ArrayLists can be used to further reduce the size of the program.

    Write a class, called MethodsOnly, that uses methods to perform the repeated statements in ProceduralExer. No loops are allowed.
    The MethodsOnly class should produce exactly the same output as ProceduralExer


    I don't see how you can make something repeat for the aid of Loops or Arrays, (i'm assuming arrays as well since the next part of the question is called "ArraysOnly"

    Any Help would be Appreciated

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Use methods to perform the repeated statements

    The MethodsOnly version sounds like it should recursive methods. There are lots of tutorials and examples on recursion.
    Start with Google or do a Search here on the forum for many code samples.

  3. #3
    Hypnos is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Use methods to perform the repeated statements

    Java Code:
    public class TEST 
    {
        public static void main(String[] args) 
        {
            double f1 = -40.0;
            double c1 = x(-40.0);
            System.out.printf("%6.2fF to %6.2fC ", f1, c1 );
    	double t1 = y(c1);
            System.out.println();
            double f2 = 32.0;
            double c2 = x(32.0);
            System.out.printf("%6.2fF to %6.2fC ", f2, c2 );
            System.out.println();
            double f3 = 68.0;
            double c3 = x(68.0);
            System.out.printf("%6.2fF to %6.2fC ", f3, c3 );
            System.out.println();
            double f4 = 212.0;
            double c4 = x(212.0);
            System.out.printf("%6.2fF to %6.2fC ", f4, c4 );
            System.out.println();
            System.out.println();
            double m1 = 1.0;
            double k1 = z(1.0);
            System.out.printf("%6.2fmiles to %6.2fkms%n", m1, k1 );
            double m2 = 0.62137;
            double k2 = z(0.62137);
            System.out.printf("%6.2fmiles to %6.2fkms%n", m2, k2 );
            double m3 = 62.137;
            double k3 = z(62.137);
            System.out.printf("%6.2fmiles to %6.2fkms%n", m3, k3 );
            double m4 = 100.0;
            double k4 = z(100.0);
            System.out.printf("%6.2fmiles to %6.2fkms%n", m4, k4 );
    	
        }
        public static double x(double a) 
        {
            if (a == -40)
                return -40;
            else
                return ((a - 32) / 1.8);
        }
        public static double y(double b) 
        {
    	if ( b < 0.0 ) 
    	{
    	    System.out.print(" Below Freezing");
    	}
    	else if ( b > 100.0 ) 
    	{
                System.out.print(" Above boiling");
            }
            else 
    	{
                System.out.print(" Water is liquid");
            }
        }
        public static double z(double c) 
        {
    	if (c == 1)
    	    return 1.61;
    	else
    	    return (c * 1.609344);
        }
    }
    Ignore the fact it's grossly long, It's kind of supposed to be, anyway, the last thing it is i'm trying to do, is to take the Celsius values, found here

    public static double x(double a)
    {
    if (a == -40)
    return -40;
    else
    return ((a - 32) / 1.8);
    }


    and say whether they are freezing or liquid or boiling, when i try however, i get the following message

    javac TEST.java
    TEST.java:58: missing return statement
    }
    ^
    1 error


    I can't figure out why!
    Any Help?!
    Last edited by Norm; 01-17-2012 at 02:38 AM. Reason: change I to code

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Use methods to perform the repeated statements

    TEST.java:58: missing return statement
    Is that inside a method that says it will return a value (not void) but the compiler can't find it and thinks the return statement is missing?

Similar Threads

  1. removing repeated entries in arraylist
    By ankit1801 in forum New To Java
    Replies: 1
    Last Post: 04-15-2011, 07:34 AM
  2. Return statements in methods
    By adjit in forum New To Java
    Replies: 12
    Last Post: 03-17-2011, 03:31 PM
  3. Finding the most repeated names in a list
    By jboy in forum New To Java
    Replies: 2
    Last Post: 09-17-2009, 04:08 PM
  4. Stoping repeated entries in JComboBox
    By MasterDeveloper in forum AWT / Swing
    Replies: 5
    Last Post: 04-05-2009, 01:07 PM
  5. Remove repeated code
    By FraggleBoDiddly in forum New To Java
    Replies: 6
    Last Post: 10-26-2008, 03:28 PM

Tags for this Thread

Posting Permissions

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