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

Similar documents
Topics. The Development Process

public class Q1 { public int x; public static void main(string[] args) { Q1 a = new Q1(17); Q1 b = new Q1(39); public Q1(int x) { this.

Programming II (CS300)

Programming II (CS300)

Program Correctness and Efficiency. Chapter 2

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

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

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

BBM 102 Introduction to Programming II Spring Exceptions

B2.52-R3: INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING THROUGH JAVA

Programming for Mobile Computing

Object Oriented Programming. Week 7 Part 1 Exceptions

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.

Declarations and Access Control SCJP tips

Java Errors and Exceptions. Because Murphy s Law never fails

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

CS 209 Sec. 52 Spring, 2006 Lab 6 - B: Inheritance Instructor: J.G. Neal

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

The price of gold. source: nowiknow.com. moodle.yorku.ca CSE 1020

Chapter 4 Introduction to Control Statements

1 Shyam sir JAVA Notes

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

Chapter 02: Using Data

Exceptions Handling Errors using Exceptions

Introduction to Computing II (ITI 1121) Final Examination

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

BBM 102 Introduction to Programming II Spring 2017

Introduction to Programming Using Java (98-388)

Exception-Handling Overview

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

Array. Prepared By - Rifat Shahriyar

ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit

11 Using JUnit with jgrasp

Lab 5: Java IO 12:00 PM, Feb 21, 2018

C# Programming for Developers Course Labs Contents

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 7

Another Example. Other Constructs

ASSIGNMENT 5 Data Structures, Files, Exceptions, and To-Do Lists

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

Stage 11 Array Practice With. Zip Code Encoding

COE318 Lecture Notes Week 10 (Nov 7, 2011)

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator

Chapter 2: Using Data

Full file at Chapter 2 - Inheritance and Exception Handling

16-Dec-10. Consider the following method:

The University of Melbourne Department of Computer Science and Software Engineering Software Design Semester 2, 2003

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

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

CMSC 331 Second Midterm Exam

Chapter 2: Using Data

Exceptions Handeling

Exceptions and Libraries

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

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

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

F I N A L E X A M I N A T I O N

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

Atropos User s manual

COP 3330 Final Exam Review

H212 Introduction to Software Systems Honors

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Exception Handling. General idea Checked vs. unchecked exceptions Semantics of... Example from text: DataAnalyzer.

Full file at

Chapter 9. Exception Handling. Copyright 2016 Pearson Inc. All rights reserved.

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

Announcements. Application of Recursion. Motivation. A Grammar. A Recursive Grammar. Grammars and Parsing

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

More about JOptionPane Dialog Boxes

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Exceptions. CSC207 Winter 2017

Errors and Exceptions

Programming for Mobile Computing

Introduction to Computing II (ITI 1121) Final Examination

Data Types Reference Types

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

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

School of Informatics, University of Edinburgh

Introduction to Java. Nihar Ranjan Roy.

LAB 11: METHODS. CPCS The Lab Note Lab 11 Page 1. Statement Purpose:

Internal Classes and Exceptions

CMSC 202. Exceptions

15CS45 : OBJECT ORIENTED CONCEPTS

Tutorial 8 Date: 15/04/2014

COMP-202 Unit 9: Exceptions

Testing on Steriods EECS /30

public static boolean isoutside(int min, int max, int value)

ASSIGNMENT 5 Objects, Files, and More Garage Management

Soda Machine Laboratory

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

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

COMP-202 Unit 9: Exceptions

CS1020 Data Structures and Algorithms I Lecture Note #8. Exceptions Handling exceptional events

INSTRUCTIONS TO CANDIDATES

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Transcription:

Lab 5 Exceptions Exceptions indicate that a program has encountered an unforeseen problem. While some problems place programmers at fault (for example, using an index that is outside the boundaries of an array) others are unpredictable (for example, a file cannot be saved because the hard drive crashed). Programming languages are now designed to throw exceptions when these problems happen. In some cases, catching exceptions allows users a choice of alternative courses of action (for example, the user can select another drive to save the file). In this lab we will practice creating, throwing and handling exceptions. 1. Combination Lock A combination lock (like the one in Figure 1) is a lock with a dial for which a series of numbers (which together are called a combination) are used to unlock it. Each lock has a dial range (which is the range of numbers in the dial, from 0 up to an upper limit), and an unlocking combination to unlock it (which for simplicity in our case is a sequence of 3 numbers). Since an unlocking combination must have only numbers that are in the dial, a lock can only be created if its unlocking combination is a valid combination which is one in which all its numbers are in the dial. For example, the combination { 2, 9, 1 } is valid if assigned to a lock whose dial goes from 0 to 9, but is invalid if it is assigned to a lock whose dial only goes up to 5. Figure 1. Combination lock. Once created, locks can receive opening combinations for unlocking; as expected, if an opening combination is not the same as the unlocking combination then the lock does not open. Your task is to create classes that mimic the behavior of a lock while making use of exceptions.

Exercise 1 1. Create a Lab05 project. 2. Create a class Combination. This is a class that simply stores 3 integers (making a lock combination), and implements the methods below: A constructor receiving 3 integer numbers (each as a separate parameter), which together represent a lock combination. The order of these numbers is important to open the lock. The method getnumbers, which returns a primitive array with numbers in the lock combination. The method iswithinrange, which receives an integer indicating an upper limit; and returns a boolean indicating whether all 3 numbers in the combination are in the range [0, upper limit] (including boundaries); true is returned if the 3 numbers are in range, false otherwise. The method equals, which overrides the homonymous method from Object. The method receives an object and returns a boolean indicating whether the object received is equal to this combination, which would be true if both have the same 3 numbers in the same order. 3. Create a class InvalidLockCombinationException that subclasses from Java s RuntimeException. This exception (which should not have any methods) will indicate that an attempt to assign a combination to a lock failed (thus making the combination invalid for that lock). 4. Create a class Lock. Locks have an unlocking combination, an upper limit for the rotating dial, and a value indicating whether they are opened or closed. This class implements the methods below: A constructor receiving the upper limit and unlocking combination. If the combination is invalid (if not all its numbers are in the dial) then the constructor should throw a new InvalidLockCombinationException. By throwing this exception we can avoid creating locks with invalid combinations (which in real life will be defective). All locks are open when created. The method getdiallimit, which returns the upper limit of the rotating dial. The method open, which receives an opening combination and opens the lock only if it is equal to the unlocking combination (hint: use the equals method from the Combination class). An opened lock will remain opened regardless of the opening combination received. The method close, which closes the lock. The method isopen, which returns a boolean indicating whether the lock is opened or not. 5. Download the JUnit test files CombinationTest.java and LockTest.java from the class website and import them into the project. Test your methods using their test cases. Get your instructor s signature when all tests pass. Exercise 1 - Get Instructor s Signature

Locks That Can Be Reset The lock business has been on the rise. However, sophisticated customers now want locks whose combination can be changed. To satisfy this market niche, you are extending the Lock class to allow locks whose combination can be reset. Figure 2. Dialog requesting a new combination. The new class will define 2 methods to read new combinations: resetnaive and resetretry. Both methods will display the JOptionPane dialog shown in Figure 2, will use a Scanner object to parse the input to numbers, and then use these numbers as the new combination for the lock. The difference between them is that resetnaive will assume that the input is always correct and will not check for exceptions, whereas resetretry will catch exceptions and recover from them by allowing the user to try typing the combination again. To display the JOptionPane dialog shown in Figure 2 look at the JOptionPane class in the Java API and use one of its showinputdialog methods: the simplest case takes null as its parent component, and the string Type a new combination as its message. Once displayed, this dialog can result in either the user canceling it, or the user typing something and pressing OK. When the dialog is cancelled, it returns a null value; thus we can use null as an indication that the user does not want to change the lock s combination. On the other hand, that the result of the dialog is not null indicates that there is an input that the user wants to use as the new combination of the lock. This is the point where the 2 methods will diverge. On the one hand, the method resetnaive will (naively) assume that the input is always given in the form of 3 integers separated by spaces and will proceed to use Scanner to read them and create a new combination. We know that careless users could types fewer than 3 numbers or could type letters instead of numbers or type 3 numbers that create an invalid combination for this lock. But that is ok; we want this method to fail and throw exceptions when they happen. The method resetretry will do exactly as resetnaive did but it will catch exceptions and show a message dialog indicating that the input was incorrect before displaying the input dialog again. Users can retry for as long as the input dialog is not canceled or a valid combination is provided. The implementation details of these methods are described next. Exercise 2 1. Go back to the class Lock and make the field that holds the unlock combination a protected field. 2. Create a class LockWithReset that subclasses from Lock. This class implements the methods below: A constructor that receives as parameters an upper limit for the dial and a combination, and invokes its super- class constructor using this data.

Figure 4. Message dialog indicating a NoSuchElementException. Figure 4. Message dialog indicating an InvalidLockCombinationException. The method resetnaive, which should display the input dialog from Figure 2 and (if the user does not cancel it) process its input to create a new combination. If the user cancels (the JOptionPane returns null), the method should terminate normally. Otherwise, use Scanner to read the three integers and use them to create a new combination. As mentioned earlier, this method should not make any attempts to recover from exceptions when scanning the input. If the input does not have 3 numbers then the method should throw a NoSuchElementException. If the input has 3 numbers but they do not make a valid combination for the lock then the method should throw an InvalidLockCombinationException. The method resetretry, which should also display the input dialog from Figure 2 and (if the user does not cancel it) process its input to create a new combination. resetretry, however, should catch exceptions and allow the user to re- enter a combination (upon failure). Although you re free to implement this method in any way you want, it is best to reuse resetnaive. Use a while loop to call this method and loop until there are no exceptions thrown. The method should show the message dialog in Figure 3 if a NoSuchElementException (thrown by Scanner) or the message dialog in Figure 4 if an InvalidLockCombinationException (from an invalid combination) is caught, before showing the input dialog in Figure 2 again. The dialogs shown in Figure 3 & Figure 4 are JOptionPane message dialogs. To program them use any of the showmessagedialog methods in the JOptionPane class. A static method main, which you can use to test your methods before attempting the automated JUnit tests (next step). You can initially use the main method below and later change the last statement to invoke the method resetretry instead: public static void main(string[] args) { Combination c = new Combination( 1, 2, 3 ); LockWithReset lock = new LockWithReset( 5, c ); } lock.resetnaive(); 3. Download the JUnit test file LockWithResetTest.java from the class website and import it into the project. Note that besides adding junit to the Build Path, you will also need to download and add an external library to make this test file compile and work. As such, direct your web browser to https://github.com/robertoaflores/gooey/ and follow instructions to download the JAR file gooey.jar (ask your instructor what JAR means if you haven t heard this term before). Once the JAR file has been downloaded, go back to Eclipse and right- click on your Lab05 project; choose the

options Build Path and Add External Archives. Direct the file dialog that appears to the location where you downloaded the JAR file. As shown in Figure 5, selecting this file should add a new project branch named Referenced Libraries, which should contain Gooey s JAR file. There should not be any compile errors in the JUnit test file at this point. Test your methods using the JUnit test cases. Note that the tests will briefly display your JOptionPane dialogs when testing. They will disappear when the tests end. Get your instructor s signature when all tests pass. Figure 5. Gooey added to the Project as a Referenced Library. Exercise 2 - Get Instructor s Signature

Lab 5 Instructor s Signature Page Student s Name: Student ID Exercise 1: JUnit tests passed: Exercise 2: JUnit tests passed: