Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: class and check method

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    35
    My Mood
    Goofy
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default class and check method

    I have the following code which has no errors when i run it:
    /*
     * 63. Write a class encapsulating the concept of the weather forecast, assuming
     * that it has the following attributes: the temperature and the sky conditions,
     * which could be sunny, snowy, cloudy, or rainy. Include a constructor, the
     * assessors and mutators,and methods toString and equals. Temperature, in 
     * Fahrenheit, should be between - 50 and + 150; the default value is 70, if needed.
     * The default sky condition is sunny. Include a method that converts Fahrenheit
     * to Celsius. Celsius temperature = (Fahrenheit temperature - 32) * 5 / 9.
     * Also include a method that checks whether the weather attributes are consistent
     * (there are two cases where they are not consistent: when the temperature is 
     * below 32 and it is not snowy, and when the temperature is above 100 and it
     * is not sunny).Write a client class to test all the methods in your class.
     */
    package natashaepperson_chapter7_exercise_63;
     
    public class WeatherForecast
    {
    private double temperature; // in Fahrenheit
    private String sky;
     
    /**
    * initializes temperature to 70.0 and sky to "sunny"
    * calls mutator methods to validate these default values
    */
    public WeatherForecast( )
    {
    temperature = 70;
    sky = "sunny";
     
    }
     
    /**
    * Overloaded constructor:
    * Allows client to set beginning values for temperature and sky
    * This constructor takes two parameters
    * Calls mutator methods to validate new values
    * @param newTemperature the temperature for the weather forecast
    * @param newSky the sky conditions for the weather forecast
    */
    public WeatherForecast( double newTemperature, String newSky )
    {
    temperature = newTemperature;
    sky = newSky;
    }
     
    /** getTemperature method
    * @return the temperature
    */
    public double getTemperature( )
    {
    return temperature; // add code here, insted of o.o we should return tempature
    }
     
    /**
    * Mutator method:
    * Allows client to set value of temperature
    * setTemperature sets the value of temperature
    * to 70.0 if newTemperature is less than -50 or greater than 150
    * @param newTemperature the new number of temperature
    */
    public void setTemperature( double newTemperature )
    {
    // add code here // make sure falls in range of -50 to 100, need if else
    if(temperature >= 0)
    temperature = newTemperature;
    else
    {
    System.err.println( "Miles driven cannot be negative." );
    System.err.println( "Value not changed."); 
    }
    } 
     
    /** getSky method
    * @return the sky conditions
    */
    public String getSky( )
    {
    return sky;
    // add code here, returns the string variable that represents your sky. You are going to some error checking and a syst
    // system.err make sure equal to sunny snowy cloudy rainy
    }
     
    /**
    * Mutator method:<BR>
    * Allows client to set value of sky
    * setSky sets the value of sky
    * to "sunny" if newSky is neither "sunny", "snowy",
    * "cloudy", nor "rainy"
    * This method is not case sensitive
    * @param newSky the new sky condition
    */
     
    // @return the temperature and the sky conditions for the weather forecast
    @Override
    public String toString( )
    {
    return( "temperature: " + temperature + "; sky: " + sky );
    }
     
    /** 
    * Compares two WeatherForecast objects for the same field values
    * @param wf another WeatherForecast object
    * @return a boolean, true if this object
    * has the same field values as the parameter wf
    */
    public boolean equals( WeatherForecast wf )
    {
    return ( Math.abs( temperature - wf.temperature ) < 0.0001 
    && sky.equals( wf.sky ) );
    }
     
    /**
    * fahrenheitToCelsius method
    * Computes the degrees Celsius temperature
    * @return a double, the temperature value in degrees Celsius
    */
    public double fahrenheitToCelsius( )
    {
         temperature = temperature - 32;
         temperature = temperature * 5;
         temperature = temperature / 9;
         return temperature;
    // add code here complete method, do the coversioan return a double data type
    }
     
    /**
    * Checks if the temperature value and the sky conditions are compatible
    * @return a boolean, false if the temperature is below 32 and the sky is not snowy,
    * or if the temperature is greater than 100 and the sky is not sunny, true otherwise 
    */
    public boolean isConsistent( )
    {
    return !( ( temperature < 32 && !sky.equals( "snowy" ) ) 
    || ( temperature > 100 && !sky.equals( "sunny" ) ) );
    }
    }

    The problem seems to be in the call method class. I have this code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package natashaepperson_chapter7_exercise_63;
     
    /**
     *
     * @author natasha
     */
    /* WeatherForecast Client
    This class is complete, do not change. 
    */
     
    public class WeatherForecastClient
    {
    public static void main( String [] args )
    {
    WeatherForecast wf1 = new WeatherForecast( 23,"sunny" );
    WeatherForecast wf2 = new WeatherForecast( 200, "rainy" );
     
    System.out.print( "The temperature of weather forecast #1 is " + wf1.getTemperature( ) );
    System.out.println( "F or " + wf1.fahrenheitToCelsius( ) + "C");
    System.out.println( "The sky condition of weather forecast #1 is " + wf1.getSky( ) );
    System.out.println( "Weather forecast #2 is " + wf2.toString( ) + "\n" );
     
    if ( wf1.isConsistent( ) )
    System.out.println( "weather forecast " + wf1 + " is consistent" );
    else
    System.out.println( "weather forecast " + wf1 + " is not consistent" );
     
    if ( wf2.isConsistent( ) )
    System.out.println( "weather forecast " + wf2 + " is consistent" );
    else
    System.out.println( "weather forecast " + wf2 + " is not consistent" );
    System.out.println( );
     
    if ( wf1.equals( wf2 ) )
    System.out.println( "Original weather forecasts #1 and #2 are identical" );
    else
    System.out.println( "Original weather forecasts #1 and #2 are different" );
     
    wf2.setTemperature( 23 );
    wf2.setSky( "sunny" );
     
    if ( wf1.equals( wf2 ) )
    System.out.println( "Original weather forecast #1 and modified weather forecast #2 are identical" );
    else
    System.out.println( "Original weather forecast #1 and modified weather forecast #2 are different" );
     
    }
    }

    This is the error i get when i run it
    The temperature of weather forecast #1 is 23.0F or -5.0C
    The sky condition of weather forecast #1 is sunny
    Weather forecast #2 is temperature: 200.0; sky: rainy

    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: natashaepperson_chapter7_exercise_63.WeatherForeca st.setSky
    weather forecast temperature: -5.0; sky: sunny is not consistent
    weather forecast temperature: 200.0; sky: rainy is not consistent

    Original weather forecasts #1 and #2 are different
    at natashaepperson_chapter7_exercise_63.WeatherForeca stClient.main(WeatherForecastClient.java:44)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 2 seconds)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: class and check method

    java.lang.RuntimeException: Uncompilable source code -
    Can you use the javac compiler to get an error message that says what the problem is?

    BTW The posted code has lost its formatting. Nested statements should be indented.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with method to check if two rectangles overlap
    By jikatz09 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 20th, 2013, 07:19 AM
  2. check for boolean method.
    By daitobu in forum Object Oriented Programming
    Replies: 1
    Last Post: September 1st, 2012, 09:54 AM
  3. Disabling Check Boxes created by different method.
    By xdega in forum AWT / Java Swing
    Replies: 3
    Last Post: April 23rd, 2012, 12:06 PM
  4. check for valid argument to a method
    By lupis in forum Exceptions
    Replies: 8
    Last Post: March 25th, 2012, 12:09 AM
  5. Method for Cedit card check, help!!!!1
    By raidcomputer in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 31st, 2009, 10:16 AM