Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Java Byte Code l The Java compiler generates Java Byte Code. (Most.

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to Computers and Java Module 1. Objectives overview computer hardware and software introduce program design and object-oriented programming.
Object Orientated Programming
1 計算機程式設計 ( 下 ) February 2002 陳正佳. 2 Course Information l 上課內容 : Java and Object-Orient Programming l 時間 : 13:10~16:00, Friday. l 地點 : 應數系 PC 教室 l TextBook:
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Introduction to Computers and Java Objects
Chapter 11 Introduction to Computers and Java Chapter 1.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 1 “ Introduction to Java and OOP”
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Outline Java program structure Basic program elements
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Introduction to C Programming
Introduction Algorithms and Programming. Computer Programming  A form of problem solving  Or, more accurately, a way to solve problems  What we will.
INTRODUCTION TO JAVA PROGRAMMING Chapter 1. What is Computer Programming?
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
CSE 1301 J Lecture 2 Intro to Java Programming Richard Gesick.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know.
CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
+ Java vs. Javascript Jessi Style. + Java Compiled Can stand on its own Written once, run anywhere Two-stage debugging Java is an Object Oriented Programming.
Lecturer: Dr. AJ Bieszczad Chapter 1 COMP 150: Introduction to Object-Oriented Programming 1-1 l Background information »important regardless of programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes, Objects, and Methods
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 1 l Background information »important regardless of programming.
O BJECT O RIENTATION F UNDAMENTALS Prepared by: Gunjan Chhabra.
Java: Chapter 1 Computer Systems Computer Programming II.
CS107 Introduction to Computer Science Java Basics.
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.
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.
The Java Programming Language
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Object Oriented Programming Computer Engineering Department JAVA Programming Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Lecture 2: Classes and Objects, using Scanner and String.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 1 l Background information »important regardless of programming.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
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.
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.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 1- Basic Computing and your first program.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Computer and Programming. Computer Basics: Outline Hardware and Memory Programs Programming Languages and Compilers.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Lesson 1 1 LESSON 1 l Background information l Introduction to Java Introduction and a Taste of Java.
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 1 l Background information »important regardless of programming.
GC101 Introduction to computer and program
Chapter No. : 1 Introduction to Java.
Introduction to Computers and Java
Data types and variables
INTRODUCTION TO OOP Objective:
Introduction to Computers and Java
Introduction to Java Brief history of Java Sample Java Program
How to Run a Java Program
Presentation transcript:

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Java Byte Code l The Java compiler generates Java Byte Code. (Most compilers generate machine language code.) l The Java source code (the code the programmer writes) is stored in a.java file. The byte code is stored in a.class file. l Java byte code can be run on any machine with a Java interpreter – therefore Java is more portable than other language.

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 2 Object-Oriented Programming: OOP l A design and programming technique l Some terminology: »object - usually a person, place or thing (a noun) »method - an action performed by an object (a verb) »type or class - a category of similar objects (such as automobiles) l Objects have both data and methods l Objects of the same class have the same data elements and methods l Objects send and receive messages to invoke actions

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 3 Design Principles of OOP Three main design principles of Object- Oriented Programming(OOP): l Encapsulation l Polymorphism l Inheritance

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 4 Encapsulation l Encapsulation means to design, produce, and describe software so that it can be easily used without knowing the details of how it works. l Also known as information hiding l Class example: Driving a car l Any other examples?

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 5 Encapsulation l Additional examples: »Elevator: When you push a button, you go to the right floor – you don’t need to know how the pulleys, lights, etc. work. »A software example: sorting a list of numbers –Another programmer would give your program a list of numbers and your program would return the list sorted. They do not need to know how your program sorted the numbers.

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 6 Polymorphism l Polymorphism—the same word or phrase can mean different things in different contexts l Analogy: in English, bank can mean side of a river or a place to put money Class example: an output method in different classes. l Any other examples?

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 7 Polymorphism l Additional examples »If someone said “Go play your favorite sport”, some would play basketball, some football... »Two classes, Cat and Dog, may both have methods called makeNoise, but they would output different things.

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 8 Inheritance l Inheritance—a way of organizing classes l Term comes from inheritance of traits like eye color, hair color, and so on. l Classes with properties in common can be grouped so that their common properties are only defined once. l Class example: vehicle hierarchy l Any other examples?

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 9 Inheritance l Additional examples »For a university program: Person FacultyStaffStudent UndergraduateGraduateCleaning StaffDept. ChairAssistant Prof.

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 10 Types of Errors l Syntax, Run-Time, and Logic l Syntax Errors: »Occurs if your program is not a valid Java program. »E.g. Typing “rturn” instead of “return” »Caught by the compiler – a compile-time error

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 11 Run-Time Errors l An execution error (during run-time) l Not always so easy to fix l Error message may or may not be helpful l Not detected by the compiler but is detected by the run-time system. Example: Division by zero - if your program attempts to divide by zero it automatically terminates and prints an error message. Note: May not happen every time you run the program!

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 12 Logic Errors Just because it compiles and runs without getting an error message does not mean the code is correct! l An error in the design (the algorithm) or its implementation »code compiles without errors »no run-time error messages »but incorrect action or data occurs during execution l Generally the most difficult to find and fix l Need to be alert and test thoroughly »think about test cases and predict results before executing the code

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 13 Logic Error Examples l Algorithm Error: »averageOfFiveScores = SumOfScores/2; (should divide by 5) l Implementation Error: »typed in wrong symbol in source code - sum = a - b; (should be sum = a + b; )

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 14 Finally! Now, a taste of Java! History l James Gosling, Sun Microsystems, Inc. l originally a language for programming home appliances l later (1994) used for World Wide Web applications (since byte code can be downloaded and run without compiling it) l eventually used as a general-purpose programming language (for the same reason as above plus it is object- oriented) l Why the name “Java”? Not sure - it may just be a name that came during a coffee break and it had not been copyrighted, yet.

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 15 Applets vs. Java Applications l Applets »Java programs intended to be downloaded via the Web and run immediately »“little applications” »requires a web browser l Applications »Java programs intended to be installed then run »often larger applications l Slightly different programming for each, but both are easy to do

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 16 public class FirstProgram { public static void main(String[] args) { System.out.println("Hello out there."); System.out.println("Want to talk some more?"); System.out.println("Answer y for yes or n for no."); char answerLetter; answerLetter = SavitchIn.readLineNonwhiteChar(); if (answerLetter == 'y') System.out.println("Nice weather we are having."); System.out.println("Good-bye."); System.out.println("Press enter key to end..."); String junk; junk = SavitchIn.readLine(); } A Sample Java Program

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 17 Explanation of Code... l Code to begin the program (to be explained later): public class FirstProgram { public static void main(String[ ] args) { l Java applications all have similar code at the beginning »The name of the class differs from one program to another. »Other information about the class might also be included on the first line.

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 18 Explanation of Code... l Code to display a text string: System.out.println("Hello out there."); System.out.println("Want to talk some more?"); System.out.println("Answer y for yes or n for no."); »Note the “dot” operator »System.out is an object »println is a method that it invokes »double-quoted text inside the parentheses is an argument to the method »general syntax: Object_Name.Method_Name(Arguments)

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 19 … Explanation of Code... Code to create a variable named answerLetter to contain a single character of data: char answerLetter; l This variable is used to store the user’s response.

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 20 … Explanation of Code... Read a character typed in from the keyboard and store it in the variable answerLetter : answerLetter = SavitchIn.readLineNonwhiteChar(); »SavitchIn is a class used for obtaining input from the keyboard »readLineNonwhiteChar() is a method that reads a single, non-blank character from the keyboard and discards any remaining characters on the line. »the equal sign is not the same as in math; it means “assign the value on the right to the variable on the left;” in this case, store the value read from the keyboard into the variable answerLetter

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 21 … Explanation of Code... Question: If “=“ means “assign the value of the expression on the right to the variable on the left,” how do we indicate “equals”? Answer: use a double equals (“==“) Example: check to see if the character entered is ‘y’: if (answerLetter == 'y') »the value inside the parentheses will be True if the letter ‘y’ was typed in, otherwise it will be False (if any other letter was typed in)

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 22 … Explanation of Code... l Code to display the line “Nice weather we are having.” if the user entered the character ‘y’: if (answerLetter == 'y') System.out.println("Nice weather we are having."); »Note that the line will not be printed if any letter other than ‘y’ is entered. l Unconditionally display the line “Good-bye.”: System.out.println("Good-bye."); »only the previous System.out.println is conditionally printed, depending on the value entered; the next instruction is executed regardless of the value entered.

Chapter 1Java: an Introduction to Computer Science & Programming - Walter Savitch 23 … Explanation of Code l Code to prevent the display from scrolling off the screen before you can read it: System.out.println("Press enter key to end program."); String junk; junk = SavitchIn.readLine(); »junk is a variable that can contain a string of characters. »readLine() is a method to read in an entire line of text. »The program halts until a character is entered. »Any character entered will make the program continue. »The character entered is assigned to the variable junk, but is ignored (it is not used). »There are no more lines of code, so the program terminates.