如何打印,排序和使温度高于90

时间:2019-07-18 21:12:40

标签: java arrays sorting printing

Place code to print elements from arr_param
Place code to sort elements in arr_param in ascending order of fahrenheit temperature
Place code to print out elements from arr_param with temperatures > 90 deg. F

There is a private class to do the conversions from F to C to K. 
public class Temperature {
   public Temperature(double p_fahren) {
      fahrenTemp = p_fahren;
   }
   public void setFahrenheit(double p_fahren) {
      fahrenTemp = p_fahren;
   }
   public double getFahrenheit() {
      return fahrenTemp;
   }
   public double getCelsius() {
      double celsius_temp;
      celsius_temp = (5.0 / 9.0) * (fahrenTemp - 32.0);
      return celsius_temp;
   }
   public double getKelvin() {
      double kelvin_temp = ((5.0 / 9.0) * (fahrenTemp - 32.0)) + 273.0;
      return kelvin_temp;
   }
   public String toString() {
     String ret_val = "";
     ret_val = String.format("%.1f F, %.1f C, %.1f K",fahrenTemp, getCelsius(), getKelvin());
     return ret_val;
   }
}

We are not allowed to use the Arrays Util

public class Asn5_Test_Temperature
{
   public static void main(String args[])
   {
      Temperature arr_temps [] = 
         {
         new Temperature(90), new Temperature(75), new Temperature(65), new Temperature(95),
         new Temperature(89), new Temperature(67), new Temperature(77), new Temperature(71),
         new Temperature(55), new Temperature(65), new Temperature(64), new Temperature(74),    
         new Temperature(91), new Temperature(86), new Temperature(78), new Temperature(73),    
         new Temperature(68), new Temperature(94), new Temperature(91), new Temperature(62)    
         };

      print_array("After population", arr_temps);
      sort_array(arr_temps);
      print_array("After sort", arr_temps);
      print_days_above_90(arr_temps);
   }
   public static void print_array(String message, Temperature arr_param[])
   {
      System.out.println("----" + message + "---");

      for(Temperature oneElem : arr_param)
         System.out.print(oneElem + "\t");
      System.out.println();
   }
   public static void sort_array(Temperature arr_param[])
   {

      int min;
      int temp = 0;
      for(int i = 0; i < arr_param.length; i++)
      {
         min = i;
         for(int j = i + 1; j < arr_param.length; j++)
         {
            if(arr_param[j] < arr_param[min])
            {
               min = j;
            }
         }
         temp = arr_param[i];
         arr_param[i] = arr_param[min];
         arr_param[min] = temp;
      }
      for(int i = 0; i < arr_param.length; i++)
      {
         System.out.print(arr_param[i] + " ");
      }
   }  
   public static void print_days_above_90(Temperature arr_param[])
   {
      System.out.println("----Days over 90 F---");

         for(int i = 0; i > 90; i++)
         {
            System.out.print(arr_param[i] + " ");
         }
   }
}

该程序应该打印出数组,然后按升序打印,然后仅打印华氏90度以上的数组。

我遇到问题,无法使排序代码正常工作,并且无法对温度超过90华氏度的温度进行排序。我在代码中遇到三个错误:错误:二进制运算符'<'的操作数类型错误,错误:类型不兼容:温度无法转换为int且错误:类型不兼容:int无法转换为Temperature

1 个答案:

答案 0 :(得分:0)

在本节中,调用getFahrenheit方法进行比较:

if(arr_param[j].getFahrenheit() < arr_param[min].getFahrenheit())
{
    min = j;
}

在本节中,使用toString方法。 toString方法的目的是将您的对象数据转换为可读的String。

for(int i = 0; i < arr_param.length; i++)
{
    System.out.print(arr_param[i].toString() + " ");
}

我希望这对您有所帮助。