0% found this document useful (0 votes)
7 views38 pages

Week 1 Slides

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views38 pages

Week 1 Slides

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Programming

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

• Point of sale systems


Introduction to Object Oriented
Programming
• Object-oriented programs are made up of
objects that interact with each other
• OOP uses classes to define objects and
their behaviour
• Objects interact to produce functionality
e.g. purchase music online, search for the
cheapest Internet provider
OO System - Example
• The computer system in the UTS library could
easily be written in Java or some other OO
language
• Examples of objects in a library system:
• Books – author, title (how many?)
• Borrowers - students & staff (how many?)
• Examples of classes in a library system:
• Book
• Student
Introduction to Java
• Java is an Object Oriented programming
language
• Most Java systems contain many classes,
this is where programmers write the Java
code
Introduction to Java
• When we run (execute) a program the
Java objects will be created and can then
run methods (behaviour)
• So we must learn how to create objects in
the Java code
Blue J
• We will use a program called BlueJ to write
and execute our Java programs
• BlueJ is an IDE (Integrated Development
Environment)
• It allows us to write and run Java programs easily
• Blue J requires an installation of the JDK
BlueJ – An IDE
• BlueJ is a tool for software programmers
• It allows us to write, compile, test and debug
code
• We can use the Object Bench for testing
• JDK & BlueJ can be downloaded freely from the
Internet
Basic Structure - Calculator
• A simple Calculator can add, subtract, multiply
and divide two numbers
Class definition for Calculator
class Calculator
Class name
{
Start of class

} End of class

This is the Java code for Calculator – we will


add the attributes (numberOne, numberTwo)
and methods (add, subtract, etc) later.
Class Definitions
• Java class definitions are saved into a file with a
.java extension

• Class definition for Calculator would be saved


as:
Calculator.java

• This is the file that is saved onto the hard drive


of your computer so the code is not lost
Adding Attributes to Calculator
class Calculator
Class name
{
int numberOne, numberTwo; Attributes

} End of class
Attributes: Data Fields
• numberOne & numberTwo are also known as
data fields of the class – attributes

• Attributes store an object’s data


• These values can be different for every object
Adding Constructor to Calculator
class Calculator
Class name
{
int numberOne, numberTwo;

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

• Why do you need many objects?


• Because each object has it’s own set of values for
each attribute
• Calculator Object 1: numberOne = 21, numberTwo = 5
• Calculator Object 2: numberOne = 17, numberTwo = 3
• Calculator Object 3: numberOne = 4, numberTwo = 12
• But there is just 1 class Calculator that defines how a
Calculator works
Why only 1 Calculator Class?
• ONE class creates MANY objects in the system
• This way we write the code ONCE, and use it
MANY times

• 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

calc1:Calculator calc2:Calculator calc3:Calculator

numberOne = 21 numberOne = 17 numberOne = 4


numberTwo = 5 numberTwo = 3 numberTwo = 12
What actions can Calculators
perform?
• Actions that a program can perform are known
as the behaviour of the program -
add
subtract
multiply & more
• These would be methods in the Calculator class
i.e. things that Calculator objects do!
Updated class diagram showing
class attributes & methods

Class name Calculator

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
}

• A void method just executes the statements in


the group
Display output to the user
• System.out.println() and System.out.print() –
both these methods print things to the screen
that the user can see
• print() – display result on current line
• println() – display on current line then
advance to a new line
• Example
System.out.println("Hello World!");
Print a message in add()
Creating Multiple Objects
• The Object Bench is good for testing 1 object at
a time
• When we have multiple objects, we need
another class to control the program
(sometimes a main() method is used)
• We will just use a test class with a constructor,
to create multiple objects and call methods on
each object
Test Calculator
class TestCalculator This is a comment in
the code (ignored
{ when the code is
Calculator calc1, calc2; executed)

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

Calculator calc1 = new Calculator();

Object name Class name

Notice how object names start with a lower


case letter, class names start with an UPPER
case letter (this is the Java convention)
Constructing an Object
• When we create an object we are
‘constructing’ the object in the memory of the
computer (in the RAM)
• Therefore we use a special method in the class
called the Constructor
• When we use this method we need to also use
the keyword new
Calculator calc1 = new Calculator();
Use the add() method
class TestCalculator
{
Calculator calc1, calc2;
TestCalculator()
{
// Create two different calculator
objects
calc1 = new Calculator();
calc2 = new Calculator();
calc1.add();
calc2.add();
}
}
Use the add() method
• When we ‘use’ a method it is referred to as
‘calling’ or ‘invoking’ a method
• When methods are used, often it means
messages are being passed between objects

• The code to do this has the format:


objectName.methodName();
calc1.add();
A note about comments
• Good code should always contain meaningful
comments
• // denotes an inline comment
• All data following this symbol (on any given line) is
ignored by the compiler
• All statements in code (not comments) must
end in a semi colon ;
• All programs should contain small comments to
explain what the code actually does
Other comments
• /**
• *
• */

This is a multi-line comment – it is used to


provide header comments for classes and
methods
Key Concepts …

What is Programming?
What is OO?
BlueJ – write code, compile, run, test
Java basics:
Class, object, constructors,
methods
Displaying output

You might also like