Jay Haring

Greenhorn

Posts: 3

posted 1 year ago

I have been given the follow code and directions. Any help would be appreciated. I cannot seem to get it to work, even though some of you will find this simple! haha

public class Example {

public static void main(String args[])

{

// declare and initialize variables

int number1 = 0;

int number2=0;

int number3=0;

// assign other values to variables number1 and number2

number1 = 10;

number2=5;

// add the values stored in number1 and number2 and assign to variable number3

number3 = number1+number2;

// print

System.out.printf("The addition of number1 and number2 is equal to %d\n, number3);

}

}

Modify the code above to print out the Addition, Subtraction, Multiplication, Division, and Remainder of number 1 and number 2 using the arithmetic operators in Fig 2.11 of the book.

Store the values of each arithmetic operation in the variable called number 3 and then print out the results of that operation (for ex: the following print out should be displayed for addition

"The addition of number 1 and number 2 is equal to : 10" and so on.

public class Example {

public static void main(String args[])

{

// declare and initialize variables

int number1 = 0;

int number2=0;

int number3=0;

// assign other values to variables number1 and number2

number1 = 10;

number2=5;

// add the values stored in number1 and number2 and assign to variable number3

number3 = number1+number2;

System.out.printf("The addition of number1 and number2 is equal to %d\n, number3);

}

}

Modify the code above to print out the Addition, Subtraction, Multiplication, Division, and Remainder of number 1 and number 2 using the arithmetic operators in Fig 2.11 of the book.

Store the values of each arithmetic operation in the variable called number 3 and then print out the results of that operation (for ex: the following print out should be displayed for addition

"The addition of number 1 and number 2 is equal to : 10" and so on.

Jay Haring

Greenhorn

Posts: 3

posted 1 year ago

The bottom instructions are directly from my professor. Modify the code to print out the addition, subtraction, multiplication, division, and remainder of number 1 and number 2. If that is considered coding a calculator then I guess that is what I'm supposed to be doing. Sorry for being unclear.

posted 1 year ago

public class oneandtwo {

public static void main(String[] args) {

add();

subtract();

multiply();

divide();

remainder();

}

public static void add(){

int sum = 1 + 2;

System.out.println("Sum of one and two is " + sum);

}

public static void subtract(){

int diff = 1 - 2;

System.out.println("Difference of one and two is " + diff);

}

public static void multiply(){

int product = 1 * 2;

System.out.println("Product of one and two is " + product);

}

public static void divide(){

double quotient = 1 / 2;

System.out.println("Quotient of one and two is " + quotient);

}

public static void remainder(){

double remainder = 1 % 2;

System.out.println("Remainder of one and two is " + remainder);

}

}

public static void main(String[] args) {

add();

subtract();

multiply();

divide();

remainder();

}

public static void add(){

int sum = 1 + 2;

System.out.println("Sum of one and two is " + sum);

}

public static void subtract(){

int diff = 1 - 2;

System.out.println("Difference of one and two is " + diff);

}

public static void multiply(){

int product = 1 * 2;

System.out.println("Product of one and two is " + product);

}

public static void divide(){

double quotient = 1 / 2;

System.out.println("Quotient of one and two is " + quotient);

}

public static void remainder(){

double remainder = 1 % 2;

System.out.println("Remainder of one and two is " + remainder);

}

}

posted 1 year ago

- 1

I don't know how much of a stickler the professor is about following instructions but that code that was provided doesn't exactly stick to what was asked for. The idea is a good start but the code could be a lot shorter. In fact, you only need one method instead of five. The method would take a couple of parameters:

In your main() method, you just call this multiple times:

In your main() method, you just call this multiple times:

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

Campbell Ritchie

Marshal

Posts: 56536

172

posted 1 year ago

Welcome to the Ranch

If you are feeling brave, go through the Java® Language Specification (=JLS) and the Java™ Tutorials about enumerated types, and see if you can understand the example in the JLS about arithmetic. It might be too difficult for beginners, but come back to it in three months and you might find it easier to understand.

If you are feeling brave, go through the Java® Language Specification (=JLS) and the Java™ Tutorials about enumerated types, and see if you can understand the example in the JLS about arithmetic. It might be too difficult for beginners, but come back to it in three months and you might find it easier to understand.

Anurag Singh

Greenhorn

Posts: 13

posted 1 year ago

I think your professor wants to test your understanding of "Arithmetic Operators" without you diving into modular programming. Below is the code he is probable looking for.

Campbell Ritchie

Marshal

Posts: 56536

172

posted 1 year ago

Please read this, which appears at the top of this forum:-

It does not help people learning if they are given a complete solution; they will learn better by working things out themselves. Please don't be annoyed, but I have pulled rank and removed your solution.We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.

Anurag Singh

Greenhorn

Posts: 13

posted 1 year ago

I will take care of that in future answers.

Regards

Anurag

Campbell Ritchie wrote:Please read this, which appears at the top of this forum:-It does not help people learning if they are given a complete solution; they will learn better by working things out themselves. Please don't be annoyed, but I have pulled rank and removed your solution.We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.

I will take care of that in future answers.

Regards

Anurag