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

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

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

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

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

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

Program Fundamentals

CS 11 java track: lecture 1

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

1.1 Your First Program

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

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

Introduction Basic elements of Java

Lecture 2: Intro to Java

1.1 Your First Program

Java Bytecode (binary file)

MODULE 02: BASIC COMPUTATION IN JAVA

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

CS11 Java. Fall Lecture 1

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

Tutorials. Inf1-OP. Learning Outcomes for this week. A Foundation for Programming. Conditionals and Loops 1

1.1 Your First Program

1.1 Your First Program

1.00/1.001 Tutorial 1

Built-in data types. logical AND logical OR logical NOT &&! public static void main(string [] args)

Inf1-OOP. OOP Exam Review. Perdita Stevens, adapting earlier version by Ewan Klein. March 16, School of Informatics

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

CS 231 Data Structures and Algorithms, Fall 2016

1.1 Your First Program

Chapter 2: Data and Expressions

Elementary Programming

Tutorials. Tutorial every Friday at 11:30 AM in Toldo 204 * discuss the next lab assignment

Built-in data types. public static void main(string [] args) logical AND logical OR logical NOT &&! Fundamentals of Computer Science

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

Java Basic Programming Constructs

Full file at

1.1 Your First Program! Naive ideal. Natural language instructions.

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

CompSci 125 Lecture 02

1.1 Your First Program

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Computer Science II Lecture 1 Introduction and Background

Chapter 2: Data and Expressions

Introduction to the course and basic programming concepts

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

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Basics of Java Programming

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

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

DATA TYPES AND EXPRESSIONS

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

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

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

CIS 110: Introduction to Computer Programming

Chapter 2: Data and Expressions

13 th Windsor Regional Secondary School Computer Programming Competition

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

Lecture 2: Intro to Java

Variables and data types

Introduction, Functions

COMP 202 Java in one week

Getting started with Java

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

Inf1-OP. Inf1-OP Exam Review. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 16, School of Informatics

CS Week 2. Jim Williams, PhD

Learning Outcomes for this week. Inf1-OP. A Foundation for Programming. A Foundation for Programming

Lesson 04: Our First Java Program (W01D4

Software and Programming 1

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

CS111: PROGRAMMING LANGUAGE II

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

History of Java. Java was originally developed by Sun Microsystems star:ng in This language was ini:ally called Oak Renamed Java in 1995

CS 106 Introduction to Computer Science I

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Course Outline. Introduction to java

Primitive Data, Variables, and Expressions; Simple Conditional Execution

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

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Pace University. Fundamental Concepts of CS121 1

1/16/12. CS 112 Introduction to Programming. A Foundation for Programming. (Spring 2012) Lecture #4: Built-in Types of Data. The Computer s View

1.1 Your First Program

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

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

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

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

CMPT 125: Lecture 3 Data and Expressions

Computer Components. Software{ User Programs. Operating System. Hardware

Topic 1: Introduction

1007 Imperative Programming Part II

Inf1-OOP. Data Types. Defining Data Types in Java. type value set operations. Overview. Circle Class. Creating Data Types 1.

Programming Language Basics

3. Java - Language Constructs I

What did we talk about last time? Examples switch statements

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

Midterms Save the Dates!

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Lecture 1. Course Overview Types & Expressions

Transcription:

Who and what can help? Inf1-OP Lecturer: Timothy Hospedales TA: Natalia Zon Course Overview Web: http://www.inf.ed.ac.uk/teaching/ courses/inf1/op/ Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein Piazza group: see sign-up link on web page Tutors and lab demonstrators: see web page School of Informatics InfBase: google infbase site:ed.ac.uk) ITO: office at Forrest Hill; source of all admin knowledge January 11, 2017 Textbooks Recommended textbook I The Java Tutorial: A Short Course on the Basics, Addison-Wesley. Not all that short! Contains a lot more than you need for this course. Page refs on slides are into 6th edition. Available from library as ebook, see course web page. Strongly recommended if you expect to go beyond the basic syllabus: advanced lab material may assume you have it.

Online Resources Weekly course events Probably the best: Oracle Java tutorials: http: //docs.oracle.com/javase/tutorial/java/index.html but there are many many sources: feel free to browse and find things that suit your own style. 1. One lecture per week 2. One 2-hour scheduled lab session per week 3. One tutorial per week, from Week 2 4. Some bonus video lectures Lectures will be videoed but IME doesn t always work Anyone is welcome to audio record for revision purposes Lecture slides are all on web page now. If they change (substantively) I will announce it. Lectures Lab Exercises Target audience for these lectures: You have taken Informatics 1 Functional Programming. 1 You have not already learned to program in an imperative, object-oriented language. 2 1 I ll allude to this from time to time, but don t worry if you haven t, just ignore my allusions. 2 If you have, the advanced tutorial stream is for you, and I will understand if you skip lectures. But do still work! Weekly lab exercises similar to weekly exercises in Haskell course. Can be carried out in the labs; you will be assigned to a two-hour lab session per week, starting from this week. If necessary, go to more than one session; can also swap. You may access the lab exercises from anywhere: physically attending the lab sessions is optional after the first week. Exercises are divided into warm-up, core and optional; the core exercises are obligatory. Feedback on lab exercises: principally via automated tests which you must use plus help from demonstrators in scheduled labs tutors can also help.

Scheduled Labs Tutorials You will be allocated to one of these see course web page for which and when. In the Drill Hall, Forrest Hill. Allocation is just to manage space: feel free to turn up to a slot you re not allocated for, but...... if you have a clash that means you can t attend your allocated slot ever, go to the ITO or fill in the ITO web form, to get it changed;... it s possible there might be contention for seats: if so, those not allocated to the lab must leave. You decide what to do! Challenge yourself. Weekly progress/feedback/advice meetings with tutor, starting in week 2. You re allocated to a time, but there will be a choice of tutorial group to join at that time, e.g. Fundamental vs Advanced. Advanced tutorials are for people who can already pass the Inf1OP exam well; Fundamentals for everyone else. You must attend a tutorial every week. If you re allocated to a time you can t make, contact the ITO (via web form or in person). See also first video lecture. Progress reporting A consequence of giving a choice of tutorials to attend is that it becomes more difficult to detect if someone is disengaging. For this reason, and to help both you and me track your progress, there will be a web form which you must fill in on Friday or Saturday every week from week 2-10 inclusive. I ll email to remind you and send the link, or find it on the course web page. This is where you say if you couldn t attend a tutorial one week, for example. Assessment Mostly formative labs and tutorials to help you learn and give you feedback on how you re doing. The only summative assessment is the final programming exam this determines your mark. It is: Scheduled as part of normal exam diet. Done in the labs on DICE machines in exam mode 2 hours long (not 3 as before). We do not aim to put you under time pressure. Open book : some online documentation provided, and you may take in anything you like on paper (books, lecture notes, solutions to lab exercises...) or on a USB stick. All code you submit must compile and pass some basic JUnit tests you re given, or it will get 0. A mock programming exam is scheduled for Week 11. Be there! There is an experimental automarking service: see web page.

Why learn another programming language after Haskell? Why Java? Haskell is not the only language you will ever need! Object orientation is the dominant paradigm, so you need to know an OO language. More importantly you need to get good at learning languages. Decently designed OO language Strong static typing Very popular > 9 million developers worldwide so: huge ecology of libraries, frameworks, tools... useful for you to have on your CV! http://www.westcanphoto.com/index.php?key=gosling How to approach this course Learning Outcomes for this week The lectures, Fundamental labs and exam cover only a small subset of Java. To pass the course, you will need to know that subset well. But further: if you plan to do more Informatics courses, that is not enough for example, if that s all you know, you will have to learn more Java as you go on in second year. When you will be busier than you are now... The better you learn Java this semester, the better off you will be. So if you have a head start: lucky you, but still work! Conversely if you don t, take full advantage of the support offered this semester: it ll never happen again. Use a text editor to create and modify simple Java programs which print strings to a terminal window. Use the command-line to compile and run Java programs. Declare int, double and String variables and assign values to them. Use Java s main() method to consume command-line arguments. Parse strings into values of type int and double. Carry out simple operations on int, double and String data values. Compute fractional results from division with integer values, using casting if necessary.

What is object orientation? A First Example It means: your program is structured like the domain (real world). Objects (organised into classes of similar objects) typically represent things (organised into types of similar things). Objects have state: they can store data behaviour: they can do things, in response to messages identity: two objects with the same state can still be different objects. Any of state, behaviour, identity can be trivial for a particular object, though. Our first objects will be just little bits of wrapped up behaviour. More later! HelloWorld.java /************************* * Prints "Hello, World!" *************************/ public class HelloWorld { public static void main (String[] args) { System.out.println("Hello, World!"); Creating a New Class 1. All Java code sits inside a class. 2. By important convention, class names are capitalized and in CamelCase. 3. Each class goes into a file of its own (usually; and always in this course). 4. So, use a text editor (e.g., gedit) to create a file called HelloWorld.java. 5. The name of the file has to be the same as the name of the class, and suffixed with.java. At the terminal gedit HelloWorld.java A First Example Declare a class public class HelloWorld { public static void main (String[] args){ System.out.println("Hello World!"); Basic form of a class definition. Class definition enclosed by curly braces.

A First Example Declare the main() method public class HelloWorld { public static void main (String[] args) { System.out.println("Hello World!"); A First Example Print a string to standard output public class HelloWorld { public static void main (String[] args) { System.out.println("Hello World!"); Compiling We need a main() method to actually get our program started. All our other code is invoked from inside main(). void means the method doesn t return a value. The argument of the method is an array of Strings; this array is called args. Definition of a method enclosed by curly braces. System.out is an object (a rather special one). println("hello World!") is a message being sent to that object: println is the method name, "Hello World!" is the argument. The whole line is a statement: must be terminated with a semi-colon (;). Strings must be demarcated by double quotes. Strings cannot be broken across a line in the file. Running the Program The program needs to be compiled before it can be executed. Use the javac command in a terminal. At the terminal javac HelloWorld.java Now that we have compiled code, we can run it. Use the java command in a terminal. At the terminal java HelloWorld Hello World! If there s a problem, the compiler will complain. If not, compiler creates a Java bytecode file called HelloWorld.class. Note that we omit the.class suffix in the run command. The java command wants a classname as argument, not a filename.

Edit-Compile-Run Cycle Edit-Compile-Run Cycle Edit Type in the program using an editor and save the program to a file. Use the name of the main class and the suffix.java for the file. This is called a source file. The program needs to be compiled before it can be executed. If you edit a program, you need to compile it again before running the new version. Eclipse will compile your code automatically. Run A java interpreter will read the bytecode file and execute the instructions in it. If an error occurs while running, the interpreter will stop its execution. Compile The process of compiling a source file generates the bytecode file. The byte code will have a.class suffix; the prefix will be the same. Arithmetic Addition and Division public class Calc { public static void main(string[] args) { System.out.print("The sum of 6 and 2 is "); System.out.println(6 + 2); Output System.out.print("The quotient of 6 and 2 is "); System.out.println(6 / 2); String Concatenation, 1 String Concatenation public class Concat { public static void main(string[] args) { System.out.println("The name is " + "Bond, " + "James Bond"); Output The name is Bond, James Bond The sum of 6 and 2 is 8 The quotient of 6 and 2 is 3

String Concatenation, 2 Assignment: Basic Definitions String Concatenation public class Concat { public static void main(string[] args) { System.out.println("Is that you, 00" + 7 + "?"); Output Is that you, 007? Variable: A name that refers to a value Assignment Statement: Associates a value with a variable variable name assignment statement declaration statement int a, b; a = 1234 ; b = 99; literal Important: = is the operator in an imperative statement, not a logical assertion. Assignment: Combining Declaration and Initialisation Hello World with Added Variables Variables that have been declared, but not assigned to, are a potential source of error. (Exercise for the keen: understand what happens to them in Java.) It s often best to declare a variable and initialise it at the same time. int a, b; a = 1234; b = 99; int c = a + b; Storing a String in a variable public class HelloWorld { public static void main ( String [] args ) { String msg = "Hello World!"; System.out.println( msg ); combined declaration and assignment statement

Built-in Data Types Integer operations type value set literal values operations char characters A, $ compare String sequences of "Hello World!", concatenate characters "Java is fun" int integers 17, 1234 add, subtract, multiply, divide double floating-point numbers 3.1415, 6.022e23 add, subtract, multiply, divide boolean truth values true, false and, or, not expression value comment 5 + 3 8 5-3 2 5 * 3 15 5 / 2 2 no fractional part 5 % 2 1 remainder 1 / 0 run-time error 3 * 5-2 13 * has precedence 3 + 5 / 2 5 / has precedence 3-5 - 2-4 left associative ( 3-5 ) - 2-4 better style 3 - ( 5-2 ) 0 unambiguous Command-line Arguments Command-line Arguments Unix commands mkdir MyJavaCode mkdir is a command and MyJavaCode is an argument Using Java to carry out commands public class Add { public static void main(string[] args) { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); System.out.println(a + b); % java Add 3 6 9 3 and 6 are command-line arguments for the program Add int a = Integer.parseInt(args[0]); This reads in a string (e.g., "3") from the command line, parses it as an int, and assigns this as the value of variable a.

Command-line Arguments Floating-Point Numbers Missing an argument % java Add 3 java.lang.arrayindexoutofboundsexception: 1 The default floating-point type in Java is double. This a run-time error we didn t provide anything as a value for args[1]: int b = Integer.parseInt(args[1]); Floating-Point Operations expression value 3.141 +.03 3.171 3.141 -.03 3.111 6.02e23 / 2.0 3.01e23 5.0 / 3.0 1.6666666666666667 10.0 % 3.141 0.577 1.0 / 0.0 Infinity Math.sqrt(2.0) 1.4142135623730951 Math.sqrt(-1.0) NaN Type Conversion Sometimes we can convert one type to another. Automatic: OK if no loss of precision, or converts to string Explicit: use a cast or method like parseint() expression result type value "1234" + 99 String 123499 Integer.parseInt("123") int 123 (int) 2.71828 int 2 Math.round(2.71828) long 3 (int) Math.round(2.71828) int 3 (int) Math.round(3.14159) int 3 11 * 0.3 double 3.3 (int) 11 * 0.3 double 3.3 11 * (int) 0.3 int 0 (int) (11 * 0.3) int 3

Type Conversion: Division Recap: Learning Outcomes for this week expression result type value 5 / 2 int 2 (double)(5 / 2) double 2.0 5 / 2.0 double 2.5 5.0 / 2 double 2.5 5.0 / 2.0 double 2.5 Moral: if you want a floating-point result from division, make at least one of the operands a double. Use a text editor to create and modify simple Java programs which print strings to a terminal window. Use the command-line to compile and run Java programs. Declare int, double and String variables and assign values to them. Use Java s main() method to consume command-line arguments. Parse strings into values of type int and double. Carry out simple operations on int, double and String data values. Compute fractional results from division with integer values, using casting if necessary. This Week s Reading Java Tutorial pp1-68, i.e. Chapters 1 Getting Started, 2 Object-Oriented Programming Concepts, and Chapter 3 Language Basics, up to Expressions, Statements and Blocks except note: We use Eclipse, not NetBeans, as our IDE. We ll come to the Chapter 2 material later. We ll talk about Arrays later. I suggest skimming Ch 2 and the Arrays section, and rereading them later.