Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review of Previous Lesson

Similar presentations


Presentation on theme: "Review of Previous Lesson"— Presentation transcript:

1 Review of Previous Lesson
Thursday, 16/08/2018 Review of Previous Lesson State as many Vocabulary words and Learning Objectives that you remember from the last lesson as you can. Remember to grade yourself from

2 Object Orientated Programming Paradigm (OOP)
Thursday, 16/08/2018 Object Orientated Programming Paradigm (OOP) Lists & ArrayLists

3 Language Features and other Testable Topics
07/04/2019 Tested in the AP CS A Exam Notes Not tested in the AP CS A Exam, but potentially relevant/useful Standard Java Library List<E>, ArrayList<E> 17, 18

4 Language Features and other Testable Topics
Thursday, 16/08/2018 Notes: 17. The use of generic collection classes and interfaces is in the AP Java subset, but students need not implement generic classes or methods. 18. Students are expected to know a subset of the constants and methods of the listed Standard Java Library classes and interfaces. Those constants and methods are enumerated in the Java Quick Reference (Appendix B).

5 Appendix B Exam Appendix ---- Java Quick Reference
Thursday, 16/08/2018 Appendix B Exam Appendix ---- Java Quick Reference interface java.util.List<E> int size() boolean add(E obj) // appends obj to end of list; returns true void add(int index, E obj) // inserts obj at position index (0 <= index <= size), // moving elements at position index and higher // to the right (adds 1 to their indices) and adjusts size E get(int index) E set(int index, E obj) // replaces the element at position index with obj // returns the element formerly at the specified position E remove(int index) // removes element from position index, moving elements // at position index + 1 and higher to the left // (subtracts 1 from their indices) and adjusts size class java.util.ArrayList<E> implements java.util.List<E>

6 ? Interfaces Java has single inheritance, only.
Thursday, 16/08/2018 Interfaces ? Java has single inheritance, only. This means that a child class inherits from only one parent class. Usually this is all you need. But sometimes multiple inheritance would be convenient, where a child class inherits characteristics from several parent classes. But this can be confusing. What happens when two parents have different versions of the same method? Interfaces give Java some of the advantages of multiple inheritance without the disadvantages. 3 slides

7 Thursday, 16/08/2018 Interfaces ? With object oriented programming, the idea is to define software objects that mimic "real world" objects. This is supposed to make programs easier to think about and more reliable. In the real world, objects are often thought about in several different ways. e.g. A car is a both a vehicle and a taxable property. It would be convenient if software objects, also, could be thought of in several ways. But a Java object belongs to just one class (single inheritance).

8 Thursday, 16/08/2018 An Interface ? Describes aspects of a class other than those that it inherits from its parent. Is a set of requirements that the class must implement (through code, not through inheritance). A list of constants and method headers. The methods are not implemented in the interface (there is no method body). A class that implements an interface must implement each of the methods listed in the interface.

9 List<E> Interface
Thursday, 16/08/2018 List<E> Interface Captures the idea of a list without saying how the idea is implemented. Making lists in everyday life is a common activity, so it is in programming too. There are no constants in List<E>, but there are many methods. A list is an ordered collection of elements. Elements in a list can be accessed by their index, as with an array. There are methods to add elements, get elements, remove elements, and search for elements (and many other methods). Null values in a List are still considered data, Lists use another ‘unspecified’ way of keeping track of empty cells (usually represented in diagrams as ‘X’). Most implementations of List<E> allow duplicate elements.

10 Summary List<E> Interface
Thursday, 16/08/2018 E = reference to object of type E or of its subclasses. The List must have been constructed to hold references to that type. Method What it Does int size() Returns the current size of the list. boolean add(E obj) Appends obj of type E to the end of the list. Returns true if the list has changed (i.e. the addition was successful). As with all methods which return a value, the return value doesn’t have to be actually used, if required then var = list.add(E obj) otherwise just list.add(E obj). However, as AP CS only deals with ArrayLists which will always allow the addition of an element (see later), this method will always return true for Arraylists, making this return value irrelevant for AP CS. void add(int index, E obj) Inserts element obj at the specified index. index must >= 0 & <= size otherwise an IndexOutOfBoundsException is thrown. Moves elements at position index and higher to the right (adds 1 to their indices) and adjusts size.

11 Summary List<E> Interface
Thursday, 16/08/2018 Method What it Does E get(int index) Returns the reference in the cell at index. If the cell is empty, an IndexOutOfBoundsException will be thrown. E set(int index, E obj) Replaces the element at position index with the specified obj. Returns the element formerly at the specified position but it seems this is rarely used in AP CS. E remove(int index) Removes element at the specified index. Moves elements at position index + 1 and higher to the left (subtracts 1 from their indices) and adjusts size. Returns the element formerly at the specified position so this can be used to remove an element from 1 list & add it to another .

12 Summary List<E> Interface - not in the AP subset
Thursday, 16/08/2018 Summary List<E> Interface - not in the AP subset Method What it Does void clear() Remove all elements, so the list is now empty. int indexOf(Object elt) Searches for the first occurrence of elt, testing for equality using the equals(Object) method. boolean isEmpty() tests if the list is empty Names Program

13 ArrayList<E> Thursday, 16/08/2018
Implements the List<E> Interface. Works much like an array with extra methods and features. Advantages: The size of an ArrayList is not fixed it expands as items are added to it and no information is lost. Disadvantages: The elements of an ArrayList must be object references, not primitive data like int or double. How can you use an ArrayList to store ints? You can encapsulate integers in Integer objects to store them in an ArrayList. ArrayList operations are slightly slower than array operations. If null is placed in a cell, the cell is regarded as containing data, ArrayLists actually use an ‘unspecified’ means to keep track of empty cells (usually represented in diagrams with an ‘X’). ?

14 Corresponding Wrapper class
Wrapper Classes 07/04/2019 Primitive type Corresponding Wrapper class boolean Boolean byte Byte char Character float Float int Integer long Long short Short double Double ? Note that primitives consist of lower case letters only, objects start with a capital letter.

15 ArrayList<E> Thursday, 16/08/2018
Has an initial capacity of 10 cells (capacity will increase as needed as references are added to the list). This may not be very efficient as expanding the capacity of an ArrayList is ‘slow’ and JRE will greatly increase the capacity of the list, if it becomes full, so that many more elements can be added (probably resulting in a lot of wasted memory – see next slide for more details). To avoid this, estimate how many elements are needed and construct an ArrayList of that many plus some extra.

16 Using ArrayLists Thursday, 16/08/2018
A program must import the java.util package to use ArrayList. import java.util.* ; /*Note that the * indicates ‘everything’ in the util package, including the scanner. If you need to import more than one thing from a package, it is useful to do this.*/

17 Using ArrayLists Thursday, 16/08/2018
ArrayLists are generic types which basically means that: It only holds objects (as mentioned earlier). You state the type the object will hold inside angle brackets like this: <DataType>

18 To create an ArrayList:
Thursday, 16/08/2018 To create an ArrayList: In one line: ArrayList<DataType> ArrayListName = new ArrayList<DataType>(); e.g. //To create an ArrayList of String references. ArrayList<String> names = new ArrayList<String>();

19 To create an ArrayList:
Thursday, 16/08/2018 In 2 separate lines: 1. Declare a reference variable for an ArrayList. ArrayList<DataType> ArrayListName; Declares that ArrayListName is a reference to a future ArrayList object. The future ArrayList object will contain an array of references to objects of type E or to a descendant class of E. 2 - Later on in your code, actually construct an ArrayList). ArrayListName = new ArrayList<DataType>(int initialCapacity); e.g. ArrayList<DataType> names; …. names = new ArrayList<DataType>(); If left blank then the ArrayList will have a default initial capacity of 10 cells (capacity will increase as needed as references are added to the list). See next slide for more details.

20 ArrayList Capacity Thursday, 16/08/2018
Has an initial capacity of 10 cells (capacity will increase as needed as references are added to the list). This is not efficient as expanding the capacity of an ArrayList is ‘slow’ and the JRE will greatly increase the capacity of the list, if it becomes full, so that many more elements can be added (resulting in a lot of wasted memory – see next slide for more details). To avoid this, estimate how many elements are needed and construct an ArrayList of that many plus some extra. Declare and construct an ArrayList for the following situation: We keep data about each student in a Student object and need to declare and construct an ArrayList to keep track of the students in a course which usually has about 25 students (a few students may be added the class and a few students may drop the class). ArrayList<Student> students = new ArrayList<Student>( 30 ); ?

21 ArrayList<E> Thursday, 16/08/2018
An ArrayList object has a capacity and a size. The capacity is the total number of cells. Remains unchanged until the ArrayList is full. When an element is added to a full list, the JRE will greatly increase the capacity of the list so that many more elements can be added. The size is the number of cells that have data in them. Increases by one each time an element is added. Cells 0 up through size-1 have data in them. Data are added in order, before cell N gets data, cells 0, 1, 2, ... N-1 must hold data. What do you think this effectively means? What cannot happen? Meaning that empty cells are not allowed between the elements of an ArrayList. ?

22 Thursday, 16/08/2018 ArrayList<E> What is the capacity of the ArrayList in the picture? 10 What is its size? 3 ? ?

23 Using ArrayLists Thursday, 16/08/2018
To use the methods described earlier: ArrayListName.method e.g. names.add(“Mary”); // Adds the string “Mary” to the ArrayList ‘names’.

24 Thursday, 16/08/2018 ArrayList<E> Here is a declaration and construction of a ArrayList: ArrayList<String> data = new ArrayList<String>(10); Which of the following statements will work? data.add( "Irene Adler" ); // OK data.add( new String("Laura Lyons") ); // OK data.add( 221 ); // wrong type of argument data.add( new Integer( 221 ) ); // wrong type of argument ? ? ? ?

25 Autoboxing & Unboxing Would these statements work in a program?
Thursday, 16/08/2018 Autoboxing & Unboxing Would these statements work in a program? ArrayList<Integer> data = new ArrayList<Integer>(); data.add( 44 ); You would not expect the 2nd statement to work, since it seems to be adding primitive data directly into the list of object references. However, as Javac expects an object reference, when it sees the 2nd statement it automatically does the equivalent of this: data.add( new Integer(44) ); // autoboxing Unboxing works the other direction. int sum = 24 + data.get(1) ; /* The int inside a wrapper object is automatically extracted if an expression requires an int. So JAVAC automatically extracts the int in cell 1 of data and adds it to the primitive int 24 */ ? Think of wrapping up the data with an object, like wrapping up a gift (& unboxing like unwrapping a gift).

26 07/04/2019 Write your own programs: Write your own programs from “scratch”. Of course you should use previous programs for reference, but write your code from “scratch” (do not copy and paste).

27 Names Write a program to create two ArrayList of names. 07/04/2019
Print one ArrayList ‘s size before and after populating it with a few names. Print out the names in the ArrayList. Make sure all methods listed on slides are used. When removing names from the list, sometimes add them to the other ArrayList.

28 Names Answer these questions in your comments: Thursday, 16/08/2018
Say that you want a list of int values. But an ArrayList can contain only object references. How can you use an ArrayList to store your ints? Examine the following: ArrayList<Integer> values = new ArrayList<Integer>(); What type of data will values hold? Suppose that the following follows the first three add statements: names.add("Daniel"); Where will the reference to "Daniel" be added? What is the only restriction when adding items to an ArrayList?

29 Names Answer these questions in your comments: Thursday, 16/08/2018
a) What is the capacity of the ArrayList in the picture? b) What is its size? Here is a declaration and construction of a ArrayList: ArrayList<String> data = new ArrayList<String>(10); Which of the following statements will work? Also if not, why not? data.add( "Irene Adler" ); data.add( new String("Laura Lyons") ); data.add( 221 ); data.add( new Integer( 221 ) );

30 Names Answer these questions in your comments: Thursday, 16/08/2018
What is the index of the first cell of a list? What does null mean in an array? How is this different to what it means in a list?

31 Names Thursday, 16/08/2018 Answer these questions in your comments:
Say that you want a list of int values. But an ArrayList can contain only object references. How can you use an ArrayList to store your ints? You can encapsulate your integers in Integer objects and store them in an ArrayList. Examine the following: ArrayList<Integer> values = new ArrayList<Integer>(); What type of data will values hold? The list will hold references to Integer objects. Suppose that the following follows the first three add statements: names.add("Daniel"); Where will the reference to "Daniel" be added? A reference to "Daniel" will be added to cell index 3 of the arraylist (right after the reference to the 3rd name). What is the only restriction when adding items to an ArrayList? You are not allowed to create gaps between elements and an index of bounds exception will occur if an attempt is made to do so.

32 Names Answer these questions in your comments: Thursday, 16/08/2018
a) What is the capacity of the ArrayList in the picture? 10 b) What is its size? 3 Here is a declaration and construction of a ArrayList: ArrayList<String> data = new ArrayList<String>(10); Which of the following statements will work? Also if not, why not? data.add( "Irene Adler" ); // OK data.add( new String("Laura Lyons") ); // OK data.add( 221 ); // wrong type of argument data.add( new Integer( 221 ) ); // wrong type of argument

33 Names Answer these questions in your comments: Thursday, 16/08/2018
What is the index of the first cell of a list? 0, just as with an array. What does null mean in an array? How is this different to what it means in a list? Null in an array is considered as ‘no data’ but null in a list is regarded as data.

34 Integers Write a program to create an ArrayList of whole numbers.
07/04/2019 Integers Write a program to create an ArrayList of whole numbers. Populate and print out the numbers in the ArrayList. Use ‘new Integer(1)’, etc… Can you write ‘ArrayList.add(44)’? How come?

35 Rewrite previous programs
Thursday, 16/08/2018 Rewrite previous programs That use Arrays so that they now use ArrayLists. Make copies, do not delete the previous programs. To fully use all ArrayList method, you must add a ‘delete’ / remove ability to your program. This is something that is rarely done with arrays as it not easy to code (although it can be done – challenge: How may it be done in an array?).

36 4/7/2019 Grade yourself Grade yourself on the vocabulary and learning objectives of the presentation.


Download ppt "Review of Previous Lesson"

Similar presentations


Ads by Google