CS100Lecture 21 Announcements For homework due Thursday, work alone -- do not work in pairs New class location: Olin 155 Office hour oops! Lyn: MW, 11:15-12:15.

Slides:



Advertisements
Similar presentations
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Advertisements

Introduction to C Programming
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
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,
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Introduction to C Programming
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
String Escape Sequences
Program Elements -- Introduction
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
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.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
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.
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.
CPS120: Introduction to Computer Science
Jaeki Song ISQS6337 Lecture 01 Introduction. Jaeki Song ISQS6337 Instructor Name: Jaeki Song Office: BA 712 Office Hours Tuesday & Thursday 2:00-3:20.
Flow of Control Part 1: Selection
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.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
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.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 2 Variables.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte, short,
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
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.
1.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
© 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.
Lecture 2 Data Types Richard Gesick.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Variables and Primative Types
The Selection Structure
Java Programming: From Problem Analysis to Program Design, 4e
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
CMSC 202 Java Primer 2.
Tutorial 10: Programming with javascript
Unit 3: Variables in Java
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
CS100A, Fall 1998 This lecture covers the declaration of variables, assignment, and the if statement. While discussing the declaration of variables,
Presentation transcript:

CS100Lecture 21 Announcements For homework due Thursday, work alone -- do not work in pairs New class location: Olin 155 Office hour oops! Lyn: MW, 11:15-12:15 Review Sessions Do not send mail that has html or attachments

CS100Lecture 22 Today’s Topics Brief review of yesterday Variables and constants Declaration of a variable Assignment to a variable Declaration of a constant The conditional (if) statement Input

CS100Lecture 23 Review Algorithms Methods Method call/method invocation Importance of comments -- document your code See the course website for information on Java programming style Any questions on homework yet?

CS100Lecture 24 Think of a variable as a box into which a value can be placed Declarations: int x; int timeOfDay Introduction to Variables 32 x-532 timeOfDay 1115

CS100Lecture 25 What Happens in a Declaration? A variable is a name for a location in memory When a variable is declared you instruct the compiler to reserve enough space for the type of variable you’re declaring A variable can only store one value of its declared type

CS100Lecture 26 Variable Names -- Identifiers Begin with a letter, _, or $ Contain letters, digits, _, and $ Generally, we don’t use _ or $ One convention: first letter small, capitalize words within the identifier –timeOfDay –f12 –f0 –restOfInput

CS100Lecture 27 int timeOfDay = 3; // timeOfDay timeOfDay = 430; // timeOfDay timeOfDay = 30 + timeOfDay + 5; // timeOfDay = 465 Assignment

CS100Lecture 28 Assignment Syntax = ; Note well: “=” does not mean equality in Java Think of x = y as “Assign y to x” or “x becomes y” or “x gets the value of y” Think of x == y as “x equals y” The expression “x==y” yields true if x and y contain the same value, false otherwise

CS100Lecture 29 Booleans Boolean is a data type like int It has two values: true and false boolean answer; int x; int y; x = 3; y = 5; answer = (x == y); What value ends up in the variable “answer” ?

CS100Lecture 210 Some history = was introduced as a sign for equality in the 1500s. It was chosen because nothing could be more equal than 2 parallel lines. The use of = for assignment was started in Fortran in the early 1950s. C and C++ continued this and introduced == for equality. This has caused great confusion and much wasted time on the part of programmers the world over. Algol 60 used := for assignment and = for equality. Remember: concepts, not syntax, but be careful of the syntax!

CS100Lecture 211 Constants If data doesn’t change throughout program, use constant Helpful to name this value final int CS100_STUDENTS = 78; final double PI = ; Use uppercase to distinguish from variables, whose values do change throughout the program

CS100Lecture 212 Naming Conventions Short variable names make programs shorter, and more manageable Long names can convey more meaning Name can almost never give full meaning, so comment when the variable is declared. Avoid names such as: “ thisIsTheVariableThatStoresTheNumberOfBooksInMyLibrary”

CS100Lecture 213 Conditional Statement -- If Conditional statements allows a choice of a command based on some condition // Store the maximum of x and y in z z = x; if (y > x) z = y; Syntax: if (boolean expression) statement

CS100Lecture 214 Block statements in if statements Suppose you would like to execute more than one thing based on the condition? // if x != y, store 0 in x, store y in z if (y != x) { x = 0; z = y; } OR // if x != y, store 0 in x, store y in z if (y != x) { x = 0; z = y; }

CS100Lecture 215 More on block statements Consider: { …. } { and } are used to delimit a sequence of statements that are to act together, just as ( and ) are used in arithmetic expressions Many options for positioning { and }, just be consistent

CS100Lecture 216 Second Form of if statement Provide multiple options: // Store maximum of x and y in z if (x >= y) { z = x; } else { z = y; }

CS100Lecture 217 Example of Nested Ifs if (coin == HEADS) if (choice == RECEIVE) System.out.println(“You won, will receive”); else System.out.println(“You won, will kickoff”); else System.out.println(“You lost.”);

CS100Lecture 218 Operator Syntax = = equal to ! = not equal to < less than > greater than < = less than or equal to > = greater than or equal to

CS100Lecture 219 Operator Precedence * / % then then = / 2 is therefore 18 (not 11) Use parentheses to make things clear: –(14 + 8) / 2 = 11 –Must be an equal number of left and right parens

CS100Lecture 220 Input (briefly) Classes that provide facilities for input and export are available in package java.io Place the phrase import java.io.* at the top of your Java source file Ok if you don’t understand all of this yet Read Section 3.3 in text for details

CS100Lecture 221 Characters and Strings (briefly) char date type –ASCII plus stuff = UNICODE –A character literal uses single quotes: ‘b’ –char firstChar = ‘b’ String is not a primitive type, it’s a Class Strings represented as objects of the String class String manipulation can get complicated -- see text for more details

CS100Lecture 222 Getting Input from user Import java.io.*; // Read a string from the user String message; message = stdin.readLine(); // Read numeric input from the user String string1; int num1; string1 = stdin.readLine(); num1 = Integer.parseInt(string1);

CS100Lecture 223 Example system.out.println(“What’s your name?”); name = stdin.readLine(); system.out.println(“Your name is: ” + name); What is your name? Milly Lunor Your name is: Milly Lunor

CS100Lecture 224 Small Formatting Tricks Java uses the \ in output statements to indicate formatting –\t tab, \n newline, \” double quote, \’ single quote, \\ backslash –“Name\tDOB” results in NameDOB –“foo\n\nbar” results in foo bar

CS100Lecture 225 Discussion Issues Java is strongly-typed. What good is that? Why use constants? Why would you use (a > 0) vs. (a >= 1) Is Y2K a compile-time, run-time or logical error? If a system fails, who is responsible?