11 ITI 1120 Lab # 2 Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

IT151: Introduction to Programming
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Lecture 2 Introduction to C Programming
JAVA BASICS SYNTAX, ERRORS, AND DEBUGGING. GCOC – A.P. Computer Science A College Board Computer Science A Topics Covered Program Design - Read and understand.
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Chapter 1: Introduction
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
How to Create a Java program CS115 Fall George Koutsogiannakis.
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
01 Introduction1June Introduction CE : Fundamental Programming Techniques.
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Writing Methods. Create the method Methods, like functions, do something They contain the code that performs the job Methods have two parts.
1. 2 Chapter 1 Introduction to Computers, Programs, and Java.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Copyright 2013 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
Using C Programming Language.  The programs that run on a computer are referred to as software.  You’ll learn key programming methodology that are enhancing.
Introducing Java.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
A First Program Using C#
Chapter 2 How to Compile and Execute a Simple Program.
CS 106 Introduction to Computer Science I 01 / 25 / 2010 Instructor: Michael Eckmann.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
© 2006 Pearson Education 1 Obj: cont 1.3 and 1.4, to become familiar with identifiers and to understand how programming languages work HW: p.51 #1.8 –
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 2: Java Fundamentals
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
Comments in Java. When you create a New Project in NetBeans, you'll notice that some text is greyed out, with lots of slashes and asterisks:
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Introduction to programming in the Java programming language.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Anatomy of a Java Program. AnotherQuote.java 1 /** A basic java program 2 * 3 Nancy Harris, James Madison University 4 V1 6/2010.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
CSC 110 – Intro to Computing - Programming
CS 177 Recitation Week 1 – Intro to Java. Questions?
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
CHAPTER 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Copyright 2010 by Pearson Education APCS Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
CSC201: Computer Programming
GC101 Introduction to computer and program
Chapter 3 GC 101 Java Fundamentals.
Completing the Problem-Solving Process
Chapter 2, Part I Introduction to C Programming
Data types and variables
Chapter 1: Computer Systems
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Computer Programming-1 CSC 111
How to Run a Java Program
Presentation transcript:

11 ITI 1120 Lab # 2 Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot

22 For today’s lab: Go the course webpage Follow the links to the lab notes for Lab 2. Save all the java programs you find there in the C:\work directory. We'll be using them later.

33 Introduction to Java Editing, compiling, and running Java programs Syntax, comments To get to the point that you can write Java programs: –Declare variables –Read data from the keyboard –Numbers, Operators, Calculations –Print the results. Translate a simple algorithm into Java

4 Translation in Java Java source code Machine code Java bytecode Java Interpreter (Java Virtual Machine) Bytecode compiler Java compiler Do you recall this image you have seen in class?

5 Basic Program Development errors Edit and save program Compile program Execute program and evaluate results These are the steps you will be following today.

66 Editing a Java program An editor is used to create and modify a program. The program is just an ordinary text file, so you can use any text editor –You don't need to use a special one, although editors specifically for programs can be much more convenient. This text file with the program in it is called the source file for the program Java source files have the extension.java

77 Compiling The source file - the program in textual format - cannot be directly executed. It needs to be translated into an executable form. A compiler also checks for certain types of errors, called syntax errors. If it detects an error you need to figure out what caused the problem and go back and edit the the source file to fix the problem.

88 Syntax Errors A “grammatical” error –Example: whle instead of while Found by compiler (“compiler-time error”) –Automatically found –Usually the easiest to fix Cannot run code until all syntax errors are fixed in all open files. Be careful when reading messages from the compiler: –Problems are described from a grammatical point of view. –Sometimes, the message is a “best guess” of what might be wrong, and may be misleading.

99 Run If the compiler detects no problems, it creates a byte code file having a.class extension. It does not automatically run your program - that is done by you manually. Since the.class file stays around you can run it whenever you like, you don't have to recompile every time. –… But of course if you change the source code you must manually recompile or you'll be using the old executable. Once the program is running it is important to test it to make sure it produces correct answers. – You'll usually run it may times on various inputs to make sure it works fine under all circumstances. If you find it is producing the wrong answer, you have to debug the program –this will involve editing the program (the source file), recompiling, rerunning.

10 Exercise 1 - Overview of a Java Program Start Dr. Java Open (“load”) the file Prog1.java –You should already have saved this file on your hard drive).

11 Compiling the Java Program To compile Prog1.java with Dr. Java, click on the button “Compile”. This will compile all files listed in the left window. Compiler messages appear under the tab “Compiler Output” at the bottom of the window. –Shows if the compilation was successful. –Otherwise the compiler produces error messages. In the directory where Prog1.java is stored –The compiler will have produced the file Prog1.class

12 Running a Program Now that the program is compiled, you can run it Click on “Run” (or type F2) This will execute the method main of the program you edited. In the Interactions zone (see tab at the bottom), you will see the program output –You can also click on the tab “Console” to see only program output with any messages generated by Dr. Java

13 General Organization Source file contains a CLASS. –We will always have one class per file. A CLASS contains one or more METHODS. A METHOD contains declarations, statements, and control structures. –This is where you will implement your algorithms. A PROGRAM must include a class that has a method called main –We shall see in the second half of the course how many classes can make up a program. COMMENTS can be placed anywhere.

14 Comments Comments are for people reading your program. –In them you explain your program in English. –The compiler completely ignores them. In Java –Comments may be placed anywhere you like. –On any line, everything after // (to the end of the line) is a comment. –Anything in between /* and */ is a comment (including multiple lines) See Prog1.java as for examples

15 Single line comment –Everything from // to the end of the line is the comment some code // This is a comment more code General comment –Everything from /* to the next occurrence of */ is a comment –Can be part of a line code /* comment */ more code –Can be several lines code /* start of comment more comment end of comment */ more code Types of comments

16 Class Definition Has these parts: –Keyword class A keyword is a word that has special meaning in the Java language. Dr. Java highlights these reserved words by colouring them blue. In this case the keyword class tells the compiler that you are beginning the definition of a class. –A name for the class Prog1 is the name of a class –Methodes An opening { <-- this symbol is called a brace or curly bracket One or more method definitions A closing } Braces are used to enclose lines of code together to form an instruction block.

17 Identifiers The class has the name Prog1 In programming, the official term for the name is an “identifier”. –Identifiers are used to name many things: classes, methods, variables, etc. There are rules for identifiers in Java –Only use the digits 0-9, letters A-Z a-z, the characters $ and _ –Identifiers cannot start with $ and it is not recommended to start them with _ (underscore)

18 main method definition The definition of main starts with a line that we will never change for this course: public static void main(String[] args) main is the name of this method; it is a special identifier, like a keyword. –The purpose of the main method is to tell Java, “when you run the program, start here.” After this opening line comes: –An opening { –The "body" of the method - in the example program main's body consists of two statements –A closing } Next week in the lab session, we shall add another method that will be called by main.

19 The println and print statements The simplest forms: System.out.println( "some string" ); Go to the next line –System.out.print ( "some string" ); Stays on the same line, any new printed character or typed in character will follow the message A STRING is a collection of characters, contained in double quotes to mark the start and the end of the string. Whatever is between the double-quotes is written ("printed") on the console (the screen). After the string is printed, the cursor marking the location of where the computer will print next is moved to the start of a new line. Note: the quotes are not part of the string.

20 The “import” Statement Indicates to the compiler which libraries (or set of predefined classes/methods) the program uses (or may potentially use). In Prog1.java, we are interested in all classes (*) and input/output methods (io). For example, this import includes System.out.println –The current version of Java does not require this particular import; it is done automatically There can be many “ import ”, usually placed at the start of the file (and always before any of its classes are used).

21 Syntax - General Features Java is "free format". –In general, you can have blank lines and space things the way you like. –However, there are some restrictions for how to space and place things. You cannot put spaces (or line breaks) in the middle of names or keywords. –There are conventions to make programs more readable and understandable by many people (e.g. indentation). Java is case-sensitive. –class and Class are two different words keywords never use capitals –This is a common source of bugs Java is VERY PARTICULAR about punctuation. –If you miss a semicolon or have mismatched brackets or braces or double-quotes, or if you use a single quote (or two) instead of a double quote, you'll get a syntax error.

22 Some general rules are: All brackets must match with a bracket of the same type, opposite direction (open and close pairs) –The open-close pairs must fit (“nest”) inside each other You can’t do this: ( [ ) ] Double quotes must match up ON THE SAME LINE All statements end with a ; (semicolon) Braces are normally NOT followed by a semicolon (there are some exceptions in special cases). The class name and the file name should be the same (except of course for the.java extension on the file name).

23 Exercise 2 – Prog2 Try the same thing with Prog2.java What happened?

24 Prog2 You will get error messages because there is one mistake in Prog2.java (the quote to end the string in the println statement is missing). This is what syntax error messages look like –Where does it say what line the error occurred on? –Why does the compiler think there are two errors? Hint: Notice that Dr. Java colours strings red. Note carefully what is coloured red in this program. Fix the error, and re-compile –When you fix the error, notice the difference in what is coloured red.

25 Exercise 3 – Prog3 This program illustrates one of the most common errors. Try it!

26 Exercise 4 – Prog4 This program shows the difference between println and print. Try it!

27 Exercise 5 – Prog5 Try to compile and run this program. What happened?

28 Exercise 6 - Prog6 - Correcting Syntax Errors Correct all errors in Prog6.java so that it will produce the following output: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! This program used to have lots of problems, but if it prints all the lines on the screen, you fixed them all. *** Hurray! *** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

29 Exercise 7 – Working with a Circle The algorithm on the following slide defines a simple program that –Gets a circle radius from the user, –Calculates the area. –Displays the result. –The constant value of 3.14 is used for π. translate the algorithm to a Java method The main method in Java calls this algorithm. Compile and test the program. 29

30 Exercise 7 (algorithm CalculateArea) 30 GIVENS: Radius (the radius of the circle) RESULTS: Area (the area) INTERMEDIATES: Pi (a constant, 3.14) HEADER: Area<- CalculateArea(Radius) BODY: Pi  3.14 Area  Pi x Radius x Radius

31 Exercise 7 (main algorithm) 31 GIVENS: (none) RESULTS: (none) INTERMEDIATES: Radius (the radius of the circle) Area (the area) HEADER: main() BODY: Print(“Please enter a value for radius: “) Radius  ReadReal() Area  CalculateArea( Radius) PrintLine(“The area of the circle is “, Area)

32 Trace the algorithm CalculateArea for Radius = 7

33 Implement the Java program