Chapters (ArrayList / Generic)

Slides:



Advertisements
Similar presentations
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Advertisements

Java Review Interface, Casting, Generics, Iterator.
Generics and The ArrayList Class
Generic programming in Java
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 17 – Generic Programming.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 18 – Generic Classes.
Generics. DCS – SWC 2 Generics In many situations, we want a certain functionality to work for a variety of types Typical example: we want to be able.
Java Generics.
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
Inheritance Review CSC 171 FALL 2004 LECTURE 19. READING Read Horstmann, Chapter 11 Look at Chapter 12 – Will not be on MT or Final – One lab Begin Reading.
CPSC150 Abstract Classes and Interfaces Chapter 10.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
1 Chapter 21 Generics. 2 Objectives F To know the benefits of generics (§21.1). F To use generic classes and interfaces (§21.2). F To declare generic.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Abstract Classes and Interfaces
CS221 - Computer Science II Polymorphism 1. CS221 - Computer Science II Polymorphism 2 Outline  Explanation of polymorphism.  Using polymorphism to.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Generics.
Combo Box, Check Boxes, and Radio Buttons. Radio Buttons User can click radio buttons, just like other buttons BUT Radio buttons are mutually exclusive.
Using Inheritance to Customize Frames Use inheritance for complex frames to make programs easier to understand Design a subclass of JFrame Store the components.
Generics In Java 1.5 By Manjunath Beeraladinni. Generics ➲ New feature in JDK1.5. ➲ Generic allow to abstract over types. ➲ Generics make the code clearer.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
1-1 Generic Types in Java Format for a generic (parameterized) type and instantiation of a generic type.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
1 Why layout managers Can we perform layout without them? –Yes. A container’s layout property can be set to null. Absolute positioning: specify size and.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Programming With Java ICS201 1 Chapter 14 Generics and The ArrayList Class.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 Generics Chapter 21 Liang, Introduction to Java Programming.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
GUI Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
Introduction to Generics
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
COMP 121 Week 8: Generic Collections. Objectives To understand type variables and how they are used in generic programming To be able to implement and.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Computer Science 209 GUIs Model/View/Controller Layouts.
Java Generics. It is nice if we could write a single sort method that could sort array of any type of elements: – Integer array, – String array, Solution:
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
GUI Tutorial 1. A Bit of Philosophy on what to Teach  There are numerous libraries, frameworks, options  Modern GUIs are often developed using XML (e.g.,
Chapter 22 Generic Programming. Chapter Goals To understand the objective of generic programming To be able to implement generic classes and methods To.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Chapter 21 Generics. 2 Objectives F To use generic classes and interfaces (§21.2). F To declare generic classes and interfaces (§21.3). F To understand.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
Java Generics.
Sixth Lecture ArrayList Abstract Class and Interface
Christopher Budo, Davis Nygren, spencer franks, Luke miller
Unit 1 Hadoop and big data
“Form Ever Follows Function” Louis Henri Sullivan
CHAPTER 7 & 8 REVIEW QUESTIONS
Chapter 13: Advanced GUIs and Graphics
Generic programming in Java
Chapter 18 – Generic Classes
Generics.
Chapter 19 Generics.
Advanced GUIs and Graphics
Presentation transcript:

Chapters 17-18 (ArrayList / Generic) Big Java Chapters 17-18 (ArrayList / Generic)

Generic Programming The design and implementation of data structures and algorithms that work for multiple types.

Generic Class Uses a type variable to achieve genericity public class ArrayList<E> { public ArrayList() { . . .} public void add(E element) { . . . } } may have one or more Can instantiate type variable with class or interface type, not primitive (use wrapper)

Example ArrayList<BankAccount> accounts1 = new ArrayList<BankAccount>(); LinkedList accounts2 = new LinkedList(); // intended for BankAccounts accounts1.add(“my savings”); // compiler error – WHY ? accounts2.add(“my savings”); // not detected

Names for Type Variables Often use short uppercase names, such as: Type Name Meaning E Element in collection K Key type in map V Value type in map T General type S,U Additional general types

Example – useful return type public class Pair<T, S> { public Pair(T firstElement, S, second Element) first = firstElement; second = second Element } public T getFirst() { return first; } public S getSecond() { return second; } private T first; private S second;

Generic Methods Method with a type variable Useful for methods that differ only by one or more types Can start with method that works on a specific types, then make generic

Generic Method Example public static void print(String[] a) { for (String e : a) System.out.print(e + “ “); System.out.println(); } public static <E> void print(E[] a) for (E e : a) Rectangle[] rectangles = . . .; ArrayUtil.print(rectangles); Methods are not required to be static Method call based on parameters, doesn’t specify E explicitly Assume in ArrayUtil class

Constraining Type Variables actually means extends or implements public static <E extends Comparable> E min(E[] a) { E smallest = a[0]; for (int i=0; i<a.length; i++) if (a[i].compareTo(smallest) < 0) smallest = a[i]; return smallest; } could also do: <E extends Comparable & Cloneable> if have more than one constraint

Genericity and Inheritance If SavingsAccount is a subclass of BankAccount, is ArrayList<SavingsAccount> a subclass of ArrayList<BankAccount>? NO. Necessary for type checking. ArrayList<SavingsAccount> savingsAccounts = new ArrayList<SavingsAccount>(); ArrayList<BankAccount> bankAccounts = savingsAccounts; // Not legal, but if it were… BankAccount myChecking = new CheckingAccount(); BankAccounts.add(myChecking); // just added checking account to savings account list

Wildcard Types – Advanced Topic A wildcard type is a type that can remain unknown. public void addAll(LinkedList<? extends E> other) { ListIterator<E> iter = other.listIterator(); while (iter.hasNext()) add(iter.next()); } Allows you to add any type that is a subtype of E. Read Advanced Topic 17.1 for more details

Raw Types The Java Virtual Machine (JVM) uses raw types in which type variables are replaced with ordinary types The compiler converts generic classes and type variables into regular classes/raw types for the JVM Type variables are replaced with Object, e.g., public Pair(Object firstElement, Object secondElement) { … } Type erasure enables generic classes to interoperate with legacy code (e.g., original ArrayList).

Limitations You cannot use type variables to define static fields, static methods or static inner classes You cannot create a new object of a type variable type (remember it is replaced with Object)

Graphical User Interfaces…

BorderLayout JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.add(component, BorderLayout.NORTH); NORTH WEST CENTER EAST SOUTH

GridLayout JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(4, 3)); buttonPanel.add(button7); buttonPanel.add(button8); … 8 9 4 5 6 1 2 3 0 . CE

Nesting Panels JPanel keypadPanel = new JPanel(); keypadPanel.setLayout(new BorderLayout()); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(4, 3)); buttonPanel.add(button7); buttonPanel.add(button8); … keypadPanel.add(buttonPanel, BorderLayout.CENTER); JTextField display = new JTextField(); keypadPanel.add(display, BorderLayout.NORTH); 7 8 9 4 5 6 1 2 3 0 . CE

Making Choices – Radios, Combos & Checks

Combo Box editable – can type in your own selection

Check Box & Border Many other border types available – see API

Radio Buttons Need ButtonGroup for grouping, only one selected at a time

Control Panel for All Controls It’s easy once the other panels are created! controls are going in the bottom

Controlling the Font access combo box for font name access check boxes to determine bold/italic settings access radio buttons to determine size settings – sometimes separate field is used for this repaint causes changes to be visible

Putting it together Same listener for all components

Reading Assignment How To 18.1 Laying Out a User Interface, pages 799-801 – summarizes how to achieve an interface

Menus

File Menus Creates menu item Creates menu item with listener

Menu and Item

Setting Font Note use of instance variables for size, facename and style

Putting it all Together Create a JMenuBar and add menus to it