Computer Science 202

Introduction to Programming

Fall 2012, The College of Saint Rose

Agenda

- Announcements
- New due date for Lab 5: Tuesday midnight

- Exam 1 Details
- In class, Thursday, October 11.
- Topics covered up to the end of class last time: binary and
hexadecimal data representations/conversions; psuedocode and
flowcharting; basic structure of a Java application; output
using
`System.out.print[ln]`;`String`concatenation; declaring variables; validity of, and naming conventions for, and appropriateness of identifiers; input using`Scanner`s; numeric input/output and data types; mathematical expressions, including operator precedence and the difference between`int`and`double`division; named constants; conditional execution including Visual Logic If Conditions, Java's`if`,`if-else`,`if-else if-...-else`, and`switch`statements, relational operators; using`JOptionPane`dialog boxes for input and output; converting a`String`to an`int`/`double`using`Integer.parseInt`/`Double.parseDouble`;`String`'s`length`method; error checking inputs and the`System.exit`method;`boolean`data and boolean expressions;`String`'s`equals`method and the difference between that and`==`

; output formatting with`DecimalFormat`objects. - No books, notes, neighbors, electronic devices permitted with the exception of one page of handwritten notes on an 8.5 x 11" sheet, double sided, plus your Java reference card that came with Visual Logic.
- We will spend as much time as you wish in class on Tuesday reviewing for the exam.
- Kinds of questions to expect: binary/hex/decimal conversions; describe an algorithm in pseudocode or with a flowchart that solves a given problem; find the errors in an algorithm; given a Java program, what is the output?; find the errors in a given Java program; write or fill in some details of a Java program given a description of what it should do.
- How to prepare: go over every lecture assignment and programming assignment and class example, and make sure you understand everything it does (is that all?). If you have made the effort and understood what your programs were doing and why, you should have no trouble with the exam questions.

- Lecture assignment 11 recap
- Repetition
- Visual Logic's While Loop
- Java's
`while`statement

- Using a
`while`loop in error checking - In-class exercise
You may do this alone or with a partner.

Your task is to take the in-class exercise program from Lecture 8 (

`ClothingTax.java`) and improve it in two ways:- Add error checking to make sure the purchase amount is greater
than 0. Use a
`while`loop to ensure that this is the case. - Use a
`DecimalFormat`object to format the answers in a standard currency style.

Submit just the

`ClothingTax.java`file in Blackboard under the "Lecture Assignment Submissions" category to the "Lecture 12 In-Class Exercise" item. This will earn you 5 lecture assignment points. - Add error checking to make sure the purchase amount is greater
than 0. Use a
- Remaining time to work on Lab 5.

Due at the start of class, Tuesday, October 9.

Nothing to turn in, but your job is to bring questions to class about past lecture assignments and programs that we can review for the exam.

You should be able to answer all of textbook's Checkpoint questions in
Chapters 1-3, and all Review Questions ane Exercises at the end of
Chapters 1-3 with the exception of the following: the Checkpoint
exercises at the end of Section 1.7, Chapter 1 short answers 10-18 and
20-22, any questions dealing with primitive types we have not studied
and used (`float`, `byte`, `short`, `long`, `char`),
questions dealing with the `Math` class (p. 62) or the combined
assignment operators (Section 2.6) or type conversions (Section 2.7),
`String` methods other than `length` and `equals`, Javadoc
comments, the Conditional Operator in Section 3.8, the `printf`
method in Section 3.11.

Here is a sample "Find the errors" problem. Your task is to find 10 errors (there are more). Label each error you find, indicate the type of error (syntax error, logic error, style error), and provide a fix. The program is intended to prompt for and read in 3 integers, print a different message depending on whether the sum is 0, negative, or positive, then compute their average and display it as a floating point number with 2 digits after the decimal point.

// Crazy Program By J. Teresco import java.util.Scanner public class silly { public static vile main(String args[]); System.out.print(Enter 3 numbers:); int NUMBER1 = input.nextInt(); int NUMBER2 = input.nextInt(); int NUMBER3 = input.nextInt(); sum = number1 + number 2 + number3; if sum=0 { System.out.println("Your sum is 0, how nice."); } else if (sum < 0); System.out.println("Your sum of " sum + " is negative, how sad."); else (sum > 0) { System.out.println(sum + "is positive, and so am I."); Double Average = sum / 3; DecimalFormat f = DecimalFormat("0.0"); System.out.println("On average, that's " + f.format(Average); } }

Examples