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

Slides:



Advertisements
Similar presentations
Introduction to Programming Overview of Week 2 25 January Ping Brennan
Advertisements

22 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
18 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Computer Systems
15 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
1 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
8 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming Java Lab 2: Variables and Number Types 1 JavaLab2 lecture slides.ppt Ping Brennan
Introduction to Programming
25 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming Java Lab 3: Variables and Number types 25 January JavaLab3.ppt Ping Brennan
Introduction to Programming
Lecture 5 Types, Expressions and Simple I/O COMP1681 / SE15 Introduction to Programming.
Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world.
Chapter Three Arithmetic Expressions and Assignment Statements
Computer Programming Lab(7).
 Base “primitive” types: TypeDescriptionSize intThe integer type, with range -2,147,483, ,147,483,647 4 bytes byteThe type describing a single.
Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 4 – Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To.
Chapter 2 Elementary Programming
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
22 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Introduction to Programming
Chapter 4 – Fundamental Data Types
Introduction to Programming
Introduction to Programming
Java Programming: From Problem Analysis to Program Design, 4e
Introduction to Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Introduction to Programming
Chapter 2: Basic Elements of Java
Introduction to Programming
Fundamentals 2.
Chapter 2: Java Fundamentals
Introduction to Programming
CS2011 Introduction to Programming I Elementary Programming
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Introduction to Programming
In this class, we will cover:
Primitive Types and Expressions
Introduction to Programming
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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

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)

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

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

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