Computer Science
Gilmour AcademyLancerTech
  • Our Curriculum and Department
  • Intro to Programming
    • 1: Parts of a Computer
    • 2: Parts of Python
    • 3: DRY Turtle
    • 4: Turtle Design App
    • Wordle with Turtles
    • 5: Interactive Turtles
    • OLD 5: Replit, GitHub, and repositories (Oh my!)
    • 6: Raspberry Pi / GoPiGo
    • 7: Kivy
  • Intro to Web Design
    • 1: Internet?
    • 2: Websites?
    • 3: Bootstrap Template
    • 4: Graphics and Branding
    • 5: Collaboration
    • 6: Advanced Editing
    • Publish Static HTML
  • AP Computer Science
    • 1: Logic & Instances
    • 2: How Java Works
    • 3: Data Types & Flow
    • 4: Strings
    • 5: Objects & References
    • 6: Inheritance & Algorithms
    • 7: Data Structures
    • 8: Sorting
    • 9: Review
    • Data Science
  • Web App Dev
    • 1: Core Concepts
    • 2: MVT Pattern
    • 3: Hello Flask
    • 4: Install Flaskinni
    • 5: Tour Flaskinni
    • 6: Visualize Your App
    • 7: Theme & Blueprint
    • 8: Standup Your DB
    • 9: Advanced Topics
    • 10: Deployment
  • 2D Game Design
    • Class Overview
    • Gamemaker Studio 2 and Github Setup
    • Game 1: Bouncing Ball
    • Turning in your games
    • Game 2: Maze
    • Game 3: Ping Pong
    • Game 4: Breakout
    • Game 5: Tank Battle
    • Game 6 Highlights
    • DO NOT DO:
    • Game 7: Final Project
    • Publish to Opera
    • FAQ
  • 3D Game Design
    • 1: Class Overview
    • 2: Installation
    • 3: Exploring the Unity UI
    • Game 1: Rolling Ball
    • Game 2: Tanks
    • Game 3: Third Person Platformer
    • Game 4: Final project
    • FAQs
    • OLD: Distance Learning Setup
    • OLD: GIT
  • 3D Modeling & Fabrication
    • Installation
    • Fusion 360 Interface and Sketch Modeling
    • Primitive Modeling
    • Patterns
    • Appearances and Rendering
    • Building Community Gallery Page 2023
    • Parametric Modeling
    • 3D Printing Concerns
    • Assemblies and Mechanical Design
    • Laser Cutting
    • Sculpt Tools
    • Milling Concerns
  • Robotics 7
    • Software Installation
    • Python basics (trinket.io)
    • Python Turtle
    • Programming for the Ev3
    • Setting up for clarity
  • Robotics 8
    • Replit
    • Python review
    • Kivy Basics
    • Calculator
  • Competitive Robotics
    • Hardware Team
      • CAD Examples
      • Elevators
    • Software Team
      • Command Pattern
      • Example Command
      • Subsystem
      • Running Your Code
      • Under the Hood
      • RoadRunner
      • Vision Processing
  • Archives
    • Adiletta Archives
      • Old Web
        • Ex: WordPress CMS
      • ItP
        • OLD: Parts of Python (old -- Mr. A)
        • OLD: 5: Raspberry Pi
        • OLD: 6: Deploying Code
        • OLD 7: Nav Algorithm
    • Vanek Archives
      • OLD Robotics 8
        • OLD: End of Class Project
      • OLD Competitive Robotics
        • Untitled
        • Webots Videos
      • OLD Robotics 7
        • Trinket Introduction
        • Lists: x/y position
        • Functions: Math program
        • Lists: Grocery List
        • Study Guide Program
        • Tic Tac Toe Game
        • Dice Roller Program
        • Visualization
        • Dice Roller + Visualization
        • OpenSCAD: Installation
        • OpenSCAD: Command Sheet and Intro
        • OpenSCAD: Difference
        • OpenSCAD: Variables
        • OpenSCAD: Union
        • OpenSCAD: For Loops
        • OpenSCAD: Final Project
      • OLD Art I - Blender Sculpting
        • Class Overview
        • Installation
        • Lesson 1 - Tools
        • Lesson 2 - Detail
        • Lesson 3 - Base Mesh: Metaballs
        • Lesson 4: Converting metaballs and adding detail
        • Lesson 5: Masking, Hiding, and Working with Multiple Objects
        • Lesson 6: Joining Objects & Basing
        • Lesson 7: Sculpture Painting
        • Student Gallery: Animal Sculpts
        • Lesson 8: 3D Compositon
        • Lesson 9: The Project - Putting it all together
        • Lesson 10: Developing the image further
        • Lesson 11: Layout the base metaball mesh.
        • Lesson 12: Final Detail
        • Lesson 13: Basing and Painting
        • Final Project Gallery
      • OLD Fab
        • OLD Building Community Project Gallery
        • Copy of Building Community Project Gallery
        • old Building Community Project Gallery
      • OLD: Turtle Design App
      • OLD Arduino Robotics 8
        • Arduino Basic Commands Cheat Sheet
        • Logging into Tinkercad
        • Arduino, Circuits, LEDs and Resistors
        • Functions and Variables
        • Serial Monitor
        • Buttons and Interrupts
        • Traffic Light Project
        • Potentiometers + Servos
        • Piezo Buzzer and Tone();
        • Sequencer Project
        • Arrays and for loops
        • Extra Loop Practice
        • Refining the Sequencer
        • Servos
        • Ultrasonic Sensors
        • Final Project
Powered by GitBook
On this page
  • Learning Targets
  • Declaring Variables
  • Naming Conventions
  • Operators
  • Arithmetic
  • Comparison
  • Conditions
  • Loops
  • for loops
  • while loops
  • Arrays
  • Array Tricks
  • Method Calls

Was this helpful?

Export as PDF
  1. AP Computer Science

3: Data Types & Flow

We will learn to solve simple problems with Java programming. We will create all sorts of variables, test their limitations and exercise basic control flow.

Previous2: How Java WorksNext4: Strings

Last updated 7 months ago

Was this helpful?

Learning Targets

  • I can declare variables of common Java data types and assign values.

  • I can control the flow of my app with loops, conditionals and method calls.

  • I can write efficient conditionals and return comparisons in my methods.

  • I can describe the difference between logic, run time and compile time errors.

Declaring Variables

Primitives are objects that live entirely in stack memory (they're lowercase like int and not like String) . We'll go over the difference between stack and heap memory later. For now, just think of stack memory as the lightweight stuff that's really fast-access. Take a look at the .

Naming Conventions

CompoundCapitalize name of classes

camelCase variables and methods

ALLCAPS variables marked as final, meaning they can't change

snake_case is typically used only in Python and I miss it every time I work in Java or C#

Operators

Arithmetic

Operator

Description

Example (a=10, b=20)

+ (Addition)

Adds values on either side of the operator.

a + b will give 30

- (Subtraction)

Subtracts right-hand operand from left-hand operand.

a - b will give -10

* (Multiplication)

Multiplies values on either side of the operator.

a * b will give 200

/ (Division)

Divides left-hand operand by right-hand operand.

b / a will give 2

% (Modulus)

Divides left-hand operand by right-hand operand and returns the remainder.

b % a will give 0

++ (Increment)

Increases the value of operand by 1.

b++ gives 21

-- (Decrement)

Decreases the value of operand by 1.

b-- gives 19

Integer Math

What's 9 / 4? In Java, it's just2, but if you wrote it like 9.0 / 2, then the answer would be 2.5. If you don't introduce a decimal to the operation, if you only give Java literal ints , it'll match that format and ignore decimals.

Comparison

We evaluate / test data and produce a true or false value.

Operator

Description

Example (a=10, b=15)

Result

==

Equality operator

a==b

false

!=

Not Equal to operator

a!=b

true

>

Greater than

a>b

false

<

Less than

a<b

true

>=

Greater than or equal to

a>=b

false

<=

Less than or equal to

a<=b

true

Short-Circuit Evaluation

This comes up all the time in problems. It's a useful trick to avoid crashing your apps, too.

Conditions

A fundamental element of flow control is whether or not you want to execute a block of code. We use a conditional or if statement to test something. That test will result in a true or false value. If it's true, we'll execute the block of code. If it's false, we'll skip to the end of the block of code.

Loops

Loops are how we repeat commands or loop through items in a collection.

for loops

standard for loop

Most for loops look something like this:

for(int x = 0; x < 20; x++) {
    // code goes in here
}

The for statement has a declaration, condition and increment. But you can skip the declaration if you'd like. Let's say you want the counter to have a wider scope (so it can be accessed outside of the loop) like this:

int x = 0;  // declared outside the loop so it can remain afterwards
for(; x < 20; x++){
    // code goes here for the loop
}
System.out.println(x);  // will print 20

foreach loop

Let's say I have an array or collection of numbers like this: int[] numberArray = [5, 10, 15, 20, 25];

Each number in that group has an address or index (that starts at 0). If I wanted to print the number 10 for example, I could write System.out.println(numberArray[1]);

So if I wanted to loop through (called iterating or traversing) all the items in this array, I could use a normal for loop like this:

for (int x = 0; x < numberArray.length; x++){
    System.out.println(numberArray[x]);
}

But there's an easier form of the for loop that's meant to easily traverse through a collection. We'll go over some of the pros and cons of this method later.

for (int x : numberArray){
    // x isn't looping through the addresses this time
    // rather, it's pretending to be each number in the collection
    System.out.println(x); 
}

while loops

Infinite loop

while(true){
    // will never stop looping unless I use the break command
}

do-while loop

do {
    // code will run at least one
} while(x > 10);

Arrays

You can declare and initialize an int like int x = 5; Similarly, you can create an array that contains three ints like this: int[] x = {5, 10, 15};

Arrays are immutable. They don't change in size. Java uses a fancier object called an ArrayList that is more flexible. So if one of your friends is trying to convince you to increase the size of an array, just say no.

Array Tricks

// Let's demonstrate some array skills!

// Here's a blank array that's got spots for five ints
int[] arr = new int[5]; 

// You can also create an array with initial values
int[] x = {5, 10, 15}; 

How long is that array?

x.length; // note: this isn't a method like it is in the String class.

What's the first element in the array?

x[0]; // this will print "5" in my example

What's the last element in the array?

x[x.length-1]; // this will print "15" in my example

What other skills do you need to know?

  • Loop through every element in the array

  • Check if an element is found in an array

  • Make an array of other types of objects

Method Calls

This is a part of control flow. The thread, the computer's train of thought, is being sent over to work through a set of commands called a method or function (either name works in this class).

The do-while is rarely used but it's handy if you want to make sure the steps run the first time before the loop condition is tested.

Read more.
types of Java primitives
This is a silly joke, not an actual way to resolve problems with your loops.
The main function is calling max(), passing it a few numbers and saving the result
The variable declared in the header is scoped to that function