CompSci 105 S2 C - ASSIGNMENT TWO -

Similar documents
Lecture Chapter 6 Recursion as a Problem Solving Technique

CS 211 Programming Practicum Fall 2018

CS 211 Programming Practicum Spring 2018

Lab 7 1 Due Thu., 6 Apr. 2017

CmpSci 187: Programming with Data Structures Spring 2015

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

Assignment 4 Publication date: 27/12/2015 Submission date: 17/01/ :59 People in-charge: R. Mairon and Y. Twitto

Assignment 5. Introduction

CS 211 Programming Practicum Spring 2017

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

CS 2210a Data Structures and Algorithms Assignment 5 Solving a Labyrinth Due Date: December 6, 11:59 pm Total marks: 20

Postfix Notation is a notation in which the operator follows its operands in the expression (e.g ).

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

CMPSC 111 Introduction to Computer Science I Fall 2016 Lab 8 Assigned: October 26, 2016 Due: November 2, 2016 by 2:30pm

SCHOOL OF COMPUTER AND COMMUNICATION ENGINEERING. EKT224: ALGORITHM AND DATA STRUCTURES (Stack, Queue, Linked list)

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

Top of the Stack. Stack ADT

CS2383 Programming Assignment 3

Stacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal.

Learning outcomes. COMPSCI 101 Principles of Programming. Drawing 2D shapes using Characters. Printing a Row of characters

2IS45 Programming

ECSE 321 Assignment 2

CPSC 217 Assignment 3

CSE P 501 Exam 8/5/04

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

CIS 110: Introduction to Computer Programming

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

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

PA3 Design Specification

An Introduction to Trees

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

CS W3134: Data Structures in Java

CS 201 Advanced Object-Oriented Programming Lab 5 - Sudoku, Part 1 Due: March 3/4, 11:30 PM

What is Flubaroo? Step 1: Create an Assignment

CS 171: Introduction to Computer Science II. Stacks. Li Xiong

Lecture 4: Stack Applications CS2504/CS4092 Algorithms and Linear Data Structures. Parentheses and Mathematical Expressions

Data Structures and Algorithms Winter Semester

Stacks. Revised based on textbook author s notes.

COMP 250 Winter 2010 Exercises 6 - trees March 2010

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Project 1: Implementation of the Stack ADT and Its Application

CS 314 Exam 2 Spring

CS Fall Homework 5 p. 1. CS Homework 5

1. Stack Implementation Using 1D Array

Top of the Stack. Stack ADT

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

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

CS 2604 Minor Project 1 Summer 2000

12 Abstract Data Types

Stacks Fall 2018 Margaret Reid-Miller

n Specifying what each method does q Specify it in a comment before method's header n Precondition q Caller obligation n Postcondition

Largest Online Community of VU Students

Operators. Java operators are classified into three categories:

Introduction to Programming Using Java (98-388)

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Stack and Its Implementation

SEMESTER 1, 2011 EXAMINATIONS. CITS1200 Java Programming FAMILY NAME: GIVEN NAMES:

MORE ARRAYS: THE GAME OF LIFE CITS1001

CISC 1600 Lecture 3.1 Introduction to Processing

King Abdulaziz University Faculty of Computing and Information Technology Computer Science Department

CMPE 180A Data Structures and Algorithms in C++ Spring 2018

Prefix/Infix/Postfix Notation

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

Homework Assignment #3

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

SimpleCalc. which can be entered into a TI calculator, like the one on the right, like this:

Csci 102: Sample Exam

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator

CS 307 Final Fall 2009

CS 134 Programming Exercise 7:

CS 2604 Minor Project 1 DRAFT Fall 2000

Linear Data Structure

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

ESC101 : Fundamental of Computing

LECTURE 17. Expressions and Assignment

CS 314 Midterm 2 Fall 2012

CS Programming Languages Fall Homework #1

Backtracking. The Eight Queens Problem. The Eight Queens Problem. The Eight Queens Problem. The Eight Queens Problem. Chapter 5

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

CS 134 Test Program #2

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Administrivia. Last modified: Fri Aug 25 10:59: CS61B: Lecture #2 1

COMP Assignment #10 (Due: Monday, March 11:30pm)

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Chapter 3 Structure of a C Program

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

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

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

CPSC 121: Models of Computation Assignment #5

Project #2: Linear Feedback Shift Register

Thomas Jefferson Invitational Open in Informatics 2012

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Programming Assignment 2 ( 100 Points )

D - Tic Tac Toe. Let's use our 9 sparkles to build a tic tac toe game! 2017 courses.techcamp.org.uk/ Page 1 of 9

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Stack Abstract Data Type

1007 Imperative Programming Part II

Programming Project: Game of Life

Transcription:

CompSci 105 S2 C - ASSIGNMENT TWO - The work done on this assignment must be your own work. Think carefully about any problems you come across, and try to solve them yourself before you ask anyone else for help. Under no circumstances should you use code written by another person in your assignment solution. Assignment Two Due 4.30pm, Thursday 27 th September, 2012 Worth 8% of your final mark Aim of the assignment practice with recursion practice with 2D arrays practice writing to a text file practice with Stacks and Queues There are two parts for this assignment. Each part is worth 50% of the assignment. For part A you need to submit SIX Java source files: A2PartA.java A2Constants.java MyColourButton.java A2JPanel.java Cell.java BlockOfCells.java For part B you need to submit the Java source file, Calculator.java. Please do not change any of the other classes in the resource files for part B. You also need to submit your feedback on the assignment (A2.txt - see later in this document) NOTE: For part A of this assignment you are required to make one change to the A2PartA class, to complete the BlockOfCells class and to complete the storegame() method in the JPanel class. The other classes are complete. Part A The ColourMe Game (50 marks) Task 1 (3 Marks) Your UPI should be displayed inside the title bar of the window. To do this, open the A2PartA.java application file and insert your UPI, e.g., JFrame colourme = new A2JFrame("A2 Part A by abcd001",... ); Task 2. The ColourMe game (37 Marks) The aim of this game is to fill the whole game board with the same colour in less than 25 moves. The board is made up of six colour buttons at the top of the JPanel and a grid of cells and, initially, each cell is assigned a random colour (there are 6 possible colours). Each cell has 4 adjacent 'connected' neighbours (above, to the right, below, to the left) except for the border cells. The user selects a colour by pressing one of the colour buttons and, starting from the top-left cell, all the cells adjacent to the top-left cell with the same colour as the top-left cell, change to the new colour selected by user.

COMPSCI 105 - A2 2 Slowly by selecting different colours the user changes the whole board to one single colour. For example, given the following section of the ColourMe board:... If the user now presses the pale green button all the touching orange cells change to pale green. If the user now presses the red button all the touching pale green cells change to red. If the user now presses the blue button all the touching red cells change to blue. and so on until the board becomes more and more filled with the same colour: Task 2 (37 Marks) Task 2 has been broken up into five stages. The classes in this program have been completed except for the BlockOfCells class (and part of the JPanel class see Task 3) which you need to complete. The BlockOfCells class represents the whole grid of coloured cells and handles all the actions to do with managing the individual cells. The skeleton of the BlockOfCells class is shown on the next page:

COMPSCI 105 - A2 3 public class BlockOfCells { public static final int NUMBER_OF_ROWS =... public static final int NUMBER_OF_COLS =... public static final int CELL_SIZE = A2Constants.CELL_SIZE; public static final Rectangle GAME_AREA =... private Cell[][] cellblock; public BlockOfCells() {... cellblock = new Cell[NUMBER_OF_ROWS][NUMBER_OF_COLS]; createthecells(cellblock); private void createthecells(cell[][] cellblock) { int x = GAME_AREA.x; int y = GAME_AREA.y; for(int i = 0; i < cellblock.length; i++) { x = GAME_AREA.x; for(int j = 0; j < cellblock.length; j++) { cellblock[i][j] = new Cell(x, y); x = x + CELL_SIZE; y = y + CELL_SIZE; // Stage 2 (4 marks) Methods to do with the colour index of // each cell public int getcellcolourindex(int row, int col) {... public void setcellcolourindex(int row, int col, int colourindex) {... // Stage 3 (4 marks) Reset hasbeenvisited for all cells private void resetallcellhasbeenvisited() {... // Stage 5 (10 marks) Recursive method which returns the number of // cells connected (i.e., with the same colour index as the // top left cell (0, 0)) public int getnumberofconnectedcells() { int colourindex = getcellcolourindex(0, 0); resetallcellhasbeenvisited(); return getnumberofcellsinuserblock(0, 0, colourindex); private int getnumberofcellsinuserblock(... ) {... // Stage 4 (10 marks) Recursively updates the colour of all cells // connected (i.e., with same colour index as the top left // cell (position 0, 0)) public void updateconnectedcells(int usercolourindex) { int colourtochangeindex = getcellcolourindex(0, 0); resetcellhasbeenvisited(); updateuserareacolours(0, 0, getcellcolourindex, colourtochangeindex); private void updateuserareacolours(... ) {... // Stage 6 (4 marks) returns a String with all the colour // indexes of the cells concatenated, row by row public String colourindexestostring() {...

COMPSCI 105 - A2 4 // Stage 1 (5 marks) Draw the 2D array of coloured cells public void drawcells(graphics g) {... Notes on Task 2 Stage 1 drawcells() 5 marks The BlockOfCells class has one instance variable: private Cell[][] cellblock; which stores the grid of Cell objects. This method draws all the Cell objects in the grid. Once you have completed stage 1, the user should see all the coloured cells. The cells are randomly assigned colours. Please note that the user can play a new game by pressing the 'n' (or 'N') key hence you can see a new randomly coloured grid by pressing the 'n' key. Stages 2 and 3 see the skeleton code above - 8 marks Stage 4 updateuserareacolours() 10 marks This instance method is called whenever the user chooses a new colour by pressing one of the Colour buttons. This method should update the colour of all the cells connected to the top left cell (0, 0) to the new colour selected by the user. Note that each cell has 4 adjacent (i.e., connected) neighbours (except for the border cells). Note that each cell stores the index of its fill colour (index of one of the colours in the COLOURS array). To complete this method you should use a recursive algorithm. Once you have completed stage 4, the user should be able to fill the grid with colour by pressing the Colour buttons. Stage 5 getnumberofcellsinuserblock() 10 marks This instance method is called after the colour in the cells has been changed (i.e., after the user has pressed one of the Colour buttons). This method returns the number of cells which have the same colour as the top left cell (0, 0) and are connected to the top left cell. Note that each cell has 4 adjacent (i.e., connected) neighbours (except for the border cells). Note that each cell stores the index of its colour (index of one of the colours in the COLOURS array). To complete this method you should use a recursive algorithm. Once you have completed stage 5, you should see the current number of connected cells in the JPanel on the right of the grid of cells. This number should change depending on how many connected cells there are. Stage 6 see the skeleton code above - 4 marks

COMPSCI 105 - A2 5 Task 3 (5 Marks) In the A2JPanel class complete the storegame() method. This method writes the current state of the ColourMe game to the file (the name of the file where the game is stored is passed as a parameter to the storegame() method). The format of the file is as shown in the screenshot below. The first line of the text file contains the values of the four instance variables: and gamehasended (boolean), userhaswon (boolean), turnsremaining (int) numberconnected (int) A new line follows and then the indexes of all the colours of each cell (taken row by row) are stored in the file (the colourindexestostring() method of the BlockOfCells class is useful here). Please Note Now that you have completed task 3, the user is able to (see the code in the keypressed() method in the JPanel class): and save the current game by pressing the 's' (or 'S') key, load the most recently saved game by pressing the 'l' (or 'L') key, reload the current game from the beginning by pressing the 'r' (or 'R') key. Note that the initial state of the current game is stored in the text file, "restartgame.txt", and the last saved game is stored in the text file, "currentgamestate.txt". You have now completed part A of this assignment.

COMPSCI 105 - A2 6 Part B A Simple Calculator Program (50 marks) Design and implement a simple mathematical expression calculator using the Stack data structure. The mathematical expressions used in this assignment are made up of: and the operators '+', '-', '*' and '/', parentheses '(' and ')', operands which consist of integer numbers (single or multi-digit). The mathematical expressions are in infix format, and the operators '*', '/' have higher precedence than the operators '+', '-'. The mathematical expressions contain no unary operators and no spaces. For this question, you need to write a program called Calculator, which 1) checks the syntax of the expression, and converts the infix form into its equivalent postfix form (using spaces to separate the numbers and operators); 2) calculates the value of the mathematical expression (after it has been converted into postfix form) and prints the result. Your program must compute the value of the expression correctly. The mathematical expression in infix form is passed to the program as a command line argument. Your program should print the infix expression, the postfix expression and the calculated value. You are required to use the stack data structure to solve this problem. Two example outputs using the Calculator program are shown below: c:\105\a2>java Calculator (8+12)*3/(14-9) A simple calculator by abdc001. Evaluating... Infix Expression: (8+12)*3/(14-9) Postfix Expression: 8 12 + 3 * 14 9 - / Result: 12.0 Note that syntax errors in the user input expressions should be detected, e.g., c:\105\a2>java Calculator 5+(2-4/5*6 A simple calculator by abdc001. Evaluating... Infix Expression: 5+(2-4/5*6 Syntax error: brackets mismatch -> too many open brackets Hints Use a Stack structure to convert an infix expression to its postfix form, and then use a Stack structure to compute the value of the postfix expression (algorithms are taught in the lectures). In the case of checking syntax errors of an infix mathematical expression, there are five major types of errors that you should consider: invalid inputs, e.g., non-digit or other characters. Note: a constant indicating the valid characters might be useful in your program, e.g., final String ALL_VALID = "0123456789+-/*()"; brackets mismatch, e.g., too many open bracket, too many closing bracket; missing operand, e.g., two consecutive operators, '(' followed by operator, operator followed by ')', expression end with '(', empty bracket '( )', expression begin with ')', expression begin with operators, expression end with operators; missing operator, e.g., digit followed by '(', ')' followed by digit, ')' followed by '('; division by zero, e.g., '/' followed by 0;

COMPSCI 105 - A2 7 Notes For Part B Your program must use the StackReferenceBased class to solve the problem. For this question, you should submit the Calculator.java class. Your UPI must appear in the output, as shown in the example above. Submission Instructions You should submit all of the following files for this assignment through the web-based Assignment Dropbox. REQUIRED FILES for part A of the assignment For this part of the assignment you need to submit FOUR Java source files and ONE text file: A2PartA.java A2Constants.java MyColourButton.java A2JPanel.java Cell.java BlockOfCells.java REQUIRED FILES for part 2 of the assignment For this part of the assignment you need to submit ONE Java source files: Calculator.java FEEDBACK FILE for the assignment A2.txt - a text file containing your feedback on the assignment (see below). MAKING MORE THAN ONE SUBMISSION You can make more than one submission - every submission that you make replaces your previous submission. Submit ALL your files in every submission. Only your very latest submission will be marked. NEVER SUBMIT SOMEONE ELSE'S WORK: If you submit an assignment you are claiming that you did the work. Do not submit work done by others. Do not under any circumstances copy anyone else's work this will be penalized heavily. Do not under any circumstances give a copy of your work to someone else. We uses copy detection tools on the files you submit. If you copy from someone else, or allow someone else to copy from you, this copying will be detected and disciplinary action will be taken. What you should include in the A2.txt file You must include a text file named A2.txt in your submission. There will be a 5 mark penalty for not doing so. This text file must contain the following information: Your full name Your login name and ID number How much time did the assignment take overall? What areas of the assignment did you find easy? What areas of the assignment did you find difficult? Any other comments you would like to make.

COMPSCI 105 - A2 8 Marking Schedule for part A Part A - Style Comment at the top of the BlockOfCells class with name, date and a comment. (1) Correct indentation. (2) Code is easy to read and understand. (2) /5 Part A - Student's UPI is in the title bar of the window. /3 Part A - Task 2 the BlockOfCells class Part A - Task 3 the storegame() method in the A2JPanel class /37 /5 Total for part 1 /50 Marking Schedule for part B Part 2 - Style Appropriate comments, structures and variable names /5 Part 2 - Task 1 Check the syntax of the mathematical expression and, if the expression is syntactically correct, convert the infix form into its equivalent postfix form. Part 2 - Task 2 Compute the value of the postfix expression. /30 /15 Total for part 2 /50 Penalties A2.txt information was not submitted - penalty of 5 marks