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.

can someone check the code for me.

Thread view: 
mandy - 25 Mar 2006 06:13 GMT
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
mandy - 25 Mar 2006 06:59 GMT
Never mind about that question but I do have another one. May be I'll
post tomorrow.
Rhino - 25 Mar 2006 15:38 GMT
> 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.

--
Rhino
mandy - 25 Mar 2006 16:59 GMT
> > Never mind about that question but I do have another one. May be I'll
> > post tomorrow.
[quoted text clipped - 4 lines]
> 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.

> 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.

Thanks.

> --
> Rhino


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.