Hello everyone, I am new to this java programming and wanted to know if anyone could solve this problem for me. I would greatly appreciate it. Thank you!

"Write a program that asks the user for 2 integers a and b, the program displays their sum, difference, product, quotient a/b, and their remainder when a is divided by b."

You need to try to do the assignment first.

If you run into problems with the code, then post it here with a specific question, and we can help you.

You need to learn how to input data and do simple operations on it.

This is what i have

import java.util.Scanner;


public class Lab2{


public static void main(String[]args)
{
// Create a Scanner object
Scanner input " new Scanner(System.in);


// Prompt the user to enter two numbers
System.out.print("Enter two numbers: ");
double number1 = input.nextDouble();
double number2 = input.nextDouble();


// Compute sum
double sum = (number1 + number2);


// Display result


Promt the user to enter two numbers
System.out.print("Enter two numbers; ");
Double number1 = input.nextDouble();
Double number2 = input.nextDouble();


// Compute sum
Double sum = (number1 + number2);


// Display result
System.out.println("The sum of " + number1 + " " + number2 + " " + " is "
" + sum);


// Compute difference
double difference = (number1 - number2);


// Display result
System.out.println("The difference of " + number1 + " " + number2 +
" is " + difference);



//  Dispute difference
Double difference = (number1 - number2);


// Display result
System.out.println("The difference of " + number1 + " " + number2 + " is "
" + difference);


// Compute product
double product = (number1 * number2);


// Display result
System.out.println("The product of " + number1 + " " + number2 + " is "
" + product);


// Compute quoient
Double quoient = (number1 / number2);


// Display result
System.out.println("The quoient of " + number1 + " " + number2 + " is "
" + quoient);


// Compute quoient
Double quoient " (number1 / number2);


// Display result
System.out.println("The difference of " + number1 + " " + number2 + " is "
" + difference);


// Compute product
double product = (number1 * number2);


// Display result
System.out.println("The product of " + number1 + " " + number2 + " is "
" + product);


// Compute quoient
Double quoient = (number1 / number2);


// Display result
System.out.println("The quoient of " + number1 + " " + number2 + " is "
" + quoient);


// Compute quoient
Double quoient " (number1 / number2);


// Display result
System.out.println("The product of " + number1 + " " + number2 + " is "
quoient);


// Compute remainder
Double remainder = (number1 % number2);


// Display remainder
System.out.println("The remainder of " + number + " " + number2 + " is "
remainder);


}

Edited 3 Years Ago by Nick Evan: Fixed formatting

Do u feel this code is wrong? Does this compile properly? Do u get the proper output? you cant just put the code out here and expect us to compile and see for ourselves can u??

your assignment says you should take two integers as input. Why are you reading two doubles?

also, be very carefull with what you do:
a double is not the same thing as a Double

since you are asking this question, I must assume you're getting an error message, or at least not the expected output.
could you please correct the errors pointed out so far, try again and tell us what error message you get at what point (compilation, runtime, ..) or what output you get, and the one you expect to get?

When getting your difference and quotient value you may want to have the larger of the two numbers first, that will probably make the numbers better looking.

Or do the math twice, once with Num1 and the other with Num2 coming first.


Also yes, make sure you check your capitalization. In Java it's very important.
When you instantiate a new variable you need to make sure it's "double" with a little d.


I prefer to instantiate all my variables right in the start
so something like

double num1=0, num2=0, sum, diff, quot, prod;
all in one line.

When you instantiate a new variable you need to make sure it's "double" with a little d.

actually, he should make sure it's an 'int'.

but he'll need to sort out some bigger issues first, like using unique variable names.

When getting your difference and quotient value you may want to have the larger of the two numbers first, that will probably make the numbers better looking.

Or do the math twice, once with Num1 and the other with Num2 coming first.


Also yes, make sure you check your capitalization. In Java it's very important.
When you instantiate a new variable you need to make sure it's "double" with a little d.


I prefer to instantiate all my variables right in the start
so something like

double num1=0, num2=0, sum, diff, quot, prod;
all in one line.

With JAVA's autoboxing and unboxing in place I dont think itd be a big deal to use Double and Integer classes to store double and ints.

and btw, For difference you can use the Math.abs() to get the absolute value. So no need to worry about bigger and smaller numbers.

Your main problems are just simple syntax errors:

  • You need to use "=" in assignment statements, not the quote character (").
  • One of your comments does not have "//" in front of it.
  • In your 'System.out.println' statements, make sure that your quote characters (") match up (IE: there should be an even number of them!), and that you have '+' characters between all the strings and variables. You're missing '+' characters in some cases where the variable is at the start of the next line.
  • You're reading the numbers and computing the sum twice. Delete the second copy of that code.
  • Likewise, you compute most of your values multiple times. These won't compile, due to the duplicate (and conflicting) variable definitions. Delete the redundant lines.
  • At the end of the program, you're missing a closing brace -- '}'.

With JAVA's autoboxing and unboxing in place I dont think itd be a big deal to use Double and Integer classes to store double and ints.

propably not, but since his assignment says he needs to write integers (primitives, not the Integer wrapper class), I don't think he's doing himself favours by using something but integers.

Yep; using 'double' and 'Double' everywhere is just wrong. The inputs, at least, need to be integer values.

Now if the inputs are the integer values 3 and 2, should '3 / 2' result in '1' (the correct integer result) or '1.5' (the correct floating point result)?

I think we can safely assume, for this assignment, that everything should be integers -- no floating point inputs, computations or outputs. But it is a concept that programmers will eventually have to think about. But probably not for this assignment.

Here's some sage advice to a beginner programmer:

When your program does not compile, read the first compile error message very carefully. Figure out what it's trying to tell you. Fix that problem.

Then run the compiler again. If you get more error messages, focus again on the first one, as above. Repeat until you get a "clean compile" -- no error messages.

You want to focus on the first compiler error message and largely ignore the others because the compiler can become "confused" with your first mistake and give misleading error messages thereafter.

As a beginner, you will probably be confused and frustrated by compiler error messages, as they prevent you from running your program. Later, you will realize that they are the least of your problems, and that they are actually quite helpful: They will prevent you from wasting your time running a program that can't possibly work correctly. Compiler error messages are the easiest to fix. You'll be happy to see and fix them. And then you'll come back here with more questions when you have to deal with logic errors in your code. :twisted:

This article has been dead for over six months. Start a new discussion instead.