CSC 222: Object-Oriented Programming Spring 2017

Slides:



Advertisements
Similar presentations
1 CSC 222: Computer Programming II Spring 2004 Structuring data & class design  Hunt the Wumpus game  Cave class  CaveMaze class.
Advertisements

Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 4.0.
Designing Classes How to write classes in a way that they are easily understandable, maintainable and reusable.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 6: File Processing.
1 CSC 222: Computer Programming II Spring 2005 Searching and efficiency  sequential search  big-Oh, rate-of-growth  binary search  example: spell checker.
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.
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.
MVC pattern and implementation in java
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Lesson 7: Improving the User Interface
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0.
1 CSC 222: Object-Oriented Programming Spring 2012 Searching and sorting  sequential search  algorithm analysis: big-Oh, rate-of-growth  binary search.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
1 CSC 321: Data Structures Fall 2013 HW1 autopsy 1.parallel lists 2.Comparable Word objects 3.Comparators 4.MVC pattern.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 3.0.
1 CSC 222: Object-Oriented Programming Spring 2012 Object-oriented design  example: word frequencies w/ parallel lists  exception handling  System.out.format.
1 CSC 221: Computer Programming I Spring 2008 ArrayLists and arrays  example: letter frequencies  autoboxing/unboxing  ArrayLists vs. arrays  example:
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/23/2012 and 10/24/2012 -Using Exceptions -Homework 4.
1 BUILDING JAVA PROGRAMS CHAPTER 6 FILE PROCESSING.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0.
1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder  netBeans IDE create/edit/run a project  GUIBuilder JFrame, JButton, JTextField,
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Session 7 Methods Strings Constructors this Inheritance.
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.
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.
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.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 6.0.
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Most of these slides are.
1 CSC 221: Computer Programming I Fall 2005 graphics & design  Dot & DotRace revisited  Circle class implementation  adding Circle methods  static.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
Programming Paradigms, Software Architectural Patterns, and MVC CS 378 – Mobile Computing for iOS Dr. William C. Bulko.
CSC 222: Object-Oriented Programming
CSC 222: Object-Oriented Programming Fall 2017
7. Modular and structured design
Lecture 7 D&D Chapter 7 & 8 Composite Classes and enums Date.
CSC 222: Object-Oriented Programming Fall 2015
Java Course Review.
Interface Java 7 COMP T1.
CSC 222: Object-Oriented Programming Fall 2017
Data Abstraction: The Walls
Building Java Programs
CPSC 315 – Programming Studio Spring 2012
Lecture 9-2: Interacting with the Superclass (super);
Objects First with Java
Programmable Logic Controllers (PLCs) An Overview.
Object Based Programming
Objects First with Java
Portability CPSC 315 – Programming Studio
Chapter 2: System Structures
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions 2nd Lecture.
CSC 581: Mobile App Development
CSC 221: Computer Programming I Fall 2009
Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract, Computer,
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
COS 260 DAY 14 Tony Gauvin.
Chapter 9 Carrano Chapter 10 Small Java
CSC 222: Object-Oriented Programming Spring 2013
CMSC 202 Exceptions 2nd Lecture.
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
CSC 222: Object-Oriented Programming Spring 2012
Random Numbers while loop
CMPE212 – Reminders Assignment 2 due next Friday.
A type is a collection of values
Observer pattern, MVC, IO & Files
CSG2H3 Object Oriented Programming
Presentation transcript:

CSC 222: Object-Oriented Programming Spring 2017 Object-oriented design, revisited highly cohesive, loosely coupled HW5: Hunt the Wumpus enumerated types MVC pattern

OO design principles recall from earlier: in a highly cohesive system: the object-oriented approach focuses on identifying the entities/objects that make up a problem solution, then build software models want code to be modular, so that it can be developed & tested independently also, want to be able to reuse useful code in a highly cohesive system: each class maps to a single, well-defined entity – encapsulating all of its internal state and external behaviors each method of the class maps to a single, well-defined behavior highly cohesive code is easier to read and reuse in a loosely coupled system: each class is largely independent and communicates with other classes via a small, well-defined interface loosely coupled code is easier to develop and modify

Previous examples

HW5: Hunt the Wumpus you will implement a variant of one of the first text-based video games written in BASIC by Gregory Yob in 1972 later ported to various PC's, (e.g., Commodore, TI) and UNIX named by Time Magazine as one of the All-Time 100 Video Games

Game rules player explores a maze of caves, with each cave connected to 3 others randomly placed wumpi (1-3), bottomless pit (1) and bat swarm (1) player can sense when an obstacle is adjacent player can move or throw a stun grenade through a tunnel, wumpi move when hear an explosion goal: avoid obstacles and capture all of the wumpi before they maul you! 5

Hunt the Wumpus design Cave: CaveContents: CaveMaze: contains all of the information about a given cave, including its contents CaveContents: special type of class for specifying the possible contents of a cave CaveMaze: models the maze of caves the caves stored in an ArrayList, linked together utilizes the Die class in order to select random locations in the maze

Cave class you must implement a class that models a single cave each cave has a name & number, and is connected to three other caves via tunnels by default, caves are empty & unvisited (although these can be updated) how do we represent the cave contents? we could store the contents as a string: "EMPTY", "WUMPUS", "BATS", "PIT" Cave c = new Cave("Cavern of Doom", 0, 1, 2, 3); c.setContents("WUMPUS"); potential problems? there are only 4 possible values for cave contents the trouble with using a String to represent these is the lack of error checking c.setContents("WUMPAS"); // perfectly legal, but ???

Enumerated types there is a better alternative for when there is a small, fixed number of values an enumerated type is a new type (class) whose value are explicitly enumerated public enum CaveContents { EMPTY, WUMPUS, PIT, BATS } note that these values are NOT Strings – they do not have quotes you specify a enumerated type value by ENUMTYPE.VALUE c.setContents(CaveContents.WUMPUS); since an enumerated type has a fixed number of values, any invalid input would be caught by the compiler

Cave javadoc be sure your class follows the javadoc specifications will need to decide what fields are needed (and only those fields!) cave contents are define by CaveContents enumerated type getCaveName will return "unknown" if that cave is unvisited, otherwise return the cave's name

CaveMaze the CaveMaze class reads in & stores a maze of caves provided version uses an ArrayList (but could have used an array) the caves and their connections are defined in a file: caves.txt cave 0 is assumed to be the start cave public class CaveMaze { private ArrayList<Cave> caves; private Cave currentCave; public CaveMaze(String filename) throws java.io.FileNotFoundException { Scanner infile = new Scanner(new File(filename)); int numCaves = infile.nextInt(); this.caves = new ArrayList<Cave>(); for (int i = 0; i < numCaves; i++) { this.caves.add(null); } int num = infile.nextInt(); int adj1= infile.nextInt(); int adj2= infile.nextInt(); int adj3= infile.nextInt(); String name = infile.nextLine().trim(); this.caves.set(num, new Cave(name, num, adj1, adj2, adj3)); this.currentCave = this.caves.get(0); this.currentCave.markAsVisited(); . . .

CaveMaze (cont.) currently, can move between caves only see the names of caves you have already visited you must add the full functionality of the game (incl. adding & reacting to dangers, winning/losing) . . . public String move(int tunnel) { int caveNum = this.currentCave.getAdjNumber(tunnel); this.currentCave = this.caves.get(caveNum); this.currentCave.markAsVisited(); return "Moving down tunnel " + tunnel + "..."; } public String showLocation() { String message = "You are currently in " + this.currentCave.getCaveName(); for (int i = 1; i <= 3; i++) { int caveNum = this.currentCave.getAdjNumber(i); Cave adjCave = this.caves.get(caveNum); message += "\n (" + i + ") " + adjCave.getCaveName(); return message;

User Interface using BlueJ, we have been able to manipulate objects directly create an object by right-clicking on the class icon (& providing inputs if necessary) call a method by right-clicking on the object icon (& providing inputs if necessary) for an interactive application like a game, you want a class to automate the top-level control convention is to have a "driver" class with a static "main" method (main is automatically called using other development environments) in this case, the main method creates the CaveMaze loops to get each player action (e.g., move or toss) calls the CaveMaze method associated with that action displays the result of the action

Terminal driver public class WumpusTerminal { public static void main(String[] args) throws java.io.FileNotFoundException { CaveMaze maze = new CaveMaze("caves.txt"); System.out.println("HUNT THE WUMPUS: Your mission is to explore the maze of caves"); System.out.println("and capture all of the wumpi (without getting yourself mauled)."); System.out.println("To move to an adjacent cave, enter 'M' and the tunnel number."); System.out.println("To toss a stun grenade into a cave, enter 'T' and the tunnel number."); Scanner input = new Scanner(System.in); while (maze.stillAble() && maze.stillWumpi()) { System.out.println("\n"+maze.showLocation()); try { String action = input.next(); if (action.toLowerCase().charAt(0) == 'q') { System.out.println("Nobody likes a quitter."); break; } int target = input.nextInt(); if (action.toLowerCase().charAt(0) == 't' && target >= 1 && target <= 3) { System.out.println(maze.toss(target)); } else if (action.toLowerCase().charAt(0) == 'm' && target >= 1 && target <= 3) { System.out.println(maze.move(target)); } else { System.out.println("Unrecognized command -- please try again."); catch (java.util.InputMismatchException e) { System.out.println("\nGAME OVER");

MVC pattern model-view-controller is a software pattern used to develop reusable, modular software goal: isolate the application-specific logic from the user interface allows for independent testing & development, easy updates for this example: the model consists of the logic of the application – Die, CaveContents, Cave, CaveMaze the view is the Java terminal window the controller is the WumpusTerminal class with its text-based input/output by separating the logic from the interface, it makes it possible to plug in a different interface, e.g., a Graphical User Interface (GUI)

GUI version for Hunt the Wumpus none of the Model classes did any input/output (other than the caves data file) the Controller (WumpusTerminal) connects the Model with the View if want to use a different View, must replace the Controller your HW5 classes should work with either WumpusTerminal or WumpusGUI