02.Java-OOP
02.Java-OOP
nl
y o
d a
T o
Learn to program in Java in
(less than) one hour!
l!
ea
l d
c ia
p e
S
y !
nl
y o
d a
T o
CS2040S
Data Structures and Algorithms
Welcome!
is open
Reminders
In t h e
f
the fo uture, you
ru m a can as
your t n d /or ch k on
utor, i e ck
f u n su w i th If the goal of the problem set is to write
re.
a sorting routine, then calling the Java
library sort defeats the purpose…
Reminders
Archipelago: (if you didn’t receive, check your spam for the link)
Lambda expressions:
Java “advanced” features:
Examples:
Type inference:
vs.
Java “advanced” features:
Examples:
Click “debug”
Set a breakpoint…
is open
More advice: use the IntelliJ debugger
Today’s goal:
To make you aware of key aspects of Java
After class:
Go look things up!
the basics of OOP and Java
Programming Paradigms
Programming paradigms:
– Procedural (imperative) languages
– Functional languages
– Declarative languages
– Object-oriented languages
Object-oriented Languages
– Examples: Java, C++, …
– Advantages:
• Near-ubiquitous in industry
• Modular
• Code re-use
• Easier to iterate / develop new versions
– Information hiding
– Pluggable
Object-oriented Paradigm
Separate interface (”what it is
Abstraction supposed to do”) from implementation
(“how it does it”).
Abstraction
Very relevant
to CS2040S
Encapsulation
Inheritance
Less relevant
to CS2040S
(but very important overall).
Polymorphism
Problem: Naruto the new
Naruto the new hirehire
Description of Naruto:
“Nice guy!”
“Really likes bananas!”
“Not the smartest fellow... but
friendly!”
“I’m afraid he’s going to $^%&
up our code, man!”
what should we do?
What should we do?
Give him some pointless work!
Send him back to the forest!
User
Implementer Object
Public Interface
Private Data
Naruto
[XKCD: Black Hat]
Private Methods
Claim: We are all Naruto!
Software is getting very complex
[http://www.informationisbeautiful.net/visualizations/million-lines-of-code/]
Software is getting very complex
[http://www.informationisbeautiful.net/visualizations/million-lines-of-code/]
Software is getting very complex
34
Encapsulation and information hiding
User
Implementer Object
Public Interface
Private Data
Naruto
[XKCD: Black Hat]
Private Methods
Object has:
– State (i.e., data)
– Behavior (i.e., methods for modifying the state)
Classroom
enter(student)
list_of_students
leave(student)
number of chairs
temperature
count()
How to implement a File System?
is open
How to implement a File System?
Files: Folders:
– Contain data – Contain files
– Edited – Contain folders
– Rename – Rename
– Moved – Moved
First principle of Java
« Everything is an object »
First principle of Java
« Everything is an object »
« Everything is an object »
int j = 7; var j = 7;
j = “7”; ERROR j = “7”;
Second principle of Java
int j = 7; var j = 7;
j = “7”; ERROR j = “7”;
Creating strings:
String str = “Some text.”;
String altStr = new String(“some text”);
Accessing a string:
– charAt(int index)
– substring(int begin, int end)
– toCharArray()
https://docs.oracle.com/en/java/javase/11/docs
– length() /api/java.base/java/lang/String.html
Java library class: String
Comparing strings:
– compareTo(String otherString)
– compareToIgnoreCase(String otherString)
– equals(Object anObject)
Using strings:
– Flexible and easy: str = str + ‘c’;
– Use with care…
https://docs.oracle.com/en/java/javase/11/docs
/api/java.base/java/lang/String.html
Java library class: String
Comparing strings:
– compareTo(String otherString)
– compareToIgnoreCase(String otherString)
– equals(Object anObject)
Common bug:
Using strings:
– Flexible and easy: str = str + ‘c’;
– Use with care…
Types
class File
{
String name = “”;
FileData contents = null;
String name;
Folder[] children;
File[] files;
Note “recursive” folder
int getNumFiles(){...}
File getFile(int i){...}
}
Class vs. Object
is open
Class vs. Object
File(String fileName)
{
name = fileName;
contents = null; Constructor:
} • Same name as class.
• Takes 0 or more parameters.
} • Called on object creation.
• Used to initialize class.
• Runs after variables initialized
on declaration.
Constructors Many rules involving constructors.
class File
E.g., when exactly are they executed
{ during object construction? In what
String name = “”;order? Etc.
FileData contents = null;
Especially complicated with inheritance.
File(String fileName)
{
name = fileName;
contents = null;
}
}
Object-oriented Paradigm
Separate interface (”what it is
Abstraction supposed to do”) from implementation
(“how it does it”).
Object has:
– State (i.e., data)
– Behavior (i.e., methods for modifying the state)
Classroom
enter(student)
list_of_students
leave(student)
number of chairs
temperature
count()
Abstraction
Classroom
enter(student)
list_of_students
leave(student)
number of chairs
temperature
count()
Defining an interface
// Explain with a comment
// what your interface is for.
interface IFile
{
// Comments explain how to use interface
void rename(String newName);
FileData getData();
FileData getData(){...}
char[] meteo;
FileData getData(){...}
char[] meteo;
FileData getData(){...}
is open
Implementing an interface
class OtherFile implements IFile
{
char[] nom;
char[] meteo;
Error!
FileData getData(){...}
char[] meteo;
FileData getData(){...}
• (none specified)
– within the same package
• public
– everywhere
• private:
– only in the same class
• protected:
– within the same package, and by subclasses
Access Control
public class A
{
private int secretFunction();
public class B
{
public int stealSecrets(A example){
public class B
{
public int stealSecrets(A example){
public class B
{
public int stealSecrets(A example){
public class B
{
public int stealSecrets(A example){
Why?
Access Control
public interface ISee
{
Why?
Access Control
• (none specified)
– within the same package
• public
– everywhere
• private:
– only in the same class
• protected:
– within the same package, and by subclasses
Access Control
• (none specified)
– within the same package
• public Advice:
– everywhere Always specify the access you intend
(even if the default behavior is okay).
• private:
– only in the same class
• protected:
– within the same package, and by subclasses
Packages
package com.mycompany.joe;
public class B
{
public int stealSecrets(A example){
}
}
For CS2040S:
public class B
{
public int stealSecrets(A example){
}
}
Import everything from CleverCode.
Importing library code
import CleverCode.ShiftRegister;
public class B
{
public int stealSecrets(A example){
}
}
public class B
{
public int stealSecrets(A example){
}
}
}
}
static methods
class File
{
private String fileName = “”;
fileName = name;
}
static methods
class File
{
private String fileName = “”;
fileName = name;
s_count++;
}
Initializing an object
Initializing an object
class File
{
private String name = “”;
name = fileName;
contents = null;
}
}
Initializing an object
class File
{
private String name = “”;
// Constructor
public File(String fileName){
name = fileName;
contents = null;
}
}
Initializing an object
class File
{
public File(String fileName){
name = fileName;
contents = null;
}
Multiple constructors with
public File(){ different signatures.
name = null;
contents = null:
}
Initializing an object with an array
class File
{
private int[] pageNumbers = new int[100];
}
}
You might use a constructor to initialize the array.
The main method
class FileSystem
root.addfile(homework);
}
Creating an object
class FileSystem
root.addfile(homework);
}
Using a constructor
class FileSystem
root.addfile(homework);
}
Invoking a method
class FileSystem
root.addFile(homework);
}
Java Operators
Operator Functionality
assignment
=
plus, minus, multiplication, division
+, –, *, /
% remainder
increment, decrement
++, – –
test equality
==, !=
less than, greater than
<, >
<=, >= less-than-or-equal, greater-than-or-equal
Library setup
Java basics:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/