can someone check the code for me.

M

mandy

The program compares 2 temperatures.
There are 2 classes: Temperature and a driver class.

When I compare 0 Celsius (via deafult constructor) with 25.33
Fahrenheit (via constructor with 2 paramenter) using equals, isGreater,
isLess method, I want to be able to say which temperature is greater by
specifically giving the value and unit scale of the 1st temperature
instead of just saying "The first temperature is greater."

Can anyone help?

Here is the code for the driver:

/**** Take 2 temperature objects to do comparison***/

import java.util.Scanner;
import java.util.*;
import java.io.*;

public class HW_413_7_driver
{

public static void main( String[] Args)
{

HW_413_7_Temperature t1 = new HW_413_7_Temperature();

System.out.print("First Temperature is ");
t1.display(); // testing to see the 1st temperature entered

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(); //testing to see the 2nd temperature entered

if ( (t1.equals(t2) )== true)
System.out.println("The two temperatures are equal.");
else
System.out.println("The two temperatures are not equal.");

if ( ( t1.isGreater(t2) )== true)
System.out.println("The 1st temperature is greater than the
second.");
else
System.out.println("The 1st temperature is NOT greater than the
second.");

if ( (t1.isLess(t2) ) == true)
System.out.println("The 1st temperature is less than the second.");
else
System.out.println("The 1st temperature is NOT less than the
second.");


} // end main

} // end class

------------------------------------------------------------------------------------------------------------

Here is for Temperature class:


/***********************************************************************************************
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; // default is 0
charScale = 'C'; // default is 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)
{
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 = this.temperature;
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

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

return (temp1<=temp2);

}
// to display temperature entered as it is
public void display()
{
System.out.print(this.temperature);
System.out.print(" in ");
System.out.println(this.charScale);

}

} // end class
 
R

Rhino

mandy said:
Never mind about that question but I do have another one. May be I'll
post tomorrow.

If you do, it might be a good idea to define what you mean by "check the
code".

It would make sense to have people on this newsgroup critique the style and
suggest a more professional approach to a problem. Unfortunately, most
students (and some instructors) don't really worry about a professional
approach that much and are more concerned with getting the assignment done
and moving on to something else.

It would make less sense to ask us if the code is going to work: you can
determine that easily enough by running the program yourself.

It would also be better if you asked homework questions on
comp.lang.java.help. That is where we traditionally prefer that homework
questions go.
 
M

mandy

Rhino said:
If you do, it might be a good idea to define what you mean by "check the
code".

It would make sense to have people on this newsgroup critique the style and
suggest a more professional approach to a problem.

I did state in the body what specifics I was asking for. But my subject
title could have been better.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top