More on Classes - PowerPoint PPT Presentation

About This Presentation
Title:

More on Classes

Description:

More on Classes & Arrays Pepper With help from http://docs.oracle.com/javase/tutorial/java/IandI/super.html What's wrong with the following program? public class ... – PowerPoint PPT presentation

Number of Views:153
Avg rating:3.0/5.0
Slides: 12
Provided by: adel159
Learn more at: https://home.adelphi.edu
Category:
Tags: classes | more

less

Transcript and Presenter's Notes

Title: More on Classes


1
More on Classes Arrays
  • Pepper
  • With help from
  • http//docs.oracle.com/javase/tutorial/java/IandI/
    super.html

2
Review - A Class Trace Exercise
  • What's wrong with the following program?
  • public class SomethingIsWrong public
    static void main(String args)
    Rectangle myRect myRect.width
    40 myRect.height 50
    System.out.println ("myRect's
    area is " myRect.area())
  • http//docs.oracle.com/javase/tutorial/java/javaOO
    /QandE/objects-questions.html

3
Class Extension
  • Make sub-classes (put "extends " classname after
    class)
  • http//home.adelphi.edu/pe16132/csc171/notes/fang
    stuff/Wackadot.java
  • Can use the entire extended class
  • Can override methods
  • This is how we use Fang's game class
  • Can see all protected and public methods and
    variables

4
Class Extension - Super
  • Can choose to execute methods from the super
    class using super.methodname
  • http//docs.oracle.com/javase/tutorial/java/IandI/
    super.html

5
Player Extension
  • Make one player
  • Extend to different player types
  • Example
  • http//home.adelphi.edu/pe16132/csc171/notes.html

6
Abstraction
  • If you know you will make a lower class, you
    don't have to implement all methods
  • You can mark a class as abstract (public abstract
    class) and then put only the method headers.
    Compiler complains if the lower class does not
    implement it.
  • http//docs.oracle.com/javase/tutorial/java/IandI/
    abstract.html

7
Interface
  • Can create a type that has only abstract methods.
  • Basically a contract to implement interfaces
  • Contains method headers with no code below it.
  • Lower classes can implement many interfaces (but
    only extend one class)
  • Implementing an interface means you promise (and
    the compiler checks) that you will implement the
    methods listed.
  • http//docs.oracle.com/javase/tutorial/java/IandI/
    interfaceDef.html

8
More on Arrays For Each
  • Shortcut for loop through an array
  • for (variable to hold value array name)
  • Ex
  • int arr 1,2,3,4int tot 0for (int x
    arr) tot tot xSystem.out.println(tot)

9
ArrayList
  • An Array class
  • Has methods you can use
  • Can make an array of anything using lttypegt
  • ArrayListltStringgt myArr new ArrayListltStringgt()
  • ArrayListltPlayergt guys new ArrayListltPlayergt()
  • http//www.anyexample.com/programming/java/java_ar
    raylist_example.xml

10
ArrayList Methods
  • Methods
  • add( Object o ) - puts reference to object into
    ArrayList
  • get( int index ) - retrieves object reference
    from ArrayList index position
  • size() - returns ArrayList size
  • remove( int index ) - removes the element at the
    specified position in this list. Shifts any
    subsequent elements to the left and returns the
    element that was removed from the list.
  • indexOf( Object o) - finds the index in this list
    of the first occurrence of the specified element
  • clear() - removes all of the elements

11
ArrayList of Players
  • guys.add(new Player("Amy"))guys.add(new
    Player("Jim"))
  • guys.
Write a Comment
User Comments (0)
About PowerShow.com