'How to fix/ learning help understanding basic method and classes, Java code and word Problem
I'm struggling on this problem, given that I'm probably making a basic mistake or I haven't got a clue what I'm doing.
platform being a Zybook
the problem reads:
One lap around a standard high-school running track is exactly 0.25
miles. Write a program that takes a number of miles as input, and outputs the number of laps.Output
each floating-point
value with two digits after the decimal point, which can be achieved as follows:
System.out.printf("%.2f", yourValue);
Your program must define and call a method: public static double
milesToLaps(double userMiles)
It gives off a couple of example input 1.5 -> 6.00, 2.2 ->8.80 etc.
it gives you a start off of:
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
}
}
Below I've edited to this point, still I don't think I know what I'm doing. However I've gotten it down to one error which states that> error: cannot find symbol numMiles.printMilesToLaps(userMiles);
. I would like someone to tell me if I'm doing this someway maybe correctly and or if they can solve this error, cause I've completely redid the code multiple times, and I'm at the point of giving up and never submitting it.
also sometimes I start to remove code that's supposed to be there, but at the same time doesn't make sense to the computer ex. being (return;), but maybe you can explain it in greater detail then the zybook
or its checking system.
The code itself looked a bit neater, but I was forced to change it after the multiple errors.
import java.util.Scanner;
public class LabProgram {
public static double MilesToLaps(double userMiles){
Scanner scnr = new Scanner(System.in);
userMiles = scnr.nextDouble();
System.out.printf("%.2f", (userMiles / 0.25));
}
public static void main(String[] args) {
LabProgram numMiles = new LabProgram();
numMiles.printMilesToLaps(userMiles);
}
}
I'm getting constant cannot find symbol errors, mostly on the secondary public method. I could think I could fix this, but it wouldn't line up with the question parameters.
I'm usually only using the example input values in the input terminal.
Solution 1:[1]
There are a few issues with your posted code. The method should be named milesToLaps
(not MilesToLaps
). The method takes the user's input, and does not prompt. The method should return the result, not output it itself. And, I would multiply by 4 instead of dividing by 0.25
. Like,
public static double milesToLaps(double userMiles) {
return userMiles * 4;
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
double userMiles = scnr.nextDouble();
System.out.printf("%.2f%n", milesToLaps(userMiles));
}
Solution 2:[2]
Please go through some basic java tutorials(JavaTpoint, Toutrialspoint etc.) where you can get a basic understanding of class, object, methods how these things works.
public static double MilesToLaps(double userMiles)
{
Scanner scnr = new Scanner(System.in);
System.out.println("Enter a number");
userMiles = scnr.nextDouble();
double x = (userMiles / 0.25);
return x;
}
public static void main(String[] args)
{
//LabProgram numMiles = new LabProgram(); this object not require because your calling method MilesToLaps() is static.
double result = MilesToLaps(5); // here in this method you need to pass a double type value.
System.out.println("user miles :"+ result);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Elliott Frisch |
Solution 2 |