ECE122 L2: Program Development February 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 2 Program Development.

Slides:



Advertisements
Similar presentations
CSci 1130 Intro to Programming in Java
Advertisements

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Constants and Data Types Constants Data Types Reading for this class: L&L,
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,
String Escape Sequences
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Program Elements -- Introduction
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
HOW COMPUTERS MANIPULATE DATA Chapter 1 Coming up: Analog vs. Digital.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Outline Questions / Review Predefined Objects Variables Primitive Data Arithmetic Expressions Interactive Programs Decision Making Assignments.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Program Statements Primitive Data Types and Strings.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyCopyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Introduction to Java Java Translation Program Structure
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 2 Data and Expressions Part One. © 2004 Pearson Addison-Wesley. All rights reserved2-2/29 Data and Expressions Let's explore some other fundamental.
Chapter 2 Variables.
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
Primitive Data Types 1 In PowerPoint, point at the speaker icon, then click the "Play" button.
CSCI 1100/1202 January 14, Abstraction An abstraction hides (or ignores) the right details at the right time An object is abstract in that we don't.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Chapter 2: Objects and Primitive Data Lian Yu Department of Computer Science and Engineering Arizona State University Tempe, AZ
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
© 2006 Pearson Education Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Object Oriented Programming Idea Computer program may be seen as comprising a collection of objects Object Fundamental entity in a JAVA program Used to.
Chapter 2 Variables.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
CSC 1051 – Data Structures and Algorithms I
Multiple variables can be created in one declaration
Escape Sequences What if we wanted to print the quote character?
Data and Expressions Part One
IDENTIFIERS CSC 111.
Escape Sequences Some Java escape sequences: See Roses.java (page 68)
Objects and Primitive Data
Chap 2. Identifiers, Keywords, and Types
Chapter 2: Objects and Primitive Data
Chapter 2 Variables.
CSC 1051 – Data Structures and Algorithms I
Instructor: Alexander Stoytchev
Java Programming Presented by Dr. K. SATISH KUMAR,
Presentation transcript:

ECE122 L2: Program Development February 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 2 Program Development

ECE122 L2: Program Development February 1, 2007 Outline °Problem: How do I define and run a Java program? What does the program do with my text? What makes Java different? °Representing computation with objects °Representing data Characters Integers Floating point °Making assignments to variables

ECE122 L2: Program Development February 1, 2007 Program Development °The mechanics of developing a program include several activities Writing the program in a specific programming language (such as Java) Translating the program into a form that the computer can execute Investigating and fixing various types of errors that can occur °Software tools can be used to help with all parts of this process Edit and Save Program Translate Program into Executable Form Execute Program and Evaluate Results errors Compilation Execution In a file using an editor

ECE122 L2: Program Development February 1, 2007 Translation of Source Code °The translation of source code into machine language °Subsequent execution on a particular type of CPU can occur in a variety of ways. By using a compiler. By using an interpreter. By using both a compiler and interpreter -This is how Java does it.

ECE122 L2: Program Development February 1, 2007 A Compiler °A compiler is a program that translates code from one language to an equivalent code in another language. °The original code is called source-code. The language into which it is translated is called the target language. For many traditional compilers, the source code is translated directly into a particular machine language. The translation process occurs once and the resulting executable program can be run whenever needed.

ECE122 L2: Program Development February 1, 2007 Java Programming Language °Each type of CPU executes only a particular machine language °A program must be translated into machine language before it can be executed °A compiler is a software tool which translates source code into a specific target language °Often, that target language is the machine language for a particular CPU type

ECE122 L2: Program Development February 1, 2007 An Interpreter °An interpreter is similar to a compiler but has important differences… An interpreter performs the translation and execution activities. -A small part of the source code, such as one statement, is translated and executed. -Then another part is translated and executed, and so on. The program runs more slowly because the translation process occurs during each execution. -Each statement is translated, then executed immediately

ECE122 L2: Program Development February 1, 2007 Java Translation Java source code Machine code Java bytecode Bytecode interpreter Bytecode compiler Java compiler Your computer reads the bytecode and “executes” the program You write this DrJava does this Machine

ECE122 L2: Program Development February 1, 2007 Java Translation °The Java compiler translates Java source code into a special representation called bytecode °Java bytecode is not the machine language for any traditional CPU °Another software tool, called an interpreter, translates bytecode into machine language and executes it °Therefore the Java compiler is not tied to any particular machine °Java is considered to be neutral

ECE122 L2: Program Development February 1, 2007 Java Program Structure °A program is made up of one or more classes °A class contains one or more methods °A method contains program statements °A Java application always executes the main method

ECE122 L2: Program Development February 1, 2007 Classes °An object is defined by a class °A class is the blueprint of an 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 °A class represents a concept, and an object represents the embodiment of that concept °Multiple objects can be created from the same class

ECE122 L2: Program Development February 1, 2007 Objects and Classes 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

ECE122 L2: Program Development February 1, 2007 Character Strings °A string of characters can be represented as a string literal by putting double quotes around the text: °Examples: "This is a string literal." "123 Main Street" "X" °Every character string is an object in Java, defined by the String class °Every string literal represents a String object There are many String objects but only one String class

ECE122 L2: Program Development February 1, 2007 Character Strings °Every character string is an object in Java, defined by the String class °Every string literal, delimited by double quotation marks, represents a String object °The string concatenation operator (+) is used to append one string to the end of another °It can also be used to append a number to a string °A string literal cannot be broken across two lines in a program

ECE122 L2: Program Development February 1, 2007 Using Classes °We invoke the println method to print a character string °The System.out object represents a destination (the monitor screen) to which we can send output System.out.println (“Hello, World"); object method name information provided to the method (parameters) What about the “print” method?

ECE122 L2: Program Development February 1, 2007 String Concatenation °The + operator is also used for arithmetic addition °The function that it performs depends on the type of the information on which it operates °If both operands are strings, or if one is a string and one is a number, it performs string concatenation °If both operands are numeric, it adds them °The + operator is evaluated left to right, but parentheses can be used to force the order System.out.println ("24 and 45 concatenated: " ); System.out.println ("24 and 45 added: " + ( ));

ECE122 L2: Program Development February 1, 2007 Example //******************************************************************** // Facts.java Author: Lewis/Loftus // // Demonstrates the use of the string concatenation operator and the // automatic conversion of an integer to a string. //******************************************************************** public class Facts { // // Prints various facts. // public static void main (String[] args) { // Strings can be concatenated into one long string System.out.println ("We present the following facts for your " + "extracurricular edification:"); System.out.println (); // A string can contain numeric digits System.out.println ("Letters in the Hawaiian alphabet: 12"); // A numeric value can be concatenated to a string System.out.println ("Dialing code for Antarctica: " + 672); System.out.println ("Year in which Leonardo da Vinci invented " + "the parachute: " ); System.out.println ("Speed of ketchup: " " km per year"); } }

ECE122 L2: Program Development February 1, 2007 Escape Sequences °What if we wanted to print a the quote character? °The following line would confuse the compiler because it would interpret the second quote as the end of the string System.out.println ("I said "Hello" to you."); °An escape sequence is a series of characters that represents a special character °An escape sequence begins with a backslash character ( \ ) System.out.println ("I said \"Hello\" to you.");

ECE122 L2: Program Development February 1, 2007 Escape Sequences °Some Java escape sequences: Escape Sequence \b \t \n \r \" \' \\ Meaning backspace tab newline carriage return double quote single quote backslash

ECE122 L2: Program Development February 1, 2007 Variables °A variable is a name for a location in memory °A variable must be declared by specifying the variable's name and the type of information that it will hold int total; int count, temp, result; Multiple variables can be created in one declaration data type variable name

ECE122 L2: Program Development February 1, 2007 Variables °A variable can be given an initial value in the declaration °When a variable is referenced in a program, its current value is used int sum = 0; int base = 32, max = 149; // note syntax…

ECE122 L2: Program Development February 1, 2007 Assignment °An assignment statement changes the value of a variable °The assignment operator is the = sign total = 55; °The value that was in total is overwritten °You can only assign a value to a variable that is consistent with the variable's declared type °The expression on the right is evaluated and the result is stored in the variable on the left

ECE122 L2: Program Development February 1, 2007 Constants °A constant is an identifier that is similar to a variable except that it holds one value while the program is active °The compiler will issue an error if you try to change the value of a constant during execution °In Java, we use the final modifier to declare a constant final int MIN_HEIGHT = 69; Note: constants are written in caps to distinguish themselves from other ‘variables’ whose values can change. give names to otherwise unclear literal values facilitates updates of values used throughout a program prevent inadvertent attempts to change a value (Discuss: final float RATE = 0.15; only change value…)

ECE122 L2: Program Development February 1, 2007 Primitive Data °There are exactly eight primitive data types in Java °Four represent integers: –byte, short, int, long (no fractions) °Two represent floating point numbers: –float, double (contain decimals) °One represents characters: char °One represents boolean values: boolean °All have different ‘sizes’ and ‘ranges’…..

ECE122 L2: Program Development February 1, 2007 Numeric Primitive Data °Sizes and Ranges of storable values below. °Use size as ‘appropriate’ but if in doubt, be generous. Type byte short int long float double Storage 8 bits 16 bits 32 bits 64 bits 32 bits 64 bits Min Value ,768 -2,147,483,648 < -9 x /- 3.4 x with 7 significant digits +/- 1.7 x with 15 significant digits Max Value ,767 2,147,483,647 > 9 x 10 18

ECE122 L2: Program Development February 1, 2007 Numeric Primitive Data °Default: int is 32 bits; but 45L or 45l => long °Default: for decimal data: assumes all literals are type double. To make ‘float’  45.6F or 45.6f Can say, if desired, 45.6D or 45.6d, but unnecessary.

ECE122 L2: Program Development February 1, 2007 Characters °A char variable stores a single character from the Unicode character set °A character set is an ordered list of characters, and each character corresponds to a unique number °The Unicode character set uses sixteen bits per character, allowing for 65,536 unique characters °It is an international character set, containing symbols and characters from many world languages °Character literals are delimited by single quotes: 'a' 'X' '7' '$' ',' '\n'  ‘7’ is not equivalent to 7 is not equivalent to “7”

ECE122 L2: Program Development February 1, 2007 Characters °The ASCII character set is older and smaller than Unicode, but is still quite popular Has evolved to eight-bits per byte. °  (char is a ‘primitive data type’; String is a class) Because String is a class, it has many methods (operations) that can be performed on String objects!) °The ASCII characters are a subset of the Unicode character set, including: uppercase letters lowercase letters punctuation digits special symbols control characters A, B, C, … a, b, c, … period, semi-colon, … 0, 1, 2, … &, |, \, … carriage return, tab,...

ECE122 L2: Program Development February 1, 2007 Boolean °A boolean value represents a true or false condition °A boolean also can be used to represent any two states, such as a light bulb being on or off °The reserved words true and false are the only valid values for a boolean type boolean done = false;

ECE122 L2: Program Development February 1, 2007 Compile-time and Run-time Errors °An error identified by the compiler is called a compile-time error. (These are syntax errors) If a compile-time error occurs, an executable version of the program is not created. Use your editor to correct the error, then recompile your program. DrJava will help you with this °A runtime error causes the program to terminate abnormally during execution. An example is an attempt to divide by zero. In Java, many runtime errors are represented as Exceptions

ECE122 L2: Program Development February 1, 2007 Logical Errors °When your program has a logical error, it will compile and execute, but produces incorrect results. (These are runtime errors too!) A logical error occurs when a value is calculated incorrectly. A programmer must test the program thoroughly, comparing the expected results to those that actually occur. -The process of finding and correcting defects in a program is called debugging.

ECE122 L2: Program Development February 1, 2007 Basic Program Development errors Edit and save program Compile program Execute program and evaluate results

ECE122 L2: Program Development February 1, 2007 Good Programming Practice °Write a comment before each class, documenting the purpose of the class. °Write end of line (single line) comment, documenting the purpose of the statement. °Use DrJava to check your syntax. °Declare each variable in each line, allowing end of line comment. E.g. int age; // The age of my dog °Choose meaningful variable names helps a program to be self-documenting. Easy to understand.

ECE122 L2: Program Development February 1, 2007 Summary °Java code is primarily interpreted so it can be used on any computer °Java is based on objects which contain information (state) and actions (methods) °Strings form an important part of data representations °Assignments and basic data types allow for data storage and manipulation °Reading: L+L: ,