How to Alternate Addition and Subtraction in Java
The Java programming language allows you to create complex programs that are useful to many professional and academic disciplines. For example, you could write a Java application that was dedicated to solving math problems. One program might need to solve a certain iterative equation that alternates between addition and subtraction with each step. Using Java, you can write a short program that creates a function that alternates between addition and subtraction each time it is invoked. You could then apply this program to other projects and solve iterative equations.
Things You'll Need
- Java Software Development Kit with NetBeans Integrated Development Environment (IDE) Bundle (see Resource for link)
Instructions
-
-
1
Load the NetBeans IDE by clicking on its program icon. When the program loads, navigate to "New/New Project" and select "Java Application" from the list on the right-hand side of the screen. A new source code file appears in the NetBeans text editor. The source code file contains an empty class declaration.
-
2
Locate the class declaration. It should look something like this:
public class className
{
-
-
3
Place a static variable inside the class declaration, right after the curly brace. Write the following:
public static Boolean current = true;
-
4
Create the code for the function "addOrSub," which either adds or subtracts two numbers. Every time it runs, it alternates between adding and subtracting. Write the following function signature:
private static int addOrSub(int x, int y)
{
}
-
5
Declare an integer data type that will hold the result of the addition or subtraction. Write the following in between the curly brackets of the "addOrSub" function:
int z;
-
6
Create an "if" statement that tests the value of "current." If the value is true, then the variables "x" and "y" are added and stored in "z."
if(current == true)
{ z = x + y; }
-
7
Create an else statement. This runs whenever "current" is false. It subtracts y from x and stores the result in x.
else
{ z = x - y;}
-
8
Alternate the value of "current" by reversing its value using the "!" operator:
current = !current
-
9
Return the value of z:
return z;
-
10
Find the main method. It will have been automatically generated along with the class. Look for the text:
public static void main(String[] args)
{}
-
11
Place the following three lines of code within the curly brackets that precede the main method. These each call "addOrSub" and store the results in separate variables.
int x = addOrSub(1,2);
int y = addOrSub(3,4);
int z = addOrSub(5,6);
-
12
Write statements that print out the values of the three variables "x," "y" and "z" using the following statements:
System.out.println(x);
System.out.println(y);
System.out.println(z);
-
13
Execute the program. The output is 3, -1, 11. This is because it adds the first two numbers, 1 and 2, and then subtracts the next two numbers (3 and 4). Finally, it adds 5 and 6 to get 11.
-
1
Resources
- Photo Credit Jupiterimages/liquidlibrary/Getty Images