The end is near - PowerPoint PPT Presentation

About This Presentation
Title:

The end is near

Description:

The end is near – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 34
Provided by: purdueEdu
Category:

less

Transcript and Presenter's Notes

Title: The end is near


1
The end is near
2
Object-Oriented Design
  • Methods
  • Extract small parts of a program and calculations
    which will be performed multiple times
  • Encapsulation
  • Classes, Objects, and Polymorphism
  • Covered in more detail later

3
Primitives
  • int, double, boolean, float, double, char
  • These are NOT Objects
  • Can be compared with , !, lt, gt, etc.
  • Cannot call methods
  • Each primitive has an associated wrapper class
  • Auto-boxing/unboxing
  • Explicit type-casting needs to be done
  • double?float?long?int?short?byte
  • Implicit type-casting need not be done
  • byte?short?int?long?float?double

4
Objects and Classes and Strings
  • Objects are handled with a reference address
  • Two objects should be compared with the .equals()
    method
  • Do NOT use to compare objects
  • Assignment operators assign references they do
    not make separate copies
  • NullPointerException always make sure your
    object is not null before calling methods with it
  • Constructors should be used to initialize class
    variables
  • Calling methods
  • Need object
  • Static methods
  • Can use class name
  • Cannot access non-static methods/variables inside
  • Pass by value always
  • Keyword this

5
Objects and Classes and Strings
  • Strings are a type of object
  • Also should not be compared with
  • Can be concatenated with the operator
  • Important String functions
  • charAt
  • indexOf
  • substring
  • length

6
Selection Statements
  • Modifies the flow of control of the program
  • if/else construct
  • Must have a boolean condition to check against
  • are important, but not necessary for one line
    statements
  • else branch is optional
  • switch construct
  • Multi-way branch which makes a decision based on
    a char, byte, short, or int
  • default case
  • break statement

7
Repetition Statements
  • for
  • while
  • do-while
  • Pitfalls
  • Off-by-one errors
  • Infinite looping
  • Overflow

8
Arrays
  • Linear collection of data
  • Can hold primitives or Objects, but must all be
    of the same type
  • length tells the number of elements in the array
  • Member, not a method
  • Indexed from 0 to length 1
  • Protect against ArrayIndexOutOfBoundsException
  • Can create multiple dimension arrays
  • Usually use for-loops to iterate through members
    of the array

9
Exceptions
  • Use a try/catch/finally block to handle
    exceptions thrown by a program
  • Use throw statement to notify caller of an error
  • Do not need to catch RunTimeExceptions
  • These should be checked for instead
  • User defined exceptions must extend Exception

10
File I/O
  • Many classes
  • FileInputStream, DataInputStream, FileReader,
    BufferedReader, Scanner, PrintWriter,
    DataOutputStream, etc.
  • ObjectInputStream, ObjectOutputStream
  • Used to write objects to a file
  • Any object serialized by a stream must implement
    Serializable
  • Which classes are used to write text and which
    are used to write binary?
  • Always close files you open

11
Inheritance and Polymorphism
  • Differences between abstract classes and
    interfaces
  • Polymorphism can simplify code
  • Extend specific classes from general classes
  • Use protected keyword to protect information and
    methods
  • No need to rewrite methods which are the same as
    in a parent class
  • Superconstructor is always called as the first
    line of constructor

12
Dynamic Data Structures and Generics
  • Inner classes
  • Lists
  • Node class
  • Circularly linked lists
  • Doubly linked lists
  • Java Collections
  • Vector
  • ArrayList
  • LinkedList

13
Recursion
  • Think about what the sub-problem is
  • Only be concerned with the current level of
    recursion
  • Two necessary cases
  • Base case
  • Recursive case

14
GUI and Event-driven Programming
  • Common classes
  • JFrame
  • JPanel
  • JLabel
  • JMenu, JMenuItem
  • JButton
  • Layouts

15
Challenges
16
What is the output?
  • public class A
  • private int x
  • public static int doStuff()
  • x 100
  • x / 3
  • x
  • return x
  • public static void main(String args)
  • System.out.println(A.doStuff())

17
What is the output?
  • public class A
  • private int x
  • public static int doStuff()
  • x 100
  • x / 3
  • x
  • return x
  • public static void main(String args)
  • System.out.println(A.doStuff())

Because this method is static, it does not
have access to non-static class variables. This
code will Not compile because x is non-static.
18
Types
  • Given the following classes, which of the
    following declarations are valid?
  • public interface I
  • public interface J extends I
  • public interface K
  • public abstract class A
  • public class B extends A implements J, K
  • public class C extends B
  • public class D extends A implements I
  • A a new B()
  • B b new J()
  • C c new B()
  • B b new C()
  • I i new A()
  • I i new B()
  • I i new D()
  • K k new C()

19
Types
  • Given the following classes, which of the
    following declarations are valid?
  • public interface I
  • public interface J extends I
  • public interface K
  • public abstract class A
  • public class B extends A implements J, K
  • public class C extends B
  • public class D extends A implements I
  • A a new B() valid B is a subclass of A
  • B b new J() invalid cannot instantiate
    interfaces
  • C c new B() invalid not all B is the
    superclass of C
  • B b new C() valid C is a subclass of B
  • I i new A() invalid A does not implement I
  • I i new B() valid A implements J, and J is a
    type of I
  • I i new D() valid D impelements I
  • K k new C() valid C extends B which
    implements K

20
Arrays
  • Write a method which takes in an array of
    integers and replaces the values of the array
    with a value ci. Define ci to be the sum of the
    numbers in indices 0i in the incoming array.
  • public void cumulativeArray(int a)

21
Arrays
  • Write a method which takes in an array of
    integers and replaces the values of the array
    with a value ci. Define ci to be the sum of the
    numbers in indices 0i in the incoming array.
  • public void cumulativeArray(int a)
  • if (a.length lt 1)
  • return
  • for (int k1 klta.length k)
  • ak ak ak-1

22
Linked Lists
  • Given an appropriate (integer) Node class, write
    a recursive method which sums up the numbers in
    the list.
  • public int sumList(Node l)

23
Linked Lists
  • Given an appropriate (integer) Node class, write
    a recursive method which sums up the numbers in
    the list.
  • public int sumList(Node l)
  • if (l null)
  • retrurn 0
  • return l.num sumList(l.next)

24
Strings
  • Write a recursive method reverse which takes in a
    String and returns the reverse of the String.
    You may NOT use the reverse method in the String
    class, however you may use other methods
    available to you.
  • public String reverseString(String s)

25
Strings
  • Write a recursive method reverse which takes in a
    String and returns the reverse of the String.
    You may NOT use the reverse method in the String
    class, however you may use other methods
    available to you.
  • public String reverseString(String s)
  • if (s null s.length() lt 1)
  • return s
  • if (s.length() 2)
  • return s.charAt(1) s.charAt(0)
  • return s.charAt(s.length()-1)
    reverseString(s.substring(1, s.length()-1))
  • s.charAt(0)

26
Linked Lists
  • Given an appropriate Node class, write a method
    which removes every other node from the list,
    starting with the second.
  • public Node removeEveryOther(Node l)

27
Linked Lists
  • Given an appropriate Node class, write a method
    which removes every other node from the list,
    starting with the second.
  • public Node removeEveryOther(Node l)
  • Node l1 l
  • while (l1 ! null l1.next ! null)
  • l1.next l1.next.next
  • l1 l1.next
  • return l

28
Linked Lists
  • Now write the same method, but recursively.
  • public Node removeEveryOther(Node l)

29
Linked Lists
  • Now write the same method, but recursively.
  • public Node removeEveryOther(Node l)
  • // base case
  • if (l null l.next null)
  • return l
  • // recursive case
  • Node l1 removeEveryOther(l.next.next)
  • l.next l1
  • return l

30
Recursion
  • Given an appropriate node class, write a
    recursive method which reverses a singly linked
    list.
  • public Node reverse(Node l)

31
Recursion
  • Given an appropriate node class, write a
    recursive method which reverses a singly linked
    list.
  • public Node reverse(Node l)
  • if (l null l.next null)
  • return l
  • Node restreversed reverse(l.next)
  • l.next.next l
  • l.next null
  • return restreversed

32
Linked Lists
  • Given an appropriate Node class, one way to
    detect a cycle in your list is by moving two
    pointers around the list, one one index at a time
    and the other two indices at a time. Write a
    method which determines whether or not there is a
    cycle in a list.
  • public boolean hasCycle(Node l)

33
Linked Lists
  • Given an appropriate Node class, one way to
    detect a cycle in your list is by moving two
    pointers around the list, one one index at a time
    and the other two indices at a time. Write a
    method which determines whether or not there is a
    cycle in a list.
  • public boolean hasCycle(Node l)
  • Node l1 l
  • Node l2 l
  • while (l1 ! null l2 ! null
  • l1.next ! null l2.next ! null
  • l2.next.next ! null)
  • l1 l1.next
  • l2 l2.next.next
  • if (l1 l2)
  • return true
  • return false
Write a Comment
User Comments (0)
About PowerShow.com