• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need Help with Temperature class  RSS feed

 
Ryan Yue
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone im new here and im having a little with my homework if anyone can help me I posted my code and I posted the code that I need my code to test with. So the assignment asks for me to create 4 constructors,
one to specify both parameters (degrees, type),
one to specify just the degrees (defaults to type Celsius)
one to specify just the type (defaults to 0.0 for the degrees value)
and the default constructor that specifies neither (defaults to 0 degrees Celsius),

Three write methods(also a little confused on this)
one to display both parameter values (use this for debugging) - writeOutput()
one to display temperature in degrees C (even if value is Fahrenheit)- writeC()
and one to display temperature in degrees F (even if value is in Celsius)- writeF()
two accessor methods for the getC and getF, three mutators methods with ,one to set the degrees value , one to set the type value, and one to set both together. (little confused on the set methods)

one of my questions is how can i get writeOutput to not write everything at once and skip to go to the next test. I have tried to use scan.next() to stop it and have user input but im not sure what to do next after that.
Also for the write class im a little confused on what to do there im not sure if what i put in for write C is correct.
Also there is extra credit i blocked out which i was trying to attempt, but couldn't figure it out it implements the readinput() i feel like i almost got it.
Any help would be great thanks in advance

MY CODE



Test Code
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Yue wrote:So the assignment asks for me to create 4 constructors,

Sorry, but I'm a little confused by the instructions you've been given - which I suspect are your interpretation of them.

Could you give us the instructions exactly as they were given to you.

That will help us to decipher your code better.

One thing I can tell is that there's too much of it - in both classes, but particularly the last one. Write ONE test, and make sure it works; then add another...then another... and so on.

HIH

Winston
 
Ryan Yue
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Winston thank you for the fast reply I tried to word everything in my own words because I didn't to make it seem like im just asking someone to do it for me but i will post up the assignment i have to do so you can better understand what i need to do. The Bottom test code was given by the teacher for my code to pair with it.
Instructions/Design
Design and implement a class, called Temperature, to represent a temperature (example: 98.6F degrees Fahrenheit.) Use the following design:

1) Put your class in a file named: Temperature.java

2) A Temperature object has two instance variables/properties:

- degrees is the temperature value (a real number with a fractional part) - use a double
- and type, either 'C' for Celsius or 'F' for Fahrenheit - use a char

3) Member functions:

Four constructors:

- one to specify both parameters (degrees, type)
- one to specify just the degrees (defaults to type Celsius)
- one to specify just the type (defaults to 0.0 for the degrees value)
- and the default constructor that specifies neither (defaults to 0 degrees Celsius)

4) Three "write" methods (these DO NOT change values of original instance data):

- one to display both parameter values (use this for debugging) - writeOutput()
- one to display temperature in degrees C (even if value is Fahrenheit)- writeC()
- and one to display temperature in degrees F (even if value is in Celsius)- writeF()

5) Two accessor methods to get (return) temperature (these DO NOT change values of instance data):

- one in degrees C
returns value in Celsius (even if current value is in Fahrenheit) - getC()
- and the other in degrees F
returns Fahrenheit value (no matter what type current temperature has) - getF()
6) Three mutator methods to set (update) the parameters:

- one to set the degrees value
- one to set the type value
- and one to set both together.

All are named set but the method is overloaded for the three variations of parameters

7) One comparison method named equals():
example: returns true if temperature1 is equivalent to temperature 2
) One toString() method
Example output: temperature 98.6F but you may determine what output is displayed

Extra Credit

Implement a readInput() method to prompt user for degrees and type and then reads the values. The Scanner class does not have a specific method for reading chars. No input checking is done for units. It is assumed that user inputs only a 'C' or 'F' for units Lower case 'c' and 'f' are acceptable and do not need to be changed to upper case




 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

As you may noticed already, you gone too far with your code without testing it and in most cases ignoring instructions you were given. Probably keep this currently written Temperature class as a reference and start over.
Task 1
1) Put your class in a file named: Temperature.java

2) A Temperature object has two instance variables/properties:

- degrees is the temperature value (a real number with a fractional part) - use a double
- and type, either 'C' for Celsius or 'F' for Fahrenheit - use a char

Basically you got correct only 1 part. Read out loud. What the second part says, and what you actually got in the Temperature file you have putted away for your references?
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Yue wrote:The Bottom test code was given by the teacher for my code to pair with it.

Oh, OK. So it's his (or her) fault, not yours.

A few things I see:
1. You could do with spacing out your code a bit. Right now. it's a bit "cramped". You don't need to go crazy, but a few judicial space lines will help "break it up" a bit.

2. Your instructions clearly state: "A Temperature object has two instance variables/properties:", and yet you've got eighteen - none of which are called what you were told to call them; degrees and type.

The problem is that your mind is leaping ahead to what you want your code to look like, and you've forgotten all about your instructions. If you do this correctly, you class will have exactly TWO instance variables, four constructors, and nine public methods - no more and no less.

And furthermore, you can write them without having to implement them (ie, write code for them) at all. The class won't run until you do, of course; but at least you can separate the business of definition (ie, following your instructions) from implementation (ie, writing the code to make those constructors/methods work).

You might also find this article - which also contains a couple of other handy tips - worth reading.

Hope it helps.

Winston
 
Ryan Yue
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Liutauras
I have been trying to do it in parts i only posted test one without my block comments. I put the double temperatures in the constructors or are they not supposed to be there? i've been kinda stuck for a while changing this and that trying to see what works and what doesn't , but i mean thats part of learning. what im really stumped on is how in the writeout() section how do i make it so it does t1 and then stop to move on to the next section as i posted above i tried to use user input to stop the program but i can seem to figure out how to jump to the different sections
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All you need to do is show us the Temperature class the fulfills parts 1 and 2 of your requirements.
 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wait wait. Read once again what Winston and myself wrote. That task looks too complicated to you, because you did that way.

You should be having class Temperature with 2 instance variables only. i.e.:
And post it here again.
Note: You shouldn't be getting more than 5 lines of code. And that will make you have done first two parts of your instructions.
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch You have got lots of help; I think it will simply confuse you if I put my oar in too.
 
Ryan Yue
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay i just tried to restart everything and now i see where i made the mistake. haha thanks guys this is what i got so far now and thinking about what im supposed to do for the writeOutput()

 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be having class Temperature with 2 instance variables only
These should be called >>> degrees and type

Ryan Yue wrote:

Note: Line numbers suggests how many you got currently. Also look how these are named.

You have to understand, that is better to get less tasks done, but correctly, rather than all and incorrectly.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Yue wrote:
3) Member functions:
Four constructors:

- one to specify both parameters (degrees, type)
- one to specify just the degrees (defaults to type Celsius)
- one to specify just the type (defaults to 0.0 for the degrees value)
- and the default constructor that specifies neither (defaults to 0 degrees Celsius)


Ryan,

Try thinking about this problem like this:

1. Pretend YOU are a Temperature object.

2. I will ask you to initialize yourself - this simulates calling one of the above-described constructors.

3. Let's try the first constructor. I will say "Ryan, you're a Temperature that represents 32 and "F" (the two parameters, degree and type)

4. You would note that down by writing those values on a little notepad that only you can see. These notes would be your instance variables.

5. Then I would ask you to "Write yourself out as a Celsius temperature." which is me calling the writeC() method on you.

6. In response to my request, you would look at your little private notebook and use the values you've noted down to make some calculations, write the result on some piece of paper and show that piece of paper to me. That's essentially what your writeC() method would do.

7. Next, I'll say, "Now you're another Temperature object, initialize yourself to 100" -- since I only specified a number and your instructions tell you to default to type "Celsius" when given only a number, you will make the appropriate notes in your little private notebook.

8. When I ask you to "Write yourself out as a Fahrenheit temperature", you will again look at your private notes and use the information you have there to make the appropriate calculations, write the result on a piece of paper and show that paper to me.

If you think about the rest of constructors as a conversation that goes along the same lines, that will help you write out your program.
s
The Temperature class needs to be general in nature so that it can handle any type of situation you'd expect ANY temperature object to handle. The code you wrote on line 4 is inappropriate because you're thinking about specific things that the Test code is going to do with your Temperature objects.

Edit: (I was looking at the original code you posted when I wrote the following)

Lines 5 to 8 don't make logical sense.

Line 5 is saying "the Celsius variable will be a character"
Line 6 is saying "the Fahrenheit variable will be a character"
Line 7 is saying "the C variable will be a character and I want it to be assigned the value that the Celsius variable currently has" (which will be 0)
Line 8 is saying "the F variable will be a character and I want it to be assigned the value that the Fahrenheit variable currently has" (which will be 0)

Imagine yourself in the role-playing scenario I describe above and think "How would I note down those initialization values that I'm given?"

That is, you could be given the following information to use to initialize yourself as a Temperature object:
1. The number degrees and the type of scale
2. Only the number of degrees, in which case you make an assumption of the type of scale (refer to your instructions)
3. Only the type of scale, in which case you make an assumption of the degrees (refer to your instructions)
4. Neither, in which case you make some assumptions for both degrees and type of scale (refer to your instructions)

None of this would make it necessary for you to note down t1, t2, t3, ... etc. -- that just would not make sense.
 
Ryan Yue
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank Junilu for your help and explanation it really helped me understand a little better this is what i got now hopefully im going on the right track now
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is improving. But don't call the parameter F in line 6. By calling it F, you will confuse yourself into thinking it will have the value 'F' or mean Fahrenheit or similar. If you call it scale or type, you will have no risk of such confusion. (Parameters should usually not start with Capital Letters, anyway.)
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan,

That's a little better. However, you're going to confuse yourself with that name that you chose. What does F represent? It's the scale type, right? What scales are you going to support with your code, both Celsius and Fahrenheit, right? Does it make sense to use a name that only implies Fahrenheit then? Use a name that would apply to ALL the valid values it can take on.

Your requirements already suggest a good name to use. To find a good name, all it usually takes is to tell the story of what your code should be doing. You will hear yourself saying the name that is most appropriate to use...

Read the following sentence OUT LOUD and see if you can hear yourself say the name that you should use in your code.

"When I initialize a Temperature object, I can pass in a number that represents degrees and a letter that represents the scale type, either 'F' for Fahrenheit, or 'C' for Celsius."
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By convention, variables should start with a lowercase letter. Avoid single letter variables because they don't convey much meaning. Again, you want your code to tell a STORY.

Imagine if you were told this story when you were little:

Once upon a time, there was a little girl named LRRH. She always wore a RRH wherever she went. One day, her mother told her to take a basket of food to her grandmother. So LRRH put on her RRH, picked up the basket and headed off to her grandmother's house. "Watch out for the BBW!" warned her mother...

Spell out your variables so people can understand the story your code wants to tell.
 
Ryan Yue
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that i was reading the instructions wrong for some reason i thought it said to choose either C or F... my mistake... i have changed it now and this is where i am at now im trying to do the writeoutput
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Yue wrote:Thank Junilu for your help and explanation it really helped me understand a little better this is what i got now hopefully im going on the right track now

You're getting there (and don't expect to get this all at once - almost nobody does); but you're STILL not following the instructions:
  char F;
should be
  char type;
and not just for all the reasons you've been given, but because you were told to do it.

It's an important lesson to learn - not least because it will happen many times before you finish your course:
You will be given instructions.

Now sometimes these will say what a program needs to do, and sometimes they will say how it needs to do it; and your #2 instruction above is definitely the latter.

So, don't "paraphrase" instructions like that. Do them.

HIH

Winston
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Yue wrote: i have changed it now and this is where i am at now im trying to do the writeoutput


"display both parameter values (use this for debugging) - writeOutput() " - does your code do this? What if you were initialized as a Fahrenheit temperature, would that output be correct?

The name writeOutput is a poorly chosen name, BTW. Think about it, is it really writing "output"? What is really writing? If I told you, "Ryan, write output" would it be reasonable for me to expect you to write down the values that you noted down when you were initialized? That's like me telling you to "Jump!" and expecting you to start dancing. It doesn't make sense, right? I know this is what your instructor told you to do but you can see how anybody can make the mistake of using a name that's not quite right. As an exercise for yourself, you should try to find a better name for that method. To Winston's point though, you should follow the instructions you were given, to the letter. If it were me though, I'd have a few words with the instructor about those requirements, if only to see where there was some wiggle room for your own (hopefully better) interpretation of how to write the program.
 
Ryan Yue
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried to think about you said and for the sake of not cofusing myself im going to keep it writeOutput since all the test is under that name but i was thinking about if i were to change the name to maybe degreesoutput or degreestype or maybe just keep it and call it dop or dt not sure if that was something your were trying to teach me and i totally ruined it sorry :/ im now trying to figure out how im going to have the code work for both cases whether its F or C im just trying to think of a way for it to work
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need an if statement. Don't overthink it. Imagine that role-playing scenario again and picture what your private notes would look like. How are you going to label the values you are initialized with? You'd probably use the same labels that you use in your code right?

Ryan's private notes:
degrees = (the number you were given)
type = (either F or C)

So, if I asked you to write down the values that you were initialized with, what would you do? That's all you need to do in that writeOutput method.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is basically the same thing you need to do.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Yue wrote:Sorry about that i was reading the instructions wrong for some reason i thought it said to choose either C or F... my mistake... i have changed it now and this is where i am at now im trying to do the writeoutput


You've jumped ahead again.
You haven't completed step 3.
Your constructors currently aren't doing a great deal.
The parameters you pass in should be used to populate the member variables, degrees and type.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Yue wrote: i was thinking about if i were to change the name to maybe degreesoutput or degreestype or maybe just keep it and call it dop or dt not sure if that was something your were trying to teach me and i totally ruined it sorry

No need to apologize. As you said before, this is a learning process. One of the best ways to learn is by making mistakes and then realizing what you did wrong.

Try to make your code conversational. Chose names that result in code that reads like a conversation. If I take it to the extreme, the conversation would be like this:


Now, obviously you were given specific instructions on how to write your code and what names to use specifically so you shouldn't do what I showed you above. The general idea is the same though. Let's try substituting the names you wanted to use:

Compared to what I wrote before, does this version make sense when you read it? It doesn't right? So try to find a method name that would make the "conversation" make sense when you read it. (EDIT: that is, if you're doing that as an exercise for your own edification. Again, follow the instructions you were given so you don't get any points deducted for your submission)
 
Ryan Yue
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is what i got now im trying to figure out now how to get the type to use the chars C and F and im having trouble figuring out how to get the value to cycle through the code
junilu thank you for all your help i see now what you mean by making the code like a conversation because it make it really easy to understand i just get confused because i have to follow specific instructions, but when i read your code it is really easy to see and understand whats going on.
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 13 and line 23 do not initialise anything, and line 19 does not initialise type. You need to add those initialisations. You may find it easier if you find out about the this(...) syntax which calls one constructor from another.Why are you importing Scanner? You appear not to use it.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!