0

after tweaking my code and moving on to the second section, I'm not sure why the output for part 2 is incorrect and it mostly outputs 0s and other numbers not in the array? I put an output for the array just to check what was going on but you can pretty much ignore it. Not sure where I'm going wrong any pointers? This time I also included my task:

//temperature management
import java.util.Scanner;
import java.util.Arrays;
public class temperaturemanagement
{
    Scanner in = new Scanner(System.in);
    //task 1
    //To simulate the monitoring required, write a routine that allows entry of the apartment’s temperature in degrees Celsius. The routine checks whether the temperature is within the acceptable range, too high or too low and outputs a suitable message in each case.
    public void task1()
    {
        System.out.println("Input Temperature: ");
        double temperature = in.nextDouble();
        double temperaturerounded = Math.round(temperature * 10) / 10.0;
        if (temperaturerounded >=22 && temperaturerounded <=24)
        {
            System.out.println("Normal");
        }
        else if (temperaturerounded <22)
        {
            System.out.println("Too Low");
        }
        else if (temperaturerounded >24.0)
        {
            System.out.println("Too High");
        }
    }
    //task 2
    //Write another routine that stores, in an array, the temperatures taken over a period of five hours. This routine calculates the difference between the highest temperature and the lowest temperature. Then it outputs the highest temperature, the lowest temperature, and the difference between these temperatures.
    public void task2()
    {
        System.out.print("Input Number of Hours: ");
        int hour = in.nextInt();
        //*12 because every 5 minutes and since there are 60 minutes in a hour
        // this means 60/5 = 12 hence 12* the number of hours inputted
        int loophours = hour*12 +1;
        //loops array, rounds input and sorts the array by ascending order
        //using Array.sort
        double[] tempstorage = new double [loophours];
        for (int i=1; i<loophours; i++)
        {
            System.out.println("Temp Count: " +i);
            double temperature = in.nextDouble();
            double temperaturerounded = Math.round(temperature * 10) / 10.0;
            tempstorage[i] = temperaturerounded;
            Arrays.sort(tempstorage);
        }
        //prints the positions 1 (lowest temperature) and loophours (highest temperature)
        for (int i=1; i<loophours; i++)
        {
            System.out.println(tempstorage[i]);
        }
        double min = tempstorage[1];
        System.out.println(min);
        double max = tempstorage[loophours-1];
        System.out.println(max);
        double difftemp = max - min;
        System.out.println("Highest Temperature: "+max);
        System.out.println("Lowest Temperature: "+min);
        System.out.println("Difference between Temperatures: "+difftemp);
    }
}
  • `double` is not `float` – Lino Nov 13 '17 at 13:51
  • You can't "round" to decimal places (at least reliably) due to how floating point math works, and I can't understand what you're trying to ask. Why do you care what the user inputs? Maybe they want to enter `23.9999`? – Kayaman Nov 13 '17 at 13:52
  • 1
    I think OP clearly explained he/she doesn't want to use strings. So this question is different than referenced "duplicate". – jurez Nov 13 '17 at 13:53
  • @jurez the OP is new to Java, so I'm making some decisions on his behalf. It's not about what you want to do, it's about what you should do. – Kayaman Nov 13 '17 at 13:53
  • 1
    @Kayaman Do what you want, but I think you're not helping him. – jurez Nov 13 '17 at 13:58
  • @Kayaman sorry I'm not making myself too clear what my task says is similar to a fetch decode execute cycle and it is a temperature regulator if the temperature is too high or too low it will output it as said. As I was writing the code I realised that if I had a number 24.02 inputted and if it had to be rounded to 1 d.p (as temperature is only to 1 d.p) it should output as 24.0 and hence be "Normal" but in my code it outputs as "Too High" I'm not too sure how to fix this and sorry for all the inconvenience caused. (and my code also doesnt work) :C – Jasper YIP Nov 13 '17 at 13:59
  • @jurez sorry it was my fault I didnt make myself clear enough – Jasper YIP Nov 13 '17 at 13:59
  • Did everyone just leave? Can someone else please help me? I'm still very confused – Jasper YIP Nov 13 '17 at 14:05
  • Well, the duplicate does have plenty of options for you to use, if you ignore the answers that are using formatting only. As you can see, it's not as simple as you might think. Floating point math is a lot more complex than what you learn in school, especially considering that it doesn't have "decimal places" at all. – Kayaman Nov 13 '17 at 14:05
  • So what should I do to improve my code right now and can you help tell me the difference between float and double? sorry I'm a IB computer science student and we just started java (new to me) – Jasper YIP Nov 13 '17 at 14:07
  • 1
    I think the OP is really looking for a simple solution like this: `roundedNumber = Math.round(number * 10) / 10.0` – jurez Nov 13 '17 at 14:08
  • @jurez what variable should roundedNumber be? nvm its a double THANKS FOR ALL YOUR HELP <3333 – Jasper YIP Nov 13 '17 at 14:09
  • @jurez that's not really a simple solution, that's a [wrong solution](https://stackoverflow.com/a/153753/2541560). I hope the homework is better designed than that. – Kayaman Nov 13 '17 at 14:12
  • @Kayaman it worked for 24.02 and gave it as a Normal answer after messing around with it some more I'm not too sure where this could be a wrong solution? If so may you please give me a better alternative in which I could improve my code? This is extremely confusing :( – Jasper YIP Nov 13 '17 at 14:18
  • 1
    `float temperaturerounded = Math.round(temperature * 10f) / 10f` – jurez Nov 13 '17 at 14:24
  • It is, which is why I wondered if your homework was poorly thought out, or if you're doing something that you're not required to do. The flaws of that approach are explained in the comments for the answer I linked. Maybe the homework is low quality and you're expected to solve it in a way that would never be acceptable in professional programming. Maybe you were supposed to only format the number, not round. Hard to say. @jurez you might want to read the links too, in case you ever have to write professional calculations. – Kayaman Nov 13 '17 at 14:24
  • 1
    Bike shed effect. QED – jurez Nov 13 '17 at 14:27
  • well anyways thanks for all the help and the time you guys spent in trying to help assist me. I really appreciate it! Wish you guys all the best and have a nice day today! – Jasper YIP Nov 13 '17 at 14:29
  • @jurez I'm not sure what you're trying to imply with that. Based on my experience on SO, it's probably an attempt at insulting me because I don't approve of your "great simple solution". If I'm wrong, please correct me. If I'm right, then imagine I insulted you back in some suitable way. – Kayaman Nov 13 '17 at 14:31
  • @jurez btw, the duplicate describes that as one of the answers, so next time before you start crying out "it's not a duplicate", actually **read** the duplicate. – Kayaman Nov 13 '17 at 15:07
  • @Kayaman if you could help me out again it would be really really nice. Can't ask another question until another 3 days :\ sorry. Is it something to do with the rounding or is it to do with the Arraysort function? – Jasper YIP Nov 13 '17 at 16:53
  • Don't call `Arrays.sort()` inside the loop. You're constantly changing the array and overwriting values. Also, arrays start from `0`, so it's `for(int i = 0;i < ...`. Lastly, it's just `hours * 12`. There's no `+1`. – Kayaman Nov 13 '17 at 17:39
  • @Kayaman thank you very very much :) – Jasper YIP Nov 13 '17 at 23:17

0 Answers0