Introduction to Programming



Similar documents
AP Computer Science Static Methods, Strings, User Input

Install Java Development Kit (JDK) 1.8

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Chapter 3. Input and output. 3.1 The System class

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Basics of Java Programming Input and the Scanner class

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Building Java Programs

Lecture 5: Java Fundamentals III

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Object-Oriented Programming in Java

Chapter 2: Elements of Java

Some Scanner Class Methods

Building Java Programs

Sample CSE8A midterm Multiple Choice (circle one)

CS 106 Introduction to Computer Science I

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

More on Objects and Classes

java Features Version April 19, 2013 by Thorsten Kracht

Introduction to Java

Using Files as Input/Output in Java 5.0 Applications

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Comp 248 Introduction to Programming

Introduction to Java. CS 3: Computer Programming in Java

Java Crash Course Part I

JAVA ARRAY EXAMPLE PDF

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Chapter 2 Elementary Programming

Arrays in Java. Working with Arrays

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

J a v a Quiz (Unit 3, Test 0 Practice)

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

The Java Virtual Machine (JVM) Pat Morin COMP 3002

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

CS 106 Introduction to Computer Science I

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

6.1. Example: A Tip Calculator 6-1

Chapter 7D The Java Virtual Machine

Data Structures Lecture 1

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

Part I. Multiple Choice Questions (2 points each):

Programming Languages CIS 443

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

Chapter 2 Introduction to Java programming

The following program is aiming to extract from a simple text file an analysis of the content such as:

AP Computer Science Java Mr. Clausen Program 9A, 9B

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Topic 11 Scanner object, conditional execution

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Java CPD (I) Frans Coenen Department of Computer Science

Programming Fundamentals I CS 110, Central Washington University. November 2015

AP Computer Science Java Subset

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Building Java Programs

CSE 8B Midterm Fall 2015

In this Chapter you ll learn:

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Homework/Program #5 Solutions

Carron Shankland. Content. String manipula3on in Java The use of files in Java The use of the command line arguments References:

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006

Programming in Java Course Technology, a part of Cengage Learning.

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Lecture 9. Semantic Analysis Scoping and Symbol Table

Chapter 20 Streams and Binary Input/Output. Big Java Early Objects by Cay Horstmann Copyright 2014 by John Wiley & Sons. All rights reserved.

Inside the Java Virtual Machine

02 B The Java Virtual Machine

WHAT ARE PACKAGES? A package is a collection of related classes. This is similar to the notion that a class is a collection of related methods.

Programmierpraktikum

Week 1: Review of Java Programming Basics

Building Java Programs

Variables, Constants, and Data Types

Programming and Data Structures with Java and JUnit. Rick Mercer

Chapter 2 Basics of Scanning and Conventional Programming in Java

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Lecture 1 Introduction to Java

Stack Allocation. Run-Time Data Structures. Static Structures

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Lecture 32: The Java Virtual Machine. The Java Virtual Machine

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

INPUT AND OUTPUT STREAMS

13 File Output and Input

Moving from CS 61A Scheme to CS 61B Java

Java Basics: Data Types, Variables, and Loops

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

HOTPATH VM. An Effective JIT Compiler for Resource-constrained Devices

Interactive Applications (CLI) and Math

Transcription:

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

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 http://www.javaworld.com/article/2077233/core-java/bytecode-basics.html 16 January 2015 Birkbeck College, U. London 2

public class HelloPrinter { My First Program /* 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 other classes in the program. */ } 16 January 2015 JFE Section 1.5 3

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 */ } 16 January 2015 JFE Section 1.5 4

MFP 3 System.out.println("Hello, World"); /* System: a class */ 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 16 January 2015 JFE Section 1.5 5

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 16 January 2015 Birkbeck College, U. London 6

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) 16 January 2015 Birkbeck College, U. London 7

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 16 January 2015 Birkbeck College, U. London 8

Graphs of the Balance Graph for 10 years Graph for 100 years 16 January 2015 Birkbeck College, U. London 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 strategy is crude but it works. */ 16 January 2015 Birkbeck College, U. London 10

Variable Declaration int cansperpack=6; /* cansperpack: name of the variable Reserved word int: type of the variable 6: value of the variable */ 16 January 2015 JFE Section 2.1 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 */ 16 January 2015 JFE Section 2.1 12

Input from the Keyboard 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. 16 January 2015 JFE Section 2.3 13

Input from the Keyboard 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. 16 January 2015 JFE Section 2.3 14

Input from the Keyboard 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. 16 January 2015 JFE Section 2.3 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); } } 16 January 2015 JFE Section 2.3 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. */ 16 January 2015 JFE Section 2.5 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". */ 16 January 2015 JFE Section 2.5 18