18 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 Java Lab 1: My First Program 11 January JavaLab1.ppt Ping Brennan
Advertisements

Introduction to Programming Overview of Week 2 25 January Ping Brennan
Research Methods Lecturer: Steve Maybank
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.
Introduction to Programming
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
15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming
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 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan
Introduction to Programming Java Lab 3: Variables and Number types 25 January JavaLab3.ppt Ping Brennan
Introduction to Programming
18 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
25 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
29 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Classes and objects Learning objectives By the end of this lecture you should be able to: explain the meaning of the term object-oriented; explain the.
Introduction to Java Applications
Introduction to Programming G51PRG University of Nottingham Revision 1
CS110 Programming Language I
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.
MSc IT Programming Methodology (2). number name number.
1 Various Methods of Populating Arrays Randomly generated integers.
Computer Programming Lab 8.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
03 Data types1June Data types CE : Fundamental Programming Techniques.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Krakatoa: Decompilation in Java “Does Bytecode Reveal Source?” Todd A. Proebsting Scott A. Watterson The University of Arizona Presented by Karl von Randow.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Introduction to Java Java Translation Program Structure
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
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.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
Introduction to Programming
Introduction to Programming
Chapter 2 Introduction to Java Applications
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
An Introduction to Java – Part I, language basics
Introduction to Programming
Introduction to Programming
Introduction to Programming
Anatomy of a Java Program
Introduction to Programming
CS Week 2 Jim Williams, PhD.
Presentation transcript:

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

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

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

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

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

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

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

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*1.05 Balance after two years: 10000*1.05*1.05 Balance after three years: 10000*1.05*1.05* January 2013Birkbeck College, U. London8

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 is printed. The method is crude but it works. */ } 18 January 2013Birkbeck College, U. London9

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

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

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

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

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

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

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

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