Lesson 4

Math and Equations

Author: Brandon Fajardo (9/10/17). Updated (8/1/19)

Introduction

Review: In the last lesson, you learned about if statement conditions.

Lesson topic: This lesson will cover how to write mathematical equations.

Topic 1 - Equations in Java

Many of the things you will want to code may best be written using mathematical equations. For example, this equation converts inches to centimeters by multiplying the inputted number of inches by 2.54.

EX:

double inches = 1;
System.out.println(inches*2.54);

Consider how useful being able to write mathematical equations in Java is. You can even use Java to simplify some of the complex equations in your math homework.

Topic 2 - Math Refresher

Subtopic 1 - PEMDAS

When writing mathematical equations in code, it is critical that you write exactly what you desire to be mathematically executed. This will produce the results you intend to produce. Therefore, it is important to correctly follow the order of operations (PEMDAS/BODMAS). This often entails knowing where to place parentheses.

EX:

double x;
System.out.println((x+2)/9);

If this equation was written x+2/9 (without parentheses around the numerator), it would divide 2 by 9 but not x+2 by 9.

Things to note: As shown above, the parentheses of a print statement can be spaced out as far as you want, such as to new lines, so long as they enclose what is being outputted.

Practice Problem: Order of Operations

Problem #1:

The following equation (found online) is an example of how the order of operations can be confusing:

6/2(1+2) How do you simplify this?

*Solutions at the bottom of this document

Subtopic 2 - More Complex Expressions

More complex equations can be written in Java such as the quadratic equation and rational equations.

Problem #2:

Practice by writing the following equations in Java:

*Solutions at bottom of this document

Topic 3 - The Math Class

As the complexity of the math you use increases, simple operations become insufficient. Therefore, it becomes necessary to use the Java Math class.

The Math class is a set of mathematical shortcuts for more complex math notation.

Subtopic 1 - Functions

The following notation will access the Math class:

Math. //with a dot and a capital M

Then the desired shortcut is written afterward.

Some notable examples include:

The full list of all the Math class shortcuts are given and described in this link.

https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html

*This website is a great tool for many of the functions that Java has even beyond mathematics.

Practice Problem #3: Quadratic Formula

To practice the order of operations and using the Math class, try writing the positive version of the quadratic formula.

*Solutions at the bottom of this document

Topic 4 - Restrictions

The restrictions of mathematics apply to mathematics in Java.

The following restrictions may occur:

*NaN stands for Not-a-Number

As you can see, our current set of Java operations does not support imaginary numbers.

Conclusion

Recap: In this lesson, you should have become comfortable handling mathematical equations in Java.

Next: In the next lesson, you will use the body of the if statement and notations associated with it.

Solutions

Problem #1:

The math problem can be simplified as:

6/2(3) or 6/2*3

6*(1/2)*3

Equals: 9

Was it easy for you? Many people get the answer of 1 instead. Why might that be?

Problem #2:

Quadratic equation: a*x*x + b*x + c

Rational equation: (6*x - 8)/(x*x)

Problem #3:

(-b + Math.sqrt(Math.pow(b,2) - 4*a*c))/(2*a)

I chose to write it using the Math.pow expression

*Math.pow(b,2) can also be written: b*b

(-b + Math.sqrt(b*b - 4*a*c))/(2*a)

*The square root could also be written as a fractional power of 1/2 instead:

(-b + Math.pow(b*b - 4*a*c, 0.5))/(2*a)

Last updated