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

Similar documents
Recursion With Data Structures

RAIK 183H Examination 2 Solution. November 10, 2014

RAIK 183H Examination 2 Solution. November 11, 2013

AP CS Unit 11: Graphics and Events

H212 Introduction to Software Systems Honors

MIT AITI Swing Event Model Lecture 17

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

To gain experience using GUI components and listeners.

CMP 326 Midterm Fall 2015

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

CSC 1214: Object-Oriented Programming

First Name: AITI 2004: Exam 2 July 19, 2004

Example 3-1. Password Validation

First Name: AITI 2004: Exam 2 July 19, 2004

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2015

Class 27: Nested Classes and an Introduction to Trees

Advanced Java Unit 6: Review of Graphics and Events

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

Compuer Science 62 Assignment 10

Original GUIs. IntroGUI 1

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

CSIS 10A Assignment 7 SOLUTIONS

Programming Languages and Techniques (CIS120)

GUI Forms and Events, Part II

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb

1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall (total 7 pages)

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

/* Write a Program implementing GUI based Calculator using Swing */

Theory Test 3A. University of Cape Town ~ Department of Computer Science. Computer Science 1016S ~ For Official Use

Swing Programming Example Number 2

JAVA NOTES GRAPHICAL USER INTERFACES

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Swing from A to Z Some Simple Components. Preface

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Graphics User Defined Forms, Part I

ANSWER KEY Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009

Java Help Files. by Peter Lavin. May 22, 2004

The first version of the bank will look like this:

Give one example where you might wish to use a three dimensional array

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

We are on the GUI fast track path

Class 16: The Swing Event Model

Window Interfaces Using Swing Objects

Chapter 13 Lab Advanced GUI Applications

Part I: Learn Common Graphics Components

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

SINGLE EVENT HANDLING

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2012

CompSci 105 S2 C - ASSIGNMENT TWO -

Dr. Hikmat A. M. AbdelJaber

Decisions: Logic Java Programming 2 Lesson 7

DM503 Programming B. Peter Schneider-Kamp.

University of Cape Town Department of Computer Science Computer Science CSC1017F

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

CS 134 Programming Exercise 7:

Window Interfaces Using Swing Objects

Graphical User Interfaces 2

Swing - JTextField. Adding a text field to the main window (with tooltips and all)

Introduction This assignment will ask that you write a simple graphical user interface (GUI).

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Midterm Test II Object Oriented Programming in Java Computer Science, University of Windsor Fall 2014 Time 2 hours. Answer all questions

Project 1. LibraryTest.java. Yuji Shimojo CMSC 335

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

TTTK Program Design and Problem Solving Tutorial 3 (GUI & Event Handlings)

Name: CSC143 Exam 1 1 CSC 143. Exam 1. Write also your name in the appropriate box of the scantron

Graphical User Interface

CS180 Spring 2010 Exam 2 Solutions, 29 March, 2010 Prof. Chris Clifton

Queens College, CUNY Department of Computer Science. CS 212 Object-Oriented Programming in Java Practice Exam 2. CS 212 Exam 2 Study Guide

CMP-326 Exam 2 Spring 2018 Solutions Question 1. Version 1. Version 2

CS 2334: Programming Structures and Abstractions Exam 2 November 4, Please wait to open this exam booklet until you are told to do so.

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

CS Exam 3 - Spring 2010

Points Missed on Page page 1 of 8

Chapter 6 Using Objects

Final Examination Semester 2 / Year 2012

CS Exam 3 - Spring 2010

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

RobotPlanning.java Page 1

University of Cape Town ~ Department of Computer Science. Computer Science 1016S / 1011H ~ November Exam

SampleApp.java. Page 1

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

Graphical User Interfaces. Swing. Jose Jesus García Rueda

CS 180 Final Exam Review 12/(11, 12)/08

Critical Thinking Assignment #6: Java Program #6 of 6 (70 Points)

1 Looping Constructs (4 minutes, 2 points)

ESC101 : Fundamental of Computing

CS Exam 1 Review Suggestions

Graphical User Interfaces

Transcription:

COMP1406 - Assignment #10 (Due: Monday, March 31st @ 11:30pm) In this assignment you will practice using recursion with data structures. (1) Consider the following BinaryTree class: public class BinaryTree { private String private BinaryTree private BinaryTree data; leftchild; rightchild; public BinaryTree(String d) { data = d; leftchild = null; rightchild = null; public BinaryTree(String d, BinaryTree left, BinaryTree right) { data = d; leftchild = left; rightchild = right; Write a recursive method in this class called hassamestructureas(binarytree tree) that returns whether or not a tree has the same structure as another tree. Two trees have the same left/right children locations all the way through from the root to the leaves. The data at each node need not be the same, however. You will not receive any marks if your code is non-recursive or if it contains any loops. Use the following to test your code: public class BinaryTreeTest { public static void main(string[] args) { BinaryTree[] trees = new BinaryTree[8]; trees[0] = new BinaryTree("A"); trees[1] = new BinaryTree("A", new BinaryTree("B"), new BinaryTree("C")); trees[2] = new BinaryTree("A", new BinaryTree("B", new BinaryTree("C"), null), new BinaryTree("D")); trees[3] = new BinaryTree("A", new BinaryTree("C", new BinaryTree("D"), new BinaryTree("E", new BinaryTree("F", new BinaryTree("G"), null), new BinaryTree("H")))); trees[4] = new BinaryTree("A", new BinaryTree("B", new BinaryTree("C", new BinaryTree("D"), new BinaryTree("E", new BinaryTree("F", new BinaryTree("G"), new BinaryTree("I")), new BinaryTree("H"))), new BinaryTree("J", new BinaryTree("K", new BinaryTree("L",

new BinaryTree("M"))), new BinaryTree("N", new BinaryTree("O")))), new BinaryTree("P", new BinaryTree("Q"), new BinaryTree("R", new BinaryTree("S", new BinaryTree("T"), null), new BinaryTree("U")))); trees[5] = new BinaryTree("A", new BinaryTree("P", new BinaryTree("R", new BinaryTree("U")))); trees[6] = new BinaryTree("A", new BinaryTree("P", new BinaryTree("R", new BinaryTree("W"), new BinaryTree("U")))); trees[7] = new BinaryTree("A", new BinaryTree("B", new BinaryTree("C", new BinaryTree("E", new BinaryTree("F", new BinaryTree("G"), new BinaryTree("I")), new BinaryTree("H"))), new BinaryTree("J", new BinaryTree("K", new BinaryTree("L", new BinaryTree("M"))), new BinaryTree("N", new BinaryTree("O")))), null); for (int i=0; i<8; i++) for (int j=0; j<8; j++) System.out.println("Tree[" + i + "] == trees[" + j + "]: " + trees[i].hassamestructureas(trees[j])); In this test case, the results should show that only a tree compared against itself will result in true and all the rest should be false. (2) Below is a RoomMaze class. This class represents a grid in which there are walls at various locations in an 8 x 8 grid. The walls separate rooms. Your task is to complete the identifyrooms() method (it MUST be recursive) and the traceroomfrom() method so that it calculates and returns an integer indicating the number of rooms in the maze. The code must assign a unique color index to each room, starting with the number 1 (i.e., don't start at 0 because 0 represents a wall location). So, each location in the maze should be assigned an integer indicating the room that that grid location belongs to. These indices will be used to color the room in the test program. Note that if two wall locations are diagonally adjacent to one another (i.e., their corners touch) then this represents a separation between rooms. The image here shows walls (as red) and 7 rooms that have been uniquely identified:

public class RoomMaze { public static byte ROWS = 8; private byte[][] walltable; public RoomMaze() { walltable = new byte[rows][rows]; resetwalls(); public void resetwalls() { for (int r=0; r<rows; r++) for (int c=0; c<rows; c++) { walltable[r][c] = (byte)(math.random()*2); public byte getwall(int r, int c) { return walltable[r][c]; public int identifyrooms() { // Go through each location in the maze. If there is not a // wall there, then start tracing out the room recursively. // Keep count of how many rooms are traced out and assign // a colorindex (i.e., store it in the walltable) to each // room being traced. Wall locations MUST have a value // of zero in the walltable. You may add attributes to this // class if you need to. return 1; // Replace this line public void traceroomfrom(int r, int c, byte colorindex) { // Code missing. This method MUST be directly recursive To test your code, use the following class: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class RoomCounterApp extends JFrame { public static ImageIcon wall = new ImageIcon("squareButton.png"); // These are the model-specific variables private RoomMaze maze; // These are window components private JButton[][] buttons; private JTextField countfield; // This constructor builds the window public RoomCounterApp(String title) { super(title); initializeboard(); setupcomponents(); setdefaultcloseoperation(exit_on_close); setsize(578,634);

setresizable(false); update(); // Initialize the board. Do this whenever a new game is started. private void initializeboard() { maze = new RoomMaze(); // Here we add all the components to the window accordingly private void setupcomponents() { getcontentpane().setlayout(null); // Setup the panel with the buttons buttons = new JButton[RoomMaze.ROWS][ RoomMaze.ROWS]; JPanel tiles = new JPanel(); tiles.setlayout(null); // Add the buttons to the tile panel for (int r=0; r<roommaze.rows; r++) { for (int c=0; c<roommaze.rows; c++) { buttons[r][c] = new JButton(wall); buttons[r][c].setbackground(color.white); buttons[r][c].setsize(69,69); buttons[r][c].setlocation(c*69, r*69); tiles.add(buttons[r][c]); tiles.setlocation(10,10); tiles.setsize(552,552); getcontentpane().add(tiles); // Add the score and buttons JLabel alabel = new JLabel("Separate Pieces:"); alabel.setsize(150,25); alabel.setlocation(10, 572); getcontentpane().add(alabel); countfield = new JTextField(); countfield.setsize(50,25); countfield.setlocation(120, 572); countfield.sethorizontalalignment(jtextfield.right); getcontentpane().add(countfield); JButton resetbutton = new JButton("Reset"); resetbutton.setsize(80,25); resetbutton.setlocation(482, 572); getcontentpane().add(resetbutton); resetbutton.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { maze.resetwalls(); update(); ); // Update the board private void update() { int count = maze.identifyrooms(); Color[] colors = new Color[count];

for (int i=0; i<count; i++) { colors[i] = new Color((int)(10+Math.random()*200), (int)(10+math.random()*200), (int)(10+math.random()*200)); countfield.settext("" + count); // Update the look of the buttons for (int r=0; r<roommaze.rows; r++) { for (int c=0; c<roommaze.rows; c++) { if (maze.getwall(r,c) == 0) { buttons[r][c].seticon(wall); else { buttons[r][c].seticon(null); buttons[r][c].setbackground(colors[maze.getwall(r,c)-1]); public static void main(string[] args) { JFrame frame = new RoomCounterApp("Room Counter"); frame.setvisible(true); On the next page there are some screen snapshots showing what your results should look like, depending on the random arrangement of the walls and the randomly chosen colors:

NOTE: Submit all.java files needed to run as well as any image icon files. You MUST NOT use packages in your code, nor projects. Submit ALL of your files in one folder such that they can be opened and compiled individually in JCreator. Some IDEs may create packages and/or projects automatically. You MUST export the.java files and remove the package code at the top if it is there. Do NOT submit JCreator projects either. JUST SUBMIT the JAVA FILES and image icon files. Note that if your internet connection at home is down or does not work, we will not accept this as a reason for handing in an assignment late... so make sure to submit the assignment WELL BEFORE it is due! Please NOTE that you WILL lose marks on this assignment if any of your files are missing. You will also lose marks if your code is not written neatly with proper indentation. See examples in the notes for proper style.