Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

CS

1101
Lecture 2

Introduc1on to Programming Using Java

Shah Ahmed

Life outside of the main method!


But rst an aside

Whats a func1on?
You give a func1on something, and it returns
something else.
For example, func1on 1mes two
I give it 3, it gives me 6

Dissec1on of a func1on
Parameters are what I give the func1on
public sta1c int 1mesTwo(int x){
return 2*x;
}
Lets try it!
See Func1onPrac1ce

These helper func1ons


These helper func1ons live outside and assist
the main method
Keeps the main method clean, can recall the
func1on mul1ple 1mes, avoid copy and
pas1ng

Object
In Java, I want to represent items as code, like
a car
We create a blueprint, then we can create any
number of those objects
We have to code up the blueprints

Object
Fields
Constructor
Creates one instance of the item

Methods, func1ons

Pointers? References?
Unlike primi1ve types (int, String), when
objects are made, variables are only poin1ng
to it
ONLY when we say new, a new object is
created
Very convoluted and very confusing

Example
Car Enzo = new Car(Enzo,200);
Car myCar = Enzo;
A new car wasnt created, we have two
pointers.

Constructor
How do we create an object?

Fields
How do we assign elds?
Change elds?
This!

Methods
Dierence of func1ons and processes
How do we call them?

Arrays

List of objects
Array of int is a bunch of them
Int[] holder = {5,56,33,43,65}
holder[2] is 33

Private versus public

You might also like