• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with question  RSS feed

 
Jay Haring
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maxwell Xu
Greenhorn
Posts: 18
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't get what you want to do. Do you want to code a calculator? Or do you just want to add two numbers? I'm sorry, but I don't get how you want us to fix the code.
 
Jay Haring
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maxwell Xu
Greenhorn
Posts: 18
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it! So this is what I would do:

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


}


]
 
Maxwell Xu
Greenhorn
Posts: 18
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whoops sorry
 
Maxwell Xu
Greenhorn
Posts: 18
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
  }




}
 
Maxwell Xu
Greenhorn
Posts: 18
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry it took so long :P
 
Jay Haring
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help!
 
Maxwell Xu
Greenhorn
Posts: 18
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your welcome! Good luck!
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anurag Singh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read this, which appears at the top of this forum:-
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.
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.
 
Anurag Singh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Please read this, which appears at the top of this forum:-
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.
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.


I will take care of that in future answers.
Regards
Anurag
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!