Monday, July 27, 2009

Please anyone know how to write this program in java programing. pleas give me some idea or out line please?

a temperature class that has two instance variables: a temperature value double and a character for the scale, either c for Celsius or F for Fahrenheit. The classe should have four constructor methods: one for each instance variable(assume zero degrees if no value is specified and Celsius if no scale is specified). One with two parameter for the two instance variable, and a no-argument constructor. include the following (10 two accessor methods to return the temperature- one to return the degree of Celsius, the other to return the degree Fahrenheit – use the this fomula to write the two methods and round to nearest tenth of a degree:


DegreeC= 5(degree-32)/9


degree=(9(degree)/5)+32;


2. three mutator methods: one to set the value, one to set the scale( F or C) and one to set both ;


3. three comparison methods, and equals method to test whether two temperature are equal, one method to test whether one temperature is greater than another

Please anyone know how to write this program in java programing. pleas give me some idea or out line please?
Done sir,


you can search at


http://expert.userstyle.com/
Reply:This little problem shows up as an example in the Swing GUI intro tutorial and as a tutorial for using Java RegEx. The RegEx with the new "printf" and its "entities" -- Java 6 -- is the most elegant, and shortest listing. But...





You program has other requirements.





I think your program is a console program with one main method ...





That means a String input


If you use Scanner, you still want String input





float celsius = Float.parseFloat( input );


int fahrenheit = (int) (celsius + 9/5 + 32); //Convert to a Fahrenheit value


m.appendReplacement(result, fahrenheit + "F");





You can take the user's input, compare for a cap F or cap C to branch logic with.
Reply:public class Temperature {





// Inst vars


double temperature;


char scale;





// Four constructors


public Temperature(double aTemp, char aScale) {


temperature = aTemp;


scale = aScale;


}





public Temperature(double aTemp) {


this(aTemp, 'c');


}





public Temperature(char aScale) {


this(0.0, aScale);


}





public Temperature() {


this(0.0, 'c');


}





// Accessor methods


public double getFarenheit() {


if (scale == 'F') return roundToTenths( temperature);


else return roundToTenths( 9.0*temperature/5.0 + 32.0);


}





public double getCelsius() {


if (scale == 'c') return roundToTenths( temperature):


else return roundToTenths( (temperature - 32.0) * 5.0 / 9.0 );


}





private double roundToTenths(double x) {


return (double) Math.round(x * 10.0) / 10.0;


}





// Mutator methods


public void setScale(char aScale) {


scale = aScale;


}





public void setTemp(double aTemp) {


temperature = aTemp;


}





public void setTempAndScale(double aTemp, char aScale) {


temperature = aTemp;


scale = aScale;


}





// Comparison methods


public boolean equals(Object anotherObj) {


// Overrides Object.equals() so we should use same signature


if (! (anotherObj instanceof Temperature)) return false;


// Use getFarenheit for comparison because


// getCelsius() rounded to tenths might compare


// as equal even when getFarenheit() does not.


Temperature t = (Temperature) anotherObj;


return getFarenheit() == t.getFarenheit();


}





public boolean isGreaterThan( Temperature anotherTemp ) {


return getFarenheit() %26gt; anotherTemp.getFarenheit();


}





public String toString() {


if (scale == 'c') return "" + getCelsius() + "c";


return getFarenheit() + "F";


}


}





=========================





The driver class would be something like this:





public class TestTemp()


{


public static void main(String[] args) {


System.out.println( "No-arg constructor = " + new Temperature());


System.out.println("Temp constructor = " + new Temperature( 100.0));


System.out.println("Scale constructor = " + new Temperature( 32.0, 'F'));





System.out.println("Freezing equals = " + ((new Temperature(32.0, 'F')).equals(new Temperature( 0.0, 'c'))));


System.out.println("Boiling equals = " + ((new Temperature(212.0, 'F')).equals(new Temperature( 100.0, 'c'))));





System.out.println("0.1F greater = " + ((new Temperature(0.0, 'F' )).isGreaterThan( new Temperature( 0.1, 'F'))));


System.out.println("0.1c greater = " + ((new Temperature(0.0, 'F' )).isGreaterThan( new Temperature( 0.1, 'F'))));





// Additional code to test accessors


// and mutators goes here


}


}
Reply:That is going to be quite difficult. If you are still stuck with your project assignment may be you can contact a java expert live at website like http://askexpert.info/ .

salary survey

No comments:

Post a Comment