Results 1 to 4 of 4
- 01-16-2012, 10:46 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 6
- Rep Power
- 0
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-16-2012, 11:03 PM #2
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.
- 01-17-2012, 01:00 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 6
- Rep Power
- 0
Re: Use methods to perform the repeated statements
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 hereJava 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); } }
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 01:38 AM. Reason: change I to code
- 01-17-2012, 01:37 AM #4
Similar Threads
-
removing repeated entries in arraylist
By ankit1801 in forum New To JavaReplies: 1Last Post: 04-15-2011, 06:34 AM -
Return statements in methods
By adjit in forum New To JavaReplies: 12Last Post: 03-17-2011, 02:31 PM -
Finding the most repeated names in a list
By jboy in forum New To JavaReplies: 2Last Post: 09-17-2009, 03:08 PM -
Stoping repeated entries in JComboBox
By MasterDeveloper in forum AWT / SwingReplies: 5Last Post: 04-05-2009, 12:07 PM -
Remove repeated code
By FraggleBoDiddly in forum New To JavaReplies: 6Last Post: 10-26-2008, 02:28 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks