1. Introduction Based on Java Software Development, 5 th Ed. By Lewis &Loftus.

Slides:



Advertisements
Similar presentations
Chapter 1: Computer Systems
Advertisements

COMP425M Introduction to Programming
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.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
1 Kursusform  13 uger med: Undervisning i klassen 1,5-2 timer Opgave ”regning” i databar (løsninger på hjemmeside) En midtvejsopgave der afleveres og.
The Java Programming Language
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 1 “ Introduction to Java and OOP”
Outline Java program structure Basic program elements
Introduction to Java CS 331. Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java.
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 1 Introduction.
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
Chapter 1 Introduction.
Prepared by Uzma Hashmi Instructor Information Uzma Hashmi Office: B# 7/ R# address: Group Addresses Post message:
HOW COMPUTERS MANIPULATE DATA Chapter 1 Coming up: Analog vs. Digital.
Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley John Lewis, Peter DePasquale, and Joseph Chase Chapter 1: Introduction.
© 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
© 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 –
1 Introduction zNow we can begin to examine the basic ideas behind writing programs zLecture 1 focuses on: ythe structure of a Java application ybasic.
1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.
Chapter 1 Introduction 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Introduction. Objectives An overview of object-oriented concepts. Programming and programming languages An introduction to Java 1-2.
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.
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Chapter 1: Introduction Java Programming Language How the Java Virtual Machine Works (compiling, etc…) Update by: Dan Fleck Coming up: The Java Programming.
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.
Introduction to Java Java Translation Program Structure
Lecture 2 Software Concepts Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Introduction to Object-oriented Programming Java API.
1 Problem Solving b The purpose of writing a program is to solve a problem b The general steps in problem solving are: Understand the problemUnderstand.
White Space Spaces, blank lines, and tabs are collectively called white space and are used to separate words and symbols in a program Extra white space.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Transition to Java Programming with Alice and Java First Edition.
Intro to Programming  Chapter 1 Part 2  Problem Solving.
 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.
Java.
Chapter 1 Introduction. 2 Focus of the Course Object-Oriented Software Development problem solving program design, implementation, and testing object-oriented.
WEEK 2 1 Software Concepts -- Introduction Now we can begin to examine the basic ideas behind writing programs Chapter 2 focuses on: history.
© 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.
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.
JAVA MULTIPLE CHOICE QUESTION.
Working with Java.
Lecture 1 Introduction Richard Gesick.
University of Central Florida COP 3330 Object Oriented Programming
1.3 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.
Unit 41 – Programing in Java
null, true, and false are also reserved.
Introduction to Java Programming
Instructor: Alexander Stoytchev
Chapter 1: Computer Systems
Units with – James tedder
Units with – James tedder
Instructor: Alexander Stoytchev
Java Software Solutions Foundations of Program Design 9th Edition
Focus of the Course Object-Oriented Software Development
Instructor: Alexander Stoytchev
Chap 2. Identifiers, Keywords, and Types
Presentation transcript:

1. Introduction Based on Java Software Development, 5 th Ed. By Lewis &Loftus

Topics Java Programming Language Overview Java Programming Language Overview Program Development Program Development Object-oriented Programming Overview Object-oriented Programming Overview

Java Characteristics Object-oriented Language Object-oriented Language Program consists of a collection of classes Program consists of a collection of classes Each class contains Each class contains one or more one or more attributes—data one or more methods—program statements one or more methods—program statements Syntax is like C++. Syntax is like C++. But it is very different from C++ But it is very different from C++ Primitive types have standard sizes Primitive types have standard sizes Automatic garbage collection Automatic garbage collection No pointers No pointers

Benefits of Java Portable Portable The same source code can be compiled and executed on any Operating System The same source code can be compiled and executed on any Operating System Secure Secure Designed from the start with security in mind Designed from the start with security in mind Designed for Network Programming Designed for Network Programming Makes network-related programming easy Makes network-related programming easy "Network is the computer"--Sun's company motto "Network is the computer"--Sun's company motto Dynamic Program Dynamic Program A program can load various modules as needed during execution. A program can load various modules as needed during execution. International International Java was developed from the start with international use in mind. E.g., Unicode (16 bits) encoding Java was developed from the start with international use in mind. E.g., Unicode (16 bits) encoding

Running a Java Program Source CodeBytecodeOutput Text EditorCompilerJVM

To Run a Java Program Suppose source code MyProgram.java exists in C:\310\. Compile the source code to bytecode, which will be named as MyProgram.class C:\310> javac MyProgram.java Compile the source code to bytecode, which will be named as MyProgram.class C:\310> javac MyProgram.java Run (interpret) the bytecode. C:\310> java MyProgram Run (interpret) the bytecode. C:\310> java MyProgram

Java Program Structure // This is a comment line class MySample { } Class body Class header

Java Program Structure (cont.) // This is a comment line class MySample { public static void main(String args[]) { } } Method body Method header

Java Program Structure (E.g.) // This is a sample program class Welcome { // prints a greeting in two lines public static void main(String[] args) { System.out.println(“Hello, Hawaii.”); System.out.println(“Welcome to Kaimuki.”); } }

RandomTest.java // This program generates 10 random numbers. import java.util.Random; public class RandomTest { public static void main(String[] args) { Random rand = new Random(); for (int n = 1; n <= 10; n++) { System.out.println(n + ". " + rand.nextFloat ()); } } }

Countdown.java // This program counts down numbers from // 10 to 0. class Countdown { public static void main(String[] args) { for (int i = 10; i >=0; i--) { System.out.println(i + "!"); } System.out.println("Lift Off!"); } }

Comments Comments do not affect how a program works Comments do not affect how a program works Java comments can take three forms: Java comments can take three forms: // This comment runs to end of line. // This comment runs to end of line. /* This form can be for one line */ /* or it can cover several lines */ /* This form can be for one line */ /* or it can cover several lines */ /** This is javadoc comment */ /** This is javadoc comment */

13 Identifiers Identifiers are the names used in a program Identifiers are the names used in a program Can be made up of letters, digits, the underscore character ( _ ), and the dollar sign Can be made up of letters, digits, the underscore character ( _ ), and the dollar sign Cannot begin with a digit Cannot begin with a digit Java is case sensitive - Total, total, and TOTAL are different identifiers Java is case sensitive - Total, total, and TOTAL are different identifiers

14 Identifiers (cont.) Java convention: Java convention: Lower case for variables: name, total Lower case for variables: name, total Upper case for constants: MAXIMUM Upper case for constants: MAXIMUM Mixed for compound words: lastDayOfWeek Mixed for compound words: lastDayOfWeek Reserved words (53 in Java) may not be used as identifiers. Reserved words (53 in Java) may not be used as identifiers.

15 Reserved Words The Java reserved words: The Java reserved words: abstract assert boolean break byte case catch char class const continue default do double else enum extends false final finally float for goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while

16 White Space Whte spaces: Whte spaces: spaces, line breaks, and tabs spaces, line breaks, and tabs used to separate words and symbols in a program used to separate words and symbols in a program Extra white space is ignored Extra white space is ignored Enhance program readability with: Enhance program readability with: Blank line to separate major section Blank line to separate major section Indentation for loop body Indentation for loop body Indentation in If statements Indentation in If statements

Outline Java Programming Language Overview Java Programming Language Overview Program Development Program Development Object-oriented Programming Overview Object-oriented Programming Overview

Programming Language Levels Machine language Machine language Assembly language Assembly language ori $8,$0,0x2 ori $9,$0,0x3 addu $10,$8,$9 ori $8,$0,0x2 ori $9,$0,0x3 addu $10,$8,$9 High-level language High-level language sum = num1 + num2 sum = num1 + num2 Fourth-generation language Fourth-generation language Report generators Form generators Report generators Form generators Increasing abstraction

Program Development Steps in Program Development Steps in Program Development Writing source code Writing source code Compiling source code into machine— readable form Compiling source code into machine— readable form Debugging and Testing program Debugging and Testing program Executing program Executing program

Typical Programming Process Write Source code in high-level language—e.g., Java Write Source code in high-level language—e.g., Java Translate source code into machine language Translate source code into machine language Compiler translates the entire source code into single machine program Compiler translates the entire source code into single machine program Interpreter translates and executes the source code one line at a time.) Interpreter translates and executes the source code one line at a time.) Checking for errors and correcting them Checking for errors and correcting them Execute program Execute program Each type of CPU requires machine code specific to it (using operating system) Each type of CPU requires machine code specific to it (using operating system)

Basic Program Development errors Edit and save program Compile program Execute program and evaluate results ©2007 Pearson Addison Wesley

Java Programming Process Write source code in Java Write source code in Java Translate source code into a bytecode (machine laguage). Translate source code into a bytecode (machine laguage). Execute the bytecode by interpretor (Java Virtual Machine) Execute the bytecode by interpretor (Java Virtual Machine) Each CPU runs the Virtual Machine Each CPU runs the Virtual Machine Which executes the bytecode Which executes the bytecode

Java Environment The Java environment consists of the following elements. Java Programming Language Java Programming Language grammar for writing application programs, applets, JavaBeans components, etc. grammar for writing application programs, applets, JavaBeans components, etc. Java Virtual Machine (JVM) Java Virtual Machine (JVM) software for executing Java binary programs, called byte code software for executing Java binary programs, called byte code Java Platform (Java Standard Library, Java API) Java Platform (Java Standard Library, Java API) set of predefined classes set of predefined classes

Java Virtual Machine Source code is compiled into a binary file known as a bytecode. Source code is compiled into a binary file known as a bytecode. Bytecode is then interpreted by the Java Virtual Machine, which sits atop an operating system-- Windows, MacOS, Lunux. Bytecode is then interpreted by the Java Virtual Machine, which sits atop an operating system-- Windows, MacOS, Lunux. Since the bytecode does not need to know the kind of OS on which it is being executed--the JVM is tailored to particular OS--a Java code is highly portable from one type of computer system to another. Since the bytecode does not need to know the kind of OS on which it is being executed--the JVM is tailored to particular OS--a Java code is highly portable from one type of computer system to another.

25 Java Translation Java source code Machine code Java bytecode Bytecode interpreter Bytecode compiler Java compiler ©2007 Pearson Addison Wesley

Terminology Syntax Syntax Rules of language (grammar) Rules of language (grammar) d = a(b + c); -- syntax error d = a(b + c); -- syntax error Semantics Semantics Meaning of the language Meaning of the language speed = distance * time; --syntactically correct, semantically wrong speed = distance * time; --syntactically correct, semantically wrong Errors Errors Compile-time error (e.g., syntax error) Compile-time error (e.g., syntax error) Run-time error (e.g., dividing by 0) Run-time error (e.g., dividing by 0) Logic error (program runs, but produces wrong result) Logic error (program runs, but produces wrong result)

Outline Java Programming Language Overview Java Programming Language Overview Program Development Program Development Object-oriented Programming Overview Object-oriented Programming Overview

Problem Solving Steps in problem solving: Steps in problem solving: Understand the problem Understand the problem Design a solution Design a solution Consider alternatives and refine the solution Consider alternatives and refine the solution Implement the solution Implement the solution Test the solution Test the solution These steps are not linear—they overlap and repeat These steps are not linear—they overlap and repeat

Problem Solving (cont.) The key strategy to solve a complex problem is to break it down into manageable pieces and solve each piece—divide and conquer method. The key strategy to solve a complex problem is to break it down into manageable pieces and solve each piece—divide and conquer method. Object-oriented approach sees a program in terms of a collection of “objects” interacting with each other. Object-oriented approach sees a program in terms of a collection of “objects” interacting with each other. With Java, objects and classes are the basic units of a program With Java, objects and classes are the basic units of a program

Objects and Classes An OO program consists of a collection of objects interacting with each other. An OO program consists of a collection of objects interacting with each other. Suppose you are writing a game program that involves several dogs, cats, and cars. (Use your imagination.) Here are some dog objects: Suppose you are writing a game program that involves several dogs, cats, and cars. (Use your imagination.) Here are some dog objects: Fido—a big brown bulldog Fido—a big brown bulldog Lassie—a tall collie Lassie—a tall collie Taro—a small, white akita Taro—a small, white akita

Objects Attributes: name = Fido breed = bulldog color = brown weight = 30 kg height = 50 cm Operations: bark() display() Attributes: name = Lassie breed = collie color = brow weight = 25 kg height = 70 cm Operations bark() display() Attributes: name = Taro breed = akita color = white weight = 10kg height = 30 cm Operations bark() display() Dog1 Dog2 Dog3

Class A class is an abstraction of the objects of the same type. Fido, Lassie, and Taro are all instances of the Dog class. A class is an abstraction of the objects of the same type. Fido, Lassie, and Taro are all instances of the Dog class. Each objects has a number of Each objects has a number of attributes (with different values) and attributes (with different values) and operations (same methods). operations (same methods). A class is A class is a template for creating (specifying) objects of a particular type. a template for creating (specifying) objects of a particular type. like blueprint for creating objects of a particular type like blueprint for creating objects of a particular type

Class (cont.) A class is composed of A class is composed of Class Name Class Name Attributes Attributes Operations (methods) Operations (methods) Once a class is defined, objects can be created (instantiated) from it. Once a class is defined, objects can be created (instantiated) from it.

Class Diagram Dog breed color weight height Bark() display() Class name Attributes Operations Car make engineSize color maxSpeed Start() accelerate() stop() display()

Class (summary) A class defines an object. A class defines an object. A class is the blueprint, or template, of an object A class is the blueprint, or template, of an object A class represents a concept, and an object represents the embodiment of that concept A class represents a concept, and an object represents the embodiment of that concept Multiple objects can be created from the same class Multiple objects can be created from the same class The class uses methods to define the behaviors of the object The class uses methods to define the behaviors of the object The class that contains the main method of a Java program represents the entire program The class that contains the main method of a Java program represents the entire program

Classe and Object Bank Account A class (the concept) John’s Bank Account Balance: $5,257 An object (the realization) Bill’s Bank Account Balance: $1,245,069 Mary’s Bank Account Balance: $16,833 Multiple objects from the same class ©2007 Pearson Addison Wesley

Inheritance One class can be used to derive another via inheritance One class can be used to derive another via inheritance Classes can be organized into hierarchies Classes can be organized into hierarchies Bank Account Account Charge Account Savings Account Checking Account ©2007 Pearson Addison Wesley

Inheritance (2) One class can be used to derive another via inheritance One class can be used to derive another via inheritance Classes can be organized into hierarchies Classes can be organized into hierarchies Full-time Employee Part-time ] Hourly- wage Annual- salary ©2007 Pearson Addison Wesley

Practice Specify some relevant attributes and operations for the following classes. Specify some relevant attributes and operations for the following classes. Bank Account Bank Account Book Book Browser window Browser window Circle (geometric figure) Circle (geometric figure) Rectangle (geometric figure) Rectangle (geometric figure)