Running Error


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Running Error

  1. #1
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173

    Running Error

    Hi all,
    I am having some difficulties, running this one.
    It does compile, but it gives error on the run time:
    Exception in thread "main" java.lang.NoClassDefFoundError: Temperature

    public static void main(String[] args) {
    Temperature t = new Temperature(100.5, 'C');
    System.out.println("\n\nt:"+t);

    ArrayList tList=new ArrayList();
    tList.add(t);
    tList.add(new Temperature(140.5,'F'));
    tList.add(new Temperature(60,'C'));
    tList.add(new Temperature(90,'F'));
    tList.add(new Temperature(70.3,'C'));
    tList.add(new Temperature(.7,'C'));
    Collections.sort(tList,new Temperature());
    System.out.println("Sorted temperatures");
    for (int i=0; i<tList.size(); i++) {
    System.out.println(tList.get(i));
    }
    }
    }
    What am I missing?

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    The Temperature class is missing.

    This looks lke something I posted some weeks ago, here is the complete
    code

    If it compiles OK but fails at runtime then the error is your package
    structure not matching your runtime setup.

    Code:
    import java.util.*;
    import java.text.*;
    /**
     * A sortable, formatted and printable temperature class
     *
     */
    public class Temperature implements Comparator   { //Class name
      /**
       * format for printing temperature values
       */
      static DecimalFormat df=new DecimalFormat("0.0");
      double temp; //Variable
    
      char scale; //Varible
    
      public Temperature() { //Default Constructor
        temp = 0;
        scale = 'C';
      }
    
      public Temperature(char tempScale) { //Scale Constructor
        temp = 0;
        scale = tempScale;
      }
    
      public Temperature(double tempValue) { //Temp Constructor
        temp = tempValue;
        scale = 'C';
      }
    
      public Temperature(double tempValue, char tempScale) { //Constructor
        temp = tempValue;
        scale = tempScale;
      }
    
      public double getCelsius() { //Used to get degrees C
        double value;
        if (scale == 'C') {
          return temp;
        }
        else {
          return ( (double) (Math.round( (5 * (temp - 32.0) / 9.0) * 10.0)) / 10);
        }
      }
    
      public double getFarenheit() { //Used to get degrees F
        if (scale == 'F') {
          return temp;
        }
        else {
          return (Math.round( (9 * (temp / 5) + 32) * 10) / 10);
        }
      }
    
      public void setTemp(double newTemp) { //set temp
        temp = newTemp;
      }
    
      public void setScale(char newScale) { //set scale
        scale = newScale;
      }
    
      public void setTempScale(double newTemp, char newScale) { //set both
        temp = newTemp;
        scale = newScale;
      }
      /**
       * return a formatted temperature string
       * @return
       */
      public String toString () {
        return df.format(temp)+" "+scale;
      }
      /**
       * the equals method should be implemented as an override of the
       * Object class' equals method, parameter should be an Object.
       * Note: the two next metods define the Comparator interface
       * Among othjer things this enables sorting.
       * @param obj
       * @return
       */
      public boolean equals(Object obj) { //compares for equality
        if (!(obj instanceof Temperature)) {
          return false;
        }
        Temperature aTemp=(Temperature)obj;
        return (getCelsius() == aTemp.getCelsius());
      }
      /**
       * Method used by  Collections.sort
       * @param o1
       * @param o2
       * @return
       */
      public int compare(Object o1, Object o2) {
        Temperature t1=(Temperature)o1;
        Temperature t2=(Temperature)o2;
        // switch t1 & t2 here to get descending sort
        return (int)(t1.getCelsius()*10.0d-t2.getCelsius()*10.0d);
      }
      // This is comparing statements to see if the first is greater than the second
      public boolean greaterThan(Temperature obj) {
        return (getCelsius() > obj.getCelsius());
      }
    
      // This is comparing  statements to see if the first is less than the second
      public boolean lessThan(Temperature obj) {
        return (getCelsius() < obj.getCelsius());
      }
    
      // Returns temp value
      public double getTemperatureTemp() {
        return temp;
      }
    
      // Returns scale character
      public char getTemperatureScale() {
        return scale;
      }
    
      public static void main(String[] args) {
        Temperature t = new Temperature(100.5, 'C');
        System.out.println("\n\nt:"+t);
        // stuff an arrayList with temperatures and sort them on actual heat value
        ArrayList tList=new ArrayList();
        tList.add(t);
        tList.add(new Temperature(140.5,'F'));
        tList.add(new Temperature(60,'C'));
        tList.add(new Temperature(90,'F'));
        tList.add(new Temperature(70.3,'C'));
        tList.add(new Temperature(.7,'C'));
        Collections.sort(tList,new Temperature());
        System.out.println("Sorted temperatures");
        for (int i=0; i<tList.size(); i++) {
          System.out.println(tList.get(i));
        }
      }
    }
    Last edited by sjalle; 06-14-2005 at 09:06 AM.
    eschew obfuscation

  3. #3
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173
    Yes you are 100% right.
    I did copy the hoie file, but I still having the same message.
    Now, I have wrong name "stubs6/temperature>"
    Can you advice me?
    Thanks

  4. #4
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173
    Solved.
    Thank you very much.
    I had one extra line in the biggining" Package6"
    //
    It runs now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center