CS288 Lab Exercise 2.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Citavi – Adding References – Articles from EBSCOhost Databases
Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
Written by: Dr. JJ Shepherd
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
Georgia Institute of Technology JavaScript part 2 Barb Ericson Georgia Institute of Technology May 2006.
Exceptions CIS 304 Intermediate Java Programming for Business.
1 of 6 This document is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS DOCUMENT. © 2007 Microsoft Corporation.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
CS288 Lab Exercise 1. UniS Open PersonGui Files Go to Course web site for PersonGui source files
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
JavaScript Form Validation
 What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
HTML Concepts and Techniques Fifth Edition Chapter 6 Using Frames in a Web Site.
XP New Perspectives on Microsoft Office FrontPage 2003 Tutorial 7 1 Microsoft Office FrontPage 2003 Tutorial 7 – Creating and Using Templates in a Web.
Sanjay Johal. Introduction(1.1) In this PowerPoint I will be explaining :  The purpose of the code for each of the two given programs, e.g. to carry.
HTML Concepts and Techniques Fifth Edition Chapter 4 Creating Tables in a Web Site.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
HTML Concepts and Techniques Fifth Edition Chapter 3 Creating Web Pages with Links, Images, and Formatted Text.
Creating and Editing a Web Page
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
1 of 6 This document is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS DOCUMENT. © 2007 Microsoft Corporation.
Perfecto Mobile Automation
1 Project 5: Leap Years. 222 Leap Years Write a program that reads an integer value from the user representing a year and determines if the year is a.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
Creating Web Pages with Links, Images, and Embedded Style Sheets
Entity Framework Database Connection with ASP Notes from started/getting-started-with-ef-using-mvc/creating-an-
Clojure.  Follow instructions on lynda.com  Can install all this in other ways, but this is the easiest way to get a development environment working.
Excel Tutorial 8 Developing an Excel Application
Unit 9.1 Learning Objectives Data Access in Code
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
2 At the top of the zone in which you want to add the Web Part, click Add a Web Part. In the Add Web Parts to [zone] dialog box, select the check box of.
BIT116: Scripting Loops.
2 At the top of the zone in which you want to add the Web Part, click Add a Web Part. In the Add Web Parts to [zone] dialog box, select the check box of.
You must know the 5-digit synonym for the course to use the
2.5 Another Java Application: Adding Integers
Project 9 Creating Pop-up Windows, Adding Scrolling Messages, and Validating Forms.
Computer Programming I
Creating Tables in a Web Site Using an External Style Sheet
Web Programming– UFCFB Lecture 17
Unit 27 - Web Server Scripting
Exception Handling Chapter 9.
Conditions and Ifs BIS1523 – Lecture 8.
Object-Oriented Software Engineering
Welcome with Ifs CSC 230 (Blum).
CS18000: Problem Solving and Object-Oriented Programming
Integrating JavaScript and HTML
CS288 Lab Exercise 3.
CS288 Lab Exercise 1.
Exception Handling Chapter 9 Edited by JJ.
A few tricks to take you beyond the basics of Microsoft Office 2007
Coding Concepts (Basics)
Lec 4: while loop and do-while loop
Do … Loop Until (condition is true)
Variables & getting info from the user
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
Object-Oriented Software Engineering
Running a Java Program using Blue Jay.
CS-1020 and Exception Handling
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
MIS 3200 – Unit 2.2 Outline What’s the problem with bad data?
Exceptions 5-Jul-19.
CMSC 202 Exceptions 2nd Lecture.
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
under the direction of Professor Susan Rodger
Intro to Programming (in JavaScript)
Defensive Programming
Presentation transcript:

CS288 Lab Exercise 2

Open PersonGui Files Go to Course web site for PersonGui source files http://www.computing.surrey.ac.uk/courses/cs288/Examples/PersonGui Make copies of these files in you home folder and then open them in some editor (e.g. Emacs): PersonGui.java PersonMaker.java Person.java

1: PersonMaker.java Open PersonMaker.java Find the setAge method: public void setAge (String stringAge) { string_age = stringAge; try { age = new Double(stringAge); } catch (NumberFormatException exp1) { string_age = handleTextInput("Age"); //Why could this result in an error? // YOU HAVE TO PUT SOME CODE HERE // TO MAKE THIS WORK WHEN THE USER // DOES NOT ENTER A VALID NUMBER VALUE age = new Double(string_age); }

1: PersonMaker.java What happens in the PersonGui if we select Set Age from the pop up menu when no value has been set? If you click on OK for value of Age with the default value (“Fluffy”) what error messages appear,and where (see command shell)?

1: PersonMaker.java Change the setAge method, so that if the string_age value given by the dialog is not a suitable value for age, then string_age changes it to some sensible value (say age = new Double(0.0)). To do this you will have to add another try catch statement, this will have to be around the second occurrence of age = new Double(string_age) in the method definition. To find out about the Double class constructor follow the link on the class web site for Specification of ALL Java classes Then find the entry for Double in the left hand column called ‘All Classes’.

1: PersonMaker.java Finally add the following expression to the setAge method. Put it immediately after the line where you set the age value to be new Double(0.0) if the user entered an invalid number. Change the text in the string values to give a meaningful message. JOptionPane.showMessageDialog( null, " Make this message meaningful ", "Make this message meaningful", JOptionPane.PLAIN_MESSAGE);

2: PersonMaker.java Open the following link to the Java tutorial. This describes how while loops work. http://www.computing.surrey.ac.uk/courses/cs288/Java_Documentation/Tutorial/java/nutsandbolts/while.html Find the setHeight method in PersonMaker.java. Define a method boolean is_ok_height (String hstring); that returns true if new Double(hstring) returns a value, but returns false if the constructor Double(hstring) throws an exception. Change the catch section of the code so that it now uses a do-while loop. The idea is to force the user to give a valid height. If it is not a valid number then the while loop will keep repeating until the user gets fed up and enters a proper value. See next page for structure of loop. Recall a do while loop looks like: do { statement(s) } while (expression);

2: PersonMaker.java The catch section will now be a do-while loop that: asks the user for a value for height using the handleTextInput("Height"); method use is_ok_height (String hstring); to check if the user has entered a valid number. if not repeat step 1 if it is OK the loop will terminate and set the value of height accordingly