Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 25 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems Spring 2013 Week 3: Variables and Number Types

2 Overview Review of week 2: declaring variables Number types Keyboard input Mathematical functions See Java for Everyone, Chapter 2 25 January 2013Birkbeck College, U. London2

3 Recall Variable Declaration int cansPerPack = 6; /* int: integer number type. All variables have a type. =: assignment cansPerPack: descriptive name in camel case. 6: initial value */ 25 January 2013Birkbeck College, U. London3

4 Alternative Declaration int cansPerPack; // variable declaration cansPerPack = 6; // initialisation Always initialise a variable before using it, even if there is a default initialisation. 25 January 2013Birkbeck College, U. London4

5 Example Code int cansPerPack = 6; System.out.println(cansPerPack); int cansPerCrate = 4 * cansPerPack; /* A variable can be initialised using an expression. */ 25 January 2013Birkbeck College, U. London5

6 Number Types 25 January 2013Birkbeck College, U. London6 TypeDescription intinteger, range –2 31 to byteinteger, range –128 to 127 shortinteger, range –2 15 to longinteger, range –2 63 to doubledouble precision floating point (15 decimal digits, range ± ) floatsingle precision floating point (7 decimal digits, range ±10 38 ). Now rarely used. charcharacter, encoded in Unicode

7 Variable Names Consist of numbers, letters and underscore _ $ is legal, but reserved for names generated by software tools Must begin with a letter or underscore Case sensitive Avoid reserved words By convention: class names begin with a capital letter, variable names begin with a lower case letter 25 January 2013Birkbeck College, U. London7

8 Examples of Variable Names 25 January 2013Birkbeck College, U. London8 counter counter67 largeNumber large_Number large Number byte 6counter counter? days/month Counter cou67ter IF _counter largenumber $largeNumber Conventional and accepted by the compiler Not conventional but accepted by the compiler Rejected by the compiler

9 Comments double canVolume=0.355; //Litres in a can /* This is a long comment */ /** This is a comment which explains the purpose of a class. */ 25 January 2013Birkbeck College, U. London9

10 Example Program /** This program computes the volume in litres of a six-pack of soda cans. */ public class Volume1 { public static void main(Strings[] args) { int cansPerPack = 6; double canVolume = 0.355; // Litres in a 12-ounce can System.out.print(A six-pack of 12-ounce cans contains ); System.out.print(cansPerPack * canVolume); System.out.println( litres.); } 25 January 2013Birkbeck College, U. London10

11 Overflow int oneBillion = ; System.out.println(3 * oneBillion); /* Output: No compile time error is flagged. No error message appears at run time. Solution: use type double but note round off errors and overflow in double, e.g */ 25 January 2013Birkbeck College, U. London11

12 Assignment of a Value to a Variable int counter = 0; int increment = 1; counter = counter+1; counter++; counter- -; counter += 1; counter += increment; counter *= 2; 25 January 2013Birkbeck College, U. London12

13 Constants final double BOTTLE_VOLUME=2; /* The reserved word final ensures that the value of BOTTLE_VOLUME will never change. Use names for constants and avoid magic numbers, eg. compare the following.*/ double volume1=bottles * 2; double volume2=bottles * BOTTLE_VOLUME; 25 January 2013Birkbeck College, U. London13

14 Strings and Characters H`: character of type char. H: string containing a single character, namely H`. String greeting = Harry; char start = greeting.charAt(0); // Value of start is `H`; int count = Integer.parseInt(34); /* parseInt is a static method in the class java.lang.Integer. It converts a string to the corresponding integer. */ 25 January 2013Birkbeck College, U. London14

15 Recall Key Board Input import java.util.Scanner; // first line of program Scanner in = new Scanner(System.in); /* create a Scanner object to read keyboard input */ System.out.print(Enter the number of bottles: );// prompt int bottles = in.nextInt(); // read integer input /* Type digits at key board, then press the enter or return key */ 25 January 2013Birkbeck College, U. London15

16 Types of Key Board Input System.out.print(Enter the price in the form a.b where a is the number of pounds and b is the number of pence: ); double unitPrice=in.nextDouble(); System.out.print(Enter your first name: ); String name = in.next(); // input a single word, e.g. John System.out.print(Enter your full name: ); String fullName = in.nextLine(); /* input a whole line, e.g. John Smith */ 25 January 2013Birkbeck College, U. London16

17 A Test of the Key Board Input System.out.println(Type an integer: ); boolean test = in.hasNextInt(); if(test) { int i = in.nextInt(); } else { System.out.println(error); } 25 January 2013Birkbeck College, U. London17

18 Arithmetic Operators 25 January 2013Birkbeck College, U. London18 +addition -subtraction *multiplication /division %modulus Multiplication and division take precedence over addition and subtraction. Use brackets to control the evaluation of expressions. If in any doubt, use brackets.

19 Examples 25 January 2013Birkbeck College, U. London19 ExpressionValue 2+6/25 (2+6)/24 5/22 5%21 6.2/23.1 (2+6.0)/160.5 (2+6)*2+319

20 Powers and Roots 25 January 2013Birkbeck College, U. London20 Math functionMethod xMath.sqrt(x) x2x2 Math.pow(x,2) x y, x>0 or x=0, y>0 or x<0, y integer Math.pow(x,y) Round x to nearest integer Math.round(x) (long integer returned) log 10 (x), x>0.Math.log10(x) |x|Math.abs(x)

21 Conversion of Floating Point to Integer double balance = total+tax; int dollars1 = (int) balance; /* The value of balance is converted to an integer by discarding the fractional part. The operator (int) is a cast operator. */ int dollars2=(int)(total + tax); 25 January 2013Birkbeck College, U. London21

22 Roundoff Errors /** This program demonstrates the effect of a round off error. */ public class RoundoffDemo { public static void main(String[] args) { double price = 4.35; int cents = (int)(100 * price); // Should be 100*4.35=435 System.out.println(cents); // Prints 434! } /* 4.35 cannot be represented exactly as a number of type double */ } 25 January 2013Birkbeck College, U. London22

23 API Application Programming Interface: contains the classes and methods in the Java library. The API documentation is at See also JFE Appendix D. 25 January 2013Birkbeck College, U. London23


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

Similar presentations


Ads by Google