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

1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

Embed Size (px)

Citation preview

Page 1: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

1 February 2013 Birkbeck College, U. London 1

Introduction to Programming

Lecturer: Steve Maybank

Department of Computer Science and Information Systems

[email protected] 2013

Week 4: Numbers and Strings

Page 2: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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 2

Page 3: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 4: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 5: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 6: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 7: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 8: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

Central Part of the CodeString 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

Page 9: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

Arithmetic Expressions

1 February 2013 Birkbeck College, U. London 9

math. exp. Java expression

(x+y)/2

x*y/2

Math.pow(1+r/100,n)

Math.sqrt(a*a+b*b)

Math.PI

2

yx

2

yx

nr

1001

22 ba

Page 10: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 11: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

Evaluation of an Expression

1 February 2013 Birkbeck College, U. London 11

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 obtain11*9

Fourth step: evaluate 11*9 to obtain 99.

Page 12: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

Formatted Outputdouble price = 1.21997;System.out.println(price);/* 1.21997 */System.out.printf(“%.2f”, price);/* 1.22with no newline */System.out.printf(“%10.2f”, price);/* 1.22with no newline*/

1 February 2013 Birkbeck College, U. London 12

Page 13: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

Format Specifier %10.2f 10: width of field in characters. 2: number of digits after the decimal

point. f: fixed floating point. If 47.21997 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

Page 14: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

Example

price=1.21997;System.out.printf(“Price per litre: %10.2f“,

price);

/* The printed output isPrice per litre: 1.22with no newline*/

1 February 2013 Birkbeck College, U. London 14

Page 15: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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 isThe volume is 10with no newline*/

1 February 2013 Birkbeck College, U. London 15

Page 16: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

Lexicographic Ordering of Strings

Dictionary order, eg. “anteater” is before “zebra” even though “anteater” is longer.

Java lexicographic ordering:uppercase letters precede lower case

lettersnumbers precede lettersthe space character precedes all

printable characters.

1 February 2013 Birkbeck College, U. London 16

Page 17: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 18: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

Examples

1 February 2013 Birkbeck College, U. London 18

preceding string

following string

car cat

car cart

cart cat

cart wheel cartwheel

car car7

7car car

Zebra zebra

Page 19: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 20: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

Inputs and Outputs Inputs:

Denomination of the note, in dollarsPrice of purchased item

Outputs:Number of dollars to be returnedNumber of quarters to be returned

1 February 2013 Birkbeck College, U. London 20

Page 21: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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 500-225=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

Page 22: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

Pseudocode

amount due = (100xnote value)-(price in cents)dollars = amount due/100 (discard remainder)amount due = amount due%100quarters = amount due/25

1 February 2013 Birkbeck College, U. London 22

Page 23: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

Variables and Constants noteValue itemPrice amountDue dollars quarters CENTS_PER_DOLLAR CENTS_PER_QUARTER

1 February 2013 Birkbeck College, U. London 23

Page 24: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

Input and OutputSystem.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

Page 25: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

Class and main methodimport 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 hereint 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

Page 26: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

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

Page 27: 1 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

Find Two Run Time Errorsimport 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:

”);x=in.nextInt();System.out.println(“The sum is ”+x+y);

}}1 February 2013 JFE Review Exercises R2.6 27