简体   繁体   中英

trying to call a toString method in different Java Class

The assignment is to create a class called Temp that runs against the instructors TestTemp class which he provided to us for free. So far everything seems to test out pretty well except for my out put in the toString that we are supposed to use. It is supposed to format like the commented out section but doesn't seem to be working. I posed the TestTemp class and my code for the Temp class. I feel like I am missing something little but just need a nudge in the right direction and my instructor doesn't have office hours again until after the assignment is due. I also pasted the assignment instructions he added to the assignment.

The class will be called Temp

Add a compareTo method. (returns -1 if the invoking object has a lower temp, 0 if the same, 1 if larger)

Add a static counter (object id)to keep track of how many Temperature objects have been created(1,2,3,...)

Add a static method to tell you how many Temperature objects have been created.

Include a toString method that displays the object as follows(assumes 3rd one created):

Object Id: 3 Temperature in F: 32.0 Temperature in C: 0.0

Note that calling getF or getC returns the value only. They do not change the native data.

To be clear the only methods are as follows: 4 constructors, getF, getC, setDegrees, setScale, equals, toString, compareTo and a static getTempCount that returns the total number of objects that have been created.

Note that the getters will return the degrees in the requested scale rounded to a tenth of a degree. Never round the native data.

Note that the equals method will return true if the temperatures are the same when compared in celsius (that was rounded to a tenth of a degree).

Be sure to make great use of this() and have only one contructor do any real work.

Besure to validate the scale and follow the default (C) if a "bad scale" is sent in

No need to validate the degrees and worry about things such as absolute zero and so on.

NOTE: Your Temp class must work correctly with the TestTemp class supplied in UNIT-04-CodeSamples

     //32 - 212  180 ticks
//
//0-100  1/10
// 

    public class TestTemp
    {
      public static void main(String [] args)
      {
         // only one constructor does any real work
         Temp temp1 = new Temp();         // 0  C
         Temp temp2 = new Temp(32);       // 32 C
         Temp temp3 = new Temp('F');      // 0  F
         Temp temp4 = new Temp(32, 'F');  // 32 F

         Temp temp5 = new Temp(); // 0 C
         temp5.setDegrees(10);
         temp5.setScale('F');     // 10 F

         System.out.println("C: " + temp1.getC() ); // C: 0.0
         System.out.println("F: " + temp1.getF() ); // F: 32.0

         System.out.println(temp1.equals(temp4)); // true

         System.out.println(temp1.equals(temp2)); // false

        System.out.println("You have " + Temp.getTempCount() ); // You have 5  

         if( temp3.compareTo(temp5)< 0 ) //temp3 is lower than than temp5
         {
            System.out.println("temp3 is lower than than temp5");
         }
         else
         {
            System.out.println("temp3 is same or larger than temp5");
         }
         System.out.println(temp1);

         /*
            TEMP OBJECT #1
            IN C:  0.0
            IN F: 32.0

         */
      }
    }

    public class Temp implements Comparable<Temp>
    {
      private double degrees;
      private char scale;
      private static int tempCount = 0;
      private int id;

      public Temp()
      {
          this.degrees = 0;
          this.scale = 'C';
    //    this(0.0, 'C');
      }
      public Temp(double degrees)
      {
          this.degrees = degrees;
          this.scale = 'C';
    //    this(degrees, 'C');
      }
      public Temp(char scale)
      {
          this.degrees = 0;
          this.scale = scale;
    //    this(0.0, scale);
      }
      public Temp(double degrees, char scale)
      {
          this.id = ++tempCount;
          this.degrees = degrees;
          this.scale = scale;
          //(degrees, scale);
      }

      public static int getTempCount()
      {
          return tempCount;
      }
      public int getId()
      {
          return this.id;
      }
      public void setScale(char scale)
      {
          if(scale == 'C')
          {
              this.scale = scale;
          }
          else
          {
              this.scale = 'F';
          }
      }
      public void setDegrees(double degrees)
      {
          this.degrees = degrees;
      }
      public double getC()
      {
          if(scale == 'C')
          {
              return degrees;
          }
          else
          {
              return (double)(5.0 * (degrees-32)/9.0);
          }
      }
        public double getF()
        {
            if(scale == 'F')
            {
                return (double) degrees;
            }
            else
            {
                return (double)(9.0*(degrees)/5.0)+32;
            }
        }
        @Override
        public int compareTo(Temp obj) 
        {
            if(this.getC() < obj.getC() )
            {
                return -1;
            }
            if(this.getC() > obj.getC() )
            {
                return 1;
            }
            return 0;
        }   
      public boolean equals(Object obj)
      {
          if(!(obj instanceof Temp))
          {
              return false;
          }
          Temp other = (Temp)obj;
          return this.getC() == other.getC();
      }
      **public String toString()
      {
          return String.format("TEMP OBJECT ", this.id) + "\n" +
                 String.format("IN C: ", this.getC() ) + "\n" +
                 String.format("IN F: ", this.getF() );
      }**

    }

Your use of String.format shouldn't require multiple creations. Just use one.

return String.format("TEMP OBJECT: $d, %nIN C: %.2f, %nIN F: %.2f", this.id, this.getC(), this.getF());

Modify the precision of the floating points by altering the value after the decimal point %.2f to %.5f will print 0.00000 instead of 0.00 for example.

If you have anymore questions on the use of format, I recommend reading the documentation for it as well to see what else it can do. Link

Edit: Added newline breaks. Forgot to mention just put %n for a newline. Do not space, after them, unless you want your newline to start with a space.

You need place holders in the formatter, Your toString method should be like

public String toString()
      {
          return String.format("TEMP OBJECT %d", this.id) + "\n" +
                 String.format("IN C: %.2f", this.getC() ) + "\n" +
                 String.format("IN F: %.2f", this.getF() );
      }

Here %d for integers and %f for decimals. and the .2f limits the number of decimal places to 2. See some more examples here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM