18 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank...

17
18 January 2013 Birkbeck College, U. London 1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems [email protected] Spring 2013 Week 2b: Review of Week 1, Variables

Transcript of 18 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank...

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

18 January 2013 Birkbeck College, U. London 1

Introduction to Programming

Lecturer: Steve Maybank

Department of Computer Science and Information Systems

[email protected] 2013

Week 2b: Review of Week 1, Variables

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

Java Byte CodeOp code: 1 byteLengths of instructions: varyLocal variables: indexed 0, 1, 2, …Working memory: stackPrefix i: integer

iload_0 // push value of local variable 0 onto stackiconst_2 // push 2 onto stackimul // pop stack twice, multiply numbers, push result

onto stackistore_0 //pop stack, store result in local variable 0

18 January 2013 Birkbeck College, U. London 2

www.javaworld.com/jw-09-1996/jw-09-bytecodes.html

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

My First Programpublic 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 2013 Birkbeck College, U. London 3

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

MFP 2public 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 2013 Birkbeck College, U. London 4

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

MFP 3System.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 2013 Birkbeck College, U. London 5

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

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 2013 Birkbeck College, U. London 6

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

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

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

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

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

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 2013 Birkbeck College, U. London 9

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

Variable Declaration

int cansPerPack=6;

/* cansPerPack: name of the variable Reserved word int: type of the variable 6: value of the variable */

18 January 2013 Birkbeck College, U. London 10

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

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 2013 Birkbeck College, U. London 11

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

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

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

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

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

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

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

Example Programimport 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 2013 Birkbeck College, U. London 15

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

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

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

Operations with Strings 2String 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 2013 Birkbeck College, U. London 17