M
mandy
I have 2 classes: Temperature and the driver class.
Can someone tell me why the following scenarios (in testing 2
temperatures) are notworking right?
Scenarios (1) and (2): If I compare the two temperature in same unit,
i.e both in C and both in F, with one number bigger than the other, I
get error.
(3): Using the same float value, I create a second object in unit C and
the third object in F.
(4): Using the same float value, I create a second object in unit C and
the third object in F.
In both (3) and (4), the program displays - I have a display method of
the temperature netered - the 3rd object with unit of 2nd object but
when comparing using comparison =, >=, <=, scenarios (3) is okay but
scenarios (4) is not.
---------------------------------------------------------------------------------------------------------------------------
Here is the code for the driver:
**********************************************************************************************
HW_413_7_driver
************************************************************************************************/
import java.util.Scanner;
import java.util.*;
import java.io.*;
public class HW_413_7_driver
{
public static void main( String[] Args)
{
System.out.println();
System.out.println();
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a float value with 2 decimal position for
a temperature.");
float temp = keyboard.nextFloat();
System.out.println("Enter one character for degree scale (C or
F).");
String strScale = keyboard.next();
char scale = strScale.charAt(0);
HW_413_7_Temperature t2 = new HW_413_7_Temperature(temp,scale);
System.out.print("Second Temperature is ");
t2.display(); System.out.println(); System.out.println();
Scanner keyboard3 = new Scanner(System.in);
System.out.println("Enter a float value with 2 decimal position for
a temperature.");
float temp3 = keyboard3.nextFloat();
System.out.println("Enter one character for degree scale (C or
F).");
String strScale3 = keyboard.next();
char scale3 = strScale.charAt(0);
HW_413_7_Temperature t3 = new HW_413_7_Temperature(temp3,scale3);
System.out.print("Third Temperature is ");
t3.display(); System.out.println(); System.out.println();
if ( (t2.equals(t3) )== true)
{
t2.display(); System.out.print(" is equal to "); t3.display();
}
else
{
t2.display(); System.out.print(" is NOT equal to ");
t3.display();
}
System.out.println();
if ( ( t2.isGreater(t3) )== true)
{
t2.display(); System.out.print(" is greater than "); t3.display();
}
else
{
t2.display(); System.out.print(" is NOT greater than ");
t3.display();
}
System.out.println();
if ( (t2.isLess(t3) ) == true)
{
t2.display(); System.out.print(" is less than "); t3.display();
}
else
{
t2.display(); System.out.print(" is NOT less than ");t3.display();
}
System.out.println();
} // end main
}
------------------------------------------------------------------------------------------------------------------------
Here is for Temperature class:
/***********************************************************************************************
HW_413_7_Temperature
Pg 413.7: Write a temperature class that has 2 parameters: a
temperature value (floating point
number) and a character for the scale ('C' or 'F');
- 4 constructors
- 2 accessor methods (get methods) where conversion is done as
required
- 3 mutator methods (set methods)
- three comparison mehtods: is equal to, is greater than, is less
than
************************************************************************************************/
import java.util.Scanner; // for Scanner class
import java.text.NumberFormat;
import java.text.DecimalFormat;
public class HW_413_7_Temperature
{
private float temperature;
private char charScale=' ';
// default constructor
public HW_413_7_Temperature()
{
temperature = 0;
charScale = 'C';
}
// constructor with a parameter
public HW_413_7_Temperature(float newTemperature)
{
setTemparature(newTemperature);
charScale = 'C';
}
// constructor with a parameter
public HW_413_7_Temperature(char newCharScale)
{
temperature = 0;
setCharScale(newCharScale);
}
// constructor with two parameters
public HW_413_7_Temperature(float newTemperature, char newCharScale)
{
//setTemparature(newTemperature);
//setCharScale(newCharScale);
setTemperatureNCharScale(newTemperature, newCharScale);
}
public String getFarenheitTemperature()
{ // use given formula: degreesF= (9 (degreesC/5) + 32;
// here, this keyword contains both float and char; so how to retrive
just float
float degreesF = (9 * ((this.temperature)/5))+ 32;
NumberFormat n = NumberFormat.getInstance();
n.setMaximumFractionDigits(2);
return(n.format(degreesF)); // return as String as required
}
// accessor method ROUND TO THE NEAREST TENTH OF A DEGREE
public String getCelsiusTemperature()
{
// use given formula: degreesC = 5*(degreesF - 32)/9;
// here, this keyword contains both float and char; so how to retrive
just float
float degreesC = 5*((this.temperature) - 32)/9;
DecimalFormat df = new DecimalFormat("###.00");
return(df.format(degreesC)); // return as String as required
}
// mutator method with 1 parameter
public void setTemparature(float newTemperature)
{
temperature = newTemperature;
}
// mutator method with 1 parameter
public void setCharScale(char newCharScale)
{
charScale = newCharScale;
}
// mutator method with 2 parameters
public void setTemperatureNCharScale(float newTemperature, char
newCharScale)
{
temperature = newTemperature;
charScale = newCharScale;
}
// call on driver as t1.equals(t2)
public boolean equals(HW_413_7_Temperature Temperature2)
{
float temp1;
char scale1 = this.charScale;
if (scale1=='C')
temp1 = this.temperature;
else
temp1 = Float.parseFloat(this.getCelsiusTemperature() );
float temp2 = Float.parseFloat(Temperature2.getCelsiusTemperature()
);
// need to parse to float in order to compare
return (temp1==temp2);
}
// call on driver as t1.isGreater(t2)
public boolean isGreater(HW_413_7_Temperature Temperature2)
{
//float temp1 = this.temperature;
//float temp2 = Float.parseFloat(Temperature2.getCelsiusTemperature()
);
// need to parse back to float in order to compare
float temp1;
char scale1 = this.charScale;
if (scale1=='C')
temp1 = this.temperature;
else
temp1 = Float.parseFloat(this.getCelsiusTemperature() );
float temp2 = Float.parseFloat(Temperature2.getCelsiusTemperature()
);
// need to parse to float in order to compare
return (temp1>=temp2);
}
// call on driver as t1.isLess(t2)
public boolean isLess(HW_413_7_Temperature Temperature2)
{
//float temp1 = this.temperature;
//float temp2 = Float.parseFloat(Temperature2.getCelsiusTemperature()
);
// need to parse to float in order to compare
float temp1;
char scale1 = this.charScale;
if (scale1=='C')
temp1 = this.temperature;
else
temp1 = Float.parseFloat(this.getCelsiusTemperature() );
float temp2 = Float.parseFloat(Temperature2.getCelsiusTemperature()
);
// need to parse to float in order to compare
return (temp1<=temp2);
}
// to display temperature entered as it is
public void display()
{
System.out.print(this.temperature);
System.out.print(" in ");
System.out.print(this.charScale);
}
} // end class
Can someone tell me why the following scenarios (in testing 2
temperatures) are notworking right?
Scenarios (1) and (2): If I compare the two temperature in same unit,
i.e both in C and both in F, with one number bigger than the other, I
get error.
(3): Using the same float value, I create a second object in unit C and
the third object in F.
(4): Using the same float value, I create a second object in unit C and
the third object in F.
In both (3) and (4), the program displays - I have a display method of
the temperature netered - the 3rd object with unit of 2nd object but
when comparing using comparison =, >=, <=, scenarios (3) is okay but
scenarios (4) is not.
---------------------------------------------------------------------------------------------------------------------------
Here is the code for the driver:
**********************************************************************************************
HW_413_7_driver
************************************************************************************************/
import java.util.Scanner;
import java.util.*;
import java.io.*;
public class HW_413_7_driver
{
public static void main( String[] Args)
{
System.out.println();
System.out.println();
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a float value with 2 decimal position for
a temperature.");
float temp = keyboard.nextFloat();
System.out.println("Enter one character for degree scale (C or
F).");
String strScale = keyboard.next();
char scale = strScale.charAt(0);
HW_413_7_Temperature t2 = new HW_413_7_Temperature(temp,scale);
System.out.print("Second Temperature is ");
t2.display(); System.out.println(); System.out.println();
Scanner keyboard3 = new Scanner(System.in);
System.out.println("Enter a float value with 2 decimal position for
a temperature.");
float temp3 = keyboard3.nextFloat();
System.out.println("Enter one character for degree scale (C or
F).");
String strScale3 = keyboard.next();
char scale3 = strScale.charAt(0);
HW_413_7_Temperature t3 = new HW_413_7_Temperature(temp3,scale3);
System.out.print("Third Temperature is ");
t3.display(); System.out.println(); System.out.println();
if ( (t2.equals(t3) )== true)
{
t2.display(); System.out.print(" is equal to "); t3.display();
}
else
{
t2.display(); System.out.print(" is NOT equal to ");
t3.display();
}
System.out.println();
if ( ( t2.isGreater(t3) )== true)
{
t2.display(); System.out.print(" is greater than "); t3.display();
}
else
{
t2.display(); System.out.print(" is NOT greater than ");
t3.display();
}
System.out.println();
if ( (t2.isLess(t3) ) == true)
{
t2.display(); System.out.print(" is less than "); t3.display();
}
else
{
t2.display(); System.out.print(" is NOT less than ");t3.display();
}
System.out.println();
} // end main
}
------------------------------------------------------------------------------------------------------------------------
Here is for Temperature class:
/***********************************************************************************************
HW_413_7_Temperature
Pg 413.7: Write a temperature class that has 2 parameters: a
temperature value (floating point
number) and a character for the scale ('C' or 'F');
- 4 constructors
- 2 accessor methods (get methods) where conversion is done as
required
- 3 mutator methods (set methods)
- three comparison mehtods: is equal to, is greater than, is less
than
************************************************************************************************/
import java.util.Scanner; // for Scanner class
import java.text.NumberFormat;
import java.text.DecimalFormat;
public class HW_413_7_Temperature
{
private float temperature;
private char charScale=' ';
// default constructor
public HW_413_7_Temperature()
{
temperature = 0;
charScale = 'C';
}
// constructor with a parameter
public HW_413_7_Temperature(float newTemperature)
{
setTemparature(newTemperature);
charScale = 'C';
}
// constructor with a parameter
public HW_413_7_Temperature(char newCharScale)
{
temperature = 0;
setCharScale(newCharScale);
}
// constructor with two parameters
public HW_413_7_Temperature(float newTemperature, char newCharScale)
{
//setTemparature(newTemperature);
//setCharScale(newCharScale);
setTemperatureNCharScale(newTemperature, newCharScale);
}
public String getFarenheitTemperature()
{ // use given formula: degreesF= (9 (degreesC/5) + 32;
// here, this keyword contains both float and char; so how to retrive
just float
float degreesF = (9 * ((this.temperature)/5))+ 32;
NumberFormat n = NumberFormat.getInstance();
n.setMaximumFractionDigits(2);
return(n.format(degreesF)); // return as String as required
}
// accessor method ROUND TO THE NEAREST TENTH OF A DEGREE
public String getCelsiusTemperature()
{
// use given formula: degreesC = 5*(degreesF - 32)/9;
// here, this keyword contains both float and char; so how to retrive
just float
float degreesC = 5*((this.temperature) - 32)/9;
DecimalFormat df = new DecimalFormat("###.00");
return(df.format(degreesC)); // return as String as required
}
// mutator method with 1 parameter
public void setTemparature(float newTemperature)
{
temperature = newTemperature;
}
// mutator method with 1 parameter
public void setCharScale(char newCharScale)
{
charScale = newCharScale;
}
// mutator method with 2 parameters
public void setTemperatureNCharScale(float newTemperature, char
newCharScale)
{
temperature = newTemperature;
charScale = newCharScale;
}
// call on driver as t1.equals(t2)
public boolean equals(HW_413_7_Temperature Temperature2)
{
float temp1;
char scale1 = this.charScale;
if (scale1=='C')
temp1 = this.temperature;
else
temp1 = Float.parseFloat(this.getCelsiusTemperature() );
float temp2 = Float.parseFloat(Temperature2.getCelsiusTemperature()
);
// need to parse to float in order to compare
return (temp1==temp2);
}
// call on driver as t1.isGreater(t2)
public boolean isGreater(HW_413_7_Temperature Temperature2)
{
//float temp1 = this.temperature;
//float temp2 = Float.parseFloat(Temperature2.getCelsiusTemperature()
);
// need to parse back to float in order to compare
float temp1;
char scale1 = this.charScale;
if (scale1=='C')
temp1 = this.temperature;
else
temp1 = Float.parseFloat(this.getCelsiusTemperature() );
float temp2 = Float.parseFloat(Temperature2.getCelsiusTemperature()
);
// need to parse to float in order to compare
return (temp1>=temp2);
}
// call on driver as t1.isLess(t2)
public boolean isLess(HW_413_7_Temperature Temperature2)
{
//float temp1 = this.temperature;
//float temp2 = Float.parseFloat(Temperature2.getCelsiusTemperature()
);
// need to parse to float in order to compare
float temp1;
char scale1 = this.charScale;
if (scale1=='C')
temp1 = this.temperature;
else
temp1 = Float.parseFloat(this.getCelsiusTemperature() );
float temp2 = Float.parseFloat(Temperature2.getCelsiusTemperature()
);
// need to parse to float in order to compare
return (temp1<=temp2);
}
// to display temperature entered as it is
public void display()
{
System.out.print(this.temperature);
System.out.print(" in ");
System.out.print(this.charScale);
}
} // end class