Hello, I'm a german college student who started learning java this year. Our first task (rather for practice than for grades) is to write a program that runs this way:
1. Type in a temperature in Celsius
2. Print out the same temperature converted to Fahrenheit
3. Type in a temperature in Fahrenheit
4. Print out a temperature in Celsius
I made the following working program
However, I'm not satisfied. I want more seperate classes. So I tried to code a class to read the input, a class to convert form Fahrenheit to Celsius, a class to convert form Celsius to Fahrenheit and a main method to print it out. Unfortunatley I'm stuck.
I use this class for the input
and want to use this class to convert
and this as a main method
(it's now only for celsius to fahrenheit since i wanted it to work first)
This classes are a big mess and I think my main problem is to give values to other classes. Could you give me a short hint of how to proceed?
One place to start is to add parameters to rechneFahrenheit() and have it return a value (I'm surprised it actually compiles)
and then in your main method you would call:
Also, just a comment, (and others might disagree), I normally would not have methods returning calls to other methods. I would create some local variables in the methods. Call some other methods as needed and then return one of the local variables as the result.
I'm no expert, but my first thought is "WHY do you want more classes?"
your original program was easy to read, and I could tell immediately what it did. i don't think your math is right (you do the same think for both F to C and C to F), but i understood it.
Your second take at it is more confusing. it's harder to follow what you're doing. More classes just to have more classes seems like the wrong way to go if you ask me.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joined: Nov 16, 2009
Thanks for the answers so far
The wrong math came instantly after the post to my mind. It was a stupid mistake and is already fixed. I just want to make a few more classes to practice programming this way. I will need it eventually, won't I?
What you want is not a class but methods. Passing 68°F and getting 20°C requires no access to any values in an object. Have a look at the java.lang.Math class, which has no instance fields. In fact it only has static members, and has a private constructor only, so you cannot instantiate the class. CtoF is just like sin or cos or log; you pass a value and get a result back. So you can design your FahrenheitAndCelsius class like the Math class. This is sometimes called a "utility class."
That is why Fred said not to create more classes.
By the way: it would be better, if you are using double arithmetic, to write c * 9.0 / 5.0 + 32.0. Note the .0 and the spaces.
eventually you will write programs with multiple classes. However, I think it's important to realize that the best approach is ALWAYS to write the simplest code you need to do the job. Doing fancy stuff just because it's cool will earn you hatred and scorn in your team and in the people who follow you.
For learning purposes, it's fine - although I'd suggest rather than forcing several classes into this project, you pick a new project where they'd make sense would be a better way to go.