List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs ar

Similar documents
17 Hello world 18 Type: String: literal 19 Standard API: System: out.println() 20 Hello world 21 Statement 22 Statement: simple statements are ended w

17 Statement: assignment statement: assigning an expression value 18 Age next year 19 Type: String: conversion: from int 21 Age next year 22 The full

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

CS 177 Recitation. Week 1 Intro to Java

Java Bytecode (binary file)

Mobile App:IT. Methods & Classes

Class 1: Homework. Intro to Computer Science CSCI-UA.0101 New York University Courant Institute of Mathematical Sciences Fall 2017

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Lab # 2. For today s lab:

Chapter Two Bonus Lesson: JavaDoc

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Program Development. Program Development. A Foundation for Programming. Program Development

Programming with Java

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Lesson 04: Our First Java Program (W01D4

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Introduction Basic elements of Java

Java Foundations: Unit 3. Parts of a Java Program

Inf1-OP. Course Overview. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

Who and what can help? Inf1-OP. Lecturer: Timothy Hospedales TA: Natalia Zon

40 Absolute difference 42 Trying it 43 Trying it 44 Concepts covered in this chapter 0-2

An overview of Java, Data types and variables

Java Just in Time: task questions

Basic Programming Language Syntax

Computer Hardware. Java Software Solutions Lewis & Loftus. Key Hardware Components 12/17/2013

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

Title. Java Just in Time. John Latham. October 25, October 25, 2017 Java Just in Time - John Latham Page 1(0/0)

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Program Fundamentals

Welcome to CSE 142! Zorah Fung University of Washington, Spring Building Java Programs Chapter 1 Lecture 1: Introduction; Basic Java Programs

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

Getting Started with Java. Atul Prakash

CS 11 java track: lecture 1

Welcome to CSE 142! Whitaker Brand. University of Washington, Winter 2018

Inf1-OOP. Textbooks. Who and What. Organisational issues. Why Java? Course Overview. Hello, World! in Java

Code Ninjas: Introduction to Computer Science. Macomb Science Olympiad Presented by Swati Dharia

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

How to make a "hello world" program in Java with Eclipse *

Creating a Program in JCreator. JCreator is then used to create our program. But the first step is to create a new file.

Lec 3. Compilers, Debugging, Hello World, and Variables

Title. Java Just in Time. John Latham. November 5, November 5, 2018 Java Just in Time - John Latham Page 1(0/0)

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

Inf1-OOP. Textbooks. Who and What. Organizational Issues. Why Java? Course Overview. Hello, World! in Java. Ewan Klein, Perdita Stevens

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

C02: Overview of Software Development and Java

Software and Programming 1

CS 231 Data Structures and Algorithms, Fall 2016

Administrative Stuff. Inf1-OP. Additional help? Who to contact for help? Course Overview

Pace University. Fundamental Concepts of CS121 1

Building Java Programs. Introduction to Programming and Simple Java Programs

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

COMP 110 Project 1 Programming Project Warm-Up Exercise

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Chapter 2 Author Notes

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

The compiler is spewing error messages.

Java: Comment Text. Introduction. Concepts

Chapter 1. Introduction

CSC116: Introduction to Computing - Java

G52PGP. Lecture oo3 Java (A real object oriented language)

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

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

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

CS11 Java. Fall Lecture 1

CS 251 Intermediate Programming Java Basics

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

1B1a Programming I Getting Started

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Chapter 2: Programming Concepts

1007 Imperative Programming Part II

CMSC 150 LECTURE 1 INTRODUCTION TO COURSE COMPUTER SCIENCE HELLO WORLD

Getting started with Java

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Department of Computer Science University of Pretoria. Introduction to Computer Science COS 151

A Foundation for Programming

CS 106 Introduction to Computer Science I

First Java Program - Output to the Screen

Programs, Statements, Variables

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

1. Download the JDK 6, from

Outline. CIS 110: Introduction to Computer Programming. What is Computer Science? What is computer programming? What is computer science?

Programming Language Concepts: Lecture 2

Course Outline. Introduction to java

Full file at

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Software and Programming 1

Transcription:

List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs are divided into classes 7 Class: public class 8 Class: definition 9 Hello world 10 Method: main method: programs contain a main method 11 Method: main method: is public 12 Method: main method: is static 13 Method: main method: is void 14 Method: main method: is the program starting point 15 Command line arguments: program arguments are passed to main 16 Method: main method: always has the same heading 0-0

17 Hello world 18 Type: String: literal 19 Standard API: System: out.println() 20 Hello world 21 Statement 22 Statement: simple statements are ended with a semi-colon 23 Hello world 24 The full HelloWorld code 25 Trying it 26 Coursework: HelloWorld in French 27 Section 3: Example:Hello world with a syntactic error 28 Aim 29 Error 30 Error: syntactic error 33 Hello world with a syntactic error 34 Trying it 35 Type: String: literal: must be ended on the same line 36 Trying it 0-1

37 Coursework: Fortune syntactic errors 38 Section 4: Example:Hello world with a semantic error 39 Aim 40 Error: semantic error 42 Error: compile time error 43 Hello world with a semantic error 44 Trying it 45 Coursework: ManchesterWeather semantic errors 46 Section 5: Example:Hello solar system 47 Aim 48 Execution: sequential execution 49 Hello solar system 50 Trying it 51 Coursework: HelloFamily 52 Section 6: Example:Hello solar system with a run time error 53 Aim 54 Error: run time error 55 Hello solar system with a run time error 0-2

56 Trying it 57 Trying it 58 Trying it 59 Coursework: Quote run time errors 60 Section 7: Example:Hello anyone 61 Aim 62 Command line arguments: program arguments are accessed by inde 63 Type: String: concatenation 64 Hello anyone 65 Trying it 66 Trying it 67 Trying it 68 Trying it 69 Trying it 70 Coursework: FlatterMe 71 Section 8: Example:Hello anyone with a logical error 72 Aim 73 Error: logical error 0-3

74 Hello anyone with a logical error 75 Trying it 76 Coursework: Birthday logical errors 77 Section 9: Hello solar system, looking at the layout 78 Aim 79 Code clarity: layout 81 Hello solar system, looking at the layout 82 Code clarity: layout: indentation 84 Coursework: Limerick layout 85 Concepts covered in this chapter 0-4

Title Java Just in Time John Latham October 1, 2009 October 1, 2009 Java Just in Time - John Latham Page 1(0/0)

Chapter 2 Sequential execution and program errors October 1, 2009 Java Just in Time - John Latham Page 2(0/0)

Chapter aims Introduce some very basic Java concepts. Especially the idea of sequential execution. Look at the kinds of errors we can have in our programs. Because you will make errors! And you don t need to be afraid of them they are part of the programming experience! October 1, 2009 Java Just in Time - John Latham Page 3(0/0)

Section 2 Example: Hello world October 1, 2009 Java Just in Time - John Latham Page 4(0/0)

Aim AIM: To introduce some very basic Java concepts, including the main method and System.out.println(). October 1, 2009 Java Just in Time - John Latham Page 5(0/0)

Class: programs are divided into classes Program source text is separated into pieces called classes. Each piece is (usually) stored in a separate file. File name is the name of the class, with.java appended. E.g. HelloWorld in HelloWorld.java. One reason for dividing is to make management easier. A program might have thousands of lines. Another is to make sharing between programs easier. software reuse helps productivity. Every program has at least one class. Its name reflects the intention of the program. Convention: class names start with an upper case letter. October 1, 2009 Java Just in Time - John Latham Page 6(0/0)

Class: public class A class declared public can be accessed from anywhere in the running Java environment; in particular the virtual machine can access it. The source text starts with the reserved word public. A reserved word is part of the Java language. E.g. you cannot have a program called public. October 1, 2009 Java Just in Time - John Latham Page 7(0/0)

Class: definition After public we write the reserved word class, then its name, then a left brace ( ), its body of text and finally a closing right brace ( ). public class MyFabulousProgram... Lots of stuff here. October 1, 2009 Java Just in Time - John Latham Page 8(0/0)

Hello world The heading for our HelloWorld class. 001: public class HelloWorld Then we have the opening bracket. 002: October 1, 2009 Java Just in Time - John Latham Page 9(0/0)

Method: main method: programs contain a main method All Java programs contain a section of code called main. This is where the computer will start to execute the program. Sections of code are called methods they contain instructions on how to do something. The main method always starts with the following heading. public static void main(string[] args) October 1, 2009 Java Just in Time - John Latham Page 10(0/0)

Method: main method: is public The main method starts with the reserved word public. So the virtual machine has access to it. public October 1, 2009 Java Just in Time - John Latham Page 11(0/0)

Method: main method: is static The main method has the reserved word static. Thus it is allowed to be used in the static context. A context is an allocation of computer memory for the program and data, etc.. The virtual machine creates the static context when the program is loaded. A dynamic context is a kind of allocation of memory made during the run of the program. The main method must be able to run in the static context else the program could not be started! public static October 1, 2009 Java Just in Time - John Latham Page 12(0/0)

Method: main method: is void In general, a method might calculate and return some result, if so we state this in its heading. E.g. a method might calculate the square root of a number, and return the answer as a number. If it does not we write the reserved word void. Void means without contents. The main method does not return a value. public static void October 1, 2009 Java Just in Time - John Latham Page 13(0/0)

Method: main method: is the program starting point The program starting part main method is always called main. It is the main part of the program. public static void main October 1, 2009 Java Just in Time - John Latham Page 14(0/0)

Command line arguments: program arguments are passed to main Recall that programs can be given command line arguments. Java programs too can have arguments. Program arguments are a list of text strings. In Java, String[] means list of strings. We must give a name for this list, usually args. So we can refer to the given data from within the program if needed. public static void main(string[] args) October 1, 2009 Java Just in Time - John Latham Page 15(0/0)

Method: main method: always has the same heading Java program main methods always have a heading like this. public static void main(string[] args) Even if we do not intend to use command line arguments. So a typical single class program looks like the following. public class MyFabulousProgram public static void main(string[] args)... Stuff here to perform the task. October 1, 2009 Java Just in Time - John Latham Page 16(0/0)

Hello world Back to HelloWorld.... 003: public static void main(string[] args) 004: October 1, 2009 Java Just in Time - John Latham Page 17(0/0)

Type: String: literal A string literal is a fixed piece of text to be used as data. We enclose the text in double quotes. "This is a fixed piece of text data -- a string literal" This might be used as a message to the user. October 1, 2009 Java Just in Time - John Latham Page 18(0/0)

Standard API: System: out.println() The simplest way to print a message on standard output: System.out.println("This text will appear on standard output"); System is a class in Java s application program interface (API). Inside System there is a thing called out. This has a method called println. Overall it is called System.out.println. It takes a string in its brackets and displays it on the standard output. October 1, 2009 Java Just in Time - John Latham Page 19(0/0)

Hello world Back to HelloWorld.... 005: System.out.println("Hello world!"); Observe the semi-colon.... October 1, 2009 Java Just in Time - John Latham Page 20(0/0)

Statement A command that makes the computer perform a task is a statement. E.g. System.out.println("I will output whatever I am told to") October 1, 2009 Java Just in Time - John Latham Page 21(0/0)

Statement: simple statements are ended with a semi-colon All simple Java statements must be ended by a semi-colon. This is a rule of the Java language syntax. Coffee time: Can you think of a reason why Java insists on the programmer putting a semi-colon at the end of statements? October 1, 2009 Java Just in Time - John Latham Page 22(0/0)

Hello world Back to HelloWorld.... 006: 007: October 1, 2009 Java Just in Time - John Latham Page 23(0/0)

The full HelloWorld code 001: public class HelloWorld 002: 003: public static void main(string[] args) 004: 005: System.out.println("Hello world!"); 006: 007: October 1, 2009 Java Just in Time - John Latham Page 24(0/0)

Trying it We create the source code and compile it. $ ls -l HelloWorld.java Console Input / Output -rw------- 1 jtl jtl 117 Apr 24 15:00 HelloWorld.java $ javac HelloWorld.java $ ls -l HelloWorld.* -rw------- 1 jtl jtl 426 Apr 24 15:00 HelloWorld.class -rw------- 1 jtl jtl 117 Apr 24 15:00 HelloWorld.java $ _ Run We run the program to get the message on its standard output. Console Input / Output $ java HelloWorld Hello world! $ _ Run October 1, 2009 Java Just in Time - John Latham Page 25(0/0)

Coursework: HelloWorld in French (Summary only) Write a program to greet the whole world, in French! October 1, 2009 Java Just in Time - John Latham Page 26(0/0)

Section 3 Example: Hello world with a syntactic error October 1, 2009 Java Just in Time - John Latham Page 27(0/0)

Aim AIM: To introduce the principle of program errors, in particular syntactic errors. We also see that a string literal must be ended on the same line its starts on. October 1, 2009 Java Just in Time - John Latham Page 28(0/0)

Error To err is Human.... when you write source code you will get somethings wrong. Lots of rules of Java to obey for a valid program. Being new to it you will break these rules. Even seasoned Java programmers make errors. October 1, 2009 Java Just in Time - John Latham Page 29(0/0)

Error: syntactic error When we break the syntax rules of Java we have a syntactic error. E.g. omitting a closing bracket, semi-colon.... Similar to grammatical error in natural language. E.g. sign strapped to the back of a poodle... October 1, 2009 Java Just in Time - John Latham Page 30(0/0)

Error: syntactic error My other dog an Alsatian. October 1, 2009 Java Just in Time - John Latham Page 31(0/0)

Error: syntactic error The compiler gives error messages for syntactic errors. Watch out: the compiler can get confused.... October 1, 2009 Java Just in Time - John Latham Page 32(0/0)

Hello world with a syntactic error 001: public class HelloWorld 002: 003: public static void main(string[] args) 004: 005: System.out.println("Hello world!); 006: 007: Coffee time: Can you spot the syntactic error? October 1, 2009 Java Just in Time - John Latham Page 33(0/0)

Trying it Console Input / Output $ javac HelloWorld.java HelloWorld.java:5: unclosed string literal System.out.println("Hello world!); ˆ 1 error $ _ Run Error messages from the compiler can look very daunting. Read carefully and observe the parts.... October 1, 2009 Java Just in Time - John Latham Page 34(0/0)

Type: String: literal: must be ended on the same line In Java, string literals must be ended on the same line they are started on. October 1, 2009 Java Just in Time - John Latham Page 35(0/0)

Trying it The compiler can get confused by bad code, but is getting better. Here is the Java 5.0 compiler processing the same program. Console Input / Output $ /usr/java/jdk1.5.0_04/bin/javac HelloWorld.java HelloWorld.java:5: unclosed string literal System.out.println("Hello world!); ˆ HelloWorld.java:6: ) expected ˆ 2 errors $ _ Run Coffee time: What has caused the second error message? October 1, 2009 Java Just in Time - John Latham Page 36(0/0)

Coursework: Fortune syntactic errors (Summary only) Take a given program that has syntactic errors in it, and get it working. October 1, 2009 Java Just in Time - John Latham Page 37(0/0)

Section 4 Example: Hello world with a semantic error October 1, 2009 Java Just in Time - John Latham Page 38(0/0)

Aim AIM: To introduce semantic errors and note that these and syntactic errors are compile time errors. October 1, 2009 Java Just in Time - John Latham Page 39(0/0)

Error: semantic error A semantic error we obey the syntax rules but we write something with no meaning (semantics). E.g. another sign, another poodle... October 1, 2009 Java Just in Time - John Latham Page 40(0/0)

Error: semantic error My other dog is a Porsche. October 1, 2009 Java Just in Time - John Latham Page 41(0/0)

Error: compile time error Java syntactic errors and semantic errors are detected by the compiler. Collectively called compile time errors. October 1, 2009 Java Just in Time - John Latham Page 42(0/0)

Hello world with a semantic error 001: public class HelloWorld 002: 003: public static void main(text[] args) 004: 005: System.out.println("Hello world!"); 006: 007: Coffee time: Can you spot the semantic error? October 1, 2009 Java Just in Time - John Latham Page 43(0/0)

Trying it $ javac HelloWorld.java Console Input / Output HelloWorld.java:3: cannot find symbol symbol : class Text location: class HelloWorld public static void main(text[] args) ˆ 1 error $ _ Run A little cryptic? Read carefully. You ll get used to it. October 1, 2009 Java Just in Time - John Latham Page 44(0/0)

Coursework: ManchesterWeather semantic errors (Summary only) Take a given program that has semantic errors in it, and get it working. October 1, 2009 Java Just in Time - John Latham Page 45(0/0)

Section 5 Example: Hello solar system October 1, 2009 Java Just in Time - John Latham Page 46(0/0)

Aim AIM: To introduce the principle of sequential execution. October 1, 2009 Java Just in Time - John Latham Page 47(0/0)

Execution: sequential execution Programs have many statements in a list. Usually placed on separate lines enhance human readability. Java doesn t care about layout we should. Statements in a list are executed one after the other. Actually the compiler turns each into byte codes. The virtual machine executes each collection of byte codes in turn. Known as sequential execution. October 1, 2009 Java Just in Time - John Latham Page 48(0/0)

Hello solar system 001: public class HelloSolarSystem 002: 003: public static void main(string[] args) 004: 005: System.out.println("Hello Mercury!"); 006: System.out.println("Hello Venus!"); 007: System.out.println("Hello Earth!"); 008: System.out.println("Hello Mars!"); 009: System.out.println("Hello Jupiter!"); 010: System.out.println("Hello Saturn!"); 011: System.out.println("Hello Uranus!"); 012: System.out.println("Hello Neptune!"); 013: System.out.println("Goodbye Pluto!"); 014: 015: October 1, 2009 Java Just in Time - John Latham Page 49(0/0)

Trying it Console Input / Output $ javac HelloSolarSystem.java $ java HelloSolarSystem Hello Mercury! Hello Venus! Hello Earth! Hello Mars! Hello Jupiter! Hello Saturn! Hello Uranus! Hello Neptune! Goodbye Pluto! $ _ Run October 1, 2009 Java Just in Time - John Latham Page 50(0/0)

Coursework: HelloFamily (Summary only) Write a program to greet some of your family. October 1, 2009 Java Just in Time - John Latham Page 51(0/0)

Section 6 Example: Hello solar system with a run time error October 1, 2009 Java Just in Time - John Latham Page 52(0/0)

Aim AIM: To introduce the principle of run time errors. October 1, 2009 Java Just in Time - John Latham Page 53(0/0)

Error: run time error Errors detected when the program is run are run time errors. Java calls them exceptions. Messages can look very cryptic? Read carefully, get used to them. E.g. Exception in thread "main" java.lang.nosuchmethoderror: main Best clue: look either side of the colon (:). October 1, 2009 Java Just in Time - John Latham Page 54(0/0)

Hello solar system with a run time error 001: public class HelloSolarSystem 002: 003: public static void Main(String[] args) 004: 005: System.out.println("Hello Mercury!"); 006: System.out.println("Hello Venus!"); 007: System.out.println("Hello Earth!"); 008: System.out.println("Hello Mars!"); 009: System.out.println("Hello Jupiter!"); 010: System.out.println("Hello Saturn!"); 011: System.out.println("Hello Uranus!"); 012: System.out.println("Hello Neptune!"); 013: System.out.println("Goodbye Pluto!"); 014: 015: Coffee time: What will cause a run time error? October 1, 2009 Java Just in Time - John Latham Page 55(0/0)

Trying it It compiles okay. Console Input / Output $ javac HelloSolarSystem.java $ _ Run But when we run it.... Console Input / Output $ java HelloSolarSystem Exception in thread "main" java.lang.nosuchmethoderror: main $ _ Run The virtual machine is telling us our program has no main method. We called it Main instead of main! October 1, 2009 Java Just in Time - John Latham Page 56(0/0)

Trying it Another example run time error. Console Input / Output $ java HelloMum Exception in thread "main" java.lang.noclassdeffounderror: HelloMum $ _ Run October 1, 2009 Java Just in Time - John Latham Page 57(0/0)

Trying it Coffee time: Imagine a version of HelloSolarSystem, called HelloSolarSystemNoArgs, with a lower case m on main, but String[] args has been omitted. Explain the following. Console Input / Output $ javac HelloSolarSystemNoArgs.java $ java HelloSolarSystemNoArgs Exception in thread "main" java.lang.nosuchmethoderror: main $ _ Run October 1, 2009 Java Just in Time - John Latham Page 58(0/0)

Coursework: Quote run time errors (Summary only) Take a given program that has run time errors in it, and get it working. October 1, 2009 Java Just in Time - John Latham Page 59(0/0)

Section 7 Example: Hello anyone October 1, 2009 Java Just in Time - John Latham Page 60(0/0)

Aim AIM: To introduce the principle of making Java programs perform a variation of their task based on command line arguments, which can be accessed via an index. We also meet string concatenation. October 1, 2009 Java Just in Time - John Latham Page 61(0/0)

Command line arguments: program arguments are accessed by index The command line arguments given to main method a list of strings from the command line. Each has an integer index, starting from zero. To access one we use its index in square brackets. E.g. args[0] is the first command line argument. October 1, 2009 Java Just in Time - John Latham Page 62(0/0)

Type: String: concatenation The + operator gives the concatenation of two strings. E.g. "Hello " + "world" has the same value as "Hello world". (Note where the space came from.) Most useful with one or more variable values. E.g. "Hello " + args[0] E.g. System.out.println("Hello " + args[0]) Coffee time: When might we concatenate two string literals? October 1, 2009 Java Just in Time - John Latham Page 63(0/0)

Hello anyone 001: public class HelloAnyone 002: 003: public static void main(string[] args) 004: 005: System.out.println("Hello " + args[0]); 006: 007: October 1, 2009 Java Just in Time - John Latham Page 64(0/0)

Trying it Console Input / Output $ javac HelloAnyone.java $ java HelloAnyone John Hello John $ java HelloAnyone Lizzy Hello Lizzy $ _ Run What if no argument? Console Input / Output $ java HelloAnyone Exception in thread "main" java.lang.arrayindexoutofboundsexception: 0 at HelloAnyone.main(HelloAnyone.java:5) $ _ Run Observe source name and line number. October 1, 2009 Java Just in Time - John Latham Page 65(0/0)

Trying it What if name contains a space? Console Input / Output $ java HelloAnyone "John Latham" Hello John Latham $ java HelloAnyone John Latham Hello John $ _ Run What about empty string? Console Input / Output $ java HelloAnyone "" Hello $ _ Run October 1, 2009 Java Just in Time - John Latham Page 66(0/0)

Trying it D:\JJIT\Example 2.7>dir HelloAnyone.java Volume in drive D is DATA Volume Serial Number is 5C90 0C33 Directory of D:\JJIT\Example 2.7 24/04/2009 15:00 130 HelloAnyone.java 1 File(s) 130 bytes 0 Dirs(s) 8,389,459,968 bytes free D:\JJIT\Example 2.7>javac HelloAnyone.java D:\JJIT\Example 2.7>dir HelloAnyone.* Volume in drive D is DATA Volume Serial Number is 5C90 0C33 Directory of D:\JJIT\Example 2.7 24/04/2009 15:00 130 HelloAnyone.java 24/04/2009 15:00 586 HelloAnyone.class 2 File(s) 716 bytes 0 Dirs(s) 8,389,459,968 bytes free D:\JJIT\Example 2.7>java HelloAnyone "John Latham" Hello John Latham D:\JJIT\Example 2.7>_ October 1, 2009 Java Just in Time - John Latham Page 67(0/0)

Trying it October 1, 2009 Java Just in Time - John Latham Page 68(0/0)

Trying it October 1, 2009 Java Just in Time - John Latham Page 69(0/0)

Coursework: FlatterMe (Summary only) Write a program to say how wonderful the user is. October 1, 2009 Java Just in Time - John Latham Page 70(0/0)

Section 8 Example: Hello anyone with a logical error October 1, 2009 Java Just in Time - John Latham Page 71(0/0)

Aim AIM: To introduce the principle of logical errors. October 1, 2009 Java Just in Time - John Latham Page 72(0/0)

Error: logical error Most tricky kind of error a logical error. No help from compiler, nor virtual machine. Code is meaningful to Java. But program does not do what we wanted. Java is too stupid to know that. Subtles ones slip through our testing. They become bugs. October 1, 2009 Java Just in Time - John Latham Page 73(0/0)

Hello anyone with a logical error 001: public class HelloAnyone 002: 003: public static void main(string[] args) 004: 005: System.out.println("Hello + args[0]"); 006: 007: Coffee time: Can you spot the logical error? October 1, 2009 Java Just in Time - John Latham Page 74(0/0)

Trying it It compiles and runs without error. Console Input / Output $ javac HelloAnyone.java $ java HelloAnyone John Hello + args[0] $ _ Run October 1, 2009 Java Just in Time - John Latham Page 75(0/0)

Coursework: Birthday logical errors (Summary only) Take a given program that has logical errors in it, and get it working. October 1, 2009 Java Just in Time - John Latham Page 76(0/0)

Section 9 Hello solar system, looking at the layout October 1, 2009 Java Just in Time - John Latham Page 77(0/0)

Aim AIM: To begin to explore the decisions behind the way we lay out the source code for a program. October 1, 2009 Java Just in Time - John Latham Page 78(0/0)

Code clarity: layout Java doesn t care about layout white space must separate symbols that would be one symbol otherwise. E.g. public void would be publicvoid. Could put program on one line, minimum space. public class HelloSolarSystem Or split just to fit on page. public static void main(string[]args) System.out.println("Hello Mercu public class HelloSolarSystem public static void main(string[]args) System.out.println("Hello Mercury!");System.out.println( "Hello Venus!");System.out.println("Hello Earth!");System.out.println ("Hello Mars!");System.out.println("Hello Jupiter!");System.out. println("hello Saturn!");System.out.println("Hello Uranus!");System. out.println("hello Neptune!");System.out.println("Goodbye Pluto!"); October 1, 2009 Java Just in Time - John Latham Page 79(0/0)

Code clarity: layout Layout is important for the human reader. Take pride in making your work most readable. Split lines in good places. Use indentation to show structure. October 1, 2009 Java Just in Time - John Latham Page 80(0/0)

Hello solar system, looking at the layout 001: public class HelloSolarSystem 002: 003: public static void main(string[] args) 004: 005: System.out.println("Hello Mercury!"); 006: System.out.println("Hello Venus!"); 007: System.out.println("Hello Earth!"); 008: System.out.println("Hello Mars!"); 009: System.out.println("Hello Jupiter!"); 010: System.out.println("Hello Saturn!"); 011: System.out.println("Hello Uranus!"); 012: System.out.println("Hello Neptune!"); 013: System.out.println("Goodbye Pluto!"); 014: 015: New line after class heading. New line plus indentation 2 or 3 spaces for main method. New line, same indentation. More indentation, each statement on own line. Line up with opening braces. October 1, 2009 Java Just in Time - John Latham Page 81(0/0)

Code clarity: layout: indentation A class contains nested structures. The class has heading and body. The body has main method. Main method has heading and body. Use indentation to show structure. The more nested, the more space. Be consistent: always same number of spaces per nesting. Two or three is good. Don t use tabs! Opening and closing braces have same indentation. October 1, 2009 Java Just in Time - John Latham Page 82(0/0)

Code clarity: layout: indentation Some people prefer this style subjectively less clear? public class HelloWorld public static void main(string[] args) System.out.println("Hello world!"); October 1, 2009 Java Just in Time - John Latham Page 83(0/0)

Coursework: Limerick layout (Summary only) Take a given program and lay it out properly. October 1, 2009 Java Just in Time - John Latham Page 84(0/0)

Concepts covered in this chapter Each book chapter ends with a list of concepts covered in it. Each concept has with it a self-test question, and a page reference to where it was covered. Please use these to check your understanding before we start the next chapter. October 1, 2009 Java Just in Time - John Latham Page 85(0/0)