IB Computer Science Unit 1
IB Computer Science Unit 2
IB Computer Science Unit 3
IB Computer Science Unit 4
IB Computer Science Unit 5
IB Computer Science Unit 6

IB Computer Science Doing Math with Java

IB Computer Science Learning Goals

In this IB Computer Science lesson you will be learning about:

  • Manipulating data using mathematical operators and functions
  • Outline program structure using pseudocode
  • Solve common mathematical problems with computer software.

Basic Operators

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

  • Addition +
  • Subtraction –
  • Multiplication *
  • Division /

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

IB-Computer-Science-Java-Math (1)

Any mathematical formula can be represented in code

Example – The surface area of a rectangular prism 𝐴=2(𝑤𝑙+𝑤ℎ+𝑙ℎ)

IB-Computer-Science-Java-Math (3)

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

IB-Computer-Science-Java-Math (5)

Dividing values can also give some interesting results in Java.

  • Dividing two integers will give an integer as the result. It will cut off all the decimals even if you are storing the result in a double variable.
  • Dividing two double values will give a double as the result. As expected.
  • Dividing an integer by a double or vice versa will give a double as the result. As expected.
IB-Computer-Science-Java-Math (7)
IB-Computer-Science-Java-Math (8)

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

IB-Computer-Science-Java-Math (9)
IB-Computer-Science-Java-Math (10)

Built in Math Functions

Java has built in functions for other types of math operations as well

Power and Square Root Functions

IB-Computer-Science-Java-Math (13)
IB-Computer-Science-Java-Math (14)

Trigonometry Functions: (Note: Java uses Radian Measure, which you may or may not have done in Math class

IB-Computer-Science-Java-Math (11)
IB-Computer-Science-Java-Math (12)

Random Numbers

IB-Computer-Science-Java-Math (17)

Integer Rounding

IB-Computer-Science-Java-Math (16)
IB-Computer-Science-Java-Math (15)

Formatting Decimal Numbers for Output

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

IB-Computer-Science-Java-Math (18)

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

  • 0.000 – 3 Decimal Places
  • 0.00 – 2 Decimal Places
  • 0.0 – 1 Decimal Place

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.

Thinking Process for More Complicated Problems

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.

  • Pseudocode is a description of your program using everyday English and Math syntax
    • There is no formal industry standard on how to write it
    • It is easy to create quickly and change as you think
    • It is not visually easy to follow
  • Flowcharts are a visual description of how the program will execute
    •  There are sets of industry standard symbols which have specific meaning
    • It is visually easy to follow
    • It is not quick to create and changes can be difficult to insert

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

IB-Computer-Science-Java-Math (19)
IB-Computer-Science-Java-Math (20)

Another Example

IB-Computer-Science-Java-Math (21)

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

IB-Computer-Science-Java-Math (22)
IB-Computer-Science-Java-Math (23)

IB Computer Science Practice Questions

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

Looking to Learn More about Computer Science and Coding?

Check out our programing in python courses that focus on high school level coding.  

  • Grade 11 Computer Science ICS3U
  • Grade 12 Computer Science ICS4U

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