You are viewing our Forum Archives. To view or take place in current topics click here.
Java Program Help!
Posted:

Java Program Help!Posted:

jkush12
  • Challenger
Status: Offline
Joined: Jan 10, 201212Year Member
Posts: 197
Reputation Power: 8
Status: Offline
Joined: Jan 10, 201212Year Member
Posts: 197
Reputation Power: 8
Question: Write a Temperature class that will hold a temperature in Fahrenheit and provide methods to convert to Celsius and kelvin.

I am getting an error and i underlined where my problem is and i don't know what to do to fix it. This is the error:

Error: constructor Temperature in class Temperature cannot be applied to given types;
required: no arguments
found: double
reason: actual and formal argument lists differ in length


Code:
import java.util.Scanner;

public class TemperatureTest {
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.print("Enter Fahrenheit temperature: ");
double ftemp = sc.nextDouble();

Temperature temp = new Temperature(ftemp);
System.out.println("The temperature in Fahrenheit is " + temp.getFahrenheit());
System.out.println("The temperature in Celsius is " + temp.getCelsius());
System.out.println("The temperature in Kelvin is " + temp.getKelvin());

}
}

class Temperature {

double ftemp;
Temperature(double ftemp) {
this.ftemp = ftemp;
}

double getFahrenheit(){
return ftemp;
}
double getCelsius(){
return ((double)5/9*(ftemp-32));
}
double getKelvin(){
return (((double)5/9*(ftemp-32))+273);
}
}
#2. Posted:
Xaldin
  • TTG Addict
Status: Offline
Joined: Oct 09, 201014Year Member
Posts: 2,358
Reputation Power: 106
Status: Offline
Joined: Oct 09, 201014Year Member
Posts: 2,358
Reputation Power: 106
try making the constructor public.
#3. Posted:
jkush12
  • Challenger
Status: Offline
Joined: Jan 10, 201212Year Member
Posts: 197
Reputation Power: 8
Status: Offline
Joined: Jan 10, 201212Year Member
Posts: 197
Reputation Power: 8
So i tried a lot of things but it kept doing the same thing. So i used the command prompt to try it out instead of using Dr.Java which for some reason worked. I'm assuming Dr.Java is very particular about the way the code is set up.
#4. Posted:
-Deano
  • Rated Awesome
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Xaldin wrote try making the constructor public.

If no access type is provided (private/protected/public) it will be public by default.

jkush12 wrote So i tried a lot of things but it kept doing the same thing. So i used the command prompt to try it out instead of using Dr.Java which for some reason worked. I'm assuming Dr.Java is very particular about the way the code is set up.


Glad you got it working.
I would try using a different IDE if DRJava was giving you problems.
#5. Posted:
Xaldin
  • TTG Addict
Status: Offline
Joined: Oct 09, 201014Year Member
Posts: 2,358
Reputation Power: 106
Status: Offline
Joined: Oct 09, 201014Year Member
Posts: 2,358
Reputation Power: 106
-Deano wrote
Xaldin wrote try making the constructor public.

If no access type is provided (private/protected/public) it will be public by default.


Are you sure? Stack overflow said that was not the case with java. I dont know that much java so im just curious
#6. Posted:
ObscureCoder
  • Resident Elite
Status: Offline
Joined: Jun 29, 201311Year Member
Posts: 211
Reputation Power: 13
Status: Offline
Joined: Jun 29, 201311Year Member
Posts: 211
Reputation Power: 13
Xaldin wrote
-Deano wrote
Xaldin wrote try making the constructor public.

If no access type is provided (private/protected/public) it will be public by default.


Are you sure? Stack overflow said that was not the case with java. I dont know that much java so im just curious


It's not public as such, it's package private which is almost the same thing. In this example, it's public enough.
Either way, visibility modifiers should be explicit anyway.
#7. Posted:
-Deano
  • PC Master Race
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
ObscureCoder wrote
It's not public as such, it's package private which is almost the same thing. In this example, it's public enough.
Either way, visibility modifiers should be explicit anyway.


Fair enough. I was informed it was fully public. I do agree they should be explicit anyway, helps with readability and easier to make mistakes without them.
#8. Posted:
Xaldin
  • TTG Addict
Status: Offline
Joined: Oct 09, 201014Year Member
Posts: 2,358
Reputation Power: 106
Status: Offline
Joined: Oct 09, 201014Year Member
Posts: 2,358
Reputation Power: 106
ObscureCoder wrote
Xaldin wrote
-Deano wrote
Xaldin wrote try making the constructor public.

If no access type is provided (private/protected/public) it will be public by default.


Are you sure? Stack overflow said that was not the case with java. I dont know that much java so im just curious


It's not public as such, it's package private which is almost the same thing. In this example, it's public enough.
Either way, visibility modifiers should be explicit anyway.

Ok thanks, yea I saw that package-private thing, good to know.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.