Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: Help with Java work.

  1. Registered TeamPlayer sickwookie's Avatar
    Join Date
    05-14-09
    Posts
    652
    Post Thanks / Like
    Stat Links

    Help with Java work.
    Gamer IDs

    Steam ID: sickwookie
    #11

    Re: Help with Java work.

    Quote Originally Posted by Imisnew2
    Lol, the irony - My next C++ project (a snipped from it anyway)

    Quote Originally Posted by Professor
    For example, the postfix expression 5 6 7 8 + - * 9 - evaluates as follows
    • First apply + to 7 8 yielding 15. The new expression is 5 6 15 - * 9 -.
    • Next apply - to 6 15 yielding -9. The new expression is 5 -9 * 9 -.
    • Third, apply the * to 5 and -9 yielding -45. The new expression is -45 9 -.
    • Last apply the final - to -45 and 9 to get -54. This is the result.
    I remember that assignment. If I recall it was an exercise in using stacks. Push values onto the stack until you hit an operator pop two values apply operator and push result until you are done.

    Kinda makes me miss school. If only real world programming problems were this much fun.

  2. Registered TeamPlayer
    Join Date
    10-29-07
    Posts
    4,953
    Post Thanks / Like
    Stat Links

    Help with Java work.
    #12

    Re: Help with Java work.

    Quote Originally Posted by sickwookie
    Kinda makes me miss school. If only real world programming problems were this much fun.
    They can be. Go find yourself a project. That's half the reason I work on lonestar, its a fun "real world" programming problem.

  3. Registered TeamPlayer sickwookie's Avatar
    Join Date
    05-14-09
    Posts
    652
    Post Thanks / Like
    Stat Links

    Help with Java work.
    Gamer IDs

    Steam ID: sickwookie
    #13

    Re: Help with Java work.

    Quote Originally Posted by Ewok
    Quote Originally Posted by sickwookie
    Kinda makes me miss school. If only real world programming problems were this much fun.
    They can be. Go find yourself a project. That's half the reason I work on lonestar, its a fun "real world" programming problem.
    Well, I was referring to the fact that in the "real world" the programming is the easy part. Dealing with management and anyone with a PHD is the real pain in my ass.

    I may check out the lonestar code just for shits though.

  4. Registered TeamPlayer
    Join Date
    10-29-07
    Posts
    4,953
    Post Thanks / Like
    Stat Links

    Help with Java work.
    #14

    Re: Help with Java work.

    Feel free, SoccerDog is starting to experiment with it, and I could use any kind of help. Even for simple things or just polishing features up.

    Although here, management is BD and the other highs, so... yeah. jk
    .. I think BD even has a PHD...

  5. Registered TeamPlayer
    Join Date
    10-15-09
    Posts
    1
    Post Thanks / Like
    #15

    Re: Help with Java work.

    I'm having trouble getting the min and max value. They come out as the same value.
    Code:
           
    import java.util.Scanner;
    public class Calc
    {
      public static void main (String[] args)
      {
       Scanner scan = new Scanner(System.in);
    
       final int SENTINEL = -999;
       final Boolean DEBUG = false;
       final char OPERATOR1 = '+', OPERATOR2 = '-',
             OPERATOR3 = '*', OPERATOR4 = '/';
       int numLines = 0;
       int min = 0;
       int max = 0;
       int num1 = 0;
       int num2 = 0;
       int result = 0;
       char math;
    
       if(scan.hasNextInt())
       {
         num1 = scan.nextInt();
       }
       else
       {
         System.out.println("Error: Input is not an integer.");
         System.exit(0);
       }
    
       System.out.println("==================");
       System.out.println("Simple calculator");
       System.out.println("==================");
         while(num1 != SENTINEL)
         {
         math = scan.next().charAt(0);
         if(scan.hasNextInt())
         {
          num2 = scan.nextInt();
         }
         else
         {
          System.out.println("Error: Input is not an integer.");
          System.exit(0);
         }
    
         if(math == OPERATOR1)
         {
          result = num1 + num2;
         }
         if(math == OPERATOR2)
         {
          result = num1 - num2;
         }
         if(math == OPERATOR3)
         {
          result = num1 * num2;
         }
         if(math == OPERATOR4)
         {
          if(num2 == 0)
          {
            System.out.println("Error: Cannot devide by 0");
            System.exit(0);
          }
          else
          {
            result = num1 / num2;
          }
         }
    
         if(math != OPERATOR1 && math != OPERATOR2 && math != OPERATOR3 &&
          math != OPERATOR4)
     
            {
          System.out.println("Error: Operator is invalid.");
          System.exit(0);
         }
    
         System.out.println("Operation: " + num1 + " " + math + " " + num2);
         System.out.println("Result  " + ": " + result);
         System.out.println();
    
         if(result >= max)
         {
          result = max;
         }
         if(result <= min)
         {
          result = min;
         }
         numLines++;
    
         if(DEBUG)
         {
          System.out.println("Debug Information:");
          System.out.println("  Number of lines read = " + numLines);
          System.out.println("  Current max: " + max);
          System.out.println("  Current min: " + min);
         }
    
         if(scan.hasNextInt())
         {
          num1 = scan.nextInt();
         }
         else
         {
          System.out.println("Error: Input is not an integer.");
          System.exit(0);
         }
       }
       System.out.println("Number of lines read: " + numLines);
       System.out.println("Maximum calculated value: " + max);
       System.out.println("Minimum calculated value: " + min);
      }
    }

  6. Registered TeamPlayer i8pptuakamonstercam's Avatar
    Join Date
    11-26-06
    Location
    Anywhere you want to be.
    Posts
    3,946
    Post Thanks / Like
    Blog Entries
    1
    Stat Links

    Help with Java work.
    #16

    Re: Help with Java work.

    In this assignment you must initialize max and min to the result of
    the first valid calculation; a request for division by zero
    is not valid. To do this you will need to use an "if" statement
    in order to initialize max and min to the calculated value
    when they have not yet been initialized and when division by zero
    is not being requested.

    example)


    if (Input your boolean statement here)
    {
    iMin = result;
    iMax = result;
    }



    so first things, for your max and min take off your '= 0'. They will be initialized to the FIRST VALID RESULT

  7. Registered TeamPlayer
    Join Date
    10-29-07
    Posts
    4,953
    Post Thanks / Like
    Stat Links

    Help with Java work.
    #17

    Re: Help with Java work.

    You never assign min or max except when you declared them

    I think you flipped your logic:
    result = min
    should be
    min = result

    Same for max

Page 2 of 2 FirstFirst 12

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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