CSC204 – Programming I Lecture 4 August 28, 2002.

Slides:



Advertisements
Similar presentations
Chapter 1: Computer Systems
Advertisements

Programming with Java. Problem Solving The purpose of writing a program is to solve a problem The general steps in problem solving are: –Understand the.
1 Java Programming Basics SE-1011 Dr. Mark L. Hornick.
 2005 Pearson Education, Inc. All rights reserved Introduction.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
The Java Programming Language
CMT Programming Software Applications
Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,
Outline Java program structure Basic program elements
Chapter 2: Introduction to C++.
Chapter 1 Introduction. © 2004 Pearson Addison-Wesley. All rights reserved1-2 Outline Computer Processing Hardware Components Networks The Java Programming.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
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:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
Java: Chapter 1 Computer Systems Computer Programming II Aug
Prepared by Uzma Hashmi Instructor Information Uzma Hashmi Office: B# 7/ R# address: Group Addresses Post message:
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
© 2006 Pearson Education Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Java: Chapter 1 Computer Systems Computer Programming II.
Java Language and SW Dev’t
System development with Java Lecture 2. Rina Errors A program can have three types of errors: Syntax and semantic errors – called.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
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.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 2: Java Fundamentals
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Chapter 1: Introduction Java Programming Language How the Java Virtual Machine Works (compiling, etc…) Update by: Dan Fleck Coming up: The Java Programming.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Anatomy of a Java Program. AnotherQuote.java 1 /** A basic java program 2 * 3 Nancy Harris, James Madison University 4 V1 6/2010.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Introduction to Java Programming by Laurie Murphy Revised 09/08/2016.
Copyright 2010 by Pearson Education APCS Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
Working with Java.
Lecture 2: Data Types, Variables, Operators, and Expressions
CSE 190D, Winter 2013 Building Java Programs Chapter 1
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Variables and Arithmetic Operators in JavaScript
University of Central Florida COP 3330 Object Oriented Programming
Statements, Comments & Simple Arithmetic
null, true, and false are also reserved.
Introduction to Java Programming
An overview of Java, Data types and variables
Chapter 1: Computer Systems
Units with – James tedder
Units with – James tedder
Anatomy of a Java Program
Focus of the Course Object-Oriented Software Development
CSE 142, Spring 2012 Building Java Programs Chapter 1
Zorah Fung University of Washington, Spring 2015
Chap 2. Identifiers, Keywords, and Types
CSE 142, Winter 2014 Building Java Programs Chapter 1
Computer Programming-1 CSC 111
Agenda Types and identifiers Practice Assignment Keywords in Java
Zorah Fung University of Washington, Winter 2016
Presentation transcript:

CSC204 – Programming I Lecture 4 August 28, 2002

8/28/2002CSC 204 Programming I2 Recap: Compilation & Execution Compiled File (a.out) Source Code (xxx.cpp) text file saved on disk editor compile edit compiler (g++) Instructions in machine language CPU specific saved on disk Memory CPU load Execution Compilation Edition

8/28/2002CSC 204 Programming I3 Recap: Interpretation & Execution Internal file Source Code (xxx.bas) text file editor interpret edit interpreter Instructions in machine language CPU specific CPU Compilation & Execution Memory Edition

8/28/2002CSC 204 Programming I4 Bytecode program (xxx.class) Source Code (xxx.java) text file saved on disk editor compile edit Compiler (javac) Instructions in bytecode saved on disk CPU load Execution Compilation Recap: Java’s Approach Memory JVM interpret Edition

8/28/2002CSC 204 Programming I5 A Simple Java Program  The following program displays the message Java rules! on the screen. // Saved in a file namedJavaRules.java // Displays the message "Java rules!" public class JavaRules { public static void main(String[] args) { System.out.println("Java rules!"); }

8/28/2002CSC 204 Programming I6 Program Layout  The JavaRules program raises a couple of issues: Why do we put comments into programs, and what are the rules for doing so? How should we “lay out” a program? Does it matter where we put spaces and blank lines? Where should the curly braces go?

8/28/2002CSC 204 Programming I7 Comments  Comments are an important part of every program.  They provide information that’s useful for anyone who will need to read the program in the future.

8/28/2002CSC 204 Programming I8 Typical Uses Of Comments  To document who wrote the program, when it was written, what changes have been made to it, and so on.  To describe the behavior or purpose of a particular part of the program, such as a variable or method.  To describe how a particular task was accomplished, which algorithms were used, or what tricks were employed to get the program to work.

8/28/2002CSC 204 Programming I9 Types of Comments  Single-line comments: // Comment style 1  Multiline comments: /* Comment style 2 */  “Doc” comments: /** Comment style 3 */  Doc comments are designed to be extracted by a special program, javadoc.

8/28/2002CSC 204 Programming I10 Problems with Multiline Comments  Forgetting to terminate a multiline comment may cause the compiler to ignore part of a program: System.out.print("My "); /* forgot to close this comment... System.out.print("cat "); System.out.print("has "); /* so it ends here */ System.out.println("fleas");

8/28/2002CSC 204 Programming I11 Single-line Comments  Many programmers prefer // comments to /* … */ comments, for several reasons: Ease of use Safety Program readability Ability to “comment out” portions of a program

8/28/2002CSC 204 Programming I12 Java Programs in General  Building blocks of a Java program: Classes. A class is a collection of related variables and/or methods (usually both). A Java program consists of one or more classes. Methods. A method is a series of statements. Each class may contain any number of methods. Statements. A statement is a single command. Each method may contain any number of statements.

8/28/2002CSC 204 Programming I13 BookApplication chaptersclasses sectionsmethods sentences words & punctuations statements tokens parts packages paragraphsblocks/ segments Natural V.S. Java Languages

8/28/2002CSC 204 Programming I14 Tokens  A Java compiler groups the characters in a program into tokens.  The compiler then puts the tokens into larger groups (such as statements, methods, and classes).  Tokens in the JavaRules program: public class JavaRules { public static void main ( String [ ] args ) { System. out. println ( "Java rules!" ) ; } }

8/28/2002CSC 204 Programming I15 Avoiding Problems with Tokens  Always leave at least one space between tokens that would otherwise merge together: publicclassJavaRules {  Don’t put part of a token on one line and the other part on the next line: public class JavaRules {

8/28/2002CSC 204 Programming I16 Indentation  Programmers use indentation to indicate nesting. An increase in the amount of indentation indicates an additional level of nesting.  The JavaRules program consists of a statement nested inside a method nested inside a class:

8/28/2002CSC 204 Programming I17 How Much Indentation?  Common amounts of indentation: 2 spaces: the bare minimum 3 spaces: the optimal amount 4 spaces: what many programmers use 8 spaces: probably too much  The JavaRules program with an indentation of four spaces: public class JavaRules { public static void main(String[] args) { System.out.println("Java rules!"); }

8/28/2002CSC 204 Programming I18 Brace Placement  Brace placement is another important issue.  One technique is to put each left curly brace at the end of a line. The matching right curly brace is lined up with the first character on that line: public class JavaRules { public static void main(String[] args) { System.out.println("Java rules!"); }

8/28/2002CSC 204 Programming I19 Brace Placement  Some programmers prefer to put left curly braces on separate lines: This makes it easier to verify that left and right braces match up properly. However, program files become longer because of the additional lines. public class JavaRules { public static void main(String[] args) { System.out.println("Java rules!"); }

8/28/2002CSC 204 Programming I20 Brace Placement  To avoid extra lines, the line containing the left curly brace can be combined with the following line: public class JavaRules { public static void main(String[] args) { System.out.println("Java rules!"); }  In a commercial environment, issues such as indentation and brace placement are often resolved by a “coding standard”

8/28/2002CSC 204 Programming I21 Using Variables  In Java, every variable must be declared before it can be used.  Declaring a variable means informing the compiler of the variable’s name and its properties, including its type.  int is one of Java’s types. Variables of type int can store integers (whole numbers).

8/28/2002CSC 204 Programming I22 Declaring Variables  Form of a variable declaration: The type of the variable The name of the variable A semicolon  Example: int i; // Declares i to be an int variable  Several variables can be declared at a time: int i, j, k;  It’s often better to declare variables individually.

8/28/2002CSC 204 Programming I23 Initializing Variables  A variable is given a value by using =, the assignment operator: i = 0;  Initializing a variable means to assign a value to the variable for the first time.  Variables always need to be initialized before the first time their value is used.  The Java compiler checks that variables declared in methods are initialized prior to their first use.

8/28/2002CSC 204 Programming I24 Initializers  Variables can be initialized at the time they’re declared: int i = 0; 0 is said to be the initializer for i.  If several variables are declared at the same time, each variable can have its own initializer: int i = 0, j, k = 1;

8/28/2002CSC 204 Programming I25 Changing the Value of a Variable  The assignment operator can be used both to initialize a variable and to change the value of the variable later in the program: i = 1; // Value of i is now 1 … i = 2; // Value of i is now 2

8/28/2002CSC 204 Programming I26 Program: Printing a Lottery Number Lottery.java // Displays the winning lottery number public class Lottery { public static void main(String[] args) { int winningNumber = 973; System.out.print("The winning number "); System.out.print("in today's lottery is "); System.out.println(winningNumber); }

8/28/2002CSC 204 Programming I27 Types  A partial list of Java types: int — An integer double — A floating-point number boolean — Either true or false char — A character  Declarations of double, boolean, and char variables: double x, y; boolean b; char ch;

8/28/2002CSC 204 Programming I28 Literals  A literal is a token that represents a particular number or other value.  Examples of int literals:  Examples of double literals: e1 4.8e+1.48e2 480e-1  The only boolean literals are true and false.  char literals are enclosed within single quotes: 'a' 'z' 'A' 'Z' '0' '9' '%' '.' ' '

8/28/2002CSC 204 Programming I29 Using Literals as Initializers  Literals are often used as initializers: double x = 0.0, y = 1.0; boolean b = true; char ch = 'f';

8/28/2002CSC 204 Programming I30 Conventions  A rule that we agree to follow, even though it’s not required by the language, is said to be a convention.  A common Java convention is beginning a class name with an uppercase letter: Color FontMetrics String  Names of variables and methods, by convention, never start with an uppercase letter.

8/28/2002CSC 204 Programming I31 Keywords  The following keywords can’t be used as identifiers: abstract double int super boolean else interface switch break extends long synchronized byte final native this case finally new throw catch float package throws char for private transient class goto protected try const if public void continue implements return volatile default import short while do instanceof static  null, true, and false are also reserved.