CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.

Slides:



Advertisements
Similar presentations
Comp 212: Intermediate Programming Lecture 18 – Java Generics By: Anupam Chanda.
Advertisements

Generics and the ArrayList Class
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Programming With Java ICS201 1 Chapter 14 Generics and The ArrayList Class.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Notices Assn 2 is due Friday, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including Thursday’s lecture: Big Topics.
Winter 2006CISC121 - Prof. McLeod1 Stuff Midterm exam in JEF234 on March 9th from 7- 9pm.
CMSC 202 ArrayList Aug 9, 2007.
Java Generics.
Sixth Lecture ArrayList Abstract Class and Interface
Java Generics.
Chapter 20 Generic Classes and Methods
COP 3503 FALL 2012 Shayan Javed Lecture 8
CISC124 Labs start this week in JEFF 155: Meet your TA.
Generics, Lambdas, Reflections
Fall 2017 CISC124 9/18/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Winter 2018 CMPE212 9/21/2018 CMPE212 – Stuff…
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Welcome to CSE 143!.
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Winter 2018 CMPE212 11/12/2018 CMPE212 – Stuff…
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Quiz 1 marking nears completion!
Fall 2018 CISC124 12/1/2018 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until.
CMSC 202 ArrayList Aug 9, 2007.
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Generic programming in Java
Arrays and Collections
Dynamic Data Structures and Generics
Comp 249 Programming Methodology
Object Oriented Programming in java
CISC124 Assignment 4 on Inheritance due today at 7pm.
CMSC 202 ArrayList Aug 9, 2007.
CISC/CMPE320 - Prof. McLeod
CISC124 Assignment 3 due tomorrow at 7pm.
CISC124 Assignment 4 on Inheritance due next Friday.
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
CISC124 TA names and s have been added to the course web site.
Comp 212: Intermediate Programming Lecture 18 – Java Generics
CISC124 Labs start this week in JEFF 155. Fall 2018
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
CMSC 202 Generics.
Dynamic Data Structures and Generics
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
CMPE212 – Reminders Course Web Site:
Review: libraries and packages
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Winter 2019 CMPE212 5/3/2019 CMPE212 – Reminders
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
Chapter 19 Generics.
CISC101 Reminders Assignment 3 due today.
CMPE212 – Reminders Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Presentation transcript:

CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm. Fall 2018 CISC124 11/24/2018 CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

Today Generics in Java: The ArrayList<T> class in java.util Building our own Generic Classes and Methods. Generic or Object? Fall 2018 CISC124 - Prof. McLeod

The ArrayList<T> Class Fall 2013 CISC124 The ArrayList<T> Class This is a generic data structure class. The ArrayList<T> class resides in the java.util package. Stores and returns Objects of type T. (Referred to as “ArrayList<E>” in the API – same difference…) You can use the class without specifying a size. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

ArrayList<T> Class Syntax to create an ArrayList<T>: ArrayList<type> list_name = new ArrayList<type>(); type must be an Object, it cannot be a primitive type, but can be an array type. For example: ArrayList<Double> myList = new ArrayList<Double>(); You can supply an initial size to the constructor, if you wish. Fall 2018 CISC124 - Prof. McLeod

ArrayList<T> Class, Cont. In newer versions of Java you do not have to specify the type twice. So, you can do: ArrayList<type> list_name = new ArrayList<>(); For example: ArrayList<Double> myList = new ArrayList<>(); The empty <> is called the “diamond”… This is an aspect of what is called “type inference”. Fall 2018 CISC124 - Prof. McLeod

ArrayList<T> Class, Cont. To add elements to the collection, for example: myList.add(new Double(456.78)); Or, myList.add(456.78); // Automatic Boxing To get the size of the collection, invoke the size() method: int myListSize = myList.size(); Fall 2018 CISC124 - Prof. McLeod

ArrayList Class<T>, Cont. The get(index) method returns the element of type T at the given index. Note that index positions are numbered from zero (of course!). The set(position, new_value) method changes the element at the given position. To insert an element, invoke add(position, new_value). All elements are moved up, and the new value is added at the given position. The method, remove(position) removes the element at the provided position. Fall 2018 CISC124 - Prof. McLeod

ArrayList Class<T>, Cont. If you are done adding elements to the ArrayList, invoke trimToSize() to remove empty element locations. Also, clone() does not work properly because it does not return an independent, un-aliased copy. The objects in the collection will still be aliased. Fall 2018 CISC124 - Prof. McLeod

ArrayList Class<T>, Cont. toArray() returns the underlying array of type T[]. Can also be invoked as toArray(tArray): Supply tArray which is an instantiated array of type T. The size will be increased if necessary, so just use a size of zero. For example, if T is String then invoke as in: arrayList.toArray(new String[0]) This method returns the array String[] of the exact size needed to hold all the elements in the arrayList. Fall 2018 CISC124 - Prof. McLeod

ArrayList Class<T>, Cont. Once you have accessed an element of an ArrayList<T>, you do not have to cast it back to type T, it is already of the correct type. For example, to get the number back out of the first element in myList: double aVal = myList.get(0); Also uses automatic unboxing. This is an advantage over using a collection like Object[], for example. Fall 2018 CISC124 - Prof. McLeod

ArrayList Class<T>, Cont. Final note: What is the underlying data structure for an ArrayList and how do you find out? Do you use the ArrayList<T> class for speed or for convenience? Fall 2018 CISC124 - Prof. McLeod

Your Own Generic Classes Fall 2013 CISC124 Your Own Generic Classes Also called “Parameterized Classes” - kind of like having a parameter in the class header - except it only specifies a type and does not pass a reference, like a parameter does. See the next slide for an example: Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

Your Own Generic Classes, Example public class Sample<T> { private T data; public void setData(T newData) { data = newData.clone(); } public T getData () { return data; } // end Sample<T> Fall 2018 CISC124 - Prof. McLeod

Your Own Generic Classes, Example - Cont. Using the Sample generic class: Sample<Integer> num1 = new Sample<Integer>(); num1.setData(new Integer(45)); System.out.println(num1.getData().intValue()); Prints out 45. Fall 2018 CISC124 - Prof. McLeod

Your Own Generic Classes, Example - Cont. Taking advantage of automatic boxing and un-boxing, as well as the “diamond”: Sample<Integer> num2 = new Sample<>(); num2.setData(67); System.out.println(num2.getData()); Prints out 67. Fall 2018 CISC124 - Prof. McLeod

Your Own Generic Classes, Example - Cont. Any object type can be used for “T”: Sample<String> string1 = new Sample<>(); string1.setData("Hello!"); System.out.println(string1.getData()); Prints out Hello!. Fall 2018 CISC124 - Prof. McLeod

Generic Classes, Limitations for T You can use an array type, so “<int[]>” would be legal. You cannot specify primitive types, so “<int>” would not be legal. Neither can you use Exception classes - (Why would you want to?) You can use interface and abstract types. Fall 2018 CISC124 - Prof. McLeod

Your Own Generic Classes, Constructor A constructor for our Sample<T> class: public Sample (T newData) { data = newData.clone(); } Note that there is no use of <T> in the constructor. Fall 2018 CISC124 - Prof. McLeod

Your Own Generic Classes, Constructor, Cont. Using the constructor and automatic boxing: Sample<Double> num3 = new Sample<>(56.7); System.out.println(num3.getData()); Fall 2018 CISC124 - Prof. McLeod

Your Own Generic Classes, Notes You can specify any number of type parameters: public class Sample2<T1, T2, T3> {… Fall 2018 CISC124 - Prof. McLeod

Your Own Generic Classes, Bounding You can also “bound” the parameters by specifying a root class: public class Sample3<T extends RootClass> So, only classes that extend RootClass, or RootClass itself, can be used for T, in this example. You can specify an upper bound for multiple classes and/or interfaces: public class Sample3<T extends A & B & C> Fall 2018 CISC124 - Prof. McLeod

Your Own Generic Classes, Bounding The extends keyword allows you to specify an “upper” bound. You could use the “super” keyword to specify a “lower” bound: public class Sample3<T super Integer> In this case T could be Integer, Number or Object. Fall 2018 CISC124 - Prof. McLeod

Your Own Generic Classes, Bounding Why bother? Upper bounding is like implementing an interface – it will ensure that the object T will have the methods in the class you are extending. For example <T extends Comparable<T>> will ensure that the type used for T will have a .compareTo() method. Fall 2018 CISC124 - Prof. McLeod

Your Own Generic Methods Fall 2013 CISC124 Your Own Generic Methods These can be defined in any class, including non-Generic classes. For example in the class Utility: public static <T> T getMidpoint(T[] a) { return a[a.length/2]; } To invoke: String mid = Utility.<String>getMidpoint(b); // b is some array of String’s Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

Your Own Generic Methods, Cont. The “<T>” has to come before any use of T as a type in the method header. When invoking a generic method in newer Java versions you can ignore the <…> in the invocation: String mid = Utility.getMidpoint(b); // b is some array of String’s This is another example of “type inference”. The type for T is inferred by looking at the type used in the expression. Fall 2018 CISC124 - Prof. McLeod

Your Own Generic Methods, Cont. How else could you write getMidpoint? public static Object getOMidpoint(Object[] objArray) { return objArray[objArray.length / 2]; } // end getOMidpoint See Utility.java and TestUtility.java. Are generic methods better? Fall 2018 CISC124 - Prof. McLeod

Why Generic Methods? In the example (strings is of type String[]): String mid = (String)Utility.getOMidpoint(strings); Suppose you wrote the following by mistake: Integer midI = (Integer)Utility.getOMidpoint(strings); You will not get a compilation error, but a “ClassCastException” will crash your program! Fall 2018 CISC124 - Prof. McLeod

Why Generic Methods?, Cont. On the other hand: Integer midI = Utility.<String>getMidpoint(b); Gives you a compilation error and will not run. The compiler is enforcing type safety, which it cannot do when you are just using Objects. This advantage applies to any generic method or class. Fall 2018 CISC124 - Prof. McLeod

Generic Methods and Java ≥ 7 Constructors can also be generic. Even in a non-generic class. And you can use type inference as demonstrated in TestUtility.java: midStrings = Utility.getMidpoint(strings); Same as: midStrings = Utility.<String>getMidpoint(strings); Fall 2018 CISC124 - Prof. McLeod

Generics & Backwards Compatibility Generic code can make for very versatile programs that can be applied to a range of objects – saving you a great deal of boring coding work. But, generic code will not work easily with pre-Java 5.0 code. And, of course, the Java 7 type inference stunts will not work with pre-Java 7.0 code. Generics are a bit like Templates in C++ and C#, but are implemented in a different way. Fall 2018 CISC124 - Prof. McLeod