Java .. it makes you wonder



  • Im submitting this program from a peer in my class .. Programming in Java

    you be the judge ..

    Assignment:
    <font face="Verdana" size="2">Write a program that accepts 5 temperatures as input
    (use a for loop, a while loop, or a do-while loop). Output the highest
    temperature and the lowest temperature.

    </font>
    <font face="Verdana" size="2">Submitted Code:

    </font>import javax.swing.*;

    public class TemperatureProgram
    {
    public
    static void main(String[] args)
    {

    JOptionPane.showMessageDialog(null,
    "Welcome to The Temperature Program.");
    String UserNumber;
    int LoopCount
    = 1;
    int Temp1 = 0;
    int Temp2 = 0;
    int Temp3 = 0;
    int Temp4 =
    0;
    int Temp5 = 0;
    int HighTemp = 0;
    int LowTemp = 0;
    int
    ConvertUserNumber = 0;
    while (LoopCount <= 5) // Start
    Loop
    {

    UserNumber = JOptionPane.showInputDialog("Enter a Temperature
    "+LoopCount+":");// User Input

    ConvertUserNumber =
    Integer.parseInt(UserNumber);// Convert User Input to
    Number

    switch(LoopCount) // switch statement that assigns user input
    according to LoopCount
    {
    case 1:
    Temp1 =
    ConvertUserNumber;
    break;
    case 2:
    Temp2 =
    ConvertUserNumber;
    break;
    case 3:
    Temp3 =
    ConvertUserNumber;
    break;
    case 4:
    Temp4 =
    ConvertUserNumber;
    break;
    case 5:
    Temp5 =
    ConvertUserNumber;
    break;
    default:
    JOptionPane.showMessageDialog(null,
    "You should never get
    here.");
    System.exit(0);
    break;
    }

    LoopCount++;

    }
    //Below
    is the algorithm to determine what the highest Temp is
    if ((Temp1 >=
    Temp2) && (Temp1 >= Temp3) && (Temp1 >= Temp4) &&
    (Temp1 >= Temp5))
    HighTemp = Temp1;
    else if ((Temp2 >= Temp1)
    && (Temp2 >= Temp3) && (Temp2 >= Temp4) && (Temp2
    >= Temp5))
    HighTemp = Temp2;
    else if ((Temp3 >= Temp1) &&
    (Temp3 >= Temp2) && (Temp3 >= Temp4) && (Temp3 >=
    Temp5))
    HighTemp = Temp3;
    else if ((Temp4 >= Temp1) && (Temp4
    >= Temp3) && (Temp4 >= Temp2) && (Temp4 >=
    Temp5))
    HighTemp = Temp4;
    else if ((Temp5 >= Temp1) && (Temp5
    >= Temp3) && (Temp5 >= Temp4) && (Temp5 >=
    Temp2))
    HighTemp = Temp5;
    JOptionPane.showMessageDialog(null, "The Highest
    Temperature is : "+ HighTemp);
    //Below is the algorithm to determine what the
    lowest Temp is

    if ((Temp1 <= Temp2) && (Temp1 <= Temp3)
    && (Temp1 <= Temp4) && (Temp1 <= Temp5))
    LowTemp =
    Temp1;
    else if ((Temp2 <= Temp1) && (Temp2 <= Temp3) &&
    (Temp2 <= Temp4) && (Temp2 <= Temp5))
    LowTemp = Temp2;
    else
    if ((Temp3 <= Temp1) && (Temp3 <= Temp2) && (Temp3 <=
    Temp4) && (Temp3 <= Temp5))
    LowTemp = Temp3;
    else if ((Temp4
    <= Temp1) && (Temp4 <= Temp3) && (Temp4 <= Temp2)
    && (Temp4 <= Temp5))
    LowTemp = Temp4;
    else if ((Temp5 <=
    Temp1) && (Temp5 <= Temp3) && (Temp5 <= Temp4) &&
    (Temp5 <= Temp2))
    LowTemp = Temp5;
    JOptionPane.showMessageDialog(null,
    "The Lowest Temperature is : "+
    LowTemp);

    System.exit(0);

    }
    }



  • Heh, that's actually quite funny, that he knows enough to make a
    functioning Swing mini-app, but is wholly lost when it comes to
    rudimentary algorithimcs, unless he stole a bare bones GUI scaffolding
    and tried to shoehorn in the solution...



    Still, I saw similar things when I taught introductory (and sometimes
    continued!) programming, but this still made me quirk an eyebrow :P




  • Yeah, it's not a Java wtf, it's a "doesn't know how to solve the problem with a proper algorithm" wtf.



  • He's a student.  For a lot of kids, a class like this is their absolute first exposure to programming.  In fact, judging by the simplicity of the assignment, this is an entry level class.  I assume you, like myself, are in the minority that actually learned to code at home as a kid.  Good for you.  I'm sure you produced code like this in your first few months coding.  I know I did.

    It's only a WTF when someone who's supposedly an expert gets paid to produce code, and comes up with something this bad.



  • @merreborn said:

    I'm sure you produced code like this in your first few months coding.

    Definitely not.



  • I was not a computer science major as an undergrad, but took my masters in it, and was essentially told during the first week: just pick it up.

    My first program (beyond hello-world in plain BASIC) was in assembly language on a Univac 11xx, and the algorithm to be implemented was to implement a rolling file buffer to parse files. I had *no clue* how to do it, so I brute forced it. Of course, after I saw the proper algorithm for how it should have been done, I felt kind of stupid, invested in an algorithms book, and educated myself.

    I guess a true noobie has to go through this. The question is: did anyone bother to show this person the *right* way to do it after the fact, so that they might learn something from it?



  • I see stuff like that ALL THE TIME at the university.
    I'm surprised that the guy found the "popup" dialog in java, and wasn't taught to use the  System.out / System.in streams with a BufferedReader/Writer.

    A friend of mine is tutoring 2nd semester uni CS students right now, and assignments such as 'write a program that prints the numbers from 1 to 10' take a good amount of time to complete. Of course, you have to start somewhere. It may be brutal, but how else are you going to start.. :)




  • Yes,
    I would say that for a newbie, this 'algorithm' denotes that this guy, once he'll learn arrays, will probably make good job.. I mean, i know a LOT of people giving up for less than that.

    By the way, this guy is OK. He made something which works, he probably passed some 10x minutes to make that.




  • @Arden679 said:

    Assignment:
    <FONT face=Verdana size=2>Write a program that accepts 5 temperatures as input (use a for loop, a while loop, or a do-while loop). Output the highest temperature and the lowest temperature.</FONT>

    //Below is the algorithm to determine what the highest Temp is
    if ((Temp1 >= Temp2) && (Temp1 >= Temp3) && (Temp1 >= Temp4) && (Temp1 >= Temp5))
    HighTemp = Temp1;
    else if ((Temp2 >= Temp1) && (Temp2 >= Temp3) && (Temp2 >= Temp4) && (Temp2 >= Temp5))
    HighTemp = Temp2;

    They say laziness is a programmer virtue.  To drive that point home, they might have given out this follow-up assignment a couple days later:

    Assignment:
    <FONT face=Verdana size=2>Write a program that accepts 60 temperatures as input (use a for loop, a while loop, or a do-while loop). Output the highest temperature and the lowest temperature.</FONT>

    (That said, of course I don't think a student's homework qualifies as a WTF.  He made it work, and we can't know how much he had been taught about programming style and best practices etc.  The algorithm to determine the highest and lowest temperature would be a WTF if seen in a professional's production code, no doubt about that.)



  • Phil ok :)
    i would write an algorithm which writes my algorithm in java to solve that (code generator).:
    :p



  • @enjoyaol said:

    Yes,
    I would say that for a newbie, this 'algorithm' denotes that this guy, once he'll learn arrays, will probably make good job.. I mean, i know a LOT of people giving up for less than that.

    (emphasis added)
    I really really hope you do not want to say that an array (or any other container) is necessary to solve the given task.



  • i do what i want with my memory..i paid it, no ?: )



  • When I went through Compsci101, they didn't actually teach arrays until quite some time into the course.
    I already knew it all ( ;) ) but almost all the students submitted code like the above, and it was expected. The above code would be considered pretty good at that level.



  • Yeah, arrays are not needed:

    int high, low
    loop 5 times
    {
       input = input number
       if input > high
          high = input
       if input < low
          low = input
    }



  • So we get two WTFs rolled into one:
    1. Learning GUI programming before basic data structures; that's like learning to run before learning to walk.
    2. Someone should teach all those beginners to f*****g think before coding. Because, as noted, there is no need for a data structure in the first place.
    3. Using Java for an introductory programming language (see also point 1). Ok, three WTFs.



  • @ammoQ said:

    @merreborn said:
    I'm sure you produced code like this in your first few months coding.

    Definitely not.

    So you're telling me that your fisrst lines of code were beyond linear thinking? I find that incredibly difficult to believe.



  • It's actually quite simple. Just use basic java functionality.
    Create a Temperature Object to hold the value.
    Implement the Comparable interface to have the higher temperature return 1 and use a TreeSet to store the temperature objects. Done...



  • @wing said:


    So you're telling me that your fisrst lines of code were beyond linear thinking? I find that incredibly difficult to believe.

    My first code might have had other deficiencies, but it didn't look like the example posted above.



  • my guess is that the person that submitted this code knew full damn well exactly what the teacher wanted.  then he went off and wrote this (which meets all the requirements) just to piss the proffessor off.



  • @Carnildo said:

    lowTemp = really large number

    highTemp = really small number


    @pests said:
    int high, low
    loop 5 times
    {
       input = input number
       if input > high
          high = input
       if input < low
          low = input
    }


    damn you.

      int i, min, max;
      scanf("%d", &min);
      max = min;
      for (i = 0; i < 4; i++)
      {
        int x;
        scanf("%d", &x);
        if (x>max) max = x;
        if (x<min) min = x;
      }
      printf("min: %d; max: %d", min, max);




  • @rebel_inside said:


      int i, min, max;
      scanf("%d", &min);
      max = min;
      for (i = 0; i < <font color="#ff0000">4</font>; i++)
      {
        int x;
        scanf("%d", &x);
        if (x>max) max = x;
        if (x<min) min = x;
      }
      printf("min: %d; max: %d", min, max);


    Looks at fingers

    Zero, One, Two, Three.
    4<=4 === False.
    There's four fingers up, you fail.

    Couldn't resist!



  • @akrotkov said:

    @rebel_inside said:

      int i, min, max;
      scanf("%d", &min);
      max = min;
      for (i = 0; i < <font color="#ff0000">4</font>; i++)
      {
        int x;
        scanf("%d", &x);
        if (x>max) max = x;
        if (x<min) min = x;
      }
      printf("min: %d; max: %d", min, max);


    Looks at fingers

    Zero, One, Two, Three.
    4<=4 === False.
    There's four fingers up, you fail.

    Couldn't resist!


    erm that code is correct, it reads the first value above the loop. You fail.
    If you were joking, well I couldn't pick it up; sorry. :)


Log in to reply