Guessing Game with Objects

Similar documents
Guessing Game with Objects

More about JOptionPane Dialog Boxes

JOptionPane Dialogs. javax.swing.joptionpane is a class for creating dialog boxes. Has both static methods and instance methods for dialogs.

Object-Oriented Programming in Java

Course PJL. Arithmetic Operations

static String usersname; public static int numberofplayers; private static double velocity, time;

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

Part I: Learn Common Graphics Components

Copyright 1999 by Deitel & Associates, Inc. All Rights Reserved.

In this lab, you will be given the implementation of the classes GeometricObject, Circle, and Rectangle, as shown in the following UML class diagram.

CST141 Thinking in Objects Page 1

OOP Lab Factory Method, Singleton, and Properties Page 1

Express Yourself. Writing Your Own Classes

Chapter 6 Lab Classes and Objects

Introduction to Java. Nihar Ranjan Roy.

Assignment 2.4: Loops

Chapter 15: Object Oriented Programming

Chapter 4 Lab. Loops and Files. Objectives. Introduction

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

Chapter 6 Lab Classes and Objects

19. GUI Basics. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

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

Java Classes, Inheritance, and Interfaces

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

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:

Lecture 17. For Array Class Shenanigans

Introduction to Objects. James Brucker

Initial Coding Guidelines

Lesson 3: Accepting User Input and Using Different Methods for Output

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

Software and Programming 1

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

CS 106 Introduction to Computer Science I

CS 11 java track: lecture 3

coe318 Lab 1 Introduction to Netbeans and Java

AP Computer Science A Summer Assignment

Object Oriented Design: Identifying Objects

CSC 111, Test 1 (Fall 2009)

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

In this lab we will practice creating, throwing and handling exceptions.

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

Object Oriented Programming and Design in Java. Session 2 Instructor: Bert Huang

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

Object Oriented Programming

Slide 1 CS 170 Java Programming 1

Text User Interfaces. Keyboard IO plus

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

Introduction to Computers and Engineering Problem Solving 1.00 / Fall 2004

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

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

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

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.

COMP19612 exam performance feedback 2014

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Advanced Programming - CS239

Classes Basic Overview

Introducing interactivity - handling commands

Introduction to Classes and Objects

Programming Exercise 14: Inheritance and Polymorphism

A Fraction Class. Using a Fraction class, we can compute the above as: Assignment Objectives

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Assignment3 CS206 Intro to Data Structures Fall Part 1 (50 pts) due: October 13, :59pm Part 2 (150 pts) due: October 20, :59pm

CSE 331 Final Exam 12/9/13

CMSC 150 Lab 8, Part II: Little PhotoShop of Horrors, Part Deux 10 Nov 2015

Write for your audience

Quarter 1 Practice Exam

Media Computation. Lecture 16.1, December 8, 2008 Steve Harrison

Creating Java Programs with Greenfoot

Chapter 02: Using Data

Lecture 14. 'for' loops and Arrays

Defining Classes and Methods

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

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

ITI 1120 Lab #9. Slides by: Diana Inkpen, Alan Williams, Daniel Amyot Some original material by Romelia Plesa

An Exceptional Class. by Peter Lavin. June 1, 2004

CMPSCI 187: Programming With Data Structures. Lecture #15: Thinking Recursively David Mix Barrington 10 October 2012

Super-Classes and sub-classes

CS 455 Midterm Exam 2 Fall 2015 [Bono] Nov. 10, 2015

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

TeenCoder : Java Programming (ISBN )

Chapter 2: Using Data

CS 139 Practice Midterm Questions #2

CSCI 2101 Java Style Guide

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

CS 106 Introduction to Computer Science I

Java and OOP. Part 2 Classes and objects

Graphical User Interfaces 2

Documentation Requirements Computer Science 2334 Spring 2016

Example Program. public class ComputeArea {

BSc. (Hons.) Software Engineering. Examinations for / Semester 2

6.170 Laboratory in Software Engineering Java Style Guide. Overview. Descriptive names. Consistent indentation and spacing. Page 1 of 5.

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

Transcription:

Objectives Lab1: Guessing Game with Objects Guessing Game with Objects 1. Practice designing and implementing an object-oriented program. 2. Use Console I/O in Java. Tasks 1. Design the program (problem 1). 2. Implement the program and write documentation.. Test it often! 3. Submit your code to Github as project named GuessingGame. Problem 1: Guessing Game Design Design a guessing game that is played on a console, like this: I'm thinking of a number between 1 and 20. What is your guess? 8 Sorry, too small. What is your guess? 14 Sorry, too large. What is your guess? 11 Right! The secret is 11. The sequence of activities in the game is: - 1 -

Game Design Using Objects In object-oriented programs, objects have responsibilities. Try to design objects so that: (a) objects behave like real things in the problem you are trying to solve (b) each object has only one set of closely related responsibilities. Try to make your objects simple and cohesive. This makes your program easier to understand, easier to maintain, and easier to modify. In the guessing game, there are 3 sets of responsibilities: 1. Interact with the user: print messages and read input 2. Manage the game: know the secret number, evaluate a guess, create hint. 3. Create objects and start the game. We will define one class (a blueprint for a kind of object) for these two sets of responsibilities: GameConsole Display messages for user Read the user's input (a guess) Use a Game object to evaluate user's guess GuessingGame Has an upper bound for secret Choose a secret number Evaluate a guess and give a reply Give hints The arrow shows that the GameConsole needs to "know" about a Game object, because GameConsole has to "ask" the Game if a user's guess is correct, and "ask" for a hint. The GameConsole and GuessingGame objects work together when you run the program. Here is an example: User print "I'm thinking of a number between 1 and 20. Your guess?" GameConsole object 10 guess(10) false gethint() GuessingGame object print "Too small. Your guess? 15 "Too small." guess(15) false gethint() "Too large." - 2 -

Application Design in UML This is a UML class diagram for the two classes in our program. GameConsole The GameConsole class interacts with the user. Responsibilities: performs input and output +play( game: Game ): int play a game. The parameter is a Game to play. The return value is the solution. GuessingGame -upperbound: int -secret: int -hint: String The Guessing Game. Responsibilities: knows about the game. UML for attributes. "-" means private. In Java write: private int secret; <<constructor>> GuessingGame(upperBound: int) <<methods>> +guess( number: int ): boolean +gethint( ): String #sethint(hint: String): void constructor: Choose a secret number. Initialize all the attributes. guess( number ) evaluate the user's guess and return true or false. Also prepare a hint. gethint( ) return the current hint. # means a protected method. It is protected so we can override it in a future version of the game. sethint( ) is used only by the Game or a subclass of GuessingGame. - 3 -

Problem 2: Write the GuessingGame Class and Test It 1. Create a new class named "Game" and open it with BlueJ editor (double click on class icon). import java.util.random; // for random numbers * Game of guessing a secret number.-- write a description here * @author Your Name -- your name, no parenthesis. public class GuessingGame { /* properties of a guessing game //TODO Declare variables for attributes of the game You can copy and paste code from the SampleGame class into the Game class to get started. 2. Define the attributes of Game. There should be 3 attributes -- as in the class diagram. For example: private int upperbound; 3. Write the constructor. The constructor initializes a new object of the Game class. Do this: * Initialize a new game. * @param upperbound is the max value for the secret number (>1). public GuessingGame( int upperbound ) { // this is a constructor } (a) set the upperbound for the secret number (b) create a secret number by calling getrandomnumber (see next item) (c) initialize the hint. Use: "I'm thinking of a number between 1 and XX". 4. Write a method to choose a random number between 1 and the upper bound. You need to import the java.util.random class for this. This is a separate method to make our code simpler and easier to read. * Create a random number between 1 and limit. * @param limit is the upper limit for random number * @return a random number between 1 and limit (inclusive) private int getrandomnumber(int limit) { long seed = System.currentTimeMillis( ); Random rand = new Random( seed ); return random.nextint(limit) + 1; } 5. Write the guess( number) method that evaluates a guess. It also creates a hint (a String) which is saved for later. public boolean guess( number ) { test whether the user's guess matches the secret. If correct, set hint to "Correct. The secret is XX.". TODO //TODO comment means something you have to do! After you do it, delete the //TODO comment. - 4 -

Otherwise, return false and set hint to "Sorry, your guess is too small." or "Sorry, you're guess is too large." Use the sethint( ) method to set the hint. 6. Create an accessor method gethint( ) that returns the hint attribute (hint variable). In Java, an accessor method begins with the word "get" (or "is" for boolean values). * Return a hint based on the most recent guess. * @return hint based on most recent guess public String gethint( ) { //TODO complete this. Its easy! 7. Compile your code and fix errors. 8. View your Javadoc in the editor. Is it complete? Do you have @author, @param, @return tags? 9. Write a main method to test this class. a) does it create random numbers between 1 and upperbound? (not 0) b) does it give the correct hint for every guess? c) what if you guess something ridiculous (negative number or greater than upperbound)? You should test every class! Don't assume that it is correct. NO JAVADOC = NO CREDIT If you don't write good Javadoc documentation for all your code, including class and method descriptions, you won't get any credit for your work. - 5 -

Problem 3: Write the GameConsole Class and Test It This class has only 1 method: play. 1. Create a new class named GameConsole. 2. Write the play( game ) method. public int play(game game) { //TODO play game on the console. Return the secret number when user guesses it. } The algorithm for play(game) is like this: Algorithm for play( game ) create a title string = "Guessing Game" create a prompt-string = "Your guess? " print the title while user has not guessed correctly get a hint from the game print the hint and prompt-string read a number from console ask game if number is correct // after while loop print a result message return the solution (the guessed secret) Call game.gethint( ) to get a hint. Call game.guess(number). Save the result as a variable so you can test it in the while( ) stmt. 4. Write Javadoc comments for the GameConsole class and the play() method. * The play method plays a game using input from a user. * @param... * @return... Problem 4: Write a Main class to create objects and start the game 1. Create a class named Main with a static main method. The job of the main method is to create objects, connect user interface to the game, and start the user interface. main is a static method. main should be simple (no application logic). create objects and start the game public static void main( String [] args ) { GuessingGame game = new GuessingGame( upperbound ); GameConsole ui = new GameConsole( ); ui.play( game ); } 5. Compile your GameConsole until there are no errors. 6. Run it as demonstrated in class (right click on GameConsole and select main). - 6 -

Problem 5: Count the guesses 1. Add a counter to the GuessingGame class that counts how many guesses the user makes. Add a counter only to the GuessingGame class. 2. Provide an accessor method for the counter. Accessor is a "getcount( )" method. 3. Write good Javadoc for the accessor method including @returns... Test the code. 4. When the user guesses the correct answer, the game should tell him how many guesses he used: Correct. You used 7 guesses. - 7 -

Problem 6: (Optional) Use Dialog Boxes instead of console input This problem shows the benefit of using separate objects for separate responsibilities. Instead of playing on the console, we can use dialog boxes. We won't need to rewrite the application! Just replace the GameConsole class with a new GameDialog class. We can reuse the Game class. In a real project, you will often write a simple console input class to test your program logic, then write a graphical interface for really playing the game. 1. Create a new class named GameDialog. Copy the methods from GameConsole to GameDialog. 2. Modify GameDialog to use dialog boxes instead of System.in and System.out. How to Use Dialog Boxes JOptionPane (in package javax.swing) contains many useful dialogs. Here are some examples. You can run these examples interactively in BlueJ. Message Dialog import javax.swing.joptionpane; String title = "Alarm Clock"; String message = "Wake Up."; int type = JOptionPane.INFORMATION_MESSAGE; JOptionPane.showMessageDialog( null, message, title, type); Message Dialog with multi-line message: message = "Wake up.\nits time for lunch"; type = JOptionPane.WARNING_MESSAGE; JOptionPane.showMessageDialog( null, message, title, type); Input Dialog: Use this to print a hint and ask user to guess a number. title = "OOP Coffee Shop"; message = "We have all kinds of coffee.\n"; message += "What would you like to drink?"; type = JOptionPane.QUESTION_MESSAGE; String reply = JOptionPane.showInputDialog(null,message,title,type); if ( reply == null ) /* user pressed Cancel ; else ordercoffee( reply ); Test if user presses Cancel The dialog returns a null. Confirm Dialog: String title = "Guessing Game"; message = "Play again?"; type = JOptionPane.YES_NO_OPTION; int opt = JOptionPane.showConfirmDialog(null, message, title, type); if ( opt == JOptionPane.YES_OPTION ) /* user pressed "Yes" ; - 8 -