• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple Java Program (Celsius to Fahrenheit converter)  RSS feed

 
Adam Pritchardson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello out there,

I know that this a topic that has been on her before, but I can't seem to find the right solution. I have a question. I am working on a homework assignment for my class, and am really struggling with the direction that I need to go for my Celsius to Fahrenheit converter. I know it is a very simple program, but I have reached a mental block and just don't know what to do. I will research the program later, but I need help to come up with a solution by tomorrow evening.

The assignment is that I have to create a program by finishing code that is started for me. The code that I am provides is below:


My thought is that I need to put in code on line 6-8 that states:


I'm stuck and need help. I don't necessarily want somebody to just tell me the solution, but I need to understand the process. Anyone?

Thank you,

(Moderator edit: added code tags. Please UseCodeTags )
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
545
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam,
Welcome to CodeRanch!

That's a good first step. Can you say what specifically you are stuck on? Did you try compiling that code? What error did you get? Do you know what it means?

(just be a little more specific of what you are looking for and we are happy to help)
 
Adam Pritchardson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess my question centers around understanding the process as a whole. I somewhat understand the class code and the return only because that is what we are currently working on. However, as I am faced with the challenge to finish the code, I don't know all of the proper terms and syntax to use. I know that I need to use the formula, that I put in my last post, to perform the conversion calculation and I understand that my code will have to return a double which allows for decimal places. What I need to know is the next steps. Where do I go from here; is my code in the right spot?

These are the errors I'm am receiving, and I'm not sure how to fix them:

CelsiusToFahrenheit.java:6: <identifier> expected
public static double CelsiusToFahrenheit (tempC) {
^
CelsiusToFahrenheit.java:8: '.class' expected
return double;
^
2 errors

The textbook that I am having to use for my class doesn't give a great explanation of the method process anyway, so I guess I am kind of taking a stab in the dark hoping that I am at least on the right track.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I see the problem is in how you define the CelsiusToFahrenheit method.

The compiler says there are 2 errors:
1) this line = public static double CelsiusToFahrenheit (tempC) {
2) missing return statement

For the first error, there's something missing. Hint looking the main method.

The 2nd error, since the method is supposed to return something, once you done calculating, which variable is returned?

Furthermore a step forward, do you know where you should call this CelsiusToFahrenheit method in the main method to complete your code?
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at this tutorial page: https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html you should see what you're missing in your code.

By the way, Java is case sensitive so the name CelsiusToFahrenheit is not the same as celsiusToFahrenheit. You have to pay close attention to this. By convention, method names start with a lowercase letter, just as you see it in the comment that says "FINISH: Define celsiusToFahrenheit method here". Class names should start with an uppercase letter. Also, constructors have to have the same name as their class, so your CelsiusToFahrenheit appears to be a constructor but since it has a declared return type, which constructors don't, it is just a plain old method that doesn't conform to normal naming conventions.

Another nitpick: try to choose variables names that you can read out loud. The name scnr is not a very good name in this respect. Something like input or console would be a better name. Vowels are your friends, don't exclude them from your variable names. How would you like it if people started writing your name as "Adm Prtchrdsn"?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!