tags:

views:

382

answers:

1

I'm trying to computing a few things in a simple java application for school. A user must into 2 integers and once you input those variables in it will spit out the sum, product, quotient and difference. I am only able to get the program to spit out sum but not product, quotient and difference. any suggestions? here is my code:

// This is for HW problem #3
// An application that asks the user to enter two 
// integers, obtains them from the user and prints their sum, 
// product, difference and quotient (division). 

import java.utl.Scanner; //program uses class Scanner

public class Solution
{ 
  // main method begins execution of Java application
  public static void ( String ags [] )
  {
    // create Scanner to obtain input from command window
    Scanner input = new Scanner ( System.in );

   int number1; //first integer to add
   int number2; //second integer to add
   int sum; // sum of number1 and number2
   int product; //product of number 1 and number2
   int differnec; //difference of number1 and number2
   int quotient; //quotient of number1 and number2

   System.out.print( "Enter first integer: " ); // prompt
   number1 = input.nextInt(); // read first number from user

   System.out.print( "Enter second integer: " ); // prompt
   number2 = input.nextInt(); // read second number from user

   sum = number1 + number 2; // add numbers
   product = number1 * number2; // multiply numbers
   difference = number1 - number2; // subtract numbers
   quotient = number1 / number2; // divide numbers

   System.out.printf( " Sum is %d\n ", sum ); //display sum

   System.out.printf( " Product is %d\n ", product ); //display prouduct

   System.out.printf( " Difference is %d\n ", difference ); //display difference

   System.out.printf( " Quotient is %d\n ", quotient ); //display quotient

  } // end method main

} // end class Addition
+2  A: 

Aside from the syntax errors:

import java.utl.Scanner;
public static void ( String ags [] )
int differnec;
difference = number1 - number2;
sum = number1 + number 2;

which would stop that code from even compiling, it looks okay.

What is the actual output you're getting? Is it wrong or is it not there?

Step 1, fix the syntax errors then try again. Make sure the code you paste here is exactly the same as the code you're testing.

Warning: If you ever want to be a decent developer, don't read below until you've spent quite a bit of time trying to fix your problems using just the suggestions above.

Myself, I don't care either way since you're unlikely to ever compete with me, due to geographical separation, skill difference and my fast-closing-on-retirement age :-)

If you're still stuck after trying that, here's my solution but I strongly suggest against using it (assuming this is homework rather than just practice) rather than fixing your own code for the following reasons:

  • you'll be a better problem solver and developer if you learn by your mistakes.
  • your educators will certainly be watching this site and, if you hand in verbatim-copied work, you will fail.

Here it is:

import java.util.Scanner;
public class Solution { 
    // main method begins execution of Java application
    public static void main (String ags []) {
        // create Scanner to obtain input from command window
        Scanner input = new Scanner ( System.in );

        int number1; //first integer to add
        int number2; //second integer to add
        int sum; // sum of number1 and number2
        int product; //product of number 1 and number2
        int difference; //difference of number1 and number2
        int quotient; //quotient of number1 and number2

        System.out.print( "Enter first integer: " ); // prompt
        number1 = input.nextInt(); // read first number from user

        System.out.print( "Enter second integer: " ); // prompt
        number2 = input.nextInt(); // read second number from user

        sum = number1 + number2; // add numbers
        product = number1 * number2; // multiply numbers
        difference = number1 - number2; // subtract numbers
        quotient = number1 / number2; // divide numbers

        System.out.printf (" Sum is %d\n ", sum );
        System.out.printf (" Product is %d\n ", product );
        System.out.printf (" Difference is %d\n ", difference );
        System.out.printf (" Quotient is %d\n ", quotient );
    } // end method main
} // end class Addition

which spits out:

Enter first integer: 5
Enter second integer: 2
 Sum is 7
 Product is 10
 Difference is 3
 Quotient is 2
paxdiablo
can't anyone write "int differrence;" correctly??
yjerem
Not even you apparently, @Jeremy :-) unless you were *trying* to be funny. I thought my fat fingers had pressed 5 somewhere within Eclipse but I couldn't find it. Thanks for pointing it out. It's annoying that, even when you run code, it leaves the cursor in the source code pane rather than the console pane.
paxdiablo
Just a niggling worry about division - are the expected answers in float/double or just int. We used to have an automated marker script in CS and it would throw fits.
whatnick