Download presentation

Presentation is loading. Please wait.

Published byPaige Nelson Modified over 2 years ago

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

2
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 2013Birkbeck College, U. London2

3
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 2013Birkbeck College, U. London3

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

5
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 2013Birkbeck College, U. London5

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

7
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

8
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 2013Birkbeck College, U. London8

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

10
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 2013Birkbeck College, U. London10

11
Evaluation of an Expression 1 February 2013Birkbeck College, U. London11 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.

12
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 2013Birkbeck College, U. London12

13
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 2013Birkbeck College, U. London13

14
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 2013Birkbeck College, U. London14

15
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 2013Birkbeck College, U. London15

16
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 2013Birkbeck College, U. London16

17
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 2013Birkbeck College, U. London17

18
Examples 1 February 2013Birkbeck College, U. London18 preceding stringfollowing string carcat carcart cat cart wheel carcar7 7carcar Zebrazebra

19
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 2013Birkbeck College, U. London19

20
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 2013Birkbeck College, U. London20

21
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 2013Birkbeck College, U. London21

22
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 2013Birkbeck College, U. London22

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

24
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 2013Birkbeck College, U. London24

25
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 2013Birkbeck College, U. London25

26
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 2013JFE Review Exercises, R2.826

27
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: ); x=in.nextInt(); System.out.println(The sum is +x+y); } 1 February 2013JFE Review Exercises R2.627

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google