Академический Документы
Профессиональный Документы
Культура Документы
Saw Part 1?
Part 1 covered all the newbie stuff, search for it J
Agenda
1.
2.
Buffered Reader
3.
Scanner
4.
Array List
5.
6.
7.
Static keyword
Agenda
8.
Enumerations
9.
coursetro.
com
What is a class?
Simple = Grouping the Java data types to make your own types
Complex = Trying to mock items in the real world
coursetro.com
coursetro.com
[Properties]
2 What does it do? What can you do
with it? (Verb) [Methods]
coursetro.com
[Properties]
Model
Size (Width, height, length, breadth)
Weight
coursetro.com
coursetro.com
[Properties]
2 What does it do? What can you do
with it? (Verb) [Methods]
coursetro.com
[Properties]
Author
Number of pages
Publication name
Title
coursetro.com
coursetro.com
coursetro.com
class Phone{
//What does it have? Properties
String model;
double weight;
coursetro.com
class Book{
//What does it have? Properties
String author;
int pages;
String publicationName;
String title;
//What does it do or you can do? Methods
coursetro.com
First Name
Last Name
Age
Occupation
Class
Object 1
Object 2
Mark
Murphy
35
Software
Engineer
Jon
Skeet
40
Developer
Frank
Underwood
60
Congressman Object 3
Raymond
Tusk
65
Entrepreneur
Object 4
Zoe
Barnes
30
Journalist
Object 5
coursetro.com
The Conclusion
Class =
Object =
coursetro.com
Google It!
1.
2.
3.
4.
5.
6.
7.
String author;
int pages;
//What does it do or you can do?
Methods
coursetro.com
coursetro.com
one
one
one
author
Herbert
Schildt
one
author
Herbert
Schildt
one.pages = 1000;
null
pages =
1000
two
two
two.author = J K Rowling;
two
author
JK
Rowling
two
author
JK
Rowling
two.pages = 1500;
null
pages =
1500
String author;
int pages;
two
author
pages =
JK
Rowling
1500
int getPages()
coursetro.com
Using a method
public static void main(String[] args){
Book two = new Book();
two.author = JK Rowling;
two
JK
Rowling
author
pages =
1500
two.pages = 1500;
println(two.getPages());
int getPages()
main()
author
int pages;
public int getPages(){
return pages;
}
pages =
JK
Rowling
1500
two
int getPages()
void setPages(int)
coursetro.com
Using a method
public static void main(String[] args){
Book two = new Book();
author
two.author = JK Rowling;
two.pages = 1500;
pages =
JK
Rowling
2000
two
int getPages()
two.setPages(2000);
println(two.getPages()) //2000
void setPages(int)
}
2000
main()
Solve this
problem
coursetro.com
coursetro.com
coursetro.com
coursetro.com
Google It!
1.
2.
3.
4.
5.
6.
7.
What is a Stream?
InputStream
Data
Source
0110
1100
1010
1011
0011
0101
1110
1111
0000
Your
Program
OutputStream
coursetro.com
coursetro.com
InputStreamReader
InputStream
0 1 00 1
01 10
Input
Stream
Reader
H i
The r e
Take the binary 0s and 1s from Input Stream and give you characters,
But only 1 character at a time
BufferedReader
Input
Stream
0 1 0 0
Input
Stream Wh a t
Reader
Buffered
Reader
What
Take the characters from InputStreamReader and read the entire line
in a single shot
coursetro.com
Need InputStreamReader
InputStreamReader isr = new InputStreamReader();
coursetro.com
Need InputStream
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(isr);
coursetro.com
Scanner
am
26
y r
Scanner
am 2 6
yr
a m
2 6 . 5
y e a r s
o l d !
a m
2 6 . 5
y e a r s
o l d !
Use the next() method of the Scanner class to get each token
Classes and
Objects
Explained II
Classes and
Objects
Example
Take Input
From User
Explained
Take Input
From User
Example
Google It!
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
coursetro.com
Wrapper
Class
byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
char
Character
boolean
Boolean
wrapper =
Double
value
= 31.25
coursetro.com
Method
Name
add
clear
contains
Returns true if ArrayList contains the element you specified, else false
get
indexOf
remove
Either specify the element and remove it when it occurs first or specify the
index and remove the element at the index
size
trimToSize
list
list.add(red);
list.add(0, yellow);
list.add(green);
list
red
list
yellow
red
list
yellow
red
list.remove(1);
list.remove(green);
list.contains(yellow)
list
yellow
green
list
yellow
green
true
coursetro.com
coursetro.com
friends =
ArrayList<String>
people =
0
1
2
Anky
Gary
Murphy
coursetro.com
Array vs.
Array List
coursetro.com
Arrays
Array Lists
Get an element
e = values[2];
e = values.get(2);
Replace an element
values[3] = 12;
values.set(3, 12);
Size
values.length
values.size();
Remove element
No inbuilt mechanism
values.remove(4);
Initialize fast
coursetro.com
Google It!
1.
2.
3.
4.
5.
coursetro.com
Primitive
type
Properties
coursetro.com
coursetro.com
Reference
Type
Properties
Store addresses
Addresses point to objects
Hence, they refer to objects
Initialized to a default value of null
Can call methods through their object
Stored on heap
coursetro.com
Memory Model
main(){
int i = 20;
String x = 20;
}
i = 20
20
x = address
Stack
Heap
Call By Value
Dont modify things like ever!
coursetro.com
modify(int number){
number = 100;
}
main(String[] args){
int original = 25;
println(Before +original) //25
modify(original);
coursetro.com
original
25
modify(original);
original
25
number
25
original
25
number
100
original
25
modify(int number){
number = 100;
coursetro.com
Call By Reference
Think carefully about what you are doing
coursetro.com
modify(Square object){
object.side = 100;
}
main(String[] args){
Square sq = new Square();
sq.side = 25;
println(Before +sq.side); //25
modify(sq);
...println(After +sq.side); //100
}
coursetro.com
main(String[] args){
Square sq = new Square();
sq
side = 25
sq
side = 25
object
sq
side =
100
object
sq
side =
100
sq.side = 25;
println(Before +sq.side); //25
modify(sq);
modify(Square object){
object.side = 100;
}
...println(After +sq.side); //100
}
coursetro.com
Array List
Explained
Relevant
Videos
Call by value vs.
reference
Explained
Google It!
1.
2.
3.
4.
Solve this
problem
coursetro.com
length
[Properties]
2 What does it do? What can you do
with it? (Verb) [Methods]
coursetro.com
[Properties]
Length
Breadth
coursetro.com
coursetro.com
double length;
double breadth;
//What does it do or you can do?
//Methods
coursetro.com
r.length = 20;
r.breadth = 10;
bill
40.35
length
breadth
0
0
length
breadth
20
0
length
breadth
20
10
coursetro.com
Square()
println(r.length + + r.breadth)
length = 0.0
breadth = 0.0
coursetro.com
Rectangle(){
length = 20;
breadth = 10;
}
println(r.length + + r.breadth)
coursetro.com
50, 40
coursetro.com
coursetro.com
Rectangle(){
length = 20;
breadth = 10;
}
Rectangle(double l, double b){
length = l;
breadth = b;
}
coursetro.com
Rules
coursetro.com
Google It!
1.
2.
3.
4.
static
variables
blocks
methods
imports
inner
classes
coursetro.com
class Rectangle{
double length;
double breadth;
Rectangle(){
println(I was called);
Rectangle r = new Rectangle();
coursetro.com
coursetro.com
class Rectangle{
double length;
double breadth;
int count = 0;
Rectangle(){
Rectangle r = new Rectangle();
count = count + 1;
//1
//1
coursetro.com
length
breadth
count
0
0
1
r2
length
breadth
count
0
0
1
coursetro.com
length
breadth
0
0
r2
length
breadth
0
0
r3
length
breadth
0
0
count
3
12
class Rectangle{
double length;
double breadth;
static int count = 0;
Rectangle(){
Rectangle r = new Rectangle();
count = count + 1;
//1
//2
coursetro.com
main(String[] args){
class Rectangle{
double length;
...println(Rectangle.count);
double breadth;
coursetro.com
Static
Variables
Rules
coursetro.com
Static Blocks
class Rectangle{
class Rectangle{
Rectangle(){
static{
count = count + 1;
count = 0;
Rectangle(){
count = count + 1;
}
}
coursetro.com
Load
class
Initialize
static
variables
Run
static
blocks
Run
construc
tor
Other
stuff
coursetro.com
Static Methods
Methods common to everyone
coursetro.com
main()
main(String[] args){
...
length
breadth
0
0
count
println(Rectangle.getCount());
Rectangle.setCount(0);
r2
length
breadth
0
0
int
getCount()
void
setCount(int)
r3
length
breadth
0
0
coursetro.com
Constructors
And Overloading
Example
Relevant
Videos
Static Keyword
Explained
Static Keyword
Example
Google It!
1.
2.
3.
4.
5.
6.
7.
8.
9.
= Monday;
= Tuesday;
= Wednesday;
= Thursday;
= Friday;
coursetro.com
coursetro.com
What is an enumeration?
A set of related constants
coursetro.com
Example
Direction
Days
Game Status
Pizza Sizes
Employee Level
Colors
enum Workday {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY
};
coursetro.com
coursetro.com
coursetro.com
println(Boring man!);
break;
case FRIDAY:
...println(Let\s party guys!);
break;
}
coursetro.com
coursetro.com
MONDAY (Monday),
TUESDAY (Tuesday),
WEDNESDAY (Wednesday),
println(w.getRepresentation());
}
THURSDAY (Thursday),
FRIDAY (Friday);
final String representation;
Workday (String rep) {
representation = rep;
}
public String getRepresentation() {
return representation;
}
}
coursetro.com
Direct Input?
valueOf() covered in the practical video of this topic.
coursetro.com
Google It!
1.
2.
3.
4.
5.
6.
coursetro.com
Local Variables
class Test {
public void calculate(){
int x = 10;
for(int i = 1...){
int prod = i * i;
}
}
}
Scope of x = Black box, Scope of i and prod = Green Box, Lifetime = White Box
Parameter Variables
class Test {
public static void setA(String t){
...println(t);
}
}
Scope = Lifetime = Black Box
Cant add a local variable and parameter variable with the same name inside a
method
coursetro.com
Static Variables
class Test {
static String a = Test;
//Test
}
public static void setA(String t){
a = t;
}
}
Scope = black box, Lifetime = as long as the program runs
coursetro.com
//20
//Test
//Parameters value
//Test
coursetro.com
Disjoint Scopes
class Test {
public void square(double number){
double result =
}
public void cube(double number){
double result =
}
}
coursetro.com
Instance Variables
class Test {
String b = Test;
//Test
}
public void setB(String t){
b = t;
}
}
Scope = black box, Lifetime = as long as the object exists
coursetro.com
}
}
coursetro.com
This Keyword
class Test {
String b = Test;
public void setB(String b){
this.b = b;
}
}
main(String[] args){
Test one = new Test();
one.setB(Hi);
Test two = new Test();
two.setB(Bye);
Test three = new Test();
three.setB(Vow);
}
one
Test
Hi
two
Test
Bye
three
Vow
Test
this
coursetro.com
Enumeration
Example
Relevant
Videos
This Keyword
and variable
hiding explained
This keyword
and variable
hiding example
Google It!
1.
2.
3.
4.
5.
6.
7.
Without Inheritance
Person
First Name
Last Name
Age
Employee
First Name
Last Name
Age
Occupation
Salary
Managing
Director
Manager
First Name
Last Name
Age
Occupation
Salary
Department
First Name
Last Name
Age
Occupation
Salary
Department
Experience
Branch
Without Inheritance
Vehicle
Wheels
Fuel
capacity
Weight
Mileage
Bike
Wheels
Fuel capacity
Weight
Mileage
Seater
capacity
Gears
Car
Wheels
Fuel
Capacity
Weight
Mileage
Seater
capacity
Gears
Type
Storage
capacity
Truck
Wheels
Fuel
Capacity
Weight
Mileage
Seater
Capacity
Gears
Type
Storage
capacity
Load
capacity
Why Inheritance?
Stop repeating yourself, try to find common stuff and declare
them only once
coursetro.com
With Inheritance
Person
First Name
Last Name
Age
Employee
Occupation
Salary
Manager
Department
Managing Director
Experience
Branch
coursetro.com
With Inheritance
Vehicle
Wheels
Fuel Capacity
Mileage
Weight
Bike
Seater Capacity
Gears
Car
Type
Storage Capacity
Truck
Load Capacity
Solve this
problem
coursetro.com
Circle
Rectangle
Triangle
Square
coursetro.com
display()
display()
length 0.0
breadth 0.0
sq
display()
length 0.0
breadth 0.0
side
0.0
println(Displaying shape);
}
}
class Rectangle extends Shape {
double length;
double breadth;
}
class Square extends Rectangle{
double side;
}
coursetro.com
Inheritance
Rules
coursetro.com
Object : The
God class
coursetro.com
Google It!
1.
2.
3.
4.
5.
6.
7.
coursetro.com
class Shape{
getArea()
returns 0.0
getArea()
returns 0.0
length
0.0
breadth
0.0
getArea()
double length;
length
0.0
breadth
0.0
side
0.0
double breadth;
}
class Square extends Rectangle{
double side;
}
sq
returns 0.0
Method Overriding
s
class Shape{
getArea()
returns 0.0
getArea()
returns l * b
}
}
length
0.0
breadth
0.0
coursetro.com
Method Overriding
class Rectangle extends Shape {
double length;
public double getArea(){
returns l * b
getArea()
double breadth;
length
0.0
breadth
0.0
getArea()
sq
length
0.0
breadth
0.0
side
0.0
coursetro.com
Difference Between
Overloading
Overriding
1.
2.
3.
4.
1.
2.
3.
4.
coursetro.com
Constructor Chaining
class Shape{
First
First
Second
sq
First
Second
Third
Shape(){
println(First); }}
Super
keyword
coursetro.com
name
I am a shape
name
I am a rectangle
I am a shape
}
class Rectangle extends Shape {
String name = I am a rectangle;
display()
...println(super.name);
}
}
coursetro.com
display()
Which shape
display()
Which shape
Rectangle
println(Which shape?);
}
}
class Rectangle extends Shape {
@Override
public void display(){
super.display();
println(Rectangle);
}
}
coursetro.com
println(name);
Shape()
Rectangle()
First
Second
}
}
class Rectangle extends Shape {
Rectangle(){
super(First);
println(Second);
}
}
coursetro.com
Inheritance
Explained
Relevant
Videos
Super keyword and
Method overriding
Explained
Google It!
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
What is Polymorphism?
Process objects with the same superclass as if they are all
objects of the superclass
coursetro.com
A closer look
class Shape{
double length;
double breadth;
return 0.0;
//Constructor
}
}
coursetro.com
main(String[] args){
double side;
//Constructor
Shape s = sq;
s.getArea();
//400
s = r;
s.getArea();
//200
coursetro.com
getArea()
length 0.0
breadth 0.0
20
side
getArea()
length 0.0
breadth 0.0
Shape s = sq;
s.getArea();
//400
s = r;
s.getArea();
//200
coursetro.com
Polymorphism
Properties
1.
coursetro.com
Solve this
problem
coursetro.com
coursetro.com
Google It!
1.
2.
3.
4.
coursetro.
com