CS 170 Java Programming 1. Week 13: Classes, Testing, Debugging

Similar documents
CS 170 Java Programming 1. Week 12: Creating Your Own Types

Express Yourself. Writing Your Own Classes

CS 170 Java Programming 1. Week 5: Procedures and Functions

CS 170 Java Programming 1. Week 9: Learning about Loops

CS 170 Java Programming 1. Week 15: Interfaces and Exceptions

Week 2: Data and Output

Week 3: Objects, Input and Processing

CS 170 Java Programming 1. Week 10: Loops and Arrays

Slide 1 CS 170 Java Programming 1

CS 170 Java Programming 1. Week 7: More on Logic

TeenCoder : Java Programming (ISBN )

CompuScholar, Inc. 9th - 12th grades

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Tips from the experts: How to waste a lot of time on this assignment

Exam 1 - (20 points)

EECS168 Exam 3 Review

Structured Programming

Express Yourself. What is Eclipse?

Guessing Game with Objects

CS 2530 INTERMEDIATE COMPUTING

This course supports the assessment for Scripting and Programming Applications. The course covers 4 competencies and represents 4 competency units.

Programming Exercise 14: Inheritance and Polymorphism

Homework 09. Collecting Beepers

CS159. Nathan Sprague. September 30, 2015

UNIT TESTING. Krysta Yousoufian CSE 331 Section April 19, With material from Marty Stepp, David Notkin, and The Pragmatic Programmer

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

! Two questions when we design an OO system : ! Our Initial goal: learn to design and implement simple objects.

Object Oriented Design: Identifying Objects

Introduction to JUnit

Introduction to Programming Using Java (98-388)

Initial Coding Guidelines

Tips from the experts: How to waste a lot of time on this assignment

CS Exam 1 Review Suggestions

Unit testing. JUnit. JUnit and Eclipse. A JUnit test class 3/6/17. A method is flagged as a JUnit test case.

Slide 1 CS 170 Java Programming 1 Testing Karel

You must pass the final exam to pass the course.

APCS Semester #1 Final Exam Practice Problems

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM

Tips from the experts: How to waste a lot of time on this assignment

Unit testing. unit testing: Looking for errors in a subsystem in isolation. The basic idea: JUnit provides "assert" commands to help us write tests.

Media Computation. Lecture 15.2, December 3, 2008 Steve Harrison

public static boolean isoutside(int min, int max, int value)

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Introduction to Programming System Design CSCI 455x (4 Units)

CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM

CSCI 200 Lab 1 Implementing and Testing Simple ADTs in Java

CSCI 136 Programming Exam #2 Fundamentals of Computer Science II Spring 2012

TOOLS AND TECHNIQUES FOR TEST-DRIVEN LEARNING IN CS1

CS112 Lecture: Defining Instantiable Classes

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

Chapter 4 Defining Classes I

JAVA MOCK TEST JAVA MOCK TEST II

Our second exam is Thursday, November 10. Note that it will not be possible to get all the homework submissions graded before the exam.

Introduction to JUnit. Data Structures and Algorithms for Language Processing

Reviewing all Topics this term

CSCI 200 Lab 3 Using and implementing sets

Lecture 15: Inheritance II

There are several files including the start of a unit test and the method stubs in MindNumber.java. Here is a preview of what you will do:

Part 1: Group Brainstorm (NO computers during this time) Part 2: Submit Individual Brainstorm (You can now use a computer)

Homework 5 B+Tree. Due Monday November 26

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Creating Java Programs with Greenfoot

COMP 111. Introduction to Computer Science and Object-Oriented Programming. Week 3

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Lecture 1. Course Overview Types & Expressions

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers Sjaak Smetsers

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Mathematics/Science Department Kirkwood Community College. Course Syllabus. Computer Science CSC142 1/10

CS201 - Assignment 3, Part 1 Due: Friday February 28, at the beginning of class

CS 132 Exam #1 - Study Suggestions

CIT 590 Homework 10 Battleship

CS 209 Sec. 52 Spring, 2006 Lab 6 - B: Inheritance Instructor: J.G. Neal

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

AP Computer Science A Summer Assignment

CSCI 1301: Introduction to Computing and Programming

Objects and State. COMP1400 Week 9. Wednesday, 19 September 12

CSE 143: Computer Programming II Summer 2015 HW6: 20 Questions (due Thursday, August 13, :30pm)

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

MEDIA COMPUTATION DRJAVA. Lecture 11.3 November 7, 2008

Java Classes, Inheritance, and Interfaces

CIS 121 Data Structures and Algorithms with Java Spring 2018

P2: Advanced Java & Exam Preparation

Classes, interfaces, & documentation. Review of basic building blocks

Inheritance and Interfaces

public static void negate2(list<integer> t)

Outline. Logistics. Logistics. Principles of Software (CSCI 2600) Spring Logistics csci2600/

Defining Classes and Methods

Summary. Recursion. Overall Assignment Description. Part 1: Recursively Searching Files and Directories

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

CS : Data Structures

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers-Weeda Sjaak Smetsers Ana Tanase

Com S 227 Spring 2018 Assignment points Due Date: Thursday, September 27, 11:59 pm (midnight) "Late" deadline: Friday, September 28, 11:59 pm

CS 177 Week 15 Recitation Slides. Review

Lab Exercise 6: Abstract Classes and Interfaces CS 2334

QUIZ: What value is stored in a after this

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

COMP 202 Java in one week

CS24 Week 4 Lecture 1

Transcription:

CS 170 Java Programming 1 Week 13: Classes, Testing, Debugging

What s the Plan? Short lecture for makeup exams Topic 1: A Little Review How to create your own user-defined classes Defining instance variables, constructors and methods Topic 2: Testing and Correctness Writing hand-crafted and JUnit unit tests No quiz or homework this week, but you must complete the IC document and there is an extra-credit assignment

Your "IC" or "Lab" Document Use Word or OpenOffice to create a new document Save the file as IC013.doc (Office 97-2003 compatible) Place on your network U: drive or on a thumb drive Put your name and today's date at the top of the sheet Title it "CS 170 Lab Exercises Week 13" Start DrJava and we'll review a few basic class concepts Copy NIM.jar from Q: and double-click it This is an extra-credit assignment using several classes Open Nim.drjava project in Nim-Project folder We are going to work the Game and Pile classes

The Game of NIM Here are the rules for our game of NIM Two players alternately take marbles from a pile Player must take one, but cannot take more than half Last player to take a marble loses Here are the classes that are already defined Nim is our "program" class with a main() method Game will contain the game-playing logic Pile controls the pile of marbles Player actually makes the decisions We'll have two kinds of players: human and computer The computer can be either smart or stupid

Review State and Data Object data is stored in instance variables or fields Defined outside of any method, but inside class private type name [ = initial-value ]; Pile class will need an instance variable for marbles Game class will need three Player fields, one Pile One Player will be an alias for each of the others Exercise 13.1: add instance variables and shoot a screenshot of each class 5

Review Initializing Objects Object fields are initialized by writing constructors Same name as class, no return type May have different constructors, different parameters Constructors should always initialize data fields Exercise 13.2: add a Pile constructor that takes a single parameter to initialize the instance variable (snap a pic) Assign the parameter value to the field Use different names, or this to refer to field Exercise 13.3: read docs and complete Game constructor

Review Encapsulation Encapsulation: objects responsible for their own data Only access to object state is through public methods Need to provide accessors and mutators to use object

Review Object Methods Methods that modify an object are called mutators Accessors return information about an object public void methodname(parameters) { // statements modify fields } public returntype methodname() { return typeexpression; Mutator } Accessor

Review Instance Methods Exercise 13.4: Let's add accessors to the Pile class getsize(): tell us how many marbles are left hasmarbles(): returns true if there are marbles left For accessors, you should add an @return to Javadoc Exercise 13.5: Now, let's add a mutator: remove(int amount): removes marbles from Pile The variable int amount is called a formal parameter For each formal parameter, add an @param tag Holds different values (actual parameters) when called

Introducing Unit Tests How do we know if we've written Pile class correctly? Don't want to wait until Nim is finished Should write independent tests to ensure it works right These are called unit tests Add a new class named PileManualTest Use ACM console program or simple console program Inside the run() method, create a Pile object Let's start with the initial value 20 Use values that let you calculate expected output easily 10

Testing the Class How do we know if the Pile constructor worked? We compare its expected value and its actual value Call getsize() and print expected and actual values Now, test two more Pile objects with different initial values Exercise 13.6: shoot a screenshot How do we know the mutator works? Call remove() (to mutate Pile), and then print the expected and actual values again Exercise 13.7: shoot a screenshot 11

Introducing JUnit Unit tests are most important testing tool you have Create instance of your class, call mutators Check expected values with actual Problem? Very time-consuming to compare values Easy to miss non-matching actual and expected values JUnit testing tool automates comparison JUnit is standard Unit Testing tool, not just for DrJava Exercise 13.8: choose File->New JUnit Test Case Name the class PileTest, save and compile Compile and Test Project

Writing Test Methods Write a separate test method for each operation void method, name starts with test, no parameters Create an object in a known state Call the method you are testing Compare the expected state with the actual state Exercise 13.9: add testpilesizezero() method Create Pile with a size of 0 Create a variable to hold the expected size Call getsize() to retrieve actual size value Call assertequals() method, passing the expected and actual values as parameters

JUnit Assertions Use assertions to compare expected versus actual Most common is assertequals(expect, actual) Here are some other useful assertions // For double or floating-point comparisons assertequals(expect, actual, tolerance) // A simple boolean expression (true or false) asserttrue(test) or assertfalse(test) // Testing for null or not null assertnull(obj) or assertnotnull(obj) Also versions that takes a string message parameter

Selecting Test Cases Positive tests: legitimate input you expect the program handles correctly All values >= 0 in a square-root program Boundary cases: also legitimate input to be handled in a trivial way 0 or the empty string may be handled as a special case Negative cases: input that you expect to fail Negative numbers, string input?

Finishing Up To complete your IC "lab" document: Read and complete the additional exercises in the lessons on Static Variables and Methods, Testing and Debugging. Submit to Blackboard when done Reading for this week: Gaddis: Finish Chapter 6, start Chapter 7 (Inheritance)