CS 106 Introduction to Computer Science I 09 / 11 / 2006 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
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 There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
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.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 13 / 2006 Instructor: Michael Eckmann.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 14 / 2007 Instructor: Michael Eckmann.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
CMT Programming Software Applications
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 25 / 2008 Instructor: Michael Eckmann.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
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.
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
CS 106 Introduction to Computer Science I 09 / 18 / 2006 Instructor: Michael Eckmann.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
Objectives You should be able to describe: Data Types
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CS 106 Introduction to Computer Science I 01 / 25 / 2010 Instructor: Michael Eckmann.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Week 1 Algorithmization and Programming Languages.
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.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Introduction to Java Java Translation Program Structure
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
ECE 122 Feb. 1, Introduction to Eclipse Java Statements Declaration Assignment Method calls.
CS 106 Introduction to Computer Science I 01 / 26 / 2007 Instructor: Michael Eckmann.
Chapter 2 Variables.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CS 106 Introduction to Computer Science I 01 / 22 / 2007 Instructor: Michael Eckmann.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 22 / 2008 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 2 Variables.
Chapter 2 Basic Computation
Chapter 2 Introduction to C++ Programming
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Escape Sequences What if we wanted to print the quote character?
Introduction to C++ Programming
Chapter 2 Variables.
Expressions and Assignment
Unit 3: Variables in Java
Chapter 2 Variables.
Just Enough Java 17-May-19.
Instructor: Alexander Stoytchev
Presentation transcript:

CS 106 Introduction to Computer Science I 09 / 11 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2006 Today’s Topics Comments and/or Questions? Review of program structure and compilation / execution process Lab comments Escape sequences String concatenation Variables & types arithmetic operators relational operators if / else structures

Hello, World! program /* CS106 first program written by Michael Eckmann purpose: print the text “Hello, World!” to the screen */ public class HelloWorld { public static void main(String args[]) { System.out.println("Hello, World!"); } // end of main method } // end of HelloWorld class Michael Eckmann - Skidmore College - CS Fall 2006

Discussion of “Hello, World!” comments in source code basic structure of a Java program every Java program must have at least one class every Java program must have exactly one main method System.out.println the use of braces -- begin and end classes and method bodies. Braces are { and } – I call them curly braces. reserved words public, static, void, class, etc... see text book Michael Eckmann - Skidmore College - CS Fall 2006

Phases of Java Programs Create / Edit the source code of the program (the.java file) Compile the source code into bytecodes (by choosing Project -> Build Project in Eclipse) –After the.java file is compiled we get a.class file E.g. if we compile a program called HelloWorld.java the result is HelloWorld.class execute the program (via the java interpreter) (by choosing Run -> Run As -> Java Application in Eclipse) –Execution is done on the.class file(s) I recommend turning off “Build Automatically” in Eclipse.

Michael Eckmann - Skidmore College - CS Fall 2006 Lab Syntax errors appear when? Where in Eclipse? Matching up and keeping track of curly braces. –A matching pair of curly braces enclose the class –A matching pair of curly braces enclose each method (e.g. the main method.) –There are other places for using matching curly braces that we'll see shortly. statements like System.out.println(“...”); must end with a semicolon. Putting all the programs you're writing into one project could cause you confusion when compiling if there are errors in some other.java file within the project.

Program style Should always have programmer's name in a comment as well as the date, purpose of the program, etc. all at the top. Should use whitespace (spaces, tabs, newlines) to enhance readability! Line up matching braces vertically and/or place a comment after the right brace explaining what it ends. Anything else you can think of? It is easily possibly to create poorly formatted syntactically correct programs. Michael Eckmann - Skidmore College - CS Fall 2006

Escape sequences A String's value can be specified between double quotes. Within a string the backslash character \ is special. The backslash character followed by another character is called an escape sequence. Escape sequences are handled differently than regular characters. \n is a common escape sequence to force a newline \t is a way to add a tab to a string E.g. System.out.println(“Hello\nWorld”); prints: Hello World Michael Eckmann - Skidmore College - CS Fall 2006

Escape sequences \b is the backspace character There are many others, some of which are listed in the book on page 43 Let’s say you wanted to print a sarcastic message using double quotes. If we do System.out.println(“Hamilton has a “great” lacrosse team”); there will be an error at compile time. Michael Eckmann - Skidmore College - CS Fall 2006

Escape sequences System.out.println(“Hamilton has a “great” lacrosse team”); The string will be –Hamilton has a and then the compiler will see a g and will generate an error because the second double quote (the one before great) causes the string to end. What do we do? Michael Eckmann - Skidmore College - CS Fall 2006

Escape sequences An escape sequence is provided to print a “. It is \” System.out.println(“ Hamilton has a \“great\” lacrosse team ”); The above line of code will do what we want now. Michael Eckmann - Skidmore College - CS Fall 2006

Escape sequences How might we add a backslash character to a String? Let’s say we want a String like: –C:\My Documents Will this work? –System.out.println(“C:\My Documents”); Michael Eckmann - Skidmore College - CS Fall 2006

Let’s try this problem Write an application that displays the numbers 1 to 4 on the same line, with each pair of adjacent numbers separated by one space. Write the program using the following methods: –A) Using one System.out statement –B) Using four System.out statements Now let's do the same thing but on 4 different lines. Michael Eckmann - Skidmore College - CS Fall 2006

String concatenation To concatenate two Strings together, Java provides the operator + e.g. System.out.println(“Hey “ + “now.”); prints Hey now. Michael Eckmann - Skidmore College - CS Fall 2006

String concatenation Strings can only be concatenated to other Strings, but numbers are converted to Strings by Java automatically if they are being concatentated to another String. e.g. System.out.println(“I am “ “ years old.”) Will work the same as: System.out.println(“I am “ + “21” + “ years old.”) Will work the same as: System.out.println(“I am 21 years old.”) Michael Eckmann - Skidmore College - CS Fall 2006

Variables and Type A variable is a location in memory (RAM) with the following properties. name --- given by the programmer --- try to be descriptive type --- decided by the programmer from all available types, depending on what kind of values the programmer wants to use size --- amount of memory used, this is determined by the type value --- self-explanatory –value can be stored / changed / read by a program. Michael Eckmann - Skidmore College - CS Fall 2006

Primitive Types for Variables byte, short, int, long --- these are integer types -- - they hold whole numbers like –31, 4256, 56, 2632, 755, -901 char --- this is a type that holds one character like a letter, digit, *, &, or an escape sequence … boolean --- holds the values of true and false float, double --- numeric types that hold numbers with decimal values like -5.5, 98.6, etc. see page 53 for the sizes / ranges of values Michael Eckmann - Skidmore College - CS Fall 2006

Variables and Type int area = 50; // This line of code not only // declares a variable but also initializes its value. name of variable is area type of variable is int size of variable is 32 bits (because an int has a size of 32 bits.) value of variable is currently 50 note the same thing can be done with 2 lines of code: int area; area = 50; Michael Eckmann - Skidmore College - CS Fall 2006

public class VariableExample { public static void main(String args[]) { int result; // declares variable with name result and type int // allocates enough memory for an int to be stored int some_number; // declares variable with name some_number and type // int and allocates enough memory for an int to be stored result = 35; // an assignment statement that stores the value 35 in the // variable result. some_number = 24; // an assignment statement that stores the value 24 // in the variable some_number. result = some_number + 5; // uses the + operator to add the value in some_number to 5 // and stores (assigns) the sum to the variable result, thereby // changing result’s value. } // end main method body } // end class VariableExample body Michael Eckmann - Skidmore College - CS Fall 2006

Types for Variables Variables can be of the primitive types as well as predefined classes provided by Java or any class that a programmer creates. For example a variable can be of type String which is a class provided by Java. We’ll see more of variables and types shortly. Michael Eckmann - Skidmore College - CS Fall 2006

Operators + when used with numeric types (e.g. int, float etc.) acts as an add, however, + when used with Strings is a concatenation operator. –example: text_string = “Hello ” + first_name; = is the assignment operator. * is multiplication, / is division, % is modulus and - is subtraction. ( ) left and right parentheses are considered operators as well. Michael Eckmann - Skidmore College - CS Fall 2006

The modulus operator % is the modulus operator int val1=16, val2 = 5, remain; remain = val1 % val2; The modulus operator returns the remainder after val1 is divided by val2. Suppose val1 = 16 and val2 = 5, remain would have the value 1 because 16/5 = 3 with a remainder of 1. Michael Eckmann - Skidmore College - CS Fall 2006

Arithmetic Operator precedence ( ) parentheses are evaluated first if parens are nested then the innermost pair is evaluated first. *, /, % multiplication, division and modulus are next --- if several of these, they are evaluated left to right. +, - addition and subtraction are last --- if several of these, they are evaluated left to right It’s useful to memorize this precedence list Michael Eckmann - Skidmore College - CS Fall 2006

Arithmetic Operator precedence example some_result = ( ) * 6 + ( ); the order in which operations are performed could change the computation's answer. (wrong way) suppose we did 2-10 = -8 and then 6-8 = -2 then = 8, 8*(-2) = -16 (correct way) 5+4-1=8, 2-10=-8, 8*6 = 48, 48-8 = 40 Michael Eckmann - Skidmore College - CS Fall 2006

String concatenation / integer addition Now that we know that operators which are at the same “level” get evaluated from left to right, we can figure out how and why the following line of code works as it does: System.out.println("10 plus 5 is " ); will print 10 plus 5 is 105 the first + acts like concatenation and joins the string and the 10 which gets converted to a String automatically. Then the second + acts like concatenation of the String to 5 which is converted to a String automatically. Michael Eckmann - Skidmore College - CS Fall 2006

Equality and Relational operators == is equal to operator != is not equal to operator < is less than operator > is greater than operator <= is less than or equal to operator >= is greater than or equal to operator Michael Eckmann - Skidmore College - CS Fall 2006

if / else structure So far all the code we’ve seen was executed sequentially --- every line of code in order from the first to the last within the main method. if (condition) { statement_to_do_if_condition_is_true; another_statement_to_do_if_condition_is_true; } else { statement_to_do_if_condition_is_false; another_statement_to_do_if_condition_is_false; yet_another_statement_to_do_if_condition_is_false; } Michael Eckmann - Skidmore College - CS Fall 2006

if / else structure condition is something that will evaluate to true or false (usually using relational or equality operators.) if you only want to do one statement curly braces are not required if you want to do more than one statement the curly braces are necessary also, the else portion of the if / else structure is optional Michael Eckmann - Skidmore College - CS Fall 2006

valid if / else example if (radius > 0) { area = * radius * radius; circumference = 2 * * radius; } else { System.out.println("Radius is invalid"); } Michael Eckmann - Skidmore College - CS Fall 2006

a few more valid if / else examples if (numerical_grade >= 90) System.out.println("your grade is an A!"); /* */ if (account_balance < 200) System.out.println("Acct below $200 minimum”); else System.out.println("Do not charge a service fee"); Michael Eckmann - Skidmore College - CS Fall 2006

Exercise Write an application given two integers, displays the larger number followed by the words “is larger”. If the numbers are equal, print the message “These numbers are equal.” Michael Eckmann - Skidmore College - CS Fall 2006

compound statements A single statement is a line of code ending in a semicolon. A compound statement is a set of statements within a pair of curly braces. A compound statement may be used anywhere in a program that a single statement can be used. example of a single statement amount = 45; example of a compound statement { area = * radius * radius; circumference = 2 * * radius; } Michael Eckmann - Skidmore College - CS Fall 2006

if / else structure revisited The if part of the if / else structure can contain a single statement or a compound statement. Same for the else portion of the if / else. if (degrees_fahr <= 45) System.out.println("It is pretty cold."); else System.out.println("It is not too cold."); System.out.println("You don't need a jacket."); What would print out if degrees_fahr = 30 before the above if / else? Michael Eckmann - Skidmore College - CS Fall 2006

if / else and compound statements The code on the preceding slide would print “You don’t need a jacket.” regardless of the value of degrees_fahr. The following code, using a compound statement, only prints that line if degrees_fahr > 45. if (degrees_fahr <= 45) System.out.println("It is pretty cold."); else { System.out.println("It is not too cold."); System.out.println("You don't need a jacket."); } Michael Eckmann - Skidmore College - CS Fall 2006

another subtlety of the if / else Java always associates an else with the preceding if unless curly braces are used to force it not to do this. example: if (i == 0) if (j == 0) System.out.println("both i and j are 0"); else System.out.println("i is not 0"); The above code will not execute like the programmer thought, regardless of the indentation. The else will be paired with the if (j==0) NOT the if (i == 0). Michael Eckmann - Skidmore College - CS Fall 2006

another subtlety of the if / else An else can be forced by the programmer to pair up with a different (not the immediately preceding) if by using curly braces. if (i == 0) { if (j == 0) System.out.println("both i and j are 0"); } else System.out.println("i is not 0"); The above code will now execute like the programmer intended. Michael Eckmann - Skidmore College - CS Fall 2006