TRENDING NEWS

POPULAR NEWS

Can Some One Explain This Basic Java Concept Of Arithmetic Operations

Java program evaluating an arithmetic expression?

Here is a suggestion.
Let N represent integer.
Let S represent subtraction
...A....addition
....M....multiplication
.....D....division
....R.....right paranthesis.
....L........left pranthesis

Define state variables:

int L = 0; //you saw left parenthesis

int NA= 1; //reading left to right, you saw integer and addition in that order

int LN = 2; //reading left to right, you saw left parenthesis, integer in that order

int LNMN = 3; //reading left to right, you saw left parenthesis, integer, multilplication, integer

//define other relevant state variables

put these states into a stack. For example, assume you have
1*(3)

and you read from left until you read 3. Your stack should contain from top to bottom

LN
L
NM
N

In this case, you have LN at the top of the stack and then you read a right paranthesis. Instead of switching to LNR state, recognize that a left parenthesis, integer, and right parenthesis will reduce to an integer if arithmetic operations are performed. Thus pop the last two states off the stack to get

NM
N

Now since the LNR reduced to an N, attempt to push NMN into the stack but stop when you realize that NMN also reduces to an integer. Once again pop two states off your stack. This time the stack is empty and you can conclude that the arithmetic input was legitimate. If you ever encounter a multiplication when you are in state NS (integer followed by subtraction sign), you know that the arithmetic input is bad. Identify all such illegal inputs associated with each state.

Hope this explanation atleast gets you started.

Java operations confusing?

1. C = B
The double ampersand is true iff both (A>B) and (B
2. Z = Y
The double pipe symbol means "or". So it is true if one or both of the expressions is true. (X>Y) and (Y

Java applications? Basic help.?

In order to create an application that does the tasks that you mentioned in your question, you should first create a class and then think about what you will need in order to get users' input, store the values, as well as perform and print out the results of the operations that are required.

Since your application requires the user to enter five integer values, you can use some "int" variables or an "int" array to store the values, an object of the "Scanner" class to get the input, as well as a "for loop" to get the values one after another. (Alternatively, you could use multiple Scanner class objects, which would also work, but makes for more code, which could be less efficient.)

Once you've read in all of the values, you can simply use the variables or array indices to perform the required mathematical operations and then you can print the results to the screen! (You could also create a few variables for this, in order to make it easier to keep track of all of the values and results.)

You may want to try writing some pseudo-code or even creating a diagram, etc. to help keep your thoughts and ideas organized, which may help in writing the code for this application.

As for an example of how to write a Java application, here's a link to some code that I wrote and posted on PasteBin.com, which shows how to get user input, use variables to store the user-entered integers, use a "for loop" and add the values together, as well as how to print the values and the total to the screen.

http://pastebin.com/WXXeWPdQ

Best of luck and I hope I helped you!

Computer Arithmetic question?

IT CANNOT DIFFER AS IT USES ASSOCIATIVE PROPERTY OF ADDITION

a+(b+c)=(a+b)+c

Java Programming: Basic?

You've made a good start. It would be a good idea to make sure you know where the dash character is in the input name. Because when you need to insert the initial and an extra dash, what you're going to have to do is move all the existing letters in the array over by 2 positions from the dash onwards. You'll also need to know how many characters you have in the whole name because you're going to have to start at the end in moving the characters.

I would recommend you also do some validation in your readname method to ensure that the user has in fact input the name in the format you've asked for, i.e. with a dash in it. Also you need to check that the name isn't too big for your array (allowing 2 extra characters for the addition of the initial and extra dash).

Providing you know the position of the dash in the array, then what you will need is a for loop in the insert method, which starts at the position of the last character and moves backwards character by character until it reaches the dash. For each of those characters you need to populate the position 2 characters away in the array with the value in this cell. Then you can insert the extra two characters once you've done that.

Java program that implements a final method and performs an arithmetic operation... Help, please?

I've been stuck on this for hours. I need to implement a final method that performs an arithmetic operation.. I've tried it out, and it says "Missing return statement". I honestly am stuck, and don't know what to do. Any help would be great, thank you! My code that I've tried is down below.

public class finalmethod
{
final int add(int x, int y)

{
int z = x y;
System.out.println("Answer: " z);
return z;
}
public int sampleMethod()
{
finalmethod obj = new finalmethod();
obj.add(5, 4);
}
}

Java program that implements a final method and performs an arithmetic operation... Help, please?

I've been stuck on this for hours. I need to implement a final method that performs an arithmetic operation.. I've tried it out, and it says "Missing return statement". I honestly am stuck, and don't know what to do. Any help would be great, thank you! My code that I've tried is down below.

public class finalmethod
{
final int add(int x, int y)

{
int z = x + y;
System.out.println("Answer: " + z);
return z;
}
public int sampleMethod()
{
finalmethod obj = new finalmethod();
obj.add(5, 4);
}
}

TRENDING NEWS