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

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

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

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

1.1 Your First Program

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

1.1 Your First Program

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

1.1 Your First Program

1.1 Your First Program

Lecture 2: Intro to Java

1.1 Your First Program

Lecture 2: Intro to Java

1.1 Your First Program

Introduction Basic elements of Java

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

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

Program Fundamentals

CS11 Java. Fall Lecture 1

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

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

Java Bytecode (binary file)

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

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

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

MODULE 02: BASIC COMPUTATION IN JAVA

Elementary Programming

CS 11 java track: lecture 1

1.1 Your First Program

DATA TYPES AND EXPRESSIONS

1.1 Your First Program

Software and Programming 1

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

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

Lesson 04: Our First Java Program (W01D4

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

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

Java Basic Programming Constructs

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

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

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

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

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

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

Introduction to the course and basic programming concepts

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

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

CS Week 2. Jim Williams, PhD

Chapter 2: Data and Expressions

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

CS 106 Introduction to Computer Science I

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

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

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

CS111: PROGRAMMING LANGUAGE II

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

Values, Variables, Types & Arithmetic Expressions. Agenda

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

Variables and data types

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

TTh 9.25 AM AM Strain 322

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Basics of Java Programming variables, assignment, and input

Advanced Computer Programming

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

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

Software and Programming 1

Index. Course Outline. Grading Policy. Lab Time Distribution. Important Instructions

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

COP4020 Programming Assignment 1 - Spring 2011

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

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Pace University. Fundamental Concepts of CS121 1

CSC116: Introduction to Computing - Java

Full file at

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

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

A Java program contains at least one class definition.

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

CSC116: Introduction to Computing - Java

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

DM550 Introduction to Programming part 2. Jan Baumbach.

Topic 1: Introduction

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

Computer Science II Lecture 1 Introduction and Background

Introduction, Functions

3. Java - Language Constructs I

Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

CS 231 Data Structures and Algorithms, Fall 2016

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

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Course Outline. Introduction to java

Operators. Java operators are classified into three categories:

CSC116: Introduction to Computing - Java

Transcription:

Organizational Issues Inf1-OOP Course Overview Ewan Klein, Perdita Stevens School of Informatics January 12, 2013 Why Java? Hello, World! in Java Built-in Types Integers Floating-Point Numbers Type Conversion Summary Who and What Textbooks Lecturer: Perdita Stevens TA: Paris Morali (until early Feb) Web: http://tinyurl.com/inf1-oop Forum: https://www.forums.ed.ac.uk/ then Informatics Teaching, inf1: then see Howto post. InfBase: google it (maybe with site:ed.ac.uk) Office hours: Mondays and Wednesdays, 10-11am, IF5.05

Textbooks Online Resources Robert Sedgewick & Kevin Wayne (2008) Introduction to Programming in Java, Pearson/Addison-Wesley. Particularly interesting example programs. Cay Horstmann (2008) Java Concepts, Wiley. A good introduction to the Java language; lots of detail, but not too technical. Sun Java tutorial: http: //docs.oracle.com/javase/tutorial/java/index.html David Eck, Introduction to Programming Using Java: http://math.hws.edu/javanotes/ Sample chapters and code downloads for Sedgewick and Wayne: http://introcs.cs.princeton.edu/home/ Sharon Zakhour et al (2006) The Java Tutorial: A Short Course on the Basics, 4th Edition, Addison-Wesley. More dense; suitable for readers with prior programming experience. Available online. Outline Course Structure Lectures 1. One lecture per week 2. One 2-hour scheduled lab session per week 3. One tutorial/project review per week, from Week 2 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. Target audience for these lectures: You have taken Informatics 1 Functional Programming. You have not already learned to program in an imperative / object-oriented language.

Lab Exercises Lab Slots Weekly lab exercises similar to weekly exercises in Haskell course. To 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. Exercises are divided into warm-up, core and optional; the core exercises are obligatory. Feedback on lab exercises: not via tutorials; instead, via web-based automarking tool, Infandango plus help from demonstrators. Current OP labs (AT5.05): Mon 1510-1700 Tue 1410-1600 Wed 1510-1700 Thu 1510-1700 Fri 1510-1700 You will be allocated to one of these. If you have a clash that means you can t attend your allocated slot ever, go to the ITO (AT Level 4) or fill in the ITO web form, to get it changed. If you can t attend a particular session, just turn up to one of the others. Tutorials / Project Small team Java projects. Based on tutorial groups. Weekly meetings with tutor, starting in week 2. Each project team will get feedback, but not part of final marks. Assessment Lab Exercises Doing these is compulsory and attracts small amount of credit: your automarked results on three sets of core exercises from weeks 2 9 will be chosen at random; the highest of these three will count as your class programming test ; worth 5% of total marks. But the real reason to do them is to learn to program! Final Exam Programming exam (the other 95% of marks), scheduled as part of normal exam diet. 3 hours allowed, open book exam. Mock programming exam scheduled for Week 11.

Why Java? Java: Procedural and Object-Oriented Why another programming language as well as Haskell? Why Java? Object Oriented Smalltalk Procedural C Platform independence Widely available and widely used Full set of modern abstraction techniques Variety of automatic checks for program mistakes > 6 million developers worldwide http://www.westcanphoto.com/index.php?key=gosling Simula Java C++ Pascal Java popularity (http://www.langpop.com/) Inf1-OP Approach to Java No language is perfect We ll only look at a subset of the Java language Goal: develop general programming skills that are applicable to many languages Data from Yahoo; Craigslist; Powells books; projects at Googlecode, Freshmeat, Ohloh; Del.icio.us bookmarks

Learning Outcomes for this week A First Example 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. 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 convention, class names are capitalized and in CamelCase. 3. Each class goes into a file of its own. 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. NB % is a terminal prompt; we will also use : for this in lab exercises.

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!"); We need a main() method to actually get our program started. All our other code is invoked from inside main(). The void modifier means that 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. Compiling System.out.println("Hello World!"); this is a statement to be executed. Statements must be terminated with a semi-colon (;). The argument is the string to be printed. Strings must be demarcated by double quotes (not single quotes, or doubled single 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.

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. 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); The sum of 6 and 2 equals 8 The quotient of 6 and 2 equals 3 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

String Concatenation, 2 Assignment: Basic Definitions String Concatenation public class Concat { Variable: A name that refers to a value Assignment Statement: Associates a value with a variable declaration statement public static void main(string[] args) { System.out.println("Is that you, 00" + 7 + "?"); Output Is that you, 007? variable name assignment statement int a, b; a = 1234 ; b = 99; int a, b; a = 1234; b = 99; literal int c = a + b; Hello World with Added Variables Storing a String in a variable public class HelloWorld { public static void main ( String [] args ) { String msg = "Hello World!"; System.out.println( msg ); Built-in Data Types combined declaration and assignment statement Important: = is the operator in an imperative statement, not a logical typeassertion. value set literal values operations char characters A $ String sequences of characters int integers 17 1234 double floating-point numbers compare "Hello, World!" "Java is fun" concatenate 3.1415 6.022e23 add, subtract, multiply, divide add, subtract, multiply, divide boolean truth values true false and, or, not

Integer operations Command-line Arguments 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 Unix commands % mkdir MyJavaCode Using Java to carry out commands % java Add 3 6 9 3 and 6 are command-line arguments for the program Add Command-line Arguments Command-line Arguments 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); int a = Integer.parseInt(args[0]); Missing an argument % java Add 3 java.lang.arrayindexoutofboundsexception: 1 This a run-time error we didn t provide anything as a value for args[1]: int b = Integer.parseInt(args[1]); 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.

Floating-Point Numbers Floating-Point Operations The default floating-point type in Java is double. 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 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.

Recap: Learning Outcomes for this week This Week s Reading 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. Sedgewick & Wayne Sections 1.1, 1.2 (pp. 3 38) Eck http://math.hws.edu/javanotes/c2/s1.html http://math.hws.edu/javanotes/c2/s2.html or Labs today In Computer Lab West, AT 5 Scheduled lab starts at 15.10 Lab exercise sheet available from OOP web page.