# Use methods to perform the repeated statements

• 01-16-2012, 11:46 PM
Hypnos
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
• 01-17-2012, 12:03 AM
Norm
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.
• 01-17-2012, 02:00 AM
Hypnos
Re: Use methods to perform the repeated statements
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?!
• 01-17-2012, 02:37 AM
Norm
Re: Use methods to perform the repeated statements
Quote:

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?