Back

Activity 1.2 Arithmetic

divider

Introduction

Activity 1.2

Arithmetic

Topics

  • Arithmetic in programming
  • Numeric Types: integer and double
  • Math operators
  • Operator precedence

Arithmetic in programming

Any useful program, from video games to online stores, relies on numbers.

Whether it's calculating scores, processing transactions, or generating graphics, math is everywhere in software.

Numeric Types: Integer and Double

In Java, numbers are categorized as either integers (for whole numbers) or doubles (for decimal numbers).

2.5  401  -65  -123.45  1_000_000  "35"

Demo

Use the System.out.println() method to display some numbers on the screen.

DemoProgram.java
public class DemoProgram {
public static void main(String[] args) {
System.out.println(303);
System.out.println(-25.2);
System.out.println(1_000_000);
}
}

Math Operators

+ Addition
- Subtraction
* Multiplication
/ Division: Divides two numbers and returns an integer when both numbers are integers, and a double when at least one of the numbers is a double.
% Modulus (Mod): Divides two numbers and returns the remainder.

Division Operations with Integers and Doubles

public class DemoProgram {
public static void main(String[] args) {
System.out.println(100 / 2);
System.out.println(14 / 3);
System.out.println(20 / 6.0);
System.out.println(10.0 / 2.5);
System.out.println(10 % 4);
}
}

Demo: Divide Integers and Doubles

public class DemoProgram {
public static void main(String[] args) {
System.out.println(100 / 2);
System.out.println(14 / 3);
System.out.println(20 / 6.0);
System.out.println(10.0 / 2.5);
System.out.println(10 % 4);
}
}
Terminal window
50
4
3.3333333333333335
4.0
2

Operator Precedence

Java follows typical order of operations rules, include parenthesis.

3 * 4 + 5

3 * (4 + 5)

Key Terms

Data Type
A classification for different kinds of information. It tells you what kind of data is being stored or used.
Integer
A data type that stores whole numbers, like 5, 25, or -100.
Double
A data type that stores numbers with decimal points, like 3.14 or -99.99.

'F' → Fullscreen

Objectives

  • icon Evaluating math expressions
  • icon Displaying numeric output
divider

Activity Tasks

  • icon Create a new Java project named 1-2-Arithmetic.
  • icon Complete each task individually.

Task 1: Addition, Subtraction, and Multiplication

  • icon Display the result of the math expressions using System.out.println().
Program.java
public class Program {
public static void main(String[] args) {
// Addition
System.out.println("I'm adding 2 and 1. The result is:");
System.out.println(2 + 1);
System.out.println();
System.out.println("Adding $1000 and $500.96 gives us:");
System.out.println(1000 + 500.96);
System.out.println();
// Subtraction
System.out.println("I have 3 apples, and I eat one. Now, I have:");
System.out.println(3 - 1);
System.out.println();
// Multiplication
System.out.println("I'm 30 years old. In months, that's: ");
System.out.println(30 * 12);
System.out.println();
//
}
}

Task 2: Division

  • icon Practice the various division operations. Take note of the result when using integers and/or doubles.
Program.java
public class Program {
public static void main(String[] args) {
// Addition
System.out.println("I'm adding 2 and 1. The result is:");
System.out.println(2 + 1);
System.out.println();
System.out.println("Adding $1000 and $500.96 gives us:");
System.out.println(1000 + 500.96);
System.out.println();
// Subtraction
System.out.println("I have 3 apples, and I eat one. Now, I have:");
System.out.println(3 - 1);
System.out.println();
// Multiplication
System.out.println("I'm 30 years old. In months, that's: ");
System.out.println(30 * 12);
System.out.println();
// Division with integers and doubles
System.out.println("If we divide 8 pizza slices between 2 people, each person gets:");
System.out.println(8 / 2);
System.out.println();
System.out.println("Dividing $100 among 6 people gives each person:");
System.out.println(100.0 / 6);
System.out.println("Without fractional amounts, each person gets:");
System.out.println(100 / 6);
System.out.println();
// Remainder using the mod operator
System.out.println("After dividing $100 among 6 people, the leftover cents are:");
System.out.println(100 % 6);
System.out.println();
//
}
}

Task 3: Operator precedence

  • icon Practice using parenthesis to control the order of operations.
Program.java
public class Program {
public static void main(String[] args) {
// Addition
System.out.println("I'm adding 2 and 1. The result is:");
System.out.println(2 + 1);
System.out.println();
System.out.println("Adding $1000 and $500.96 gives us:");
System.out.println(1000 + 500.96);
System.out.println();
// Subtraction
System.out.println("I have 3 apples, and I eat one. Now, I have:");
System.out.println(3 - 1);
System.out.println();
// Multiplication
System.out.println("I'm 30 years old. In months, that's: ");
System.out.println(30 * 12);
System.out.println();
// Division - Quotient and remainder
// Quotient
System.out.println("If we divide 8 pizza slices between 2 people, each person gets:");
System.out.println(8 / 2);
System.out.println();
System.out.println("Dividing $100 among 6 people gives each person:");
System.out.println(100.0 / 6);
System.out.println("Without fractional amounts, each person gets:");
System.out.println(100 / 6);
System.out.println();
// Remainder using the modulus, or mod, operator
System.out.println("After dividing $100 among 6 people, the leftover cents are:");
System.out.println(100 % 6);
System.out.println();
// Order of operations
System.out.println("The expression 4 * 10 + 12 equals:");
System.out.println(4 * 10 + 12);
System.out.println("The expression 4 * (10 + 12) equals:");
System.out.println(4 * (10 + 12));
//
}
}
divider

Sample Output

Your program output should something similar to the sample output below.

Sample Output
I'm adding 2 and 1. The result is:
3
Adding $1000 and $500.96 gives us:
1500.96
I have 3 apples, and I eat one. Now, I have:
2
I'm 30 years old. In months, that's:
360
If we divide 8 pizza slices between 2 people, each person gets:
4
Dividing $100 among 6 people gives each person:
16.666666666666668
Without fractional amounts, each person gets:
16
After dividing $100 among 6 people, the leftover cents are:
4
The expression 4 * 10 + 12 equals:
52
The expression 4 * (10 + 12) equals:
88
divider

Reflection Questions

You may write your reflection answers as comments at the bottom of your code.

  1. What's the difference between the / operator and the % operator, and when would you use each one? Use an example from the lesson to support your answer.
  2. Explain why it's important to differentiate between whole numbers (integers) and decimal numbers (doubles) when writing code. How did using a decimal number change the output of a division problem in this activity?
  3. The lesson shows that 4 * 10 + 12 and 4 * (10 + 12) produce different results. Why is it important to use parentheses in an expression, and what do they do?
divider

Submission

Submit your activity and reflection answers to the appropriate dropbox.

Activity Complete