Presentation is loading. Please wait.

Presentation is loading. Please wait.

COS 260 DAY 11 Tony Gauvin.

Similar presentations


Presentation on theme: "COS 260 DAY 11 Tony Gauvin."— Presentation transcript:

1 COS 260 DAY 11 Tony Gauvin

2 Agenda Questions Assignment 2 Not corrected yet Will be done soon Assignment 3 posted Due Oct 19 Next mini quiz (5) will be on chapter 5 & 6 on Friday the 13TH after break More Sophisticated Behaviors –Continued

3 Capstone Project Capstone Project Description Fall 2017.pdf
October Proposal Due ed to Tony in Blackboard October 27 Progress Report ed to Tony in Blackboard November 16 Progress Report ed to Tony in Blackboard November Progress Report ed to Tony in Blackboard December All Deliverables & Presentation Due Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

4 Objects First with Java
Assignment 3 Beginning with this assignment make sure all of your code is properly documented as shown in section 6.11 of the textbook (pages ). Proper documentation will be 10% of the grade for this and future assignments.    Problem 1  (30 points)      Complete exercises 5.29 on page 197.  Zip the modified music-organizer-v5 directory, name the zip file "problem1" and upload. Make sure you update the documentation to reflect the changes to the music-organizer-v5 project. Problem 2 (40 points)     Complete exercises 6.62, , 6.65, and 6.67 on page 237 & 238  of the textbook.  For exorcise 6.67,  draw a Jack O Lantern (carved pumpkin) with the words "Happy Halloween" somewhere on the canvas.  Save the results of these five exercises to a new project called scribble-ver1, Zip the project directory, name the zip file "problem2" and upload. Make sure you update the documentation to reflect the changes to the scribble-ver1  project.  Problem 3 (30 points)     Complete exercises  6.68, 6.70, 6.71 and 6.72 on page 239.   Save the results of these three exercises to a new project called bouncing-balls-ver1, Zip the project directory, name the zip file "problem3" and upload. Make sure you update the documentation to reflect the changes to the bouncing-balls-ver1 project.  Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

5 More-Sophisticated Behavior
Objects First with Java More-Sophisticated Behavior Using library classes to implement some more advanced functionality 6.0 © David J. Barnes and Michael Kölling

6 Further library classes
Objects First with Java Further library classes Using library classes to implement some more advanced functionality © David J. Barnes and Michael Kölling

7 Main concepts to be covered
Objects First with Java Main concepts to be covered Further library classes: Set – avoiding duplicates Map – creating associations Writing documentation: javadoc © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

8 Basic Data Structures Set  an unordered collection with no duplicates
List  an ordered collection with the possibility of duplicates Map  a collections of key/value pairs. Keys must be unique, values may be null Hash  any (one way) function that can be used to map data of arbitrary size to data of sized size Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

9 Objects First with Java
Using sets import java.util.HashSet; ... HashSet<String> mySet = new HashSet<>(); mySet.add("one"); mySet.add("two"); mySet.add("three"); for(String element : mySet) { do something with element } Compare with code for an ArrayList! © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

10 Objects First with Java
Tokenising Strings public HashSet<String> getInput() { System.out.print("> "); String inputLine = reader.nextLine().trim().toLowerCase(); String[] wordArray = inputLine.split(" "); HashSet<String> words = new HashSet<String>(); for(String word : wordArray) { words.add(word); } return words; © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

11 Objects First with Java
Maps Maps are collections that contain pairs of objects. Parameterized with two types. Pairs consist of a key and a value. Lookup works by supplying a key, and retrieving a value. Example: a contacts list. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

12 Objects First with Java
Using maps A map with strings as keys and values :HashMap "Charles Nguyen" "(531) " "Lisa Jones" "(402) " "William H. Smith" "(998) " © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

13 Objects First with Java
Using maps HashMap <String, String> contacts = new HashMap<>(); contacts.put("Charles Nguyen", "(531) "); contacts.put("Lisa Jones", "(402) "); contacts.put("William H. Smith", "(998) "); String number = contacts.get("Lisa Jones"); System.out.println(number); © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

14 HashMap in Responder private HashMap <String, String> responseMap; ... responseMap.put("crash", "Which version is this?"); String response = responseMap.get(word); if(response != null) { } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

15 List, Map and Set Alternative ways to group objects.
Varying implementations available: ArrayList, LinkedList HashSet, TreeSet But HashMap is unrelated to HashSet, despite similar names. The second word reveals organizational relatedness. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

16 Collections and primitive types
Objects First with Java Collections and primitive types The generic collection classes can be used with all class types ... … but what about the primitive types: int, boolean, etc.? Suppose we want an ArrayList of int? © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

17 Objects First with Java
Wrapper classes Primitive types are not objects types. Primitive-type values must be wrapped in objects to be stored in a collection! Wrapper classes exist for all primitive types: simple type wrapper class int Integer float Float char Character Note that there is no simple mapping rule from primitive name to wrapper name! © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

18 Objects First with Java
Wrapper classes wrap the value int i = 18; Integer iwrap = new Integer(i); int value = iwrap.intValue(); unwrap it In practice, autoboxing and unboxing mean we don't often have to do this explicitly © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

19 Autoboxing and unboxing
Objects First with Java Autoboxing and unboxing private ArrayList<Integer> markList; ... public void storeMark(int mark) { markList.add(mark); } autoboxing int firstMark = markList.remove(0); unboxing © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

20 Writing class documentation
Objects First with Java Writing class documentation Your own classes should be documented the same way library classes are. Other people should be able to use your class without reading the implementation. Make your class a potential 'library class'! © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

21 Elements of documentation
Objects First with Java Elements of documentation Documentation for a class should include: the class name a comment describing the overall purpose and characteristics of the class a version number the authors’ names documentation for each constructor and each method © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

22 Elements of documentation
Objects First with Java Elements of documentation The documentation for each constructor and method should include: the name of the method the return type the parameter names and types a description of the purpose and function of the method a description of each parameter a description of the value returned © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

23 Objects First with Java
javadoc Class comment: /** * The Responder class represents a response * generator object. It is used to generate an * automatic response. * Michael Kölling and David J. Barnes ( ) */ @author is a Javadoc tag © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

24 Objects First with Java
javadoc Method comment: /** * Read a line of text from standard input (the text * terminal), and return it as a set of words. * prompt A prompt to print to screen. A set of strings, where each String is * one of the words typed by the user */ public HashSet<String> getInput(String prompt) { ... } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

25 Objects First with Java
Public vs private Public elements are accessible to objects of other classes: Fields, constructors and methods Fields should not be public. Private elements are accessible only to objects of the same class. Only methods that are intended for other classes should be public. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

26 Objects First with Java
Information hiding Data belonging to one object is hidden from other objects. Know what an object can do, not how it does it. Information hiding increases the level of independence. Independence of modules is important for large systems and maintenance. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

27 Code completion ShortCuts
The BlueJ editor supports lookup of methods. Use Ctrl-space after a method-call dot to bring up a list of available methods. Use Return to select a highlighted method. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

28 Code completion in BlueJ
© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

29 Objects First with Java
Review Java has an extensive class library. A good programmer must be familiar with the library. The documentation tells us what we need to know to use a class (interface). The implementation is hidden (information hiding). We document our classes so that the interface can be read on its own (class comment, method comments). © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

30 Class variables and constants

31 Class variables A class variable is shared between all instances of the class. In fact, it belongs to the class and exists independent of any instances. Designated by the static keyword. Public static variables are accessed via the class name; e.g.: Thermometer.boilingPoint © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

32 Objects First with Java
Class variables © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

33 Constants A variable, once set, can have its value fixed.
Designated by the final keyword. final int SIZE = 10; Final fields must be set in their declaration or the constructor. Combing static and final is common. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

34 Objects First with Java
Class constants static: class variable final: constant private static final int gravity = 3; Public visibility is less of an issue with final fields. Upper-case names often used for class constants: public static final int BOILING_POINT = 100; © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

35 Class methods A static method belongs to its class rather than the instances: public static int getDaysThisMonth() Static methods are invoked via their class name: int days = Calendar.getDaysThisMonth(); © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

36 Limitations of class methods
A static method exists independent of any instances. Therefore: They cannot access instance fields within their class. They cannot call instance methods within their class. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

37 Learning from Interfaces
Scribble Examine Documents Examine Draw demo Examine Pen Examine Canvas Make smiley-face Bouncing Ball Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

38 Review Class variables belong to their class rather than its instances. Class methods belong to their class rather than its instances. Class variables are used to share data among instances. Class methods are prohibited from accessing instance variables and methods. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

39 Review The values of final variables are fixed.
They must be assigned at declaration or in the constructor (for fields). final and static are unrelated concepts, but they are often used together. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.


Download ppt "COS 260 DAY 11 Tony Gauvin."

Similar presentations


Ads by Google