Refactoring Encapsulation and Unit Testing Lesson Two: Encapsulation and Unit Testing.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Refactoring This lecture is divided into and introduction to refactoring and then several lessons. The intent is not to teach you all the refactorings.
Written by: Dr. JJ Shepherd
CIT 590 Intro to Programming Java lecture 3. Hashmaps The equivalent of python dictionaries. With both ArrayLists and Hashmaps, the syntax only allows.
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Road Map Introduction to object oriented programming. Classes
Access to Names Namespaces, Scopes, Access privileges.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Ranga Rodrigo. Class is central to object oriented programming.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
1 v1.6 08/02/2006 Overview of Eclipse Lectures 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Refactoring 5.Debugging 6.Testing.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Refactoring Self-Documenting Code and Functional Testing Topics: Beginning Program Bog Lesson One Documentation Comments Refactored Code.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise www2.hh.se/staff/jebe/oop2005/
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Slide 1 Unit Testing. Slide 2 Unit Testing Options l Use N-Unit In a microsoft environment.NET… you can use their supplied N-Unit testing to test your.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
PHY281Flow ControlSlide 1 Decisions In this section we will learn how to make decisions in a Java program  if Statements  if... else Statements  Comparison.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
10-Nov-15 Java Object Oriented Programming What is it?
12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
CSC 142 D 1 CSC 142 Instance methods [Reading: chapter 4]
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
02/14/2005 Introduction to Programming with Java, for Beginners Midterm 1 Review.
Refactoring Classes Lesson Six: Classes. Refactoring Classes Class Decomposition is difficult. Several Techniques exist: Behavioral Driven Design[Kowal]
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
Written by: Dr. JJ Shepherd
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Refactoring Constants and Variables Lesson Three: Constants and Variables.
CSSE 375 Organizing Data – Part 1 Shawn and Steve Q1.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
Catalog of Refactoring (3) Organizing Data. Catalog Self Encapsulate Field Replace Data Value with Object Change Value to Reference Change Reference to.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Catalog of Refactoring (6) Making Method Calls Simpler.
Catalog of Refactoring
Module Road Map Refactoring Why Refactoring? Examples
Understand the Fundamentals of Classes
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Software Development Java Classes and Methods
Class Structure 16-Nov-18.
Class Structure 28-Nov-18.
Classes, Encapsulation, Methods and Constructors (Continued)
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
Class Structure 25-Feb-19.
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Defining Classes and Methods
design OO words Debug Variables Data types
ITE “A” GROUP 2 ENCAPSULATION.
Presentation transcript:

Refactoring Encapsulation and Unit Testing Lesson Two: Encapsulation and Unit Testing

Refactoring Encapsulation and Unit Testing Process 1. Encapsulate ALL class variables 2. Setup unit test program 3. Unit test 4. Functionally test 5. Perform Refactorings

Refactoring Encapsulation and Unit Testing 1. Encapsulate ALL class variables A.Write get and set methods for ALL class variables Modify ALL references to class variables to use get and set methods. Test methods.

Refactoring Encapsulation and Unit Testing public int getComputerStatus () { return computerStatus; } public Image getUserImage (){return userImage;} public boolean getFirst () { return first; } public Image getComputerImage () { return computerImage;} public void setComputerStatus (int computerStatus) { this.computerStatus = computerStatus; } public void setUserImage (Image userImage) { this.userImage = userImage;} public void setFirst (boolean first) { this.first = first; } public void setCommputerImage (Image computerImage) { this.computerImage = computerImage; } Write getters and setters for all class variables

Refactoring Encapsulation and Unit Testing 5.Perform Refactorings 1. Self encapsulating field 2. Encapsulate field 3. Encapsulate collection

Refactoring Encapsulation and Unit Testing Summary: You are accessing a field directly, but the coupling to the field is becoming awkward. Create getting and setting methods for fields and use ONLY those to access the field. Self Encapsulating Field

Refactoring Encapsulation and Unit Testing Self Encapsulating Field: Motivation: Data fields should always be declared as private. Getter and Setter methods should be used to access the data. Debate exist about using getter and setter methods inside the class. |I recommend you use these at ALL times.

Refactoring Encapsulation and Unit Testing Self Encapsulating Field: Example: class IntRange { private int _low, _high; boolean includes (int arg) { return arg >= _low ** arg <= _high; } void grow (int factor) { _high = _high * factor; } IntRange (int low, int high) { _low = low; _high = high; } // end IntRange

Refactoring Encapsulation and Unit Testing Self Encapsulating Field: Example: class IntRange { private int _low, _high; public getLow() { return _low} public setLow (int arg) { _low = arg; } public getHigh() { return _high} public setHigh (int arg) { _high = arg;} boolean includes (int arg) { return arg >= getLlow () && arg <= getHigh(); } void grow (int factor) { getHigh() = getHigh() * factor; } IntRange (int low, int high) { _low = low; _high = high; } // end IntRange Encapsulate and reference. Leave initializations as direct.

Refactoring Encapsulation and Unit Testing Self Encapsulating Field: Mechanics: create a getter and setter method for the field find references to field - replace with get and set methods make field private compile and test

Refactoring Encapsulation and Unit Testing Summary: There is a public field. Make it private and provide accessors. Encapsulate Field

Refactoring Encapsulation and Unit Testing Encapsulate Field: Motivation: Sharing data reduces modularity of the program. Encapsulation hides the data and adds accessors to protect the data and program integrity.

Refactoring Encapsulation and Unit Testing Encapsulate Field: Example: public String _name; private String )name; public String getName () { return )name; } public void setName (String arg) { _name = arg; } Should be

Refactoring Encapsulation and Unit Testing Encapsulate Field: Mechanics: create get and set methods for the field find all clients and change references declare field as private compile and test.

Refactoring Encapsulation and Unit Testing Summary: A method returns a collection. Make it return a read-only view and provide add/remove methods. Encapsulate Collection

Refactoring Encapsulation and Unit Testing Encapsulate Collection: Motivation: A collection needs the same modularization and encapsulation that one class does. Add getter and setter methods for the collection.

Refactoring Encapsulation and Unit Testing Encapsulate Collection: Example: class Course public course (String name, boolean isAdvanced) { … } public boolean isAdvanced ( ) { …}; class Person public Set getCourses() { return _courses; } public void setCourses (Set arg) { _courses = arg; } private Set _courses; A person is taking a course

Refactoring Encapsulation and Unit Testing Encapsulate Collection: Example: Person kent = new Person(); Set s = new HashSet(); s.add(new Course (“Smalltalk Programming”, false)); s.add(new Course (“Appreciating Single Malts”, true)); kent.setCourses(s); Assert.equals (2,kent.getCourses().size()); Course refact – new Course (“Refactoring”, true); kent.getCourses().add(refact); kent.getCourses().add(new Course (“Brutal Sarcasm”, false)); Assert.equals (4, kent.getCourses().size())); kent.getCourses().remove (refact); Assert.equals (3,kent.getCourses().size());

Refactoring Encapsulation and Unit Testing Encapsulate Collection: Example: Iterator iter = person.getCourses().iterator(); int Count = 0; while (iter.hasNext*(( { Course each – (Course) iter.next(); if (each.isAdvanced*(( count ++; }// end while A client that to know about courses might do this

Refactoring Encapsulation and Unit Testing Encapsulate Collection: Example: Class Person public void addCourse (Course arg) { _courses.add(arg); } public void removeCourse (Course arg) { _courses.remove(arg); } private Set )courses = new HashSet(); Create the modifiers for the collection Initialize the field

Refactoring Encapsulation and Unit Testing Encapsulate Collection: Example: Class Person public void setCourses (Set arg) { Assert.isTrue()courses.isEmpty()); Iterator iter – arg.iterator(); while (iter.hasNext()) { addCourse ((Course) iter.next())}; Write the setter methods

Refactoring Encapsulation and Unit Testing Encapsulate Collection: Example: public void initialzeCourses (Set arg) { Assert.isTrue()courses.isEmpty()); Iterator iter – arg.iterator(); while (iter.hasNext()) { addCourse ((Course) iter.next())}; public void initializeCourses (Set arg) } Assert.isTrue(_courses.isEmpty(); _courses.addAll(arg); } // end initializeCourses Rename the method for clarity Initialize and remove loop

Refactoring Encapsulation and Unit Testing Encapsulate Collection: Example: Person kent = new Person (); Set a = new HashSet(); s.add(new Course (“Samlltalk Programming”, false)); s.add(new Course (“Appreciting Single Malts”, true)); kent.initializeCourses|(s); Person kent = new Person(); kent.addCourse(new Course (“Samlltalk Programming:, false)); kent.addCourse(new Course (“Appreciating Single Malts”, true)); becomes

Refactoring Encapsulation and Unit Testing Encapsulate Collection: Example: kent.getCourses().add(new Course (“Brutal Sarcasm”, false)); kent.addCourse(new Course (“Brutal Sarcasm”, false)); public Set getCourses() { return Collections.unmodifiableSet (_courses|); } becomes I can then check that it is not modified

Refactoring Encapsulation and Unit Testing Encapsulate Collection: Example: Iterator iter = person.getCourses().iterator(); int count = 0; while (iter.hasNext() { Course each – (Course) iter.next(); if (each.isAdvanced()) count ++; } // end while Now I can move the behavior to the class This method is better placed in the person class

Refactoring Encapsulation and Unit Testing Encapsulate Collection: Example: class Person… int numberOfAdvancedCourses() { Iterator iter = getCourses().iterator(); int count = 0; while (iter.hasNext() { Course each – (Course) iter.next(); if (each.isAdvanced()) count ++; } // end while } // end numberOfAdvancedCourses

Refactoring Encapsulation and Unit Testing Encapsulate Collection: Example: Kent.getCourses().size() Kent.numberOfCourses() Class Person public int numberOfCourses() { return _courses.size(); } Is more readable as

Refactoring Encapsulation and Unit Testing Mechanics: Add and add and remove method initialize the field to an empty collection find callers of set method – modify or call find all users of getter and modify collection modify getter to return a read-only view find all users of getter – move to host change name of current getter compile and test Encapsulate Collection

Refactoring Encapsulation and Unit Testing // GETS AND SETS ADDED TO CODE public int getComputerStatus () { return computerStatus; } public Image getUserImage (){return userImage;} public boolean getFirst () { return first; } public Image getComputerImage () { return computerImage;} …… public void setComputerStatus (int computerStatus) { this.computerStatus = computerStatus; } public void setUserImage (Image userImage) { this.userImage = userImage;} public void setFirst (boolean first) { this.first = first; } public void setCommputerImage (Image computerImage) { this.computerImage = computerImage; }

Refactoring Encapsulation and Unit Testing In this lesson, we do not make gets and sets for arrays or other collections. That is handled later. But for all primitive variables, and class instance variables we must change EVERY access to these variables to gets and sets. Many people say that you don’t have to do this inside the class but it is simply a barrier to good method and class partitioning so we will change every one. The problem is that some of the variables are used as parameters passed into methods and this must be addressed properly for good scoping of variables.

Refactoring Encapsulation and Unit Testing //METHODS that need changes in their access to variables int bestMove(int computerStatus, int userStatus) { has parameters making them local variables – scoping? ok boolean legalUserMove(int canidateMove) { boolean legalComputerMove() { int gameStatus(int computerStatus, int userStatus) { public void paint(Graphics g) { // paint the screen public void mouseReleased(MouseEvent e) {

Refactoring Encapsulation and Unit Testing //METHODS that need changes in their access to variables computerStatus = userStatus = 0; BECOMES setComputerStatus(0); setUserStatus(0); if ((userStatus | computerStatus) == ENDINGSTATE) { BECOMES if ((getUserStatus() | getComputerStatus()) == ENDINGSTATE) { computerImage = getImage(getCodeBase(), "oimage.gif"); BECOMES setComputerImage(getImage(getCodeBase(), "oimage.gif"));

Refactoring Encapsulation and Unit Testing 2. Setup Unit Test Program A.Either implement J or N unit testing in Java or C sharp OR Write your own unit testing program. 1) Divide your program into two programs a) One that executes b) One that contains all methods etc. 2) Write a program that uses your containing class methods unit by unit for tests.

Refactoring Encapsulation and Unit Testing 2. Setup Unit Test Program Exec classYour class Makes an instance All code in constructor needed To execute

Refactoring Encapsulation and Unit Testing 2. Setup Unit Test Program Your class Method addit (int a, int b):int return a + b; Your test class Method addit (int a, int b):int int a = 4; int b = 5; int answer = super (a,b); if answer <> 9 print Error in addit; print expected, “9”; print received, answer;

Refactoring Encapsulation and Unit Testing 2. Setup Unit Test Program Exec classYour test class Makes an instance of the TEST CLASS Execute as usual

Refactoring Encapsulation and Unit Testing 2. Setup Unit Test Program Copy your program and call it xxxxTest extending your class Take out any GUI stuff (not this test) Here we are testing methods. What else to do??????

Refactoring Encapsulation and Unit Testing 2. Setup Unit Test Program Decide what you are going to test…… int bestMove(int computerStatus, int userStatus) { boolean legalUserMove(int canidateMove) { boolean legalComputerMove() { We will not test since it is tested functionally public void init() { public void paint(Graphics g) { // paint the screen

Refactoring Encapsulation and Unit Testing 2. Setup Unit Test Program We do need to test public void mouseReleased(MouseEvent e) { However we will have to separate the GUI from the domain functionality

Refactoring Encapsulation and Unit Testing 2. Setup Unit Test Program // user clicked applet // GUI code int x = e.getX(); // get mouse x location int y = e.getY(); // get mouse y location Needs to stay in mouseReleased() since it is GUI

Refactoring Encapsulation and Unit Testing // domain functionality and GUI switch (gameStatus()) { // determine status case WIN: case LOSE: case STALEMATE: play(getCodeBase(), "audio/return.au"); computerStatus = userStatus = 0; if (first) { // reset first computerStatus |= 1 << (int)(Math.random() * 9); }// end if first = !first; repaint(); // GUI controlling when to display // RED LINED code NEEDS TO BE A METHOD TO TEST

Refactoring Encapsulation and Unit Testing Make it a METHOD public void resetFirst() { if (getComputerFirst()) { // reset who is first setComputerStatus ( 1 << (int)(Math.random() * 9)); }// end if setComputerFirst (!getComputerFirst()); } // end resetStatus

Refactoring Encapsulation and Unit Testing Call the METHOD Now this is all GUI code switch (gameStatus()) { // determine status case WIN: case LOSE: case STALEMATE: play(getCodeBase(), "audio/return.au"); resetStatus(); repaint(); return; } // end switch

Refactoring Encapsulation and Unit Testing 3. Unit test program shown in the next slides 4. Functionally test program

Refactoring Encapsulation and Unit Testing UNIT methods to test int bestMove(int computerStatus, int userStatus) boolean legalComputerMove() boolean legalUserMove(int canidateMove) int gameStatus( ) public void resetFirst()

Refactoring Encapsulation and Unit Testing FIRST METHOD TO TEST int bestMove(int computerStatus, int userStatus) // bestMove TEST 1 // test if can find best strategic move 4 th position // bestMove TEST 2 // test if can find next best strategic move 0th // bestMove TEST 3 // test if can block win // bestMove TEST 4 // test if can take win

Refactoring Encapsulation and Unit Testing FIRST METHOD TO TEST -- WRITE THE bestMove test int bestMove(int computerStatus, int userStatus) public int bestMove (int computerStatus, int userStatus, int correctMove ) { int tempBestMove = super.bestMove ( computerStatus, userStatus) ; if (tempBestMove == correctMove) { System.out.print (" good ");} else System.out.print ("NOT good "); System.out.print (" tempBestMove = " + tempBestMove); System.out.print (" correctMove = " + correctMove); System.out.println (" userStatus = " + getUserStatus () ); return tempBestMove; } // end best move 1 1 Add a variable of return type that is the CORRECT return value

Refactoring Encapsulation and Unit Testing FIRST METHOD TO TEST -- WRITE THE bestMove test int bestMove(int computerStatus, int userStatus) public int bestMove (int computerStatus, int userStatus, int correctMove ) { int tempBestMove = super.bestMove ( computerStatus, userStatus) ; if (tempBestMove == correctMove) { System.out.print (" good ");} else System.out.print ("NOT good "); System.out.print (" tempBestMove = " + tempBestMove); System.out.print (" correctMove = " + correctMove); System.out.println (" userStatus = " + getUserStatus () ); return tempBestMove; } // end best move 2 2 Add a statement to call the super.xxxx method for testing

Refactoring Encapsulation and Unit Testing FIRST METHOD TO TEST -- WRITE THE bestMove test int bestMove(int computerStatus, int userStatus) public int bestMove (int computerStatus, int userStatus, int correctMove ) { int tempBestMove = super.bestMove ( computerStatus, userStatus) ; if (tempBestMove == correctMove) { System.out.print (" good ");} else System.out.print ("NOT good "); System.out.print (" tempBestMove = " + tempBestMove); System.out.print (" correctMove = " + correctMove); System.out.println (" userStatus = " + getUserStatus () ); return tempBestMove; } // end best move 3 3 Add an IF statement to see if the returned value is correct

Refactoring Encapsulation and Unit Testing FIRST METHOD TO TEST -- WRITE THE bestMove test method int bestMove(int computerStatus, int userStatus) public int bestMove (int computerStatus, int userStatus, int correctMove ) { int tempBestMove = super.bestMove ( computerStatus, userStatus) ; if (tempBestMove == correctMove) { System.out.print (" good ");} else System.out.print ("NOT good "); System.out.print (" tempBestMove = " + tempBestMove); System.out.print (" correctMove = " + correctMove); System.out.println (" userStatus = " + getUserStatus () ); return tempBestMove; } // end best move 4 4 I like to print out pertinent values to allow easy debugging.

Refactoring Encapsulation and Unit Testing FIRST METHOD TO TEST -- NOW write the tests int bestMove(int computerStatus, int userStatus) public void testBestMove () { System.out.println (" "); System.out.println ("testBestMove"); // bestMove TEST 1 // test if can find best strategic move 4 th position bestMove (0, 0 |( 1 << 1) | (1 << 8), 4); // bestMove TEST 2 // test if can find next best strategic move 0th bestMove (0, 0 |( 1 << 4), 0); // bestMove TEST 3 // test if can block win bestMove (0, 0|( 1 << 3)|( 1 << 4), 5); // TEST 4 // test if can take win bestMove (0 |( 1 << 6)|( 1 << 7), 0|( 1 << 3)|( 1 << 4), 8); } // end testBestMove

Refactoring Encapsulation and Unit Testing FIRST METHOD TO TEST -- NOW run the tests int bestMove(int computerStatus, int userStatus) Place the test calls in the main or paint method of the sub class public void paint(Graphics g) { testBestMove (); testLegalComputerMove(); testLegalUserMove(); testGameStatus( ); bestMove: Test 1: good temp = 4 correct = 4 user = 258

Refactoring Encapsulation and Unit Testing // bestMove TEST 1 // test if can find best strategic move bestMove (0, 0 |( 1 << 1) | (1 << 8), 4); public int bestMove (int computerStatus, int userStatus, int correctMove ) { int tempBestMove = super.bestMove ( computerStatus, userStatus) ; if (tempBestMove == correctMove) { System.out.print (" good ");} else System.out.print ("NOT good "); System.out.print (" tempBestMove = " + tempBestMove); System.out.print (" correctMove = " + correctMove); System.out.println (" userStatus = " + getUserStatus () ); return tempBestMove; bestMove: Test 1: good temp = 4 correct = 4 user = 258 TEST 2, 3, 4 similar

Refactoring Encapsulation and Unit Testing boolean legalUserMove(int legalComputerMove, int legalUserMove, int canidateMove, boolean correctAns) { boolean tempLegalUserMove = super.legalUserMove (computerStatus, userStatus, canidateMove); if(tempLegalUserMove == correctAns ) {System.out.print ("good "); } else System.out.print ("NOT good "); System.out.print (" computer = " + computerStatus); …… System.out.print (" user = " + userStatus); System.out.print (" canidate = " + canidateMove); System.out.print ( " userMove " + tempLegalUserMove); System.out.println (" correctAns " + correctAns); return tempLegalUserMove; } // end legalUserMove

Refactoring Encapsulation and Unit Testing // legalUserMove TEST 1 // test if within range of squares userStatus = 0 |( 1 << 0) ; computerStatus = 0 |( 1 << 2) ; legalUserMove (computerStatus, userStatus, 9, false); // TEST 1 // legalUserMove TEST 2 // test if empty false // legalUserMove TEST 3 // test if empty true legalUserrMove: Test 1: good computer = 4 user = 1 canidate = 9 userMove = false correctAns = false

Refactoring Encapsulation and Unit Testing boolean legalComputerMove (int computerStatus, int userStatus, boolean correctAns) { boolean tempLegalComputerMove = super.legalComputerMove (computerStatus, userStatus) ; if(tempLegalComputerMove == correctAns) {System.out.print ("good "); } else System.out.print ("NOT good "); System.out.print (" computer = " + computerStatus); System.out.print (" user = " + userStatus); System.out.print (" computerMove "+ tempLegalComputerMove); System.out.println (" correctAns " + correctAns); return tempLegalComputerMove; } // end tryLegalComputerMove

Refactoring Encapsulation and Unit Testing // legalComputerMove TEST 1 // test if ending state userStatus = 0 |( 1 << 0) | (1 << 1)| (1 << 3)| (1 << 5)| (1 << 8); computerStatus = 0 |( 1 << 2) | (1 << 4)| (1 << 6)| (1 << 7) ; legalComputerMove (computerStatus, userStatus, false); // TEST 1 // legalComputerMove TEST 2 // test if NOT in ending state legalComputerMove: Test 1: good computer 212 user 299 computerMove = false correctAns = false

Refactoring Encapsulation and Unit Testing int gameStatus(int computerStatus, int userStatus, int correctStatus) { int tempGameStatus = super.gameStatus (computerStatus, userStatus); if (tempGameStatus == correctStatus) {System.out.print ("good "); } else System.out.print ("NOT good "); System.out.print (" computer = " + computerStatus); System.out.print (" user = " + userStatus); System.out.print (" status = " + tempGameStatus); System.out.println (" correctStatus " + correctStatus); return tempGameStatus; } // end gameStatus

Refactoring Encapsulation and Unit Testing void testGameStatus() { // gameStatus TEST 1 // test if WIN userStatus = 0 |( 1 << 0) ; computerStatus = 0 | (1 << 3)| (1 << 4) | (1 << 5) ; gameStatus (computerStatus, userStatus, WIN); // TEST 1 // gameStatus TEST 2 // test if LOSE // gameStatus TEST 3 // test if STALEMATE // gameStatus TEST 4 // test if CONTINUE testGameStatus: Test 1: good computer 56 user 1 status = 1 correctStatus = 1

Refactoring Encapsulation and Unit Testing public boolean resetFirst (boolean computerFirst, boolean correctFirst) { boolean tempResetFirst = super.resetFirst (computerFirst); if (tempResetFirst == correctFirst) {System.out.print ("good "); } else System.out.print ("NOT good "); System.out.print (" computer = " + computerStatus); System.out.print (" computerFirst = " + computerFirst); System.out.print (" reset = " + tempResetFirst); System.out.println (" correctFirst " + correctFirst); return tempResetFirst; } // end resetFirst

Refactoring Encapsulation and Unit Testing public void testResetFirst () { // TEST 1 resetFirst ( false, true); // TEST 2 resetFirst ( true, false); } // end resetFirst testResetForst : Test 1: good computerfirst = false reset = true correct = true

Refactoring Encapsulation and Unit Testing NOW we have defined our functional test and our unit testing Other refactorings we do will be tested each time to assure we do not loose functionality or correctness in our program.