1 Building Java Programs Chapter 1: Introduction to Java Programming These lecture notes are copyright (C) Marty Stepp and Stuart Reges, 2006. They may.

Slides:



Advertisements
Similar presentations
Building Java Programs Chapter 1 Introduction to Java Programming.
Advertisements

1 Building Java Programs Introduction to Java Programming Dept. of Computer Science - SSBN Vishnuvardhan.M.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 1: Introduction to Java Programming.
Building Java Programs Chapter 1 Introduction to Java Programming.
Portions Copyright 2008 by Pearson Education CSE 142 Lecture 1 Course Introduction; Basic Java.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Copyright 2008 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading: self-check: #1-14.
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()
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 1: Introduction to Java Programming.
 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.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 1: Introduction to Java Programming.
1 Building Java Programs Chapter 1: Introduction to Java Programming These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may.
Copyright 2013 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
Copyright 2008 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading: self-check: #1-14.
Introducing Java.
Topic 2 Java Basics “When a programming language is created that allows programmers to program in simple English, it will be discovered that programmers.
CS 112 Introduction to Programming Lecture #2: Java Program Structure Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Introduction to Computers and Java Chapter 1.3. A Sip of Java: Outline History of the Java Language Applets A First Java Program Compiling a Java Program.
Building Java Programs Chapter 1 Introduction to Java Programming Copyright (c) Pearson All rights reserved.
CSCI 161: Introduction to Programming 1
Chapter 1: Introduction to Programs, and Java 1. Objectives To review programs (§ ). To understand the relationship between Java and the World Wide.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Java Programming, Second Edition Chapter One Creating Your First Java Program.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Copyright 2009 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading: self-check: #1-14.
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
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.
1 CSE 142 Lecture Notes Introduction These lecture notes are copyright (C) Marty Stepp May not be rehosted, copied, sold, or modified without Marty.
Building Java Programs Chapter 1 Introduction to Java Programming Copyright (c) Pearson All rights reserved.
Building Java Programs Chapter 1 Introduction to Java Programming.
Object- Oriented Programming (CS243)
1 WELCOME TO CIS 1068! Instructor: Alexander Yates.
Building Java Programs Chapter 1 Introduction to Java Programming Copyright (c) Pearson All rights reserved.
Copyright 2010 by Pearson Education CSE 142, Fall 2011 Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
Advanced Programming CS206 Introduction 1. Programming Language Code A programming language is a language that uses specially defined words, grammar,
Copyright 2010 by Pearson Education CSE 142, Spring 2012 Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading: 1.1 -
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Welcome to CSE 142! Zorah Fung University of Washington, Summer Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs.
Copyright 2010 by Pearson Education APCS Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
Building Java Programs Chapter 1 Introduction to Java Programming Copyright (c) Pearson All rights reserved.
CSc 127a/110, Autumn 2016 Lecture 1: Introduction; Basic Python Programs.
Zorah Fung University of Washington, Winter 2016
Chapter 1 – Introduction
Lecture 1: Introduction; Basic Python Programs
Welcome to CSE 142! Whitaker Brand and Benson Limketkai
CSCI 161 – Introduction to Programming I William Killian
CSE 190D, Winter 2013 Building Java Programs Chapter 1
Advanced Programming CS206
Java programming lecture one
Benson Limketkai and Marty Stepp University of Washington, Spring 2010
Chapter 1 Introduction to Computers, Programs, and Java
Benson Limketkai University of Washington, Spring 2011
Building Java Programs
Whitaker Brand University of Washington, Winter 2018
Brett Wortzman University of Washington, Summer 2011
Advanced Programming CS206
Topic 2 Java Basics “When a programming language is created that allows programmers to program in simple English, it will be discovered that programmers.
Building Java Programs
Building Java Programs
Welcome to CSE 142!.
CSE 142, Summer 2012 Building Java Programs Chapter 1
Building Java Programs Chapter 1
CSE 142, Spring 2012 Building Java Programs Chapter 1
Lecture 1: Introduction; Basic Python Programs
Zorah Fung University of Washington, Spring 2015
CSE 142, Winter 2014 Building Java Programs Chapter 1
Consult America Technology Consulting Services
Zorah Fung University of Washington, Winter 2016
Presentation transcript:

1 Building Java Programs Chapter 1: Introduction to Java Programming These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or modified without expressed permission from the authors. All rights reserved.

2 Chapter outline Programs and programming languages Basic Java programs output with println statements syntax and errors Structured algorithms with static methods Identifiers, keywords, and comments

3 Computer programs program: A set of instructions that are to be carried out by a computer. program execution: The act of carrying out the instructions contained in a program. programming language: A systematic set of rules used to describe computations, generally in a format that is editable by humans. This textbook teaches programming in a language named Java.

4 A partial history of programming languages: --> Some of the most influential: FORTRAN science / engineering COBOL business data LISP logic and AI BASIC a simple language Languages

5 Some modern languages procedural languages: programs are a series of commands C (1972): low-level operating systems and device drivers Pascal (1970): designed for education functional programming: functions map inputs to outputs Lisp (1958) / Scheme (1975), ML (1973), Haskell (1990) object-oriented languages: programs use interacting "objects" Smalltalk (1980): first major object-oriented language C++ (1985): "object-oriented" additions to C; successful in industrial programming (Windows is built in C++) Java (1995): Sun Microsystems' language designed for embedded systems, web applications, servers Runs on many platforms (Windows, Mac, Linux, cell phones...) The language taught in this textbook

6 Basic Java programs with println statements suggested reading:

7 A basic Java program public class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); } code or source code: The sequence of instructions in a particular program. The code in this program instructs the computer to display a message of Hello, world! on the screen. output: The messages printed to the user by a program. console: The text box onto which output is printed. Some editors pop up the console as an external "DOS" window, and others contain their own console window.

8 Compiling, running a program compiler: A program that translates a computer program written in one language into an equivalent program in another language. The Java Development Kit includes a Java compiler that can be run from editors such as DrJava. The Java compiler converts your source code into a special format named byte code that can be executed on many different kinds of computers. Java runtime: A program that executes Java bytecodes. Installing the Java Development Kit installs a Java runtime on your computer. source code (Hello.java) compile byte code (Hello.class) execute output

9 Another Java program public class Hello2 { public static void main(String[] args) { System.out.println("Hello, world!"); System.out.println(); System.out.println("This program produces"); System.out.println("four lines of output"); } The code in this program instructs the computer to print four messages on the screen.

10 Structure of Java programs public class { public static void main(String[] args) { ;... ; } Every executable Java program consists of a class... that contains a method named main... that contains the statements (commands) to be executed The previous program is a class named Hello, whose main method executes one statement named System.out.println

11 Java terminology class: A module that can contain executable code. Every program you write will be a class. statement: An executable command to the computer. method: A named sequence of statements that can be executed together to perform a particular action. A special method named main signifies the code that should be executed when your program runs. Your program can have other methods in addition to main. (seen later)

12 Syntax syntax: The set of legal structures and commands that can be used in a particular programming language. some Java syntax: every basic Java statement ends with a semicolon ; The contents of a class or method occur between { and }

13 Syntax and syntax errors syntax error or compiler error: A problem in the structure of a program that causes the compiler to fail. If you type your Java program incorrectly, you may violate Java's syntax and see a syntax error. public class Hello { pooblic static void main(String[] args) { System.owt.println("Hello, world!")_ } H:\cse142\Hello.java:2: expected pooblic static void main(String[] args) { ^ H:\cse142\Hello.java:5: ';' expected } ^ 2 errors compiler output:

14 Fixing syntax errors Notice how the error messages are cryptic and do not always help us understand what is wrong: H:\summer\Hello.java:2: expected pooblic static void main(String[] args) { ^ We'd have preferred a friendly message such as, "You misspelled 'public' " The compiler does tell us the line number on which it found the error, which helps us find the place to fix the code. The line number shown is a good hint, but is not always the true source of the problem. Java has a fairly rigid syntax. Several languages are friendlier than Java, but we must pay this price in order to learn an industrial-strength language that is used in real jobs.

15 System.out.println Java programs use a statement called System.out.println to instruct the computer to print a line of output on the console pronounced "print-linn"; sometimes called a println statement for short Two ways to use System.out.println : 1. System.out.println(" "); Prints the given message as a line of text on the console. 2. System.out.println(); Prints a blank line on the console.

16 Strings and string literals string: A sequence of text characters that can be printed or manipulated in a program. sometimes also called a string literal strings in Java start and end with quotation mark " characters Examples: "hello" "This is a string" "This, too, is a string. It can be very long!"

17 Details about Strings A string may not span across multiple lines. "This is not a legal String." A string may not contain a " character. (The ' character is okay) "This is not a "legal" String either." "This is 'okay' though." A string can represent certain special characters by preceding them with a backslash \ (this is called an escape sequence). \t tab character \n new line character \" quotation mark character \\ backslash character Example: System.out.println("\\hello\nhow\tare \"you\"?"); Output: \hello howare "you"?

18 Questions What is the output of each of the following println statements? System.out.println("\ta\tb\tc"); System.out.println("\\\\"); System.out.println("'"); System.out.println("\"\"\""); System.out.println("C:\nin\the downward spiral"); Write a println statement to produce the following line of output: / \ // \\ /// \\\

19 Questions What sequence of println statements will generate the following output? This program prints a quote from the Gettysburg Address. "Four score and seven years ago, our 'fore fathers' brought forth on this continent a new nation." What sequence of println statements will generate the following output? A "quoted" String is 'much' better if you learn the rules of "escape sequences." Also, "" represents an empty String. Don't forget to use \" instead of " ! '' is not the same as "