Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

Testing the following scenarios (of 2 temperatures in Celsius and Fahrenheit)

Thread view: 
mandy - 25 Mar 2006 07:40 GMT
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
Roedy Green - 25 Mar 2006 08:25 GMT
> if ( (t2.equals(t3) )== true)
>    {
[quoted text clipped - 5 lines]
>t3.display();
>    }

That could be collapsed to

System.out.print(" is " + ( t2.equals( t3) ? "" : " NOT " ) + " equal
to" );
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Tom Leylan - 25 Mar 2006 17:08 GMT
Mandy:  Your code is overly verbose but frankly I must warn against the
concatenating of individual words into messages based upon conditionals.  It
is bad practice and nobody would get away with doing it on a project I was
in charge of.  Imagine the trouble to translate into the Spanish, French and
German versions of your software and the cost to identify the messages which
can be output.

>> if ( (t2.equals(t3) )== true)
>>    {
[quoted text clipped - 10 lines]
> System.out.print(" is " + ( t2.equals( t3) ? "" : " NOT " ) + " equal
> to" );
mandy - 25 Mar 2006 19:34 GMT
> Mandy:  Your code is overly verbose but frankly I must warn against the
> concatenating of individual words into messages based upon conditionals.

I fixed all that.  Thanks. I took Roedy's suggestion and alos turned
display() to toString().

> It  is bad practice and nobody would get away with doing it on a project I was
> in charge of.  Imagine the trouble to translate into the Spanish, French and
> German versions of your software and the cost to identify the messages which
> can be output.

Gotcha.
Tom Leylan - 25 Mar 2006 21:01 GMT
Not a problem... I was however suggesting that you "not follow" that
suggestion as it turns the "simple" into the "complex".  Software
development isn't about being clever.

>> Mandy:  Your code is overly verbose but frankly I must warn against the
>> concatenating of individual words into messages based upon conditionals.
[quoted text clipped - 11 lines]
>
> Gotcha.
mandy - 25 Mar 2006 21:50 GMT
> Not a problem... I was however suggesting that you "not follow" that
> suggestion as it turns the "simple" into the "complex".

Oh, then I misunderstood.

>Software  development isn't about being clever.

I would love to know what are the criteria in SW devleopment.
Tom Leylan - 25 Mar 2006 22:58 GMT
The secret to good software design is "simplicity".

Take a look at this part of your code:

if ( ( (scale1 == 'f' || scale1 == 'F') && (scale2 == 'f' || scale2 ==
'F') ) || ( (scale1 == 'c' || scale1 == 'C') && (scale2 == 'c' || scale2 ==
'C') ) )

The level of complexity alone tells me the approach is wrong and I suspect
the answer is "probably" wrong or at best the answer has been brute-forced
to return a correct value.  I wouldn't trust the algorithm because I don't
see one. :-)

Consider that a simple task like comparing two temperatures _can't_ require
unreadable code or what will happen when you start computing things that
aren't so easily understood?  What happens to this code when you add "K" to
support Kelvin to TempConvert 2.0?

As you review your code ask yourself questions like; Why can't I be certain
that the "scale" is an upper or lower case letter?

Have you failed to define it one way or the other in your class?  By not
defining it you're forcing yourself (and you did some 8 times) to compare
both upper and lowercase with every use.  You had one task and in the course
of solving it managed to created an impediment to solving it.  For
argument's sake let us say that you can't control the code that returns
charScale.  Surely you could capitalize the return value in the assignment
to scale1 and by doing this eliminate 1/2 of the tests.

Better yet your Temperature class could forget about letters.  How about a
few static final public int values representing FAHRENHEIT, CELSIUS and
KELVIN?  That way nothing that uses the objects needs to know about the
magic letters and they can test for Temperature.FAHRENHEIT instead.

Look again at your isLess() method.  Why is isLess() trying to determine
what charScale is doesn't the temp object process the conversion?  If so
doesn't it become something along the lines of the following:

public boolean isLess( Temperature temp2 ) {
   return ( this.AsCelsius() <= temp2.AsCelsius() )
}

Do we care what setting either temperature has?  I think we just need them
using the same scale for the sake of comparison.

SIMPLE is good.

>> Not a problem... I was however suggesting that you "not follow" that
>> suggestion as it turns the "simple" into the "complex".
[quoted text clipped - 4 lines]
>
> I would love to know what are the criteria in SW devleopment.
Roedy Green - 26 Mar 2006 00:31 GMT
On Sat, 25 Mar 2006 16:58:30 -0500, "Tom Leylan"
<gee@iamtiredofspam.com> wrote, quoted or indirectly quoted someone
who said :

>if ( ( (scale1 == 'f' || scale1 == 'F') && (scale2 == 'f' || scale2 ==
>'F') ) || ( (scale1 == 'c' || scale1 == 'C') && (scale2 == 'c' || scale2 ==
>'C') ) )

here is a case where an enum comes is handy.  You have an enum for
each temperature scale F C K .

The letter becomes a property of the enum.  The magic numbers become a
property of the enum. You could then add new temperature scales just
by adding another enum constant.

You keep distinct your code of factoids about a particular scale out
of the general code.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Andrew McDonagh - 26 Mar 2006 00:44 GMT
> On Sat, 25 Mar 2006 16:58:30 -0500, "Tom Leylan"
> <gee@iamtiredofspam.com> wrote, quoted or indirectly quoted someone
[quoted text clipped - 6 lines]
> here is a case where an enum comes is handy.  You have an enum for
> each temperature scale F C K .

No a enum with with anything other than its enumeration value, is
normally a State object in hiding.

That said, for this particular case, this is where a Strategy Object is
handy.  Mixing Enums and States and Strategies into one class will
create a mess of a design.

You really seemed to have a thing about Enums lately....  almost to the
point where you have a hammer and everything is a nail...


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.