COMP1007 Principles of Programming

Similar documents
1B1a Programming I Getting Started

CPS109 Lab 1. i. To become familiar with the Ryerson Computer Science laboratory environment.

CS 177 Recitation. Week 1 Intro to Java

Tutorial 1 CSC 201. Java Programming Concepts عؾادئماظربجمةمبادؿكدامماجلاصا

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

Fundamentals of Programming. By Budditha Hettige

CS 11 java track: lecture 1

C++ Spring Break Packet 11 The Java Programming Language

Chapter 1 Introduction to Computers, Programs, and Java. What is a Computer? A Bit of History

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 1

Lecture 1: Overview of Java

Java Programming Language Mr.Rungrote Phonkam

Introduction to Computers and Java

Java Programming. Zheng-Liang Lu. Java 308 Spring Department of Computer Science & Information Engineering National Taiwan University

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

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

Programming. Syntax and Semantics

CHAPTER 1. Introduction to JAVA Programming

From High Level to Machine Code. Compilation Overview. Computer Programs

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science A: Java Programming

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

Introduction to Computers and Java. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

IQTIDAR ALI Lecturer IBMS Agriculture University Peshawar

Introduction to Computers and Java

Introduction to Computers and Java. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Introduction to Computers and Java

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

Chapter 1. Introduction to Computers and Java Objects. Background information. » important regardless of programming language. Introduction to Java

MEAP Edition Manning Early Access Program Get Programming with Java Version 1

The Programming Process Summer 2010 Margaret Reid-Miller

AP Computer Science Unit 1. Writing Programs Using BlueJ

Chapter 1 Introduction to Computers, Programs, and Java

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

COMP519 Practical 5 JavaScript (1)

Basic Programming Language Syntax

COMP 110 Project 1 Programming Project Warm-Up Exercise

Java Programming. Zheng-Liang Lu. Java 301 Summer Department of Computer Science & Information Engineering National Taiwan University

CS 231 Data Structures and Algorithms, Fall 2016

II. Compiling and launching from Command-Line, IDE A simple JAVA program

Chapter 1: Introduction to Computers and Java

8/23/2014. Chapter Topics. Introduction. Java History. Why Program? Java Applications and Applets. Chapter 1: Introduction to Computers and Java

Lab # 2. For today s lab:

St. Edmund Preparatory High School Brooklyn, NY

Chapter 1 Introduction to Computers, Programs, and Java

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

9/11/08 (c) 2008 Matthew J. Rutherford Class (c) 2008 Matthew J. Rutherford Class

Laboratory 1: Eclipse and Karel the Robot

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

GETTING STARTED. The longest journey begins with a single step. In this chapter, you will learn about: Compiling and Running a Java Program Page 2

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

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

Class List. Java Must Have. Class Goals. Class Goals. Schedule

1007 Imperative Programming Part II

Introduction to Java Unit 1. Using BlueJ to Write Programs

Chapter. Focus of the Course. Object-Oriented Software Development. program design, implementation, and testing

Object Oriented Concepts and Programming (CSC244) By Dr. Tabbasum Naz

Introduction to OOP Using Java Pearson Education, Inc. All rights reserved.

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

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

Java is a high-level programming language originally developed by Sun Microsystems and released in Java runs on a variety of

Lecture 1 - Introduction (Class Notes)

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

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

Exercises: Instructions and Advice

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

Outline. Introduction to Java. What Is Java? History. Java 2 Platform. Java 2 Platform Standard Edition. Introduction Java 2 Platform

You should now start on Chapter 4. Chapter 4 introduces the following concepts

CompSci 125 Lecture 02

Chapter 1 Getting Started

2 Getting Started. Getting Started (v1.8.6) 3/5/2007

Getting Started (1.8.7) 9/2/2009

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

Lecture 1: Introduction to Java

Lesson 1A - First Java Program HELLO WORLD With DEBUGGING examples. By John B. Owen All rights reserved 2011, revised 2015

AP Computer Science A

Exercise 6 - Addressing a Message

Life Without NetBeans

This lecture will take you through simple and practical approach while learning Java Programming language.

Unit E Step-by-Step: Programming with Python

Introduction to Java. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Chapter 2: Programming Concepts

Lesson 01 Introduction

Structured Programming

ALICE: An introduction to progamming

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Lecture 2, September 4

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

Course Outline. Introduction to java

COMP : Practical 8 ActionScript II: The If statement and Variables

Visit for more.

1. What is Jav a? simple

1. Java is the fastest growing programming language in the world.

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

CHAPTER 1 Introduction to Computers and Java

Introduction to Programming (Java) 2/12

Java Programming. Zheng-Liang Lu. Java 304 Fall Department of Computer Science & Information Engineering National Taiwan University

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

Software and Programming 1

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Transcription:

Agenda COMP1007 Principles of Programming Definitions. What is programming? What is Java? Writing your first program. Classes and Objects. 3 Reading Program You should be reading chapters 1 & 2 of the text book. A sequence of instructions carried out by a computer (or run, or executed). The sequence is typically long and complex. Running a program will result in millions or billions of instructions being executed. The instruction sequence has to be correct or the program will fail. 4 5 Programmer Application Program Person who writes programs! Responsible for identifying the correct sequence of instructions required and writing them down. Who you want to be :-) A program that does something useful for the end user. Word processor, spreadsheet, web browser, etc. A tool to perform tasks to achieve a goal. 6 7

Systems Programming Software Engineering An operating system controls and manages a computer. DOS, Unix, GNU/Linux, Mac OS X, Windows XP, etc. Systems programming is the process of developing operating system software. And supporting tools. The process of developing programs. Involves: Requirements what is the program meant to do? Analysis how should the program behave? Design how is the program structured? Coding writing the program code. Debugging fixing errors. Testing making sure the program works. Deployment putting the program into use. Maintenance keeping the program working. 8 9 Processor Programming Language Microprocessor, CPU, chip. The computer hardware that executes program instructions (machine code). Intel PentiumTM, Sparc, PowerPC Each has its own specific instruction set. A textual language used to write a program. Read/written by programmer. English-like appearance (sort of). The meaning of what you write has to be completely and precisely defined. Java is a programming language. 10 11 Syntax Semantics Syntax describes the grammatical rules of a language. Valid words. Punctuation. Sentence construction. Rules of use. Programs must be syntactically correct. Semantics give the meaning of what you write with a language. A program must be semantically correct to do what you expect. A programming language must precisely define the meaning of every statement that can be written with it. 12 13

Syntax v. Semantics Nonsense programs... This sentence is an elephant. Grammatically correct but no sensible meaning. This sentence is false. Grammatically correct but logically inconsistent. A program can be syntactically correct but not do anything useful or sensible. However, what a program does can always be exactly determined. Is that actually true? 14 15 Determinism v. Non-Determinism Communication What a deterministic program does can always be known. The behaviour of non-deterministic programs can t (easily) be predicted reliably. Your programs should be deterministic! Programming involves a lot of communication. But non-deterministic programs can be written with Java. Most communication occurs between people (or with yourself!) 16 17 Communication skills Readability Speech, writing, drawing, diagrams, etc. To talk about the design of a program with other people is hard and you need to be quite precise. To describe a program to a computer you have to be absolutely precise. Programming languages are for people to describe programs. The text of a program should be written for other people to read. Think about what makes a book, newspaper or website easy to read and understand. 18 19

Questions? Writing a Program We want to get you started as soon as possible! But at first you will have to take a lot of things on trust. We will return to the details later on. 20 21 A Program! class Welcome public void sayhello() System.out.println("Hello World"); public static void main(string[] args) Welcome welcome = new Welcome(); welcome.sayhello(); Ordinary text written with an editor. Called Source Code. Obeys syntax rules & semantics of Java. 22 Compilation A computer cannot directly understand or run source code! A translation from source code to processor instructions has to be performed. This is known as compilation. 23 The Compiler Fortunately, a tool called a Compiler can do the text to processor instruction translation. The compiler knows and checks all syntax rules but only some of the semantics. The compiler is itself a program. Writing a Java Program Use an editor to type in or edit the program source code. Save the code to a file. Fix syntax Compile the file with the Java compiler. errors Run the program and see what happens. Remove the bugs! Fix semantic errors 24 25

Hello A comment Which editor? // Say hello! class Welcome public void sayhello() System.out.println("Hello World"); public static void main(string[] args) Welcome welcome = new Welcome(); welcome.sayhello(); Our class is called Welcome. Save in a file called Welcome.java There are several choices: JEdit BlueJ Emacs (or XEmacs) + command line BlueJ or JEdit are preferred but Emacs is also very powerful. Experiment. Take the time to learn to use your editor effectively. 26 27 Java Compiler Running a Java Program The Java compiler is called javac. To compile use: javac Welcome.java Java source code file names must always end with.java To run a Java program use java. java Welcome This is the Java interpreter which actually runs the program. Name the program you want to run. This will create a file called Welcome.class Hello World appears in xterm. 28 29 Let s Try This Being efficient Cue the demos... JEdit + command line Use one or more xterm windows for typing commands. Don t close the editor every time you edit a file, simply save the file. 30 31

Remember Being even more efficient Use JEdit to type and edit source code. Compile program via xterm window, or Use the JEdit Console. Run the program via xterm window, or Use the JEdit Console. Typing!!<return> repeats the previous command. This means you can type: javac Hello.java once. And repeat it using!! Works for any command. 32 33 Being yet more efficient Questions? The command history will display a numbered list of the past commands you have used.!5 will repeat command 5,!10 command 10 and so on.!ji will repeat the last command starting with the letters ji 34 35 Drawing Shapes and Pictures An Example Dawing A number of exercise questions ask you to write programs that draw pictures. You are given the source code of a complete program to copy and edit. Another class. Made up of lines and rectangles. 36 37

Let s see the program How do you draw? OK, another demo // This part of the program does the actual drawing. public void dodrawing(graphics g) // You add/change the statements here to draw // the picture you want. // For example, draw a diagonal line. g.drawline(0,0,300,300); Make changes here. See the notes. 38 39 A Drawing Object? What does g.drawline() mean? What is g? g is a reference to an object. The object knows how to draw. You tell it what to draw. Your own drawings Select a new class name. The name of your drawing. Copy the template file and save as <yourname>.java Make the changes described next. Compile, run. 40 41 Changes Yet more to change? Change the name of the class. See the line that says: class Drawing extends JFrame Yes. Wherever you see the name Drawing in the program, replace it with the new name. Drawing drawing = new Drawing( MyDrawing ); Here s the name. Change it! Change these as well. Don t forget: save the class to a new.java file. I wonder if the editor does search and replace? 42 43

Drawing something different What else can you draw? // This part of the program does the actual drawing. public void dodrawing(graphics g) // You add/change the statements here to draw // the picture you want. g.drawrect(150,150,50,50); New lines of code. g.fillrect(20,20,50,50) ; The order of the lines give the sequence by which the picture is drawn. g.drawarc(x, y, width, height, startangle, arcangle); g.drawline(x1,y1,x2,y2); g.drawoval(x,y,width,height); g.drawrect(x,y,width,height); g.drawstring(text, x,y); A String is a line of text (sequence of characters). 44 45 Drawing a more complicated picture Work out how to draw a complicated picture by using a series of simpler shapes. Questions? Problem decomposition. 46 47 A Closer Look class Welcome public void sayhello() System.out.println("Hello World"); public static void main(string[] args) Welcome welcome = new Welcome(); welcome.sayhello(); Infrastructure Statement we want to execute. Infrastructure Infrastructure Necessary to support the code we actually want to run. Its full purpose will become clear as the course proceeds. For now cut and paste job. But will elaborate a bit 48 49

What happened? Class and Object? The program created an object. The object was asked to say Hello. sayhello() Welcome Object Our program actually consisted of a single class declaration. The class is a template that describes what a Welcome object is and does. Running the program creates the object and asks it to sayhello. 50 51 Objects Have responsibilities to carry out actions (e.g., say Hello) to know things (e.g., what to actually say) Can collaborate with other objects to perform more complex tasks. Our Welcome object actually uses another object, System.out, which has the responsibility of directing text to the screen. Objects with BlueJ BlueJ is another programming tool you will be using. It allows you to directly manipulate objects. Here is the demo 52 53 Questions? Huh? These programs seem more complicated than necessary! Why not just a simple statement on its own? print( Hello World ); 54 55

Because And The programming language works this way. Any non-trivial program needs structure to have any chance of being manageable. Classes and objects provide that structure. You need to learn to do things properly from the start! Classes and objects provide the components, or building blocks, to construct a program from. Program statements provide the detail describing how objects perform operations. Like saying Hello. Think about levels of detail, or abstraction. 56 57 But! Well The program could have been written like this: class Welcome public static void main(string[] args) System.out.println("Hello World"); Why not? We want to emphasise objects from the start. The simpler program form is only of any use for very small examples. Chap. 2 of the text book uses the simple form but you should use objects from the start! 58 59 Before we finish The Java Virtual Machine (JVM) If the compiler translates to processor instructions, then what is the java interpreter doing? You need to ask: which processor? Java programs are compiled to bytecodes for a virtual processor. The command java runs the JVM which simulates the virtual processor. So your program is run by the JVM that is, in turn, run by the real processor. 60 61

Why? So, where did Java come from? A Java program can run on any real processor that can run the JVM. Compile once, run everywhere. Designed by a group at Sun Microsystems Inc. Originally called Oak - a language for programming consumer devices (Talkie Toaster!). Renamed to Java and moved to the web. Developed into a full scale application programming language. Continues to evolve. 62 63 James Gosling and the Duke Mascot Find out more Visit the official Sun Java web site: java.sun.com or www.java.net 64 65 Summary Defined some basic terms. Introduced the ideas of a programming language and compilation. Seen how to write, compile and run small programs. First look at classes and objects. 66