I am writing a code in 2 separate classes that takes a temp as 2 separate variables and prints it out and then runs conversion methods to get temps in other scales. I keep getting the error non-static variable value cannot be referenced from a static context in the Temperature.java part of my code because of the variables being in the constructor which is not static. The whole concept is a bit confusing to me and I'd love any input on hope static and non-static works and how you can switch between them without issue.
code as follows:
public class Temperature
{
int value = 50;
String scale = "F";
public Temperature(int value, String scale){
value = value;
scale = scale;
}
public static void value(int value){
int number;
}
public static boolean scale(String scale){
if (scale == "C"){
return true;
}
else if (scale == "F"){
return true;
}
else if (scale == "K"){
return true;
}
else{
return false;
}
}
public static void convertToCelsius(int value, String scale){
if (scale == "F"){
int newValue = (5/9) * (value - 32);
System.out.println("The equivalent tempurature is " + newValue + "C");
}
else if (scale == "K"){
double newValue = value - 273.15;
System.out.println("The equivalent tempurature is " + newValue + "C");
}
else{
}
}
public static void convertToFarenheit(int Value, String scale){
if (scale == "C"){
int newValue = ((9/5) * (value + 32));
System.out.println("The equivalent tempurature is " + newValue + "F");
}
else if (scale == "K"){
int newValue = ((9/5) * (value - 273) + 32);
System.out.println("The equivalent tempurature is " + newValue + "F");
}
else{
}
}
public static void convertToKelvin(int value, String scale){
if (scale == "F"){
int newValue = ((5/9) * (value - 32) + 273);
System.out.println("The equivalent tempurature is " + newValue + "C");
}
if (scale == "C"){
double newValue = (value + 273.15);
System.out.println("The equivalent tempurature is " + newValue + "C");
}
else{
}
}
}
for the main method the code is in the separate class as follows:
public class UsingTemperature
{
public static void main(String[] args)
{
Temperature t = new Temperature(50, "F");//declaring an object
System.out.println(t);
// Test out conversions:
System.out.println("Test out conversions:");
// F to C
t.convertToCelsius();
System.out.println(t);
// C to F
t.convertToFahrenheit();
System.out.println(t);
// F to K
t.convertToKelvin();
System.out.println(t);
// K to F
t.convertToFahrenheit();
System.out.println(t);
// F to K:
t.convertToKelvin();
System.out.println(t);
// K to C
t.convertToCelsius();
System.out.println(t);
// C to K
t.convertToKelvin();
System.out.println(t);
}
}