question archive Fix the bugs in the following code

Fix the bugs in the following code

Subject:Computer SciencePrice:2.87 Bought7

Fix the bugs in the following code.

public class Temperature

{

 private double temperature;

 public static double maxTemp = 0;

 

 public Temperature(double t)

 {

   temperature = t;

   if (t > maxTemp)

     maxTemp = t;

 }

 

 public static printMax()

 {

   System.out.println(temperature);

 }

 

 public static void main(String[] args)

 {

   Temperature t1 = new Temperature(75);

   Temperature t2 = new Temperature(100);

   Temperature.printMax();

 }

 }

 

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Answer:

public class Temperature

{

 private double temperature;

 public static double maxTemp = 0;

 public Temperature(double t)

 {

  temperature = t;

  if (t > maxTemp)

    maxTemp = t;

 }

 public static void printMax()

 {

  System.out.println(maxTemp);

 }

 public static void main(String[] args)

 {

  Temperature t1 = new Temperature(75);

  Temperature t2 = new Temperature(100);

  Temperature.printMax();

 }

 }

Errors in your code:

  • printMax() function is a static function, so you cant not access non static members in this function.
  • According to the question it seems that you want to print maximum Temperature seen so far. So in printMax() function you should write maxTemp instead of temperature.
  • Second error was that, printMax function should have some return type in its prototype.
  • I write it as void.
  • output screen shot:

PFA