EECS1022
Programming for Mobile
Computing
S2024, Section A
Java Basics, Separation of Concerns
Labs
Labs start on May 28: this week (!)
Time & Location: depends on your [course and
lab] section; WSC 106 or WSC 108
When: see course outline for deadlines
You must attend the lab section in which you
are officially enrolled
Your lab work may be graded by TAs during the
lab
Do App Zero if have not done it already!
York University EECS 1022 A (SU2024) Java Basics 2
Last Week
Java
“Hello World”
Identifiers, operators, separators…
Packages, access modifiers
Errors, Testing
Libraries and Software reuse
“Separation of Concerns”
Client vs. Implementer view
APIs
York University EECS 1022 A (SU2024) Java Basics 3
Today
Java Basics
Features, syntax, conventions…
“Separation of Concerns”
Client vs. Implementer view
APIs
York University EECS 1022 A (SU2024) Java Basics 4
“Big Picture”
Computer Science
Information and its processing
What problems are solvable by algorithms?
Computational complexity of algorithms
and problems
Programming and abstraction
York University EECS 1022 A (SU2024) Java Basics 5
Computational Complexity
Search
Linear search
O(n)
Binary search
Also, search trees
O(log n)
Hashing
O(1)
Average (expected), no guarantee, but 99+ % probability!
York University EECS 1022 A (SU2024) Java Basics 6
“Big Picture”
Software Development
Requirement Analysis
Design
Implementation
Testing
(repeat)
Deployment
York University EECS 1022 A (SU2024) Java Basics 7
Java Basics (cont’d)
Classes, intro to OOP
Java Basics 8
Recap: Data Types
Java is strongly-typed
Every variable is declared with a data type
Type specifies the kind and the range of values it can hold
Once the variable is declared, its data type
cannot change
The data types are checked during the
compile time
Some errors can be caught early
York University EECS 1022 A (SU2024) Java Basics 9
Types and Errors
Before Java program can run it must be
translated into Java bytecodes (compiled)
Compile time errors: easy to detect
Syntax problems: double 1d = 0.0;
Type mismatches: int a = 1.0/2.0;
Runtime errors: harder to catch
10/0
NPE, AIOOB
Logic errors: even harder
double massInKg = massLb / 0.454;
York University EECS 1022 A (SU2024) Java Basics 10
Recap: 8 Primitive Data Types
York University EECS 1022 A (SU2024) Java Basics 11
Recap: strings
String is NOT a primitive type
Object
Has methods
Has constructors
Space occupied varies
Immutable: cannot be changed (efficiency reasons)
String literals: in double [straight] quotes: "something"
Cannot be compared with char
public int compareTo(String anotherString)
public boolean equals(Object anObject)
York University EECS 1022 A (SU2024) Java Basics 12
Boolean
York University EECS 1022 A (SU2024) Java Basics 13
Integers (int)
Operations: + − *, / %
5/2
result = 2
5.0/2
result = 2.5
5%2
result 1
-1 % 2 //note: remainder can be used with doubles
result -1
[Link](-1, 2); //== 1
York University EECS 1022 A (SU2024) Java Basics 14
Type Conversion, Casting
Widening (promotion): automatically
5/2
result = 2
5.0/2
result = 2.5
"2*3.0"
result = ??
Casting: explicitly
double average = (double) 12 / 5;
int feet = (int) (28.3 / 12.0);
weightView = (EditText) findViewById([Link]);
York University EECS 1022 A (SU2024) Java Basics 15
char
York University EECS 1022 A (SU2024) Java Basics 16
Operations: Assignment
variableName = value;
variableName – defined, in scope, type can hold value
value – otherVariableName, literal, or expression
York University EECS 1022 A (SU2024) Java Basics 17
Overflow and Underflow
When we assign a value that is out of range of the
data type
2147483646 //initial number
2147483647 //added 1
-2147483648 //added 1
-2147483647 //added 1
Most CPUs able to detect it, but not Java
Can use another type
Can use special methods in newer Java ver’s
[Link](value, 1); //can generate an exception!
York University EECS 1022 A (SU2024) Java Basics 18
Overflow and Underflow
Order matters
mid = (low + high)/2;
mid = low + (high - low)/2
Similar issues with floating point types, e.g., double:
2^1073 = 1.0E-323
2^1074 = 4.9E-324
2^1075 = 0.0
[Link]
Mind the loss of precision for math/comparisons
(10.0+0.000000001) - (10.0+0.000000001)
vs. (0.000000001) - (0.000000001)
Sqrt (2) * Sqrt (2) = 2.000000000000004
+0f == -0f //true vs. 1/+0f == 1/-0f //false
York University EECS 1022 A (SU2024) Java Basics 19
Separation of Concerns
Client
Uses the class
Knows the interface only
mySin = [Link]([Link]*45.6);
Implementer
Knows how the class works inside
York University EECS 1022 A (SU2024) Java Basics 20
Client View Example
Rectangle API
public static int getArea (int width, int height)
int area = [Link](5, 6)
York University EECS 1022 A (SU2024) Java Basics 21
Client View Example: another way
Rectangle API
public Rectangle(int width, int height)
public int getArea ()
rectangle = new Rectangle (5, 6)
int area = [Link]()
York University EECS 1022 A (SU2024) Java Basics 22
The Implementer View
public class Rectangle
{
public static int getArea (int w, int h)
{
return w*h;
}
}
York University EECS 1022 A (SU2024) Java Basics 23
static?
static
means no instances (no objects) of that
class are going to be created
No data is kept
Any data comes from the outside whenever
needed, right before it’s used
York University EECS 1022 A (SU2024) Java Basics 24
The Implementer View: Another
Way (non-static)
public class Rectangle
{
private int width;
private int height;
public Rectangle (int w, int h)
{
width = w;
height = h;
}
public int getArea ()
{
return w*h;
}
}
York University EECS 1022 A (SU2024) Java Basics 25
What about this?
public class Rectangle
{
private int width;
private int height;
public Rectangle (int width, int height)
{
[Link] = width;
[Link] = height;
}
public int getArea ()
{
return [Link] * [Link];
}
}
York University EECS 1022 A (SU2024) Java Basics 26
Some Important Libraries
[Link]
[Link]
[Link]("%.2f", [Link])
Result: “3.14” (a String)
[Link]()
Length of the string
[Link]
int num = [Link](2, 10); //1024
Also, sqrt(), abs(), log(), exp(), random(), etc.
Integer
int num = [Link]("123");
Double (similar)
York University EECS 1022 A (SU2024) Java Basics 27
i2c API
[Link]
York University EECS 1022 A (SU2024) Java Basics 28
York University EECS 1022 A (SU2024) Java Basics 29
York University EECS 1022 A (SU2024) Java Basics 30
Can you write your own?
public static int findFactorial(int n)
{
int i, factorial = n;
for (i= n -1; i>= 1; i--)
factorial = factorial * i;
return factorial;
}
Use
= [Link](5);
= [Link](5);
York University EECS 1022 A (SU2024) Java Basics 31
OOP
An object is a conceptually integrated
data collection that encapsulates state
and behavior
A class is a template that defines the
common structure for all objects of that
class
Variables and methods = class members
York University EECS 1022 A (SU2024) Java Basics 32
Field, variable, attribute, property
Variable
Name given to a memory location
Field
Data member of a class
Can be public, static, not static and final
Attribute
Typically a public field accessed directly
Property
Typically has getter and setter
York University EECS 1022 A (SU2024) Java Basics 33
The Student Class
/**
* The Student class keeps track of the following pieces of data
* about a student: the student's name, and ID number
* All of this information is entirely private to the class.
* Clients can obtain this information only by using the various
* methods defined by the class.
*/
public class Student {
/* Private instance variables */
private String studentName; /* The student's name */
private int studentID; /* The student's ID number */
Instance variables maintain the internal state of the class
All instance variables used are private
York University EECS 1022 A (SU2024) Java Basics 34
Constructors and getters
/**
* Creates a new Student object with the specified name and ID.
* @param name The student's name as a String
* @param id The student's ID number as an int
*/
public Student(String name, int id){
studentName = name;
studentID = id;
Constructor
}
/**
* Gets the name of this student.
* @return The name of this student
*/
public String getName() {
return studentName;
Get method
}
/**
* Gets the ID number of this student.
* @return The ID number of this student
*/
public int getID() {
return studentID;
Another getter
}
York University EECS 1022 A (SU2024) Java Basics 35
Constructor vs Method
Constructor
No explicit return value (it constructs an
object)
Cannot call it arbitrarily (it’s invoked when
object is being created)
York University EECS 1022 A (SU2024) Java Basics 36
Private, get, set…
Generally, fields are private
can’t be seen (and changed directly!) from
outside the class
May add getter and setter methods to
allow access to some or all fields
Constructors can initialize fields of a
new object (when new operator is used)
York University EECS 1022 A (SU2024) Java Basics 37
Declaring a Class
York University EECS 1022 A (SU2024) Java Basics 38
Static (Utility) class
A static class has features (fields and
methods) that are all static
Belong to the class itself, not to the created
objects –as in non-static classes
Can use any methods without having to
create objects
double y = [Link](x); //no object!
York University EECS 1022 A (SU2024) Java Basics 39
Non-static class
Most classes are non-static
Each object has state: its own copy of
non-static fields
The methods (behavior) are technically
shared, but each method when used on
an object can access only the object’s
data
e.g., [Link]();
York University EECS 1022 A (SU2024) Java Basics 40
OOP Idea
Classes define templates
Objects = instances of classes
Created at runtime (from non-static classes)
Objects usually mimic real-world entities
Student
Date
Rectangle
Bank account
…
York University EECS 1022 A (SU2024) Java Basics 41
toString()
/**
* Creates a string identifying this student.
* @return The string used to
*display this student
*/
public String toString() {
return studentName+"(#"+studentID+")";
} The toString() method tells Java how to
display values of this class. Classes typically
override the toString() of the Object superclass
York University EECS 1022 A (SU2024) Java Basics 42
Using the Student Class
Use the constructor to create instances
Student stdA= new Student("StudentA", 100056);
Student stdB= new Student("StudentB", 300059);
York University EECS 1022 A (SU2024) Java Basics 43
Objects vs. Primitive Types
A primitive data type (e.g., int, float,
char ) represents a single simple piece of
information
An object represents multiple pieces of
information that are grouped together
Can consist of primitives and/or objects
York University EECS 1022 A (SU2024) Java Basics 44
Other Objects
We create new type of objects by
defining a class
Each class we define = new type (or
category) of an object
Public fields:
Not a great idea
usually
York University EECS 1022 A (SU2024) Java Basics 45
An Address object
Address addr;
addr = new Address();
[Link] = "EECS Student One";
[Link] = 1022;
[Link] = "EECS Department";
[Link] = "Toronto";
[Link] = "ON";
[Link] = "M3J 1P3";
[Link]([Link] + " lives at ");
[Link]([Link] + " " +
[Link]);
York University EECS 1022 A (SU2024) Java Basics 46
Other Class Examples
[Link]
Instance of time
Comparing dates
String representation
Used by other classes
York University EECS 1022 A (SU2024) Java Basics 47
Other Class Examples
[Link]
Text Label
Text, style, layout…
York University EECS 1022 A (SU2024) Java Basics 48