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

Similar documents
CSE 143: Computer Programming II Spring 2015 HW7: 20 Questions (due Thursday, May 28, :30pm)

CSE 143: Computer Programming II Summer 2017 HW5: Anagrams (due Thursday, August 3, :30pm)

CSE 143: Computer Programming II Summer 2017 HW4: Grammar (due Tuesday, July :30pm)

CSE 143: Computer Programming II Winter 2019 HW6: AnagramSolver (due Thursday, Feb 28, :30pm)

CSE 143: Computer Programming II Spring 2015 HW2: HTMLManager (due Thursday, April 16, :30pm)

CSE 143, Winter 2013 Programming Assignment #8: Huffman Coding (40 points) Due Thursday, March 14, 2013, 11:30 PM

15-110: Principles of Computing, Spring 2018

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

CSCI Lab 9 Implementing and Using a Binary Search Tree (BST)

Account joeacct = new Account (100, new Account (500)); Account joeacct = new Account (100, new Account (500, null));

Background: This programming assignment focuses on implementation and usage of a graph data structure.

Background: This programming assignment focuses on implementation and usage of a graph data structure.

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

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

CSE 142/143 Unofficial Commenting Guide Eric Arendt, Alyssa Harding, Melissa Winstanley

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

CSE143 Summer 2008 Final Exam Part A KEY August 21, 2008

CSE143X: Computer Programming I & II Programming Assignment #9 due: Monday, 11/27/17, 11:00 pm

CS 142 Style Guide Grading and Details

CIS 121 Data Structures and Algorithms with Java Spring 2018

Blocky: A Game of Falling Blocks

CIS 121 Data Structures and Algorithms with Java Spring 2018

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

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

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

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Compuer Science 62 Assignment 10

Array Based Lists. Collections

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

CSE143X: Computer Programming I & II Programming Assignment #10 due: Friday, 12/8/17, 11:00 pm

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

CS 3114 Data Structures and Algorithms DRAFT Project 2: BST Generic

CSCI 200 Lab 3 Using and implementing sets

Building Java Programs

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

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

CSE 142, Autumn 2018 Programming Assignment #9: Critters (20 points) Due Tuesday, December 4th, 9:00 PM

Animals Due Part 1: 19 April Part 2: 26 April

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator

Good Coding Practices Spring 2018

Procedural Abstraction

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

Introduction to Programming Using Java (98-388)

Lab 4: Super Sudoku Solver CSCI 2101 Fall 2017

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CSE 143, Winter 2009 Final Exam Thursday, March 19, 2009

EE 422C HW 6 Multithreaded Programming

CMPSCI 187 / Spring 2015 Sorting Kata

Project Compiler. CS031 TA Help Session November 28, 2011

CSE 331 Midterm Exam Sample Solution 2/18/15

Adam Blank Lecture 3 Autumn 2016 CSE 143. Computer Programming II

Summer Final Exam Review Session August 5, 2009

CSE 143. More ArrayIntList; pre/post; exceptions; debugging. Computer Programming II. CSE 143: Computer Programming II

Programming Assignment: recursion, recursion, recursion Due date: Thursday, July 26 at 11:29 am

Programming Project 1

Express Yourself. The Great Divide

XC Total Max Score Grader

Decision Logic: if, if else, switch, Boolean conditions and variables

CSE332: Data Abstractions Lecture 22: Shared-Memory Concurrency and Mutual Exclusion. Tyler Robison Summer 2010

CSCI 1301: Introduction to Computing and Programming Spring 2018 Project 3: Hangman 2.0 (A Word Guessing Game)

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Objectives for this class meeting. 1. Conduct review of core concepts concerning contracts and pre/post conditions

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

These are notes for the third lecture; if statements and loops.

EECE.2160: ECE Application Programming

If you took your exam home last time, I will still regrade it if you want.

CMPSCI 187 / Spring 2015 Hangman

Lab 4: Imperative & Debugging 12:00 PM, Feb 14, 2018

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

CIS 110: Introduction to Computer Programming

2. [20] Suppose we start declaring a Rectangle class as follows:

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

CSE 12 Abstract Syntax Trees

CS 314 Exam 2 Spring

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

CSCI 200 Lab 1 Implementing and Testing Simple ADTs in Java

Computer Science E-119 Fall Problem Set 3. Due before lecture on Wednesday, October 31

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

CS2110 Assignment 2 Lists, Induction, Recursion and Parsing, Summer

Assignment #4 Hangman Due: 10:30am on Friday, Febuary 17th This assignment may be done in pairs (which is optional, not required)

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


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

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:

Project 3: Implementing a List Map

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

CS 2223 B15 Term. Homework 4

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Hangman YEAH Hours. Thursday, February 14, 7:30 9:00PM Andrew Tierno

Due Date: Two Program Demonstrations (Testing and Debugging): End of Lab

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

CS3114 (Fall 2013) PROGRAMMING ASSIGNMENT #2 Due Tuesday, October 11:00 PM for 100 points Due Monday, October 11:00 PM for 10 point bonus

CS2110 Assignment 3 Inheritance and Trees, Summer 2008

ITI Introduction to Computing II

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

Chapter 6 Introduction to Defining Classes

UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering. Program 8 EECE.3220 Data Structures Fall 2017

Assignment #4 Hangman Due: 11am on Monday, May 14th This assignment may be done in pairs (which is optional, not required)

Transcription:

CSE 143: Computer Programming II Summer 2015 HW6: 20 Questions (due Thursday, August 13, 2015 11:30pm) This assignment focuses on binary trees and recursion. Turn in the following files using the link on the course website: QuestionGame.java A class that represents a tree of questions and answers QuestionNode.java A class that represents a single question and its answers You will need the support files QuestionMain.java, questions.txt, and big-questions.txt; place these in the same folder as your program or project. You should t modify the provided files. The code you submit must work properly with the unmodified versions. The Game of Twenty Questions 20 Questions is a guessing game in which the objective is to ask / questions to determine an object. In our version, the human begins a round by choosing some object, and the computer attempts to guess that object by asking a series of / questions until it thinks it kws the answer. Then, the computer makes a guess; if its guess is correct, the computer wins, and otherwise you win. For example, consider the following game: (y/n)? n (y/n)? y I guess that your object is bicycle! Am I right? (y/n)? y Awesome! I win! In this assignment, you will create classes QuestionGame and QuestionNode to represent the computer s tree of / questions and answers for playing games of 20 Questions. You are provided with a client QuestionMain.java that handles user interaction and calls your tree s methods to play games. QuestionNode The contents of the QuestionNode class are up to you. Though we have studied trees of ints, you should create des specific to solving this problem. Your QuestionNode class should have at least one constructor used by your tree. Your de s fields must be public. Any fields representing data must be final. QuestionNode should t contain any actual game logic. It should only represent a single de of the tree. For reference, you can look at the AssassinNode class from assignment 3 or the IntTreeNode class from lecture. QuestionGame This class represents a game of 20 Questions. It keeps track of a binary tree whose des represent questions and answers. (Every de s data is a string representing the text of the question or answer.) The leaves of the tree represent possible answers (guesses) that the computer might make. All the other des represent questions that the computer will ask to narrow the possibilities. The left branch indicates the next question the computer asks if the answer is, and the right branch is the next question if the answer is. Note that even though the name of the game is 20 questions, the computer will t be limited to only twenty; the tree may have a larger height. 1

QuestionGame should have the following constructors: public QuestionGame(String object) This constructor should initialize a new QuestionGame object with a single leaf de representing the object object. You may assume the String is t null. To get QuestionMain to use this constructor, you should supply an empty file or one that doesn t exist. public QuestionGame(Scanner input) This constructor should initialize a new QuestionGame object by reading from the provided Scanner containing a tree of questions in standard format. You may assume the Scanner is t null and is attached to a legal, existing file in standard format. Make sure to read entire lines of input using calls on Scanner s nextline. QuestionGame should also implement the following methods: public void savequestions(printstream output) This method should store the current questions tree to an output file represented by the given PrintStream. This method can be used to later play ather game with the computer using the questions from this one. You should throw an IllegalArgumentException if the PrintStream is null. public void play() This method should use the current question tree to play one complete guessing game with the user, asking / questions until reaching an answer object to guess. A game begins with the root de of the tree and ends upon reaching an answer leaf de. If the computer wins the game, this method should print a message saying so. Otherwise, this method should ask the user for the following: what object they were thinking of, a question to distinguish that object from the player s guess, and whether the player s object is the or answer for that question. 2

Question Tree Standard Format In savequestions and the second constructor, your class will be interacting with files containing questions. Just like with BNF where we used a common format so everyone could share the same grammars, here, we specify a common question tree format. A single QuestionNode should be represented as a n-empty sequence of line pairs. The first line of the pair should contain either or to differentiate between questions (branches) and answers (leaves). The second line of the pair should contain the text for that de (the actual question or answer). The des of the tree should appear in pre-order. The readtree and writetree methods from section will be very helpful for any method that deals with this format. Full Example Walk-Through spec-questions.txt mouse spider bicycle Is it nice? TA teacher mouse spider bicycle Is it nice? TA teacher The following output log shows one game being played on the above tree: Welcome to CSE 143 Questions Game! Which questions file would you like to use? questions.txt Let's play! Please choose your object, and I'll start guessing. Press Enter when you're ready to begin! (y/n)? n (y/n)? y I guess that your object is bicycle! Am I right? (y/n)? y Awesome! I win! Do you want to play again (y/n)? n Initially, the computer is t very intelligent, but it grows smarter each time it loses a game. If the computer guesses incorrectly, it asks you to give it a new question to help in future games. For example, suppose in the preceding log that the player was thinking of a car instead. You might get this game log: 3

Welcome to CSE 143 Questions Game! Which questions file would you like to use? questions.txt Let's play! Please choose your object, and I'll start guessing. Press Enter when you're ready to begin! (y/n)? n (y/n)? y I guess that your object is bicycle! Am I right? (y/n)? n Boo! I Lose. Please help me get better! What is your object? car Please give me a / question that distinguishes between car and bicycle. Does it get stuck in traffic? Is the answer "" for car? (y/n)? y Do you want to play again (y/n)? n The computer takes the new information from a lost game and uses it to replace the old incorrect answer de with a new question de that has the old incorrect answer and new correct answer as its children. After the preceding log, the computer s overall game tree would be the following: spec-questions.txt mouse spider Does it get stuck in traffic? Does it get stuck in traffic? car bicycle Is it nice? TA teacher mouse spider car bicycle TA Note that, as usual, your output must match the output in the specification exactly. The output comparison tool will be helpful to make sure that your output is identical. Is it nice? teacher 4

User Input: Yes and No With interactive programs, you dont want to have more than one Scanner linked to System.in. For that reason, your QuestionGame class will construct the console Scanner, and allow QuestionMain to use it to ask a / question of the user. The interface for your class will include a method To that main will call when it wants to interact with the user. You must construct a single console Scanner that you store in a data field and use throughout your class. All calls to the console Scanner should be on the nextline method so that you always read an entire line of user input. The details of the To method are fairly uninteresting, so it is being provided for you (this code assumes a data field called console has been initialized). You are to include this method without modification in your QuestionGame class. 1 // post: asks the user a question, forcing an answer of "y" or "n"; 2 // returns true if the answer was, returns false otherwise 3 public boolean To(String prompt) { 4 System.out.print(prompt + " (y/n)? "); 5 String response = console.nextline().trim().tolowercase(); 6 while (!response.equals("y") &&!response.equals("n")) { 7 System.out.println("Please answer y or n."); 8 System.out.print(prompt + " (y/n)? "); 9 response = console.nextline().trim().tolowercase(); 10 } 11 return response.equals("y"); 12 } Development Strategy We suggest that you develop the program in the following stages: (1) Before you begin, you should stub the methods you intend to write. In other words, you want to be able to test using QuestionsMain; so, you should provide dummy methods that do thing for it to call. (2) First, you should decide what fields belong in the QuestionNode and QuestionGame classes. Once you ve chosen the fields, you should implement the first constructor (the one that takes a String). (3) Next, you should implement savequestions (because it s easier than loading them). Make sure to look back at writetree from the section handout! (4) Then, you should implement the second constructor which reads in a question tree. Make sure to look back at readtree form the section handout! (5) Finally, you should implement play. At this point, you ll be able to play the game, even though you aren t able to save or load any questions. When you play, you can add questions one by one and play with your game to check if it s working. Style Guidelines and Grading Part of your grade will come from appropriately utilizing binary trees and recursion to implement 20 questions as described previously. Every method with complex code flow should be implemented recursively, rather than with loops. A full-credit solution must have zero loops. We will also grade on the elegance of your recursive algorithm; don t create special cases in your recursive code if they are unnecessary. x = change(x) An important concept introduced in lecture was called x = change(x). This idea is related to proper design of recursive methods that manipulate the structure of a binary tree. You must follow this pattern 5

on this assignment to receive full internal correctness credit. For example, at the end of a game lost by the computer, you might be tempted to morph what used to be an answer de of the tree into a question de. Since the data in your question de must be final, this should t compile. One of the reasons this is the case is question des and answer des are fundamentally different kinds of data. You can rearrange where des appear in the tree, but you shouldn t turn a former answer de into a question de just to simplify the programming you need to perform. Avoid Redundancy Create helper method(s) to capture repeated code. As long as all extra methods you create are private (so outside code cant call them), you can have additional methods in your class beyond those specified here. If you find that multiple methods in your class do similar things, you should create helper method(s) to capture the common code. Generic Structures You should always use generic structures. If you make a mistake in specifying type parameters, the Java compiler may warn you that you have unchecked or unsafe operations in your program. If you use jgrasp, you may want to change your settings to see which line the warning refers to. Go to Settings/Compiler Settings/Workspace/Flags/Args and then uncheck the box next to Compile and type in: -Xlint:unchecked Data Fields Properly encapsulate your objects by making your fields private. (You can and should make your fields in QuestionNode public.) Avoid unnecessary fields; use fields to store important data of your objects but t to store temporary values only used in one place. Fields should always be initialized inside a constructor or method, never at declaration. Java Style Guidelines Appropriately use control structures like loops and if/else statements. Avoid redundancy using techniques such as methods, loops, and factoring common code out of if/else statements. Properly use indentation, good variable names, and types. Do t have any lines of code longer than 80 characters. Commenting You should comment your code with a heading at the top of your class with your name, section, and a description of the overall program. All method headers should be commented as well as all complex sections of code. Make sure you describe complex methods inside methods. Comments should explain each method s behavior, parameters, return values, and assumptions made by your code, as appropriate. The ArrayIntList class from lecture provides a good example of the kind of documentation we expect you to include. You do t have to use the pre/post format, but you must include the equivalent information including the type of exception thrown if a precondition is violated. Write descriptive comments that explain error cases, and details of the behavior that would be important to the client. Your comments should be written in your own words and t taken verbatim from this document. 6