Title: CS 101
1CS 101
2OBJECTS
- You MAKE an object by making a class with the
name of the object type you want and then making
the appropriate CONSTRUCTOR
public class Square public Square(int s)
//do something with s
3OBJECTS
- You USE an object with the new keyword.
public class Square public Square(int s)
public static void main(String args)
Square sq new Square(5) //do
something with Square sq
4OBJECTS
Square sq new Square(5)
- sq is
- the name of a variable
- If I want to use a non-static method from the
Square class, I will use sq.func() - not
Square.func() - an object
- type Square
- What type of variable is it? type Square
- (opposed to int, double, String)
NOT RingBuffer.enqueue() buffer.enqueue()
5OBJECTS
- Square.func() vs sq.func()
- Integer.parseInt(args0)
- Math.random()
- Math.pow(2, 3) lt
Classes - StdOut.println(Hi!)
- buffer.enqueue() lt
Objects - s.length() where s is a String
- s.charAt(0)
What about System.out.println() ?
6OBJECTS
- System.out.println()
- The System class contains a variable
- static Printstream out
- (so out is an object of type Printstream)
- println() is a method in the Printstream class
- Similar to back.value or back.next when back is a
Node
7OBJECTS
- Static vs non-static
- Static means TIED TO THE CLASS
- Non-static means TIED TO AN OBJECT
- Consider
public static int square (int x) return x
x
8OBJECTS
public class Square private int side
//instance variable(s) public Square(int s)
//constructor side s public
int area() //no static return
square(side) public static int
square(int x) public static void
main(String args)
9OBJECTS
public class Square //pretend all the
other methods are there public static void
main(String args) Square sq new
Square(5) int a sq.area() //a 25
Next Making two squares
10OBJECTS
public class Square //pretend all the
other methods are there public static void
main(String args) Square sq new
Square(5) Square sq2 new Square(4)
int a sq.area() // a 25 a
sq2.area() // a 16 gt a 41 a
square(a) // a 4141
Next an array of Squares
11OBJECTS
public class Square public static
void main(String args) Square sq
new Square5 // compare to Square sq
new Square(5) int a 0 for
(int i 0 i lt sq.length i)
sqi new Square(i) a
sqi.area() //what is a at the end of
the loop?
12OBJECTS
- Lets make an Employee class.
public class Employee
private String name
private double rate
public Employee(String n, double r)
name n
rate r
// create pay(int hrs)
public double pay(int hrs)
return hrs rate
// create hrs(int shiftHrs, int wks)
public static int hrs(int shiftHrs, int wks)
return shiftHrs wks
13OBJECTS
Employee (String n, double r) hrs(int hrs, int
wks) pay(int hrs)
- Lets use the Employee class.
public class Employee //
public static void main (String args)
Employee me new Employee(Lesley,
8.50)
int myHours hrs(4, 2) // 4hrs x 2wks
double myPay me.pay(myHours)
Employee g new Employee(Greg,
Double.POSITIVE_INFINITY)
Employee v new Employee(Volunteer, 0)
//etc
14EXCEPTIONS
- Exceptions are Javas way of being nice when
telling you that your code isnt working. - What, you dont believe Java is being nice?
- Weird errors/handling in other languages
PHP unexpected T_PAAMAYIM_NEKUDOTAYIM Perl
fatalsToBrowser, strict, warnings Python
display_errors
15EXCEPTIONS
- You Who cares about exceptions?
- TAs When you ask us what an exception means, it
makes us feel like a human debugger. - If you can debug exceptions and errors on your
own, we can spend our time helping you understand
concepts instead.
16EXCEPTIONS
- ArrayIndexOutOfBounds
- Thrown when using index greater than capacity of
array - Consider StringIndexOutOfBounds
int i 10 int a new inti System.out.print
ln(ai) String s Hi! System.out.println(s
.charAt(10))
17EXCEPTIONS
- NullPointer
- Thrown when trying to do something with a
variable stored as null - Fix (instead of null)
QueueltStringgt myQueue null myQueue.enqueue(Hi!
) myQueue new QueueltStringgt()
18EXCEPTIONS
public class piQueue private Queue myQueue
//implied myQueue null public piQueue()
Queue myQueue new Queue()
myQueue.enqueue(3.14) //apparently stores
doubles public double peek()
//should return 3.14 public static void
main(String args) piQueue p new
piQueue() double myPi p.peek()
//throws exception // fix remove the
highlighted Queue
19EXCEPTIONS
public class piQueue // public static
void main(String args) piQueue p
new piQueue3 //need this loop or else
exception on peek() for (int i 0 i lt
p.length() i) p new
piQueue() double myPi
p0.peek()
20EXCEPTIONS
- One more example
- This throws a StackOverflowError (too much
recursion) Java uses a stack to keep track of
where it is in your program - More errors online
- http//www.cs.princeton.edu/introcs/11cheatsheet/e
rrors.pdf
public int fact(int n) if (n0) return 1
return n fact(n 1) //should be n-1
21NODES
- Nodes are created with the new keyword just like
other objects.
Node n new Node() //n.value is a String, which
is an //object it is null until defined n.value
A new node!
22NODES
- You NEED to use the new keyword to make a new
Node. - In this example, there is only ONE Node if you
change a.value, b.value changes too. n is null.
Node n null Node a new Node() Node b a
23NODES
Node q new Node() q.value Q Node r new
Node() r.value R Node s q q new
Node() q.value Q2
24NODES
Node s q //s contains reference
//to Node with value Q q new Node() //q
no longer references the same //memory
location that s does q.value Q2
25NODES
- Try this with arrays, too
int a new int3 a0 2 int b a a
new int5 System.out.println(a0 , a4
, b0) //prints 0, 0,
2 System.out.println(b4) //exception
26NODES
- Compare to PRIMITIVE types
- In this case, c will remain as 5 but m.value and
n.value are both Bye!
int a 5 Node n new Node() int b
7 n.value Hi! int c a
Node m n a 3 n.value Bye!
27NODES
- ?! Why?!
- Java stores PRIMITIVE types (int, double, etc)
values directly, but only stores a REFERENCE (a
location in the computers memory) for objects
and arrays.
28NODES
int a 5 //a gets its own memory
//location which contains 5 int c a //c gets
its own memory location //and stores
whats in as memory location //but
directly (stores 5) a 3 //as location now
contains 3
29NODES
Node n new Node() //creates reference
//to some location in memory n.value
Hi! //references location
//to String Hi! Node m n //creates
reference to //SAME memory location
as n n.value Bye! //reference now points
//to String Bye! for m and n
30NODES
Node n new Node() System.out.println(n)
//prints something like
//Node_at_4a5ab2 Node m n System.out.println(m)
//also Node_at_4a5ab2 m new Node() System.out.prin
tln(m) //now Node_at_1888759 int a new
int3 System.out.println(a) //prints something
like //I_at_12b6651