I need Help! I am taking my very first class in Java

+1 Nova Dadow · January 24, 2015
I have to write a Java program that calculates and displays the result of addition, subtraction, multiplication, division, modulus, bitwise & or bitwise | operation on two integer values input by the user. The program should prompt the user for the two integers and the operation to be performed. Here is a sample run:

Enter the first integer: 24
Enter the second integer: 4
Enter the operation: +
The result is 24 + 4 is 28

I am having trouble trying to figure out how to link the operation with input from the integers. Please help if you can/
Thanks Alisha

Post a Reply

Replies

Oldest  Newest  Rating
+1 Jag Singh · January 24, 2015
you can create the functions such as add and subtract then call them as per the operation required. e.g.:

char Operator;
if (Operator == '+'){
answer = obj.add(num1, num2);
System.out.println(answer);
}
else if (Operator == '-'){
answer = obj.subtract(num1, num2);
System.out.println(answer);
}
else {
System.out.println("invalid operator. Operation failed.");
}
0 Jag Singh · January 24, 2015

if (Operator == '+'){
answer = obj.add(num1, num2);
System.out.println(answer);
}
else if (Operator == '-'){
answer = obj.subtract(num1, num2);
System.out.println(answer);
}
else {
System.out.println("invalid operator. Operation failed.");
}

create functions such as add or subtract which return int or double or long. then you can use the above logic to call them.
+4 Bernard Pyc0d333 Parah · January 24, 2015
I don't think this is up to her level considering the fact that this is her first java class, I think solving the problem in a procedural manner will give her better understanding of the basics.
here is a simple example
char operator;
int num1;
int num2;
int result;

Scanner in = new Scanner(System.in);
System.out.println("enter first integer");

num1=in.nextInt();

System.out.println("enter first integer");

num2=in.nextInt();

System.out.println("select operation");

operation = in.next();
if (operation=='+')
{
 
result=num1+num2;
System.out.print("the result is "+ result);

}

if(operation=='-')
{
result==num1-num2
System.out.print("the result is "+ result); // though you can use this just once outside all blocks.
}


you can just follow this logic to do all others,  not forgetting the fact that switchcases are more efficient in this situations..

BTW you could also practice Jag's answer, it's gonna help you alot when you begin to learn methods.
+1 Nova Dadow · January 24, 2015
First off I want to say Thank you both soo much!!

This is what I have so far what do you guys think? I am using Eclipse and it will not allow me to run it. But I am not sure what I am doing wrong. Also did I use the bitwise & an bitwise | correctly? I wasnt really sure how to use them.


//Import each required Java Class
import java.io.IOException;
import java.util.Scanner;
import java.io.InputStreamReader;
import java.io.BufferedReader;


public class HomeWork1Part1 {
    public static void main(String [] args) {
        
//Variables to hold values
        int integerOne;
        int integerTwo;
        int result;
        char operation;
    
        
                
        System.out.println("Welcome to the simple Java calculator");
        
//Use the Scanner class to read System.in
        Scanner scannerIn = new Scanner(System.in);
        
//Prompt user to enter integer one
        System.out.println("Enter the first integer: ");
//the nextInt() method scans the int value
        integerOne = scannerIn.nextInt();
        System.out.println("The first integer you entered is: " + integerOne);

//Prompt user to enter integer two
        System.out.println("Enter the second integer: ");
//The nextInt() method scans int value
        integerTwo = scannerIn.nextInt();
        System.out.println("The second integer you entered is: " + integerTwo);        

//Operations
        System.out.println("Enter the operation: ");
       operation = scannerIn.nextChar();
    
    if (operation == '+'){
        result = integerOne + integerTwo;
        System.out.println("Result is: " + result);
    }
    else if (operation == '-'){
        result = integerOne - integerTwo;
        System.out.println("Result is: " + result);
        }
    else if (operation == '/'){
        result = integerOne / integerTwo;
        System.out.println("Result is: " + result);
        }
    else if (operation == '*'){
        result = integerOne * integerTwo;
        System.out.println("Result is: " + result);
    }
    else if (operation == '%'){
        result = integerOne % integerTwo;
        System.out.println("Result is: " + result);

    else if (operation == '&'){
        result = integerOne AND integerTwo;
        System.out.println("Result is: " + result)
    }
    else if (operation == '|'){
        result = integerOne OR integerTwo;
        System.out.println("Result is: " + result);
    }
}
   
+1 Jag Singh · January 24, 2015
Some of the wrong things that I came across in your code:

operation = scannerIn.nextChar(); // Java doesn't have a nextChar

Instead you can use:
operation = scannerIn.next().charAt(0);

The logical operators you used OR AND are incorrect. correct ones are & |. Please refer to the below link for the list.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
+1 Jasmin Bakalović · January 24, 2015
This is okay, but there's one catch why its not working:

Somehow Java doesn't have .nextChar() so instead of Char type use String, which have .next() for reading text in one line without spacing.

Here's your new code with some modifications: http://pastebin.com/9qwUBwTY

Again, in Java you can't use "==" signs just nameofyourvaraible.equals("something");

By Bernard and Jag it will look like this: http://pastebin.com/Hq5fztCp
0 Nova Dadow · January 25, 2015
Jasmin Bakalovic, Thanks for the idea of using the case statements. I changed my code to those, it seemed to be a little simpler. You both were an excellent help. I am taking an online class in America while I live in Japan so it is hard to connect with my teacher.
Thanks again!
Alisha (Nova) Hager
  • 1

Java / Android Development

107,215 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator