1B1a Programming I Getting Started

Similar documents
COMP1007 Principles of Programming

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

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

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

CS 177 Recitation. Week 1 Intro to Java

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 1

Java Programming Language Mr.Rungrote Phonkam

Fundamentals of Programming. By Budditha Hettige

CS 11 java track: lecture 1

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

Lecture 1: Overview of Java

C++ Spring Break Packet 11 The Java Programming Language

Introduction to Computers and Java

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

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

CHAPTER 1. Introduction to JAVA Programming

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

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

AP Computer Science Unit 1. Writing Programs Using BlueJ

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.

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)

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

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science A: Java Programming

St. Edmund Preparatory High School Brooklyn, NY

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

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

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

Laboratory 1: Eclipse and Karel the Robot

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

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

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

Introduction to Java Unit 1. Using BlueJ to Write Programs

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

Chapter 1 Introduction to Computers, Programs, and Java

Basic Programming Language Syntax

IQTIDAR ALI Lecturer IBMS Agriculture University Peshawar

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

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

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

CHAPTER 1 Introduction to Computers and Java

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

Lab # 2. For today s lab:

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

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

Chapter 1 Introduction to Computers, Programs, and Java

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

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

Getting Started (1.8.7) 9/2/2009

Lecture 1: Introduction to Java

CS 231 Data Structures and Algorithms, Fall 2016

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

Programming. Syntax and Semantics

Exercise 6 - Addressing a Message

1007 Imperative Programming Part II

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

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

COMP 110 Project 1 Programming Project Warm-Up Exercise

Life Without NetBeans

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

The Programming Process Summer 2010 Margaret Reid-Miller

COMP519 Practical 5 JavaScript (1)

Lecture 2, September 4

Chapter 2: Programming Concepts

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

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

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

Structured Programming

ALICE: An introduction to progamming

Lecture 1 - Introduction (Class Notes)

CompSci 125 Lecture 02

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 Getting Started

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

Identifiers. Identifiers are the words a programmer uses in a program Some identifiers are already defined. Some are made up by the programmer:

AP Computer Science A

1 Getting started with Processing

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

Eng. Mohammed Alokshiya

CSC System Development with Java Introduction to Java Applets Budditha Hettige

2 Introduction to Java. Introduction to Programming 1 1

Course Outline. Introduction to java

Visit for more.

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

Unit E Step-by-Step: Programming with Python

Lesson 01 Introduction

Chapter Two Bonus Lesson: JavaDoc

Exercises: Instructions and Advice

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

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

CS 112 Introduction to Programming

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

Transcription:

1B1a Programming I Getting Started Agenda Definitions. What is programming? What is Java? Writing your first program. Classes and Objects. 1 2 Reading You should be reading chapters 1 & 2 of the text book. Program 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. 3 4 Programmer Person who writes programs! Responsible for identifying the correct sequence of instructions required and writing them down. What you want to be :-): Application Program 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. 5 6 1

Systems Programming 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. Software Engineering 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. 7 8 Processor Microprocessor, CPU, chip. The computer hardware that executes program instructions (machine code). Intel Pentium TM, Sparc,, Motorola PowerPC Each has its own specific instruction set. Programming Language A textual language used to write a program. The meaning of what you write has to be completely and precisely defined. Java is a programming language. 9 10 Syntax Syntax describes the grammatical rules of a language. Valid words. Punctuation. Sentence construction. Rules of use. Programs must be syntactically correct. Semantics 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. 11 12 2

Syntax v. Semantics This sentence is an elephant. Grammatically correct but no sensible meaning. I never tell the truth. Grammatically correct but logically inconsistent. Nonsense programs... A program can be syntactically correct but not do anything useful or sensible. However, what a program does can always be exactly determined. Err, is that true? 13 14 Determinism v. Non-Determinism 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! But non-deterministic programs can be written with Java. Communication Programming involves a lot of communication. Most communication occurs between people (or with yourself!) 15 16 Communication skills 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. Readability 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. 17 18 3

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. 19 20 21 A Program! class Welcome Ordinary text written with an editor. Called Source Code. public void sayhello() System.out.println("Hello World"); Obeys syntax rules & semantics of Java. public static void main(string[] args) Welcome welcome = new Welcome(); welcome.sayhello(); 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. 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. Compile the file with the Java compiler. Run the program and see what happens. Fix the bugs! Fix semantic errors Fix syntax errors 23 24 4

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

Remember 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. Being even more efficient Typing!!<return> repeats the previous command. This means you can type: javac Hello.java once. And repeat it using!! Works for any command. 31 32 Being yet more efficient 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 Questions? 33 34 Drawing Shapes and Pictures 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. An Example Dawing Made up of lines and rectangles. 35 36 6

Let s see the program OK, another demo How do 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. // For example, draw a diagonal line. g.drawline(0,0,300,300); Make changes here. See the notes. 37 38 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. 39 40 Changes Change the name of the class. See the line that says: class Drawing extends JFrame Here s the name. Change it! Don t forget: save the class to a new.java file. Yet more to change? Yes. Wherever you see the name Drawing in the program, replace it with the new name. Drawing drawing = new Drawing( MyDrawing); Change these as well. I wonder if the editor does search and replace? 41 42 7

Drawing something different // 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); g.fillrect(20,20,50,50) ; New lines of code. The order of the lines give the sequence by which the picture is drawn. What else can you draw? 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. 43 44 Drawing a more complicated picture Work out how to draw a complicated picture by using a series of simpler shapes. Questions? Problem decomposition. 45 46 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 47 48 8

What happened? The program created an object. The object was asked to say Hello. Welcome Object Class and 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. sayhello() 49 50 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 51 52 Questions? Huh? These programs seem more complicated than necessary! Why not just a simple statement on its own? print( Hello World ); 53 54 9

Because 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! And 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. 55 56 But! The program could have been written like this: class Welcome public static void main(string[] args) System.out.println("Hello World"); Why not? Well 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! 57 58 Before we finish If the compiler translates to processor instructions, then what is the java interpreter doing? You need to ask: which processor? The Java Virtual Machine (JVM) 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. 59 60 10

Why? A Java program can run on any real processor that can run the JVM. Compile once, run everywhere. So, where did Java come from? Designed by a group at Sun MicroSystems. 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. 61 62 James Gosling and the Duke Mascot Find out more Visit the official Sun Java web site: http://java.sun.com 63 64 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. 65 11