Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "18 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems."— Presentation transcript:

1 18 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Spring 2013 Week 2b: Review of Week 1, Variables

2 Java Byte Code Op code: 1 byte Lengths of instructions: vary Local variables: indexed 0, 1, 2, … Working memory: stack Prefix i: integer iload_0 // push value of local variable 0 onto stack iconst_2 // push 2 onto stack imul // pop stack twice, multiply numbers, push result onto stack istore_0 //pop stack, store result in local variable 0 18 January 2013Birkbeck College, U. London2 www.javaworld.com/jw-09-1996/jw-09-bytecodes.html

3 My First Program public class HelloPrinter { /* every Java program consists of one or more classes. This program has only one class. By convention, class names begin with a capital letter. */ /* reserved word public: class available to all users. */ } 18 January 2013Birkbeck College, U. London3

4 MFP 2 public class HelloPrinter { public static void main(String[] args) { /* The class HelloPrinter contains a single method: main. Reserved word static: main is available for use. Reserved word void: main does not return a value. String[] args: type of arguments for main. Not used in ITP */ } 18 January 2013Birkbeck College, U. London4

5 MFP 3 System.out.println(Hello, World); /* System.out: object available for use in the program. System.out.println: method which is applied to the (hidden) data in System.out. Hello, World: string which is a parameter for the method System.out.println */ 18 January 2013Birkbeck College, U. London5

6 Remember Edit->Compile->Run Every statement ends with ; Brackets occur in nested pairs {{…} …{…}} Spelling must be accurate: e.g. not maiin or Main Small errors will prevent compilation Add comments to your code 18 January 2013Birkbeck College, U. London6

7 Investment Problem You put £10,000 into a bank account that earns 5% interest per year. How many years does it take for the account balance to be double the original? (JFE, Section 1.7) 18 January 2013Birkbeck College, U. London7

8 Solution to Investment Problem Initial balance: £10000 Interest rate: 5% per year Interest earned after 1 year: 10000*5/100 = 500 Balance after 1 year: initial amount+interest = 10000+500 = 10000*1.05 Balance after two years: 10000*1.05*1.05 Balance after three years: 10000*1.05*1.05*1.05 18 January 2013Birkbeck College, U. London8

9 Program public class AccountBalance { public static void main(String[] args) { System.out.println(10000*1.05*1.05*1.05); /* Include additional factors 1.05 until a number greater than or equal to 20000 is printed. The method is crude but it works. */ } 18 January 2013Birkbeck College, U. London9

10 Variable Declaration int cansPerPack=6; /* cansPerPack: name of the variable Reserved word int: type of the variable 6: value of the variable */ 18 January 2013Birkbeck College, U. London10

11 Variable Declaration 2 double canVolume=0.355; /* canVolume: name of the variable Reserved word double: type of the variable 0.355: value of the variable */ 18 January 2013Birkbeck College, U. London11

12 Input from the Key Board 1 In the very first line of your program file write import java.util.Scanner; java.util is a package, i.e. a collection of classes with a related purpose. java.util.Scanner is the class Scanner in the package java.util. The class Scanner has already been written. It is used to read input from the key board. For other classes in java.util, see JFE, Appendix D. 18 January 2013Birkbeck College, U. London12

13 Input from the Key Board 2 In your program, create an object in which belongs to the class Scanner Scanner in = new Scanner(System.in); The reserved word new indicates that a new object is to be created. The new object is of type Scanner. The argument System.in is an object which identifies the key board as the source of input. The left most Scanner is analogous to a type declaration such as int or double. The leftmost in can be replaced by any legitimate name of a variable. 18 January 2013Birkbeck College, U. London13

14 Input from the Key Board 3 Use the object in to read from the key board int bottles = in.nextInt(); in.nextInt is a method in the object in. When this method is called, as in the above statement, it reads an integer which is typed at the key board. To assign the value 65 to the variable bottle, type 65 at the key board, followed by Enter (or Return). For further methods in objects belonging to the class Scanner, see JFE Appendix D. 18 January 2013Birkbeck College, U. London14

15 Example Program import java.util.Scanner; public class TestInput { public static void main(String[] args) { System.out.print(Enter the number of bottles: ); Scanner in = new Scanner(System.in); int bottles = in.nextInt(); System.out.println(The number of bottles is +bottles); } 18 January 2013Birkbeck College, U. London15

16 Operations with Strings 1 String str = "Ja"; // Declare the string variable str. Set it to "Ja". str = str+"va"; /* The symbol + specifies string concatenation. str is set to "Java". */ String greeting = "H & S"; int n = greeting.length(); /* The method greeting.length returns the length of the string that is the value of the string variable greeting. In this case n is set to 5. */ 18 January 2013Birkbeck College, U. London16

17 Operations with Strings 2 String str = "Sally"; String str2 = str.substring(1,4); /* Extract from str the substring starting at position 1 and ending immediately before position 4. Set str2 equal to this substring (str2 now has the value "all"). Note that string indexing begins with 0. */ String str = "Sally"; String str3 = str.substring(1); /* Set str3 equal to the substring of str that starts at position 1 and includes all the characters in str up to and including the last character. Thus str3 now has the value "ally". */ 18 January 2013Birkbeck College, U. London17


Download ppt "18 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems."

Similar presentations


Ads by Google