Using Loops, Arrays, Methods to do stuff in Java

Methods

Methods

Think of methods as machines that take some input and produce some output

Methods are defined by their input and their output

Methods

Think of methods as machines that take some input and produce some output

You can also think of them as mathematical functions

Methods are defined by their input and their output

Methods

Think of methods as machines that take some input and produce some output

You can also think of them as mathematical functions

Methods are defined by their input and their output

Methods in Java

Methods in Java are defined by:

Methods in Java


Methods in Java are defined by:

Methods in Java


Methods in Java are defined by:

Methods in Java


Methods in Java are defined by:

A method that checks if a number is prime


Methods in Java are defined by:

A method that prints the first N primes


Methods in Java are defined by:

An application: Generating random rumbers
Please, take a look at the code here

Random numbers in Java

Let's write a simple game with the following rules:

Random numbers in Java

Let's write a simple game with the following rules:

Let's add another rule:

Another application: A simplsitic version of computer displays

How can we display images on computer displays?

How can we display images on computer displays?

Every cell in the grid is called a pixel

Pixel : Picture element

Pixel coordinates on a display

Changing coordinate origin to the center of the screen

Changing coordinate origin to the center of the screen

Let's emulate a computer display with the Java console

Our display will only have two "colors"

How do we draw a circle of radius R in our display?

x^2 + y^2 = R

How do we draw a circle of radius R in our display?

x^2 + y^2 = R


Since our display is discrete we will have to be more forgiving

R - line_width ≤ x^2 + y^2 ≤ R + line_width

How do we draw a circle of radius R centered at (a, b) in our display?

(x - a)^2 + (y - b)^2 = R

Resources

/