COS 260 DAY 15 Tony Gauvin.

Slides:



Advertisements
Similar presentations
Comp1004: Building Better Classes I Testing and Debugging Partly based on BlueJ Book – Chapter 6.
Advertisements

Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 4.0.
Objects First With Java A Practical Introduction Using BlueJ Designing object-oriented programs How to write code in a way that is easily understandable,
Well-behaved objects 4.0 Testing. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to.
Well-behaved objects Debugging. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Prevention vs Detection.
Designing Classes How to write classes in a way that they are easily understandable, maintainable and reusable.
Well-behaved objects Improving your coding skills 1.0.
Testing and Debugging. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling (Reminder) Zuul Assignment Two.
Design: Coupling and Cohesion How to write classes in a way that they are easily understandable, maintainable and reusable.
Make Sure You Know All This!. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 2 Objects and Classes.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 3.0.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Designing Classes 2 How to write classes in a way that they are easily understandable, maintainable and reusable.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0.
Designing Classes. Software changes Software is not like a novel that is written once and then remains unchanged. Software is extended, corrected, maintained,
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
OOPDA Intro 5.0. Topics Website and Syllabus Rowan VPN and H:drive BlueJ application and projects Programming Style (Appendix J) Javadoc (Appendix I)
Testing. Have you contacted your project members lately?
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.
Objects First With Java A Practical Introduction Using BlueJ Well-behaved objects 2.1.
1 COS 260 DAY 15 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz Today –Chapter 6 Assignment 4 posted –Due Nov 9 Capstone progress reports are due –Brief.
Objects First With Java A Practical Introduction Using BlueJ Designing classes How to write classes in a way that they are easily understandable, maintainable.
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.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
6.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 6.0.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability © 2017 Pearson Education, Inc. Hoboken,
Review. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Objects and Classes Objects and Classes –State.
Objektorienterad programmering d2, förel. 6
Objects First with Java
for-each PROS CONS easy to use access to ALL items one-by-one
Objektorienterad programmering d2, förel. 8
Unit Testing.
Objects First with Java A Practical Introduction using BlueJ
Objects First with Java Creating cooperating objects
John D. McGregor Session 9 Testing Vocabulary
Objektorienterad programmering d2, förel. 6
John D. McGregor Session 9 Testing Vocabulary
COS 260 DAY 17 Tony Gauvin.
Objects First with Java
COS 260 DAY 9 Tony Gauvin.
Objects First with Java
John D. McGregor Session 9 Testing Vocabulary
COS 260 DAY 13 Tony Gauvin.
COS 260 DAY 6 Tony Gauvin.
Objects First with Java
COS 260 DAY 19 Tony Gauvin.
COS 260 DAY 16 Tony Gauvin.
COS 260 DAY 10 Tony Gauvin.
COS 260 DAY 2 Tony Gauvin.
COS 260 DAY 18 Tony Gauvin.
Chapter 1 Introduction(1.1)
Exercise 1 Declare a constant of type int called SIZE and initialize it to value 10 Declare two int arrays of size “SIZE” Assume that those int arrays.
COS 260 DAY 16 Tony Gauvin.
Objects First with Java A Practical Introduction using BlueJ
COS 260 DAY 11 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
Objektorienterad programmering d2, förel. 8
COS 260 DAY 14 Tony Gauvin.
COS 260 DAY 4 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
Objects First with Java A Practical Introduction using BlueJ
Chapter 9: Implementation
Presentation transcript:

COS 260 DAY 15 Tony Gauvin

Agenda Assignment 5 will be posted soon Questions? Assignment 3 Over Due Assignment 4 posted Due 0ct 31 Assignment 5 will be posted soon Mini-quiz Six today Chapter 7 30 min Password “JohnvonNeumann” Mini-Quiz 7 Friday Oct 27 Chap 8 Finish Designing classes

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

Review Explain & Discuss Cohesion Coupling Code Duplications Responsibility-driven design Localizing Change Implicit Coupling Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Thinking ahead When designing a class, we try to think about changes likely to be made in the future. We aim to make those changes easy. Requires a little more effort now to greatly reduce effort later. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Refactoring When classes are maintained code is usually added. Classes and methods tend to become longer. Every now and then, classes and methods should be refactored to maintain cohesion and low coupling. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

An exercise in the need for refactoring Start with Zuul-better See 8.12.2 on page Add item to rooms One item for room One item for player Add more items to room and player Player limited by weight Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Refactoring and testing When refactoring code, separate the refactoring from making other changes. First do the refactoring only, without changing the functionality. Test before and after refactoring to ensure that nothing was broken. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Design questions Common questions: How long should a class be? How long should a method be? These can now be answered in terms of cohesion and coupling. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Design guidelines A method is too long if it does more then one logical task. A class is too complex if it represents more than one logical entity. Note: these are guidelines - they still leave much open to the designer. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Enumerated Types A language feature. Uses enum instead of class to introduce a type name. Their simplest use is to define a set of significant names. Alternative to static int constants. When the constants’ values would be arbitrary. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

A basic enumerated type public enum CommandWord { // A value for each command word, // plus one for unrecognised commands. GO, QUIT, HELP, UNKNOWN; } Each name represents an object of the enum type, e.g., CommandWord.HELP. Enum objects are not created directly. Enum definitions can also have fields, constructors and methods. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

The Switch Statement switch (something) { case valueofsomething: java statement; break; break: default: } https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Review Programs are continuously changed. It is important to make this change possible. Quality of code requires much more than just performing correct at one time. Code must be understandable and maintainable. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Review Good quality code avoids duplication, displays high cohesion, low coupling. Coding style (commenting, naming, layout, etc.) is also important. There is a big difference in the amount of work required to change poorly structured and well structured code. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Objects First with Java Well-behaved objects 6.0 © David J. Barnes and Michael Kölling

Main concepts to be covered Objects First with Java Main concepts to be covered Testing Debugging Test automation Writing for maintainability © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Code snippet of the day public void test() { int sum = 1; for (int i = 0; i <= 4; i++); sum = sum + 1; } System.out.println("The result is: " + sum); System.out.println("Double result: " + sum+sum); What is the output? The result is: 2 Double result: 22 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Possible results Which is printed? The result is: 5 The result is: 6 The result is: 11 The result is: 2 Double result: 22 The result is: 2 Double result: 12 Double result: 4 Double result: 22 Double result: 66 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Code snippet of the day public void test() { int sum = 1; for (int i = 0; i <= 4; i++); sum = sum + 1; } System.out.println("The result is: " + sum); System.out.println("Double result: " + sum+sum); The result is: 2 Double result: 22 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

We have to deal with errors Objects First with Java We have to deal with errors Early errors are usually syntax errors. The compiler will spot these. Later errors are usually logic errors. The compiler cannot help with these. Also known as bugs. Some logical errors have no immediately obvious manifestation. Commercial software is rarely error free. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Prevention vs Detection (Developer vs Maintainer) We can lessen the likelihood of errors: Use software engineering techniques, like encapsulation. Pay attention to cohesion and coupling. We can improve the chances of detection: Use software engineering practices, like modularization and good documentation. We can develop detection skills. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Objects First with Java Testing and debugging These are crucial skills. Testing searches for the presence of errors. Debugging searches for the source of errors. The manifestation of an error may well occur some ‘distance’ from its source. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Testing and debugging techniques Objects First with Java Testing and debugging techniques Unit testing (within BlueJ) Test automation Manual walkthroughs Print statements Debuggers © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Unit testing Each unit of an application may be tested. Method, class, module (package in Java). Can (should) be done during development. Finding and fixing early lowers development costs (e.g. programmer time). A test suite is built up. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Testing fundamentals Understand what the unit should do – its contract. You will be looking for violations. Use positive tests and negative tests. Test boundaries. Zero, One, Full. Search an empty collection. Add to a full collection. Search for/remove the only element. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Test automation Well-behaved objects Test automation © David J. Barnes and Michael Kölling

Main concepts to be covered Unit testing JUnit Regression testing Test cases Test classes Assertions Fixtures © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Unit testing within BlueJ Objects First with Java Unit testing within BlueJ Objects of individual classes can be created. Individual methods can be invoked. Inspectors provide an up-to-date view of an object’s state. Explore through the online-shop project. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Demo of online-shop

Objects First with Java Test automation Good testing is a creative process, but ... ... thorough testing is time consuming and repetitive. Regression testing involves re-running tests. Use of a test rig or test harness can relieve some of the burden. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Test harness Additional test classes are written to automate the testing. Objects of the harness classes replace human interactivity. Creativity and imagination required to create these test classes. Test classes must be kept up to date as functionality is added. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Objects First with Java Test automation Test frameworks exist to support automation. Explore fuller automation through the online-shop-junit project. Intervention only required if a failure is reported. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Demo of online-shop-junit © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

JUnit JUnit is a Java test framework Test cases are methods that contain tests Test classes contain test methods Assertions are used to assert expected method results Fixtures are used to support multiple tests © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Well-behaved objects Debugging

Prevention vs Detection (reprise) We can lessen the likelihood of errors: Use software engineering techniques, like encapsulation. Pay attention to cohesion and coupling. We can improve the chances of detection: Use software engineering practices, like modularization and good documentation. We can develop detection skills. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Debugging techniques Manual walkthroughs Print statements Debuggers © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Modularization and interfaces Objects First with Java Modularization and interfaces Applications often consist of different modules: E.g. so that different teams can work on them. The interface between modules must be clearly specified. Supports independent concurrent development. Increases the likelihood of successful integration. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Modularization in a calculator Objects First with Java Modularization in a calculator Each module does not need to know implementation details of the other. User controls could be a GUI or a hardware device. Logic could be hardware or software. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Method headers as an interface Objects First with Java Method headers as an interface // Return the value to be displayed. public int getDisplayValue();   // Call when a digit button is pressed. public void numberPressed(int number); // Plus operator is pressed. public void plus(); // Minus operator is pressed. public void minus(); // Call to complete a calculation. public void equals(); // Call to reset the calculator. public void clear(); © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Debugging It is important to develop code-reading skills. Debugging will often be performed on others’ code. Techniques and tools exist to support the debugging process. Explore through the calculator-engine project. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Manual walkthroughs Relatively underused. A low-tech approach. More powerful than appreciated. Get away from the computer! ‘Run’ a program by hand. High-level (Step) or low-level (Step into) views. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Tabulating object state Objects First with Java Tabulating object state An object’s behavior is largely determined by its state … … so incorrect behavior is often the result of incorrect state. Tabulate the values of key fields. Document state changes after each method call. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Verbal walkthroughs Explain to someone else what the code is doing: They might spot the error. You might spot the error, through the process of explaining. Group-based processes exist for conducting formal walkthroughs or inspections. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Print statements The most popular technique. No special tools required. All programming languages support them. Only effective if the right methods are documented. Output may be voluminous! Turning off and on requires forethought. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Choosing a test strategy Objects First with Java Choosing a test strategy Be aware of the available strategies. Choose strategies appropriate to the point of development. Automate whenever possible. Reduces tedium. Reduces human error. Makes (re)testing more likely. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Debuggers Debuggers are both language- and environment-specific. BlueJ has an integrated debugger. Support breakpoints. Step and Step-into controlled execution. Call sequence (stack). Object state. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Debugging streams (advanced) A pipeline of multiple operations might be hard to debug. The peek operation can provide insights. Consumer that passes on its input unchanged; e.g.: peek(s -> System.out.println(s)) © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Objects First with Java Review Errors are a fact of life in programs. Good software development techniques can reduce their occurrence. Testing and debugging skills are essential. Make testing a habit. Automate testing where possible. Continually repeat tests. Practice a range of debugging skills. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling