# Introduction to Programming

## Presentation on theme: "Introduction to Programming"— Presentation transcript:

Introduction to Programming
Lecturer: Steve Maybank Department of Computer Science and Information Systems Spring 2013 Week 4: Numbers and Strings 1 February 2013 Birkbeck College, U. London

Birkbeck College, U. London
Overview Java Lab 2, Exercise 5 Arithmetical Expressions Parentheses Formatted output Lexicographic ordering of strings Example: a vending machine See Java for Everyone, Ch. 2 and 3 1 February 2013 Birkbeck College, U. London

Birkbeck College, U. London
Java Lab 2, Exercise 5 We are given a string str and two positions i and j (i comes before j). Set first to the substring from the start of the string to the last position before i. Set middle to the substring from position i+1 to j-1. Set last to the substring from position j+1 to the end of the string. Concatenate the following five strings: first, the string containing just the character at position j, middle, the string containing just the character at position i, and last. 1 February 2013 Birkbeck College, U. London 3

Birkbeck College, U. London
Example Input: str = “Boston”, i=2, j=4. Output: “Bootsn”. first = “Bo”. si = “s”; middle = “t”. sj = “o”. last = “n”. 1 February 2013 Birkbeck College, U. London 4

Birkbeck College, U. London
Strategy First version of the program: choose simple input and output. Divide the string str into five pieces Reassemble the pieces to obtain the result. Later versions: improve input and output, add error checking, examine special cases carefully. 1 February 2013 Birkbeck College, U. London 5

Birkbeck College, U. London
String Operations Check the properties of the operators and methods you might use. Concatenation: + Substring: str.substring(0,1) 1 February 2013 Birkbeck College, U. London 6

Properties of substring
str.substring(i,j): return the string consisting of the characters in str from position i to position j-1, counting from 0. Note i, j are integers such that i<j. Example: “Sally”.substring(1,4) is “all”. If j is omitted the string of characters from position i to the end of str is returned. Example: “Sally”.substring(1) is “ally”. 1 February 2013 Birkbeck College, U. London 7

Central Part of the Code
String str = “Boston”; int i = 2, j = 4; String first = str.substring(0, i); String si = str.substring(i, i+1); String middle = str.substring(i+1, j); String sj = str.substring(j, j+1); String last = str.substring(j+1); String result = first+sj+middle+si+last; 1 February 2013 Birkbeck College, U. London 8

Arithmetic Expressions
math. exp. Java expression (x+y)/2 x*y/2 Math.pow(1+r/100,n) Math.sqrt(a*a+b*b) Math.PI 1 February 2013 Birkbeck College, U. London 9

Birkbeck College, U. London
Parentheses Each left parenthesis in an expression must be paired with a right parenthesis. Incorrect expression: (3+9)+1)*(4+(2*3) Correct expression: ((3+9)+1)*(4+(2*3)) Check: working from left to right, count +1 for a left parenthesis and –1 for a right parenthesis. If the count drops below 0 or is not 0 at the end then there is an error. How about working right to left? 1 February 2013 Birkbeck College, U. London 10

Evaluation of an Expression
Work from the “inside outwards”, e.g. evaluate (3+4*(9-7))*((2*7)-5) First step: evaluate 9-7 and 2*7 to obtain (3+4*2)*(14-5) Second step: evaluate 4*2 and 14-5 to obtain (3+8)*9 Third step: evaluate 3+8 to obtain 11*9 Fourth step: evaluate 11*9 to obtain 99. 1 February 2013 Birkbeck College, U. London 11

Birkbeck College, U. London
Formatted Output double price = ; System.out.println(price); /* */ System.out.printf(“%.2f”, price); /* 1.22 with no newline */ System.out.printf(“%10.2f”, price); /* 1.22 with no newline*/ 1 February 2013 Birkbeck College, U. London 12

Birkbeck College, U. London
Format Specifier %10.2f 10: width of field in characters. 2: number of digits after the decimal point. f: fixed floating point. If is printed using this format specifier, then how many spaces are there to the left of the printed number? 1 February 2013 Birkbeck College, U. London 13

Birkbeck College, U. London
Example price= ; System.out.printf(“Price per litre: %10.2f“, price); /* The printed output is Price per litre: 1.22 with no newline */ 1 February 2013 Birkbeck College, U. London 14

Decimal Integer Format
int volume = 10; System.out.printf(“The volume is %5d”, volume); /* %5d: decimal integer format specifier. The field width is 5. Printed output is The volume is 10 with no newline */ 1 February 2013 Birkbeck College, U. London 15

Lexicographic Ordering of Strings
Dictionary order, eg. “anteater” is before “zebra” even though “anteater” is longer. Java lexicographic ordering: uppercase letters precede lower case letters numbers precede letters the space character precedes all printable characters. 1 February 2013 Birkbeck College, U. London 16

Algorithm for Lexicographic Ordering
Given two distinct strings, compare them character by character starting from position 0. Stop at the first pair of characters which fail to match.The strings have the same order as the corresponding characters. If one string ends, then it precedes the other string. 1 February 2013 Birkbeck College, U. London 17

Birkbeck College, U. London
Examples preceding string following string car cat cart cart wheel cartwheel car7 7car Zebra zebra 1 February 2013 Birkbeck College, U. London 18

Program to Simulate a Vending Machine
A person pays for an item with a dollar note and receives change. All prices are multiples of 25 cents (quarters) and change is given in dollar notes and quarters. Compute the numbers of dollars and quarters to be returned. (See JFE, Section 2.3.) 1 February 2013 Birkbeck College, U. London 19

Birkbeck College, U. London
Inputs and Outputs Inputs: Denomination of the note, in dollars Price of purchased item Outputs: Number of dollars to be returned Number of quarters to be returned 1 February 2013 Birkbeck College, U. London 20

Birkbeck College, U. London
Example Note: \$5 Price: \$2.25 Change: \$2.75 which is \$2 and 3x25 cents Strategy: work in cents. Number of cents in the change is =275. Number of dollars in 275 cents is 275/100. Number of quarters is (275%100)/25 1 February 2013 Birkbeck College, U. London 21

Birkbeck College, U. London
Pseudocode amount due = (100xnote value)-(price in cents) dollars = amount due/100 (discard remainder) amount due = amount due%100 quarters = amount due/25 1 February 2013 Birkbeck College, U. London 22

Variables and Constants
noteValue itemPrice amountDue dollars quarters CENTS_PER_DOLLAR CENTS_PER_QUARTER 1 February 2013 Birkbeck College, U. London 23

Birkbeck College, U. London
Input and Output System.out.print(“Enter note value (1=\$1 note, 5=\$5 note, etc.): ”); int noteValue=in.nextInt(); System.out.print(“Enter price in cents: ”); int itemPrice=in.nextInt(); … System.out.printf(“Dollars: %6d”, dollars); System.out.printf(“Quarters: %6d”, quarters); 1 February 2013 Birkbeck College, U. London 24

Birkbeck College, U. London
Class and main method import java.util.Scanner; /** This program simulates a vending machine that gives change. */ public class VendingMachine { public static void main(String[] args) Scanner in = new Scanner(System.in); final int CENTS_PER_DOLLAR = 100; final int CENTS_PER_QUARTER = 25; ….. //input here int amountDue = CENTS_PER_DOLLAR*noteValue-itemPrice; int dollars = amountDue/CENTS_PER_DOLLAR; amountDue=amountDue%CENTS_PER_DOLLAR; int quarters = amountDue/CENTS_PER_QUARTER; …//output here } 1 February 2013 Birkbeck College, U. London 25

Find Five Compile Time Errors
public class HasErrors { public static void main(String[] args); System.out.print(Pleese enter two numbers: ) x=in.readDouble(); y=in.readDouble(); System.out.println(“The sum is ”+x+y); } 1 February 2013 JFE Review Exercises, R2.8 26

Find Two Run Time Errors
import java.util.Scanner; public class HasErrors { public static void main(String[] args) int x=0, y=0; Scanner in = new Scanner(“System.in”); System.out.print(“Please enter an integer: ”); x=in.nextInt(); System.out.print(“Please enter another integer: ”); System.out.println(“The sum is ”+x+y); } 1 February 2013 JFE Review Exercises R2.6 27