COS 260 DAY 11 Tony Gauvin.

Slides:



Advertisements
Similar presentations
More Sophisticated Behaviour 1 Using library classes to implement more advanced functionality.
Advertisements

Fields, Constructors, Methods
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Improving structure with inheritance Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Improving structure with inheritance (Chapters 8 and 9)
More sophisticated behaviour Using library classes to implement some more advanced functionality.
Using Collections. Review of Collections Using an ArrayList It increases its capacity as necessary. It keeps a private count ( size() accessor). It keeps.
More sophisticated behavior Using library classes to implement some more advanced functionality 4.0.
More Sophisticated Behaviour 2 Using library classes to implement more advanced functionality. Also … class ( static ) fields.
More sophisticated behavior Using library classes to implement some more advanced functionality 3.0.
More sophisticated behavior Using library classes to implement some more advanced functionality.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
1 COS 260 DAY 2 Tony Gauvin. 2 Agenda Questions? Class roll call Blackboard Web Resources Objects and classes 1 st Mini quiz on chap1 terms and concepts.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
Classes CS 21a: Introduction to Computing I First Semester,
More sophisticated behavior Using library classes to implement some more advanced functionality 5.0.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming1 Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
More sophisticated behavior Using library classes to implement some more advanced functionality 5.0.
1 COS 260 DAY 10 Tony Gauvin. 2 Agenda Questions? 4 th Mini quiz Today –Chapter 4 Assignment 2 Due Capstone Discussion Proposals Due Oct 15 No class on.
1 COS 260 DAY 14 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz graded  Oct 29 –Chapter 6 Assignment 4 will be posted later Today –First two problems.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
1 COS 260 DAY 12 Tony Gauvin. 2 Agenda Questions? 5 th Mini quiz –Chapter 5 40 min Assignment 3 Due Assignment 4 will be posted later (next week) –If.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
1 COS 260 DAY 9 Tony Gauvin. 2 Agenda Questions? 3 rd Mini quiz –Good results ->All A’s –Threw out question on name overloading 4 th Mini quiz next class.
1 COS 260 DAY 17 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz –Chapter 7 –Password “GoBengals” –40 min Assignment 4 posted –Due Nov 9 (one week) Capstone.
1 COS 260 DAY 22 Tony Gauvin. 2 Agenda Questions? 9 th Mini Quiz corrected –Good results Assignment 5 Not corrected yet Assignment 6 Posted (one more)
1 COS 260 DAY 18 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz Graded –Good results 8 th Mini Quiz –Chap 8  Next class Assignment 4 Due Assignment.
6.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
More-Sophisticated Behavior Using library classes to implement some more advanced functionality 6.0.
5 More sophisticated behavior
BlueJ Chapter 5 More sophisticated behavior: Library classes and documentation.
More-Sophisticated Behavior
More Sophisticated Behavior
3 Introduction to Classes and Objects.
Objects First with Java A Practical Introduction using BlueJ
Objects First with Java Creating cooperating objects
Objects First with Java Introduction to collections
Libraries CITS1001.
Functional Processing of Collections (Advanced)
COS 260 DAY 17 Tony Gauvin.
COS 260 DAY 19 Tony Gauvin.
COS 260 DAY 9 Tony Gauvin.
More sophisticated behavior
COS 260 DAY 13 Tony Gauvin.
Understanding class definitions
COS 260 DAY 6 Tony Gauvin.
Chapter 3 Introduction to Classes, Objects Methods and Strings
COS 260 DAY 19 Tony Gauvin.
COS 260 DAY 11 Tony Gauvin.
COS 260 DAY 10 Tony Gauvin.
COS 260 DAY 2 Tony Gauvin.
COS 260 DAY 18 Tony Gauvin.
COS 260 DAY 8 Tony Gauvin.
Applying OO Concepts Using Java
COS 260 DAY 11 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
COS 260 DAY 14 Tony Gauvin.
COS 260 DAY 4 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
Classes CS 21a: Introduction to Computing I
COS 260 DAY 6 Tony Gauvin.
Presentation transcript:

COS 260 DAY 11 Tony Gauvin

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

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

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 229- 232). 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.63. 6.64, 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

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

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

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

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

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

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

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

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

Objects First with Java Using maps HashMap <String, String> contacts = new HashMap<>(); contacts.put("Charles Nguyen", "(531) 9392 4587"); contacts.put("Lisa Jones", "(402) 4536 4674"); contacts.put("William H. Smith", "(998) 5488 0123"); 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

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.

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.

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

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

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

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

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'! http://www.oracle.com/technetwork/articles/java/index-137868.html © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

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

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

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

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. * * @param prompt A prompt to print to screen. * @return 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

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

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

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.

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

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

Class variables and constants

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.

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

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.

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

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.

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.

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

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.

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.