In this IB Computer Science lesson you will be learning about:
A mathematical operator is an instruction that tells you what to do with two or more numbers. You have been using operators in math since you started school.
They are the addition, subtraction, multiplication, and division.
They are each given a symbol in Java
Calculations are performed Left to Right and Java will follow the rules of BEDMAS. If you wish to use brackets, you must use ( ) not [ ] or { }
In order to do mathematical calculation you perform a calculation and then assign the result of that calculation to a variable. Then use that variable however you wish afterwards.
Example
Any mathematical formula can be represented in code
Example – The surface area of a rectangular prism 𝐴=2(𝑤𝑙+𝑤ℎ+𝑙ℎ)
The result of a math operation can only be placed in a variable that can contain the result.
Remember that variables take up different amounts of memory based on their type. Integers take up less space than doubles, so it is possible to put an integer math result into a double variable (a bigger box) but trying to do the opposite will cause Java to generate an error
Dividing values can also give some interesting results in Java.
Sometimes you might want to take two integers and divide them and actually get the decimal answer. To solve the integer division problem, we can use something called type casting. We can force an integer to behave like a double or a double to behave like an integer
Java has built in functions for other types of math operations as well
Power and Square Root Functions
Trigonometry Functions: (Note: Java uses Radian Measure, which you may or may not have done in Math class
Random Numbers
Integer Rounding
You have probably noticed that when you perform a calculation and output the result to the screen, you get about 8 or 9 decimal places some times. To fix this problem, you need to create a new decimal format using the DecimalFormat Class. In order to use this DecimalFormat Class you must import java.text at the start of your program
You must create a decimal format object, it can be called whatever you wish, I choose dp3, dp2, dp1 in my example above
The string inside the brackets indicates how many decimal places to display
This decimal format can then be applied to any variable to any integer or real valued variable using the format method. This doesn’t actually change what is stored in memory, it just displays a chosen number of decimal places.
When writing computer programs that solve problems is often helpful to have a game plan for how your program will be structured before you actually start writing code. This doesn’t necessarily have to be a complete plan of everything that will be in your program, but it should give a pretty good idea of the logic that is being used to solve the problem.
In this course we will use the Input – Processing – Output Model of problem solving. We can represent this process through either pseudocode or flowcharts.
For now we will stick to writing pseudocode when solving problems. Flowcharting will be introduced later with more complex problems.
Example: Paint Store App (Text Based Input / Output)
Let’s say you want to write an app that helps a user determine how many cans of paint they will need to paint the walls and what the total cost will be. You know that each can of paint will cover 4 m2 of wall space and costs $17.50. We can assume that you are living in Ontario so the HST on the sale is 13%
Pseudo Code
Input:
• Length of the wall
• Height of the wall
• # of Coats
Processing:
• Calculate the area needed to be painted (area = length x height x coats)
• Calculate the number of buckets (buckets = area / 4) – round up
• Calculate the before tax cost (subtotal = buckets * 17.50)
• Calculate the tax (tax = subtotal x 0.13)
• Calculate the total cost (total = subtotal + tax)
Output:
• Display the number of buckets required and cost per bucket
• Display the subtotal, tax, and total (2 decimal spots)
Here would be the complete program code for the paint store app
Another Example
We want to write a program to compute the 1-3-Sum of an ISBN number. For Simplicity lets assume the first 10 digits are always going to be 9780921418 so your program will only need to get the last 3 digits.
Pseudocode
Input
• Digit #11
• Digit #12
• Digit #13
Processing
• Since the first 10 digits are always the same compute the 1-3-Sum of those values
9*1+7*3+8*1+0*3+9*1+2*3+1*1+4*3+1*1+8*3 = 91
• Compute the 1-3-Sum of the remaining digits
Remaining = digit11 * 1 + digit12*3 + digit13*1
• Add the those two to get the 1-3-Sum
OneThreeSum = 91 + Remaining
Output
• Display the whole ISBN number
• Display OneThreeSum to the screen
Write Programs to accomplish the following
1. The diameter of a circle and prints the Area and Circumference to the screen.
2. The temperature in degrees Celsius and displays the temperature in Fahrenheit. (YOU NEED TO REARRANGE THE FORMULA!!!)
3. The 3 integer coefficients of a quadratic in standard form and then uses the quadratic formula to output the two roots. Format the results to 2 decimal places. (BE AWARE WHAT HAPPENS IN JAVA WHEN YOU GET INVALID ROOTS)
4. Asks the user to enter the two sides of a right angle triangle (not the hypotenuse) and then outputs the hypotenuse and the two interior angles of the triangle. Angles can be displayed to the nearest degree and the hypotenuse to one decimal place.
5. 3 sides (integers only) of any triangle and uses the cosine law to find all 3 interior angles and then uses Heron’s formula to calculate the area. Angles can be displayed to the nearest degree.
YOU SHOULD VERIFY ALL YOUR PROGRAMS OUTPUT BY HAND WITH YOUR CALCULATOR
Check out our programing in python courses that focus on high school level coding.
These courses are complete with interactive coding lessons, teacher led videos, and more practice questions with complete solutions
Return To International Baccalaureate Computer Science Main Page