Results 1 to 6 of 6

Thread: Tell me if I am doing this right

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    19

    Tell me if I am doing this right

    Can somebody tell me if I am doing this right. If not please tell me what I am doing wrong.

    1. Write a program that prompts the user for a temperature, then prints out the activity appropriate for that temperature.

    2. Modify your program so that if the temperature is greater than 95 or less than 20, it prints "Visit our shop!"

    temp >= 80 Swimming
    60 <= temp <80 Tennis
    40 <= temp < 60 Golf
    temp < 40 Skiing

    import java.util.*;
    import java.util.Scanner;

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


    final int Swimming; //read Swimming
    final int Tennis; //read Tennis
    final int Golf; //read Golf
    final int Skiing; //read Skiing

    Scanner scan = new Scanner(System.in);

    int temp;

    System.out.print ("Enter the temperature: ");
    temp = scan.nextInt();

    System.out.println ("Activity: " + Swimming + Tennis + Golf + Skiing);

    if (temp >= 80);
    System.out.println("Swimming");


    if (temp > 95);
    if (temp < 20);
    System.out.println("Visit our Shop!");


    if (temp <= 60);
    if (temp < 80);
    System.out.println("Tennis");


    if (temp <= 40);
    if (temp < 60);
    System.out.println("Golf");


    if (temp < 40);
    System.out.println("Skiing");


    }

    }

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Tell me if I am doing this right

    Code:
    import java.util.* ;
    
    public class MyApp
    {
      public static void main ( String[] args )
      {
        Scanner scan = new Scanner( System.in ) ;
        int temp ;
        System.out.print( "Enter the temperature: " ) ;
        temp = scan.nextInt() ;
        System.out.print( "Activity: ") ;
        if ( temp >= 80 && temp <= 95 )
        {
          System.out.println( "Swimming" ) ;
        }
        else if ( temp > 95 || temp < 20 )
        {
          System.out.println( "Visit our Shop!" ) ;
        }
        else if ( temp >= 60 || temp < 80 )
        {
          System.out.println( "Tennis" ) ;
        }
        else if ( temp >= 40 || temp < 60 )
        {
          System.out.println( "Golf" ) ;
        }
        else if ( temp < 40 )
        {
          System.out.println( "Skiing" ) ;
        }
      }
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    Lively Member
    Join Date
    Dec 2005
    Posts
    68

    Re: Tell me if I am doing this right

    ComputerJy, you have a TON of unnessesary code in there.

    Code:
    import java.util.* ;
    
    public class MyApp
    {
      public static void main ( String[] args )
      {
        Scanner scan = new Scanner( System.in ) ;
        int temp ;
        System.out.print( "Enter the temperature: " ) ;
        temp = scan.nextInt() ;
        System.out.print( "Activity: ") ;
        if ( temp > 95 || temp < 20 )
        {
          System.out.println( "Visit our Shop!" ) ;
        }    
        else if ( temp >= 80)
        {
          System.out.println( "Swimming" ) ;
        }
        else if ( temp >= 60)
        {
          System.out.println( "Tennis" ) ;
        }
        else if ( temp >= 40)
        {
          System.out.println( "Golf" ) ;
        }
        else if (temp >= 20)
        {
          System.out.println( "Skiing" ) ;
        }
      }
    }
    explained:

    if (temp >= 90)
    statement;
    else if (temp >= 80)
    statement;

    If temperature is NOT greater than or equal to 90, it MUST be less than 90. Therefore:
    else if (temp >= 80 && temp <= 90) is unnessesary.

    And you were lecturing ME for lack of effieciency?

    EDIT: spelling
    Last edited by lunchboxtheman; Oct 10th, 2006 at 03:49 PM.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Tell me if I am doing this right

    Quote Originally Posted by lunchboxtheman
    ComputerJy, you have a TON of unnecessary code in there.
    I didn't write the code for the best performance, I wrote it so tmlucky14 knows what's s/he doing wrong
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5
    Lively Member
    Join Date
    Dec 2005
    Posts
    68

    Re: Tell me if I am doing this right

    That's fine, totally understandable. But don't lecture other people for things that you do wrong (purposely or not) as well. If I put unessesary code in my post even for debugging purposes it's poor programming. If you do it, it's fine? Besides, msot of the problems the OP was having lie in the syntax of the if statements, not in the logic of the boolean expressions themselves.

    Anyway, the extra lines that I took out are unnessesary, tmlucky14, and shouldn't really be used. It is syntactically correct and will work logically, although not efficient and would be considered poor programming in a real-world environment. If this is for school you may get points taken off for using the extra code, I know that I would have in school. Your main problem was just forgetting to ad the "else" clauses to your "ifs". There were a few other small things, but nothing a little testing and debugging couldn't fix :-)
    Last edited by lunchboxtheman; Oct 10th, 2006 at 03:57 PM.

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Tell me if I am doing this right

    That's fine, totally understandable. But don't lecture other people for things that you do wrong as well. If I put unnecessary code in my post even for debugging purposes it's poor programming.
    The code isn't wrong and I didn't give a false advice. You just optimized the code. [for god's sake you told the man to add extra lines on that post]
    This is a professional forum, we are not here to hate or track each other mistakes..
    Point is taken, and thanks for fixing my code
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width