Week 1 Slides
Week 1 Slides
Fundamentals
Week 1
Introduction to Java
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F Insearch Limited is a controlled entity of the University of Technology Sydney (UTS), and a registered private higher education provider of pathways to UTS.
What is a program?
• A set of statements that are executed in order
to complete a task
Statement
1
Statement
2
Statement
3
What is a program ?
Program Binary
Problem Solution
Statements code
• A program is • Each program • Each program • The binary
written to statement is statement is code is
solve a written to converted into executed in
problem help achieve binary code so order to
the goal the computer achieve the
can execute it goal
Examples of Programs
• Webpages
• Phone applications
} End of class
} End of class
Attributes: Data Fields
• numberOne & numberTwo are also known as
data fields of the class – attributes
Calculator()
{ Constructor
}
}
End of class
Constructors
• A constructor constructs (or creates) objects
• Once an object is created, we need to store
data for it, so the constructor can set attribute
values
Calculator()
{
numberOne = 5;
numberTwo = 7;
}
Adding Constructor to Calculator
class Calculator
Class name
{
int numberOne, numberTwo;
Calculator()
{
numberOne = 5;
Setting
numberTwo = 7; attribute
values
}
} End of class
Class versus Object
• 1 class, MANY objects
• Example:
Write 1 Calculator class
Use it to create 100’s of calculator objects if
required
Graphical representation of
classes and objects
Calculator
Class names
are not numberOne
underlined, numberTwo
Object names
are
numberOne
Attributes numberTwo
Methods have ()
add() after them
Methods subtract()
multiply()
Methods
• A method is a named group of statements
• When a method is called the statements in the
group are executed
name()
{
//statements
}
Create an add() method in
Calculator
class Calculator
{
int numberOne, numberTwo;
Calculator()
{
}
void add()
{
}
}
Void Methods
• add() is a void method – it has the keyword
void
void add()
{
//Statements go here
}
TestCalculator() // Constructor
{
// Create two different calculator
objects
calc1 = new Calculator();
calc2 = new Calculator();
}
}
Creating Calculator Objects
• After the Calculator class is defined, we need
code to create the calculator objects so they
can execute and perform tasks:
• Declare a calculator object:
Calculator calc1;
• Create the object:
calc1 = new
Calculator();
Or
Calculator calc1 = new Calculator();
Object name vs Class name
What is Programming?
What is OO?
BlueJ – write code, compile, run, test
Java basics:
Class, object, constructors,
methods
Displaying output