我的目标是创建一个以摄氏度和华氏度表示温度的类"Temperature"。该类需要四个构造如下的构造函数。我需要处理的部分是Two访问器方法,因为我还不太熟悉它。我已经写了代码,但不确定它是否会成功,我很感激的一些见解
四名建设者:1.一代表学位数2.天平一个3.一个代表度数和刻度4.默认构造函数
两种存取器方法:
- 一个以摄氏度为单位返回温度
- 另一个以华氏度为单位返回
w/下面给出的公式C=5(F–32)/9F=9*C/5+32
从我目前的设置方式来看,我相信我正准备从celsius转换为fahrenheit ONLY。。。我怎么能让它互换
请不要讨厌我只是一个新手,可能会有致命的错误
package temperatureapparatus;
public class Temperature {
private float degrees;
char scale;
public static final float fahrenheitForm = ((9.0*(degrees/5.0))+32.0);
public Temperature(){
degrees = 0;
scale = 'C';
}
public Temperature(float degrees){
this.degrees = degrees;
degrees = 0;
}
public Temperature(char scale){
this.scale = scale;
scale = 'C';
}
public Temperature(float degrees, char scale){
this.degrees = degrees;
this.scale = scale;
}
public float getTempCels (float degrees, char scale){
return degrees;
}
public float getTempFehr (float degrees, char scale){
return fahrenheitForm;
}
}
下面是您的代码的注释正确版本。但我不明白为什么要使用实例变量,因为您从未使用过它们。在您的情况下,构造函数也是无关的。对于优雅的代码,您可以遵循Alexander Kleinhans的编码实现,也可以将转换方法(即getTempCels和getTempFehr)标记为静态,并且不必创建实例来使用这些方法。您可以查看此链接来了解静态方法与非静态方法。
public class Temperature {
private float degrees;
char scale;
public Temperature(){
degrees = 0;
scale = 'C';
}
public Temperature(float degrees){
this.degrees = degrees;
scale = 'C';
}
public Temperature(char scale){
// change the given scale to uppercase
scale = Character.toUpperCase(scale);
this.scale = 'C';
// if the given scale is 'F' then change the instance variable scale (this.scale) to scale passed in parameter
if(scale == 'F'){
this.scale = scale;
}
this.degrees = 0;
}
public Temperature(float degrees, char scale){
scale = Character.toUpperCase(scale);
this.scale = 'C';
if(scale == 'F'){
this.scale = scale;
}
//set instance variable degrees (this.degrees) to the degrees passed in parameter
this.degrees = degrees;
}
public float getTempCels (float degrees, char scale){
scale = Character.toUpperCase(scale);
// if the given scale is celsius then just return whatever was passed in parameter
if(scale == 'C'){
return degrees;
}
// else if the given scale is fahrenheit then use the formula to convert to celsius and return the result
else if(scale == 'F'){
return ((degrees - 32.0f) * (5.0f/9.0f));
}
// if the given scale is anything else than celsius or fahrenheit then print a message and return zero.
else{
System.out.println("Invalid scale. Try again with scale either C (for celsius) or F (for fahrenheit)");
return 0;
}
}
public float getTempFehr (float degrees, char scale){
scale = Character.toUpperCase(scale);
//if the given scale is fahrenheit then just return whatever was passed in parameter
if(scale == 'F'){
return degrees;
}
// else if the given scale is celsius then use the formula to convert to fahrenheit and return the result
else if (scale == 'C'){
return ((9.0f*(degrees/5.0f))+32.0f);
}
// if the given scale is anything else than celsius or fahrenheit then print a message and return zero.
else{
System.out.println("Invalid scale. Try again with scale either C (for celsius) or F (for fahrenheit)");
return 0;
}
}
}
您需要将farenheitForm
移动到访问器方法中。如果它是一个私有字段,那么只有当类被构造并保持在32
时,它才会被初始化。
public float getTempFehr (float degrees, char scale){
return (9 * degrees / 5f) + 32;
}
从我目前的设置方式来看,我相信我正准备从celsius转换为fahrenheit ONLY。。。我怎么能让它互换
如果你想用学位或分数来初始化这个类,你应该修改你的第二个构造函数,使其采用一个表示传入度量的标志;或者创建另一个采用double
参数而不是浮点值的构造函数,以表示farenheit形式。一旦进入构造函数,您就可以将farenheim值转换为度数值,并将其存储在私有字段中。
public Temperature(double farenheit){
degrees = 5 * (farenheit - 32) / 9;
}
在这一点上,添加另一个构造函数来同时获取farenheit值和scale也是值得的,以便与等价度构造函数保持一致。
这可能不是您想要的答案,但我认为构造函数不应该这样设置。此外,我认为您应该对接口进行编程。
此外,java在数字方面有点不稳定。我第一次尝试编译你的代码时,得到了:
incompatible types: possible lossy conversion from double to float
对于任何使用那些硬编码转换的东西,都会浮动,直到我键入casted所有内容。
如果我用不同的方式来做这件事,我会用明确定义的访问器方法编程到某种接口。对于你目前的构造函数,它还不清楚如何使用,而且很危险
我会设置一个这样的界面:
package temp;
public interface TemperatureInterface
{
// Sets internal degrees to celcius
public void setCelcius(float degreesCelcius);
// Sets internal degrees to fahrenheit
public void setFahrenheit(float degreesFehrenheit);
// Gets internal degrees in celcius
public float getDegreesCelcius();
// Gets internal degrees in fahrenheit
public float getDegreesFahrenheit();
}
使用这样的实现:
package temp;
public class Temperature
implements TemperatureInterface
{
private boolean temperatureSet;
private float celciusInternal;
private float _convertToCelcius(float degreesFehrenheit)
{
return (((float)9.0*((float)degreesFehrenheit/(float)5.0))+(float)32.0);
}
private float _convertToFehrenheit(float degreesCelcius)
{
return (((float)degreesCelcius*(float)1.8)+(float)32.0);
}
public Temperature() {
this.temperatureSet = false;
}
// Sets internal degrees to celcius
public void setCelcius(float degreesCelcius)
{
this.temperatureSet = true;
// We need to set the internal
// degrees in celcius, so just
// set it.
this.celciusInternal = degreesCelcius;
}
// Sets internal degrees to fahrenheit
public void setFahrenheit(float degreesFehrenheit)
{
this.temperatureSet = true;
// We need to set the internal
// degrees in celcius, so first
// convert it.
float degreesCelcius = this._convertToCelcius(degreesFehrenheit);
this.celciusInternal = degreesCelcius;
}
// Gets internal degrees in celcius
public float getDegreesCelcius()
{
// First make sure the temperature
// has been set.
if (this.temperatureSet == false) {
System.out.println("Error: no temperature set.");
System.exit(1);
}
// We already have degrees celcius,
// so just give the value back.
return this.celciusInternal;
}
// Gets internal degrees in fahrenheit
public float getDegreesFahrenheit()
{
// First make sure the temperature
// has been set.
if (this.temperatureSet == false) {
System.out.println("Error: no temperature set.");
System.exit(1);
}
// First we have to convert the
// internal degrees celcius.
float celcius = this._convertToFehrenheit(this.celciusInternal);
return celcius;
}
}
请注意,我首先编写了接口,您只需要了解该接口就可以知道如何使用该类。通常我会抛出异常而不是System.ou.println("ERROR THING")
,但现在这无关紧要。
不管怎样,这可能对你的家庭作业问题没有帮助,但我认为这样的课应该这样写。
您的上述编码方法需要在其他方面进行更正,以避免代码重复和可能的错误。
package temperatureapparatus;
public class Temperature {
private char scale;
private float degrees;
// Constructor
public Temperature(float degrees, char scale) {
this.scale = scale;
this.degrees = degrees;
}
// reuse already defined constructor
public Temperature(char scale){
this(0, scale);
}
public float getTemp(char scale) {
if (this.scale == scale) return degrees;
if (scale == 'F')
return Temperature.convertToFah(degrees);
else
return Temperature.convertFromFah(degrees);
}
public static float convertFromFah(float degrees) {
return (float) ((degrees-32.0)*(5.0/9.0));
}
public static float convertToFah(float degrees) {
return (float) ((9.0*(degrees/5.0))+32.0);
}
}