COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Intro to Java Programming  A computer follows the instruction precisely and exactly.  Anything has to be declared and defined before it can be used.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java Fundamentals Asserting Java Chapter 2: Introduction to Computer Science ©Rick Mercer.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
CPS120: Introduction to Computer Science
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 Elementary Programming
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Chapter 2: Java Fundamentals
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Introduction to Programming
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
CPS120: Introduction to Computer Science Variables and Constants.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Chapter 2 Basic Computation
Chapter 1.2 Introduction to C++ Programming
Elementary Programming
Introduction to Computer Science / Procedural – 67130
Revision Lecture
Java Programming: From Problem Analysis to Program Design, 4e
Introduction to the C Language
IDENTIFIERS CSC 111.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2: Basic Elements of Java
Fundamentals 2.
Chapter 2: Java Fundamentals
Anatomy of a Java Program
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Primitive Types and Expressions
Chapter 2 Primitive Data Types and Operations
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers –Data Types –Variables –Constants –Text Input and Output Lecture 2

Introduction to Programming A program is a set of instructions –It instructs the computer What resources it needs How to use the resources How to handle the results –Real-life analogy examples: A recipe for baking cookies Instructions for assembling a table Computers can only perform simple instructions –Our program instructions must be simple We need to break a problem into simple steps Computers can perform instructions very fast

Executing Programs 3 types of executable programs –Compiled (C, C++) The source code must be compiled to machine language –Translation »Convert source code into symbolic language –Compilation »Convert source code into machine language The program is ready to run without additional resources –Interpreted (JavaScript, PHP, Python) The source code is “interpreted” by the computer –Each line of source code is compiled and executed »Occurs in real time –The interpreter must be running to run the program –Hybrid (Java, Visual Basic)) Mix between interpreted and compiled programs –Source code is translated into symbolic language –A real-time compiler compiles each symbolic line individually »The real-time compiler must be running to run the program

Introduction to Java Java is a hybrid language –Source code is translated into symbolic language Referred to as Java bytecode –Bytecode is compiled into machine language Performed using a real-time compiler –Referred to as the Just-In-Time compiler (JIT) –A Java virtual machine (JVM) is required to run a Java program

Commenting Comments are notes to the programmers –Similar to college students’ note taking Highlighting important words or phrases Underlining passages Writing notes in the margins –Java supports 3 forms of commenting Line or statement comments –// This is a line comment Block comment –/* This is a block comment */ JavaDoc comment –/** This is a javadoc */

JavaDoc

Structure of Java Program (section 1.4) A Java program has 3 primary sections –Import Statements Add additional methods and operations to the program import java.util.Scanner; –Class Declarations Provides the name of the class Contains all of the program code –Global Declarations Declare items that are accessible anywhere in the program –Global variables –Global constants –Methods 1 or more methods that contain all the program’s statements –Every Java program must contain a main() method public static void main(String[] args)

Identifiers (section 1.4) Used to refer to items created by the programmer –Variables –Constants –Methods 4 rules for creating identifiers –Consist of letters, numbers and underscore characters only A-Z, a-z, 0-9, _ –1 st character must be a letter or underscore firstGrade and _1stGrade = OK 1stGrade, first-grade and first grade = Not OK –Cannot duplicate a keyword or existing identifier (of same scope) Keyword is also referred to as Reserved Word –Identifiers are case-sensitive age, Age, aGe, agE, AGe, AgE, aGE and AGE are considered unique

Identifiers (section 1.4) Additional identifier naming guidelines –Identifiers should be self-explanatory Avoid single-letter identifiers a = Not self-explanatory (Bad) age = Self-explanatory (Good) –Single-word identifiers Use all lowercase letters –age, count, cost, salary, status, etc. –Multi-word identifiers Use all lowercase letters Capitalize first letter of each subsequent word –userAge, itemCount, totalCost, empSalary, errorStatus, etc.

Data Types (section 2.3) A data type defines –A set of values –The operations performed on those values Valid data types in Java –Many more exist, but only these will be used in class –Integral Boolean (referred to as “boolean”) –Represents an true/false condition Character (referred to as “char”) –Represents a displayable characters (65 = ‘A’) Integer (referred to as “int”) –Represents all integer values (i.e., -3, -2, -1, 0, 1, 2, 3, etc.) –Floating-point Double (referred to as “double”) –Represents all numeric values containing a decimal point (i.e., -2.0, -1.0, 0.0, 1.0, 2.0, etc.) –String Represents a collection of characters

Data Types (chapter 2) CategoryData Type# BytesMin ValueMax Value Integralboolean2FalseTrue char int4-2,147,483,6482,147,483,647 Floating- Point double8-1.7x x String 2n2*0 Characters2*n Characters

Variables (section 2.2) Variables –Store data values during runtime Referred to as “assigning a value” –Refer to locations in memory –Must be declared before using Specify the identifier name and data type Using the following form DataType Identifier; Value can be assigned when declared (“initializing”) DataType Identifier = Value; –Values can be changed during runtime

Variables (section 2.2) When a variable is declared –The program requests memory from the OS The amount of memory depends on the data type –The OS locates the required memory The memory address is returned to the program –The program references the memory address Using the specified identifier –If the variable is initialized with a value The value is assigned to the memory address

Variables (section 2.2) Important issue with variable declaration –A variable must be initialized before it is accessed If a variable is accessed before it is initialized –The compiler will display an error Always initialize variables before using them

Variables (section 2.2) Variable declaration and assignment examples Declared w/o ValueDeclared w/ Value int age; // Not initialized age = 29; // Assign value // More lines of code age = 21; // Assign new value int age = 0; // Initialized age = 29; // Assign value // More lines of code age = 21; // Assign new value Example Declarations w/o ValueExample Declarations w/ Value boolean valid; char initial; int count; double salary; String name; boolean valid = true; char initial = ‘A’; char initial = 65; int count = 0; double salary = 9.5; String name = “Jim”; Both mean the same

Variables (section 2.2) 3 ways to declare/initialize variables Note: Ordering variables names alphabetically improves readability –One variable per line –Multiple variables per line Declare variables of different data types on separate lines –Variables can be initialized or not int age; int count; char initial; double ratio; double salary; Dim age, count As Integer Dim initial As Char Dim ratio, salary As Double int age; int age, count = 0; int count = 0;char initial = ‘A’; char initial = ‘A’;double ratio = 0.5, salary; double ratio = 0.5; double salary;

Constants (section 2.2) A constant is a value that cannot be changed –Only the programmer can change the value Java supports 2 types of constants –Literal constant (no memory required) int a = 0; In the above example, 0 is a literal example –Only the programmer can change the value –Memory constant (memory required) final double PI = 3.14; Used similar to a variable, but value cannot be changed

Input/Output Java has access to 3 data streams –Input i.e., keyboard The characters typed on the keyboard –Output i.e., monitor The characters displayed on the monitor –Input/Output i.e., file (discussed later in the course) The characters read to/written from a file

Input/Output (section 2.1) The print() and println() output functions –Displays data on the monitor –System.out.print Displays the contents between the parentheses Leaves the cursor at the end of the line –System.out.println Displays the contents between the parentheses Moves the cursor to the beginning of the next line –Data Values can be “concatenated” into the display string Variables Constants Method return values String name = “Kelly”; int salary = 45000; System.out.print(name + “ has an annual salary of $” + salary + “\n”; System.out.println(name + “ has an annual salary of $” + salary; Output: Kelly has an annual salary of $45000

Input/Output (section 2.1) The Scanner class –Reads data from the monitor –Common Scanner class methods nextInt() – reads an integer value from the keyboard nextDouble() – reads a double value from the keyboard next() – reads a string from the keyboard –If the user can enter non-numeric values »Use the next() method »Then use the appropriate “parse” method for the expected data type Scanner input = new Scanner(System.in); int salary; System.out.print(“Enter your annual salary (in $): ”; salary = input.nextInt(); // User enters System.out.println(“You entered an annual salary of $” + salary); Output: You entered an annual salary of $45000 Non-numeric input will cause error