Monday, July 27, 2009

Java Program, heres the question?

Write a temperature class that ahs two parameters: a temp. value(a floating-point number) and a character for the scale C, and F. The class should have four constructor methods: on for each instance variable, one with two parameters for he two instance variables, and a default constructor( set to 0 Celsius) Include (1) two accessor methods to return the temperature, on to return the degrees C. the other to return the deg. F Three comprison methods, one to set teh scale, and one to set both, and three comparison methods, one to test whether two temperatures are equal, on to test whether one temperature is greater than another, and one to test whether one temperature is greater than the other. and one to test if one temp is less than the other. then write a driver program that tests all the methods. be sure to use each of the constructors, to include at least on true false case for each comparison method. and to test at least the following temp equal: 0.0 C=32.0 F. -40.0 C= -40.0 F

Java Program, heres the question?
Well, you've included no specific question so I can only assume you're hoping someone will write your code for you and hand it to you. Frankly, I'm not going to do that. Your best off at least trying it on your own.





If your stuck on the formulation of what to code, I will help you there. For the most part, the problem description gives you exactly what constructs you need. You need to think about what happens when you've stored a value in one scale and when it is that you have to convert to the other.





First start with knowing the conversions between scales:


deg-C = (5.0f/9.0f) * (deg-F - 32.0f) and


deg-F = (deg-C * (9.0f / 5.0f)) + 32.0f, note the usage of the 'f' since it's a float.





Next, look at your constructors. The default assumes 0 degrees Celsius. The others allow you to manually set one or both of the parameters. I suggest this: If the scale is set but not the temp, assume 0. If the temp is set but not the scale, assume Celsius. This allows the user to expect some consistency...0 degrees and Celsius unless the user changes it.





So, now look at how the parameters might be accessed. You have one method to return degrees Celsius; the other to return degrees Fahrenheit. Whichever one is executed, check your current scale. If it's the same, all you need to do is output the current temperature value. If it's the opposite, then run the temp value through the appropriate conversion and output it.





If the temp is modified but not the scale, then your job is simple...just change the temp scale. The same goes if they input both the temp and the scale.





Where you have to make decisions is


a) if the user only inputs a change of scale. Do you just change the parameter that stores the scale character or do you also convert the temperature if it is different from what the scale used to be.





b) the user requests a comparison of equal, greater than, or less than. How do you set up the input parameters to the comparisons. The best way would be to allow the user to enter both a temperature and a scale. I think this is what the instructor wants otherwise why else would you ask if the two were equal? For instance, if your stored temperature is 0.0f and stored scale is 'C', you could execute IsEqual(32.0f, 'F') and should get a return of 'true'. Again, check the input scales. If they are equal, it is a simple comparison, if not, then convert the input parameter temp to the stored temp scale.





Because of all of the possible temp conversions, I would even recommend that you create two private methods for the conversions. One that converts from Celsius to Fahrenheit and one that converts from Fahrenheit to Celsius. That way, you don't need to repeat the conversion code throughout your code, it's all in one place... well, two.





Finally, we get to your test case. The driver program as the problem description calls it. To do this, create another class in the same project folder as the temperature class. That way, it will have access to all of the parameters and methods in the first class that are not declared 'private'. Have the main() function create an instance of the Driver class and the constructor of the Driver class would first create an instance of the Temperature class and then send it input, call Temp class methods, and output the results. As a reminder, you can access the methods using the dot operator ('.'). For example, if you had an object of Temperature called temp_object, you could call





boolean answer = temp_object.IsEqual(32.0f, 'F');





to use my example from above.





The project description gave you the structure, I've given you some major steps in the logic, you're left with the coding. You should really try and understand what it is I'm saying and suggesting as far as the logic. If any doesn't make sense to you, feel free to e-mail me with a specific question.
Reply:I was actually going to do this for you, but it'll take me an hour. This is your major assignment, isn't it?


No comments:

Post a Comment