Reputation: 37
I've been at my wits end with this problem. In a class right now and for this assignment I'm needed to use the Scanner to input data and use this same data within my nested methods to calculate two things; Temperature Conversion and Currency Conversion.
Problem I'm having is I have no idea how to define my variables throughout my whole piece of code, as I get compiler errors saying that certain variables aren't defined; e.g. tC
, and euro
.
How do I assign those two variables to be able to compile this correctly?
import java.util.Scanner;
public class MethodsMS {
// required main method of the class
public static void main(String [] args) {
performTemperatureConversion();
System.out.println(); // blank line
performCurrencyConversion();
double numDouble;
} // end main
// (no changes needed above this point)
//--------------------------------------------------------------
// (write the following methods)
//--------------------------------------------------------------
// write method to perform temperature conversion...
public static void performTemperatureConversion()
{
double numDouble;
readDouble();
degreeConversion();
display();
}
//--------------------------------------------------------------
// write method to perform currency conversion...
public static void performCurrencyConversion()
{
readDouble();
currencyConversion();
display();
}
//--------------------------------------------------------------
// write method to obtain an input double value (use Scanner)...
public static double readDouble()
{
double numDouble;
Scanner input = new Scanner(System.in);
System.out.print("Enter a double number: ");
numDouble = input.nextDouble();
return numDouble;
}
//--------------------------------------------------------------
// write method to print an input string inside a formatted box...
public static void display()
{
System.out.print("Conversion number is now: " + numDouble);
}
//--------------------------------------------------------------
// write method to convert F to C...
public static void degreeConversion()
{
double numDouble, tC;
tC = (numDouble - 32) * 5/9;
return;
}
//--------------------------------------------------------------
// write method to convert $ to Euros...
public static void currencyConversion()
{
double euro;
euro = numDouble * 0.894614;
return euro;
}
} // end class
Upvotes: 1
Views: 6014
Reputation: 580
As I understand you want to be numDouble and tC two variables visible in all of those methods. Obviously you use the keyword "double" more like a variable import similar to the keyword "global" in PHP.
But your code does in fact declare the variable method local and throw it away at the end of the method. This way you can't share the variables over several method calls. The simplest way to fix this is to define two static methods at class level:
package stackoverflow;
import java.util.Scanner;
public class MethodsMS {
private static double numDouble, tC, euro;
// required main method of the class
public static void main(String[] args) {
performTemperatureConversion();
System.out.println(); // blank line
performCurrencyConversion();
} // end main
// (no changes needed above this point)
// --------------------------------------------------------------
// (write the following methods)
// --------------------------------------------------------------
// write method to perform temperature conversion...
public static void performTemperatureConversion() {
readDouble();
degreeConversion();
display();
}
// --------------------------------------------------------------
// write method to perform currency conversion...
public static void performCurrencyConversion() {
readDouble();
currencyConversion();
display();
}
// --------------------------------------------------------------
// write method to obtain an input double value (use Scanner)...
public static double readDouble() {
try (Scanner input = new Scanner(System.in)) {
System.out.print("Enter a double number: ");
numDouble = input.nextDouble();
}
return numDouble;
}
// --------------------------------------------------------------
// write method to print an input string inside a formatted box...
public static void display() {
System.out.print("Conversion number is now: " + numDouble);
System.out.print("tC is now: " + tC);
System.out.print("Euro is now: " + euro);
}
// --------------------------------------------------------------
// write method to convert F to C...
public static void degreeConversion() {
tC = (numDouble - 32) * 5 / 9;
}
// --------------------------------------------------------------
// write method to convert $ to Euros...
public static void currencyConversion() {
euro = numDouble * 0.894614;
}
} // end class
BTW: You also forgot to close the input variable. I fixed this.
Upvotes: 0
Reputation: 154
You need your numDouble variable to be a Class Member Variable.
This means that any method in your class can "see" it (i.e. can use the value or update the value.
To do this just define the variable outside of the methods, but still within the class itself.
By convention, and in most cases, you'll want this to be a private member variable, so you'll want to do something like this:
// Defined here as a private member variable
private double numdouble;
public static double readDouble()
{
// numDouble definition removed here, no need.
Scanner input = new Scanner(System.in);
System.out.print("Enter a double number: ");
numDouble = input.nextDouble();
return numDouble;
}
Then you just need to make sure that this readDouble() is called before numDouble is used.
Upvotes: 0
Reputation: 1
In the readDouble() method you are returning a double value but you are not getting back that value anywhere in your performTemperatureConversion() and performCurrencyConversion() methods. If you want to use the same value throughout the class declare a static variable and get the scanner value in it instead of using the local variable in methods. Pass the same value in methods degreeConversion() and currencyConversion().
Upvotes: 0
Reputation: 11882
You do not need any class-level variables for this particular example, as you can utilize method return types and parameters.
The currencyConversion
method should be defined like this:
public static double currencyConversion(double num)
{
return num * 0.894614;
}
And used like this in performCurrencyConversion
:
display(currencyConversion(readDouble()));
The same applies for display
:
public static void display(double num)
{
System.out.print("Conversion number is now: " + num);
}
And for the sake of completeness, your degreeConversion
:
public static double degreeConversion(double num)
{
return (num - 32) * 5 / 9;
}
And in performTemperatureConversion
:
display(degreeConversion(readDouble()));
If you do want to define a class-level variable, you can add a static
variable to the body of your class:
static double euro;
And use it in your methods.
Upvotes: 3
Reputation: 681
Instead of declaring (e.g.: double euro;
) inside the method, you can declare it as a class member:
static double euro;
Outside of the method, at the class level.
This should work for both euro
and tC
, but I need to advise that this is not the best way to use java, as it is an Object-Oriented language. But I'm sure you'll get there :).
Upvotes: 1