Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Objects, Classes and
Syntax
Dr. Andrew Wallace PhD BEng(hons) EurIng
andrew.wallace@cs.umu.se
Overview
• Objects
• Classes
• Syntax
Objects
Objects
• A “real, existing, item”
• “Instance” of a class
Classes
• Class
• Specifies attributes and methods for an object
• Encapsulated data
• Template
• Create objects from
• Instantiate
Quiz
• Define “class” in the context of OO design.
• Define “object” in the context of OO design.
Syntax
• Computers only do what they are told!
• You have to be priciest what and how you tell a computer what to
do!
• Exactly!
Syntax
• Syntax diagram
• Start point
• Term
• Identify
• Path
• Termination
=
Expression
Syntax
• Variable
• A box to put things in
• Can change!
25
Syntax
Expression
• private int
• protected float
• String
Identifier
nVar;
fVar;
sText;
;
Syntax
• Identifiers
Java Letter
Java Letter
a .. z
Java Digit
A .. Z
$
_
OtherJava
Letter
0 .. 9
Syntax
• nText12
• $Text
• m_oObj2
• 12Def
• #we23
Syntax
Primative
type
Identifier
Class type
,
;
Syntax
• private int
• protected float
• String
nVar;
fVar;
sText;
Syntax
• Variables are:
• Boxes you save data in
• You have to declare variables
• What type are they?
• What names does it have?
Syntax
• Class type
•
•
•
•
Built into Java
Data + methods
Create your own
User defined
• String
• Integer
• FileDialog
Syntax
• Primitive data types
• Built into Java
• int – integer (whole numbers)
• short
• long
• float – real numbers (approximately)
• double
• boolean – logical. True or false
• char – a single Unicode character
• byte – 8 bits of data
Syntax
Type
Size
Min
Max
byte
8 Bits
-128
127
short
16 bits
-32 768
32 767
int
32 bits
-2 147 483 648
2 147 483 647
long
< -9 x 1018
> -9 x 1018
float
32 bits
+/- 3.4 x 1038
7 decimal digits
double
64 bits
+/- 1.7 x 10308
15 decimal digits
Quiz
• Define “variable” in the context of software engineering
• List the inbuilt primitive data types in Java
Syntax
• Char
• Unicode character
• 16 bits
• 65 536 unique characters
• A .. Z, Å, Ä, Ö, 1 .. 9, Æ Ë Σ Ω הּ ش
• Ordered
• Numbered
• \u2000 = 丠
• www.unicode.org
Syntax
• Boolean
• True or false
• Used in comparisons
• Not a number
• Not the same as C
Syntax
• Objects
• Instances of classes
• How to create an object?
• String
strText = new String();
Syntax
• Constructor
•
•
•
•
A method called when the object is created
Sets up the object
Same name as the class
No return type
• New
• Key word in Java
• Used when creating an object
• Create the object in memory
Syntax
• String
• Reference
strText = new String();
• A variable that tells use where to find an object
• A pointer
strText
• Value
• What’s in the box
• Primitive data type
Hello World
Syntax
• Aliasing
• More than one variable can point to the same object
String
String
strText1 = new String();
strText2 = strText1;
strText1
strText2
Hello World
Syntax
• Garbage collection
• When an object no longer has a reference pointing to it
strText
Hello World
Syntax
class
identifier
Class
associations
Class body
modifier
{
Field
declaration
Constructor
declaration
Method
declaration
}
Class
member
Syntax
public class HelloWorld
{
}
Syntax
type
modifier
identifier
parameters
void
public static void main(String[] args)
{
System.out.println(“Hello World”);
}
Method
body
Quiz
• List the parts of a class definition used in Java
• List the parts of a method definition in Java
Syntax
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println(“Hello World”);
}
}
Syntax
public class FunctionCall
{
private int
m_nCount = 0;
public void func1()
{
m_nCount++;
System.out.println(“Count is : “ + m_nCount);
}
public static main(String[] args)
{
func1();
}
}
Questions?