COMP 110: Spring 20091 Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson January 26, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
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.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
COMP 110 Primitive Types, Strings, and Console I/O Tabitha Peck M.S. January 23, 2008 MWF 3-3:50 pm Philips
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Using Data.
COMP String and Console I/O Yi Hong May 18, 2015.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
C++ Programming: Basic Elements of C++.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
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.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by
Chapter 2 Variables.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
COMP Primitive and Class Types Yi Hong May 14, 2015.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
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.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
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.
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.
CS 106A, Lecture 4 Introduction to Java
Chapter 2 Variables.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Java Programming: From Problem Analysis to Program Design, 4e
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
Expressions and Assignment
elementary programming
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2 Variables.
Presentation transcript:

COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today

COMP 110: Spring Questions? Last week’s lecture: Declarations Variables Assignments Operators Pseudocode

COMP 110: Spring Today in COMP 110 Type Casting More Operators Strings Console I/O

COMP 110: Spring From Last Time Type Name Kind of Value Memory Used Range of Values byteInteger1 byte-128 to 127 shortInteger2 bytes-32,768 to 32,768 intInteger4 bytes-2,147,483,648 to 2,147,483,647 longInteger8 bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 floatFloating-point4 bytes± x to ± x doubleFloating-point8 bytes± x to ± x charCharacter2 bytes0 to 65,535 boolean 1 bitTrue or False (0 to 1)

COMP 110: Spring Assignment Compatibilities You cannot store a value of one type in a variable of another type int i = 0.1; //this does not compile! However, some types can be converted into other types double d = 1; //1 is automatically converted to 1.0

COMP 110: Spring Assignment Compatibilities byte » short » int » long » float » double A value of any type in the list can be assigned to a type further down the list Example int iVar = 7; double dVar = iVar; //this is legal

COMP 110: Spring Assignment Compatibilities In some cases, you may want to override the assignment compatibilities This can be done using type casting

COMP 110: Spring Type Casting Syntax (Type_Name) Expression Example double distance = 9.0; int points = (int)distance; //cast distance to an int //distance is not changed in any way //the value of points will be 9

COMP 110: Spring Type Casting Type casting is not rounding! The effect of a type cast is truncation Any fractional portion is discarded Example double bill = 25.75; int dollars = (int)bill ; //cast bill to an int //bill is not changed in any way //the value of dollars will be 25

COMP 110: Spring From Last Time Operators Addition (+) a + b Subtraction (-) a - b Multiplication (*) a * b Division (/) a / b Remainder or Modulo (%) a % b

COMP 110: Spring Parentheses and Precedence Parentheses can be used to indicate the order in which operations should be performed Example What is the value of “result”? int a = 3, b = 1, c = 2, result = 0; result = (a + b) * c; result = a + (b * c); result = 8 result = 5

COMP 110: Spring Binary Operators Binary operators take two operands An operand is just a number or variable For example, +,-,*,/ are binary operators i = a + b; i = a - b; i = a * b; i = a / b;

COMP 110: Spring Unary Operators Unary operators take a single operand + and – can also be unary operators i = -7; f = ;

COMP 110: Spring Specialized Assignment Operators += a += 3; //same as a = a + 3; -= a -= 4; //same as a = a – 4; *= a *= 2; //same as a = a * 2; /= a /= 3; //same as a = a / 3;

COMP 110: Spring Increment and Decrement Operators Increment Operator (++) count++; //same as count = count + 1; Decrement Operator (--) count--; //same as count = count – 1;

COMP 110: Spring Increment and Decrement Operators Can also be used in expressions in either prefix (++m) or postfix (m++) form Example n * (m++) Increases m by 1 after the multiplication n * (++m) Increases m by 1 before the multiplication

COMP 110: Spring Increment and Decrement Operators int n = 2; int m = 4; int result = n * (m++); //produces result = 8, m = 5 result = n * (++m); //produces result = 12, m = 6

COMP 110: Spring Operator Precedence Java evaluates expressions in an order specified by precedence rules Certain operations are performed before others Example i = * 3 – 1; // the value of i will be 9

COMP 110: Spring Java Precedence Rules Highest Precedence First: the unary operators +, -, ++, -- Second: the binary operators *, /, % Third: the binary operators +, - Lowest Precedence Binary operators of equal precedence are evaluated from left to right

COMP 110: Spring Precedence Examples -3 * 7 % 3 – 4 – % 3 – 4 – 6 0 – 4 – * (3 % 2 ) / -2 3 * 1 / -2 3 / -2 NOTE: Truncation of division operator on integers

COMP 110: Spring Constants Constants or literals are values that don’t change 2, 6, ‘B’, 5.433e8 NOTE: 5.433e8 means x 10 8 Example uses of constants int i = 2; // i is a variable, 2 is a constant char l = ‘J’; // l is a variable, ‘J’ is a constant double d = 6.1e38; //d is a variable, 6.1e38 is a //constant

COMP 110: Spring Named Constants A constant that is given a name and can be used much like a variable Syntax final Type Variable = Constant; Examples final double PI = ; final int HOURS_PER_DAY = 24;

COMP 110: Spring Strings How to represent sequences of characters in Java programs such as “Enter a whole number from 1 to 99.” Java provides the class “String” for this purpose

COMP 110: Spring Strings Messages like these are called String literals or String constants “Enter a whole number from 1 to 99.” “What is your name?” “I will add two numbers for you.” They have the type “String”

COMP 110: Spring Strings We can declare Strings in Java programs Example String greeting; greeting = “Hello!”; System.out.println(greeting); //prints Hello! to screen System.out.println(“Hello!”); //also prints Hello! to //screen

COMP 110: Spring String Indices Strings consist of a sequence of characters Each character has a position in the String UNCisGreat Positions Characters

COMP 110: Spring String Concatenation We can concatenate two Strings together using the (+) operator Example String name = “Bob”; String greeting = “Hi ” + name; System.out.println(greeting); //prints “Hi Bob” to the screen

COMP 110: Spring String Concatenation We can also concatenate Strings with other data types System.out.println("I am " " years old!"); //prints “I am 21 years old!” to screen

COMP 110: Spring Strings as a Class String is a class type, not a primitive type Class types have both data and methods (actions)

COMP 110: Spring String Length The “length()” method of the String class returns the # of characters in the String String myString = “Hi there!”; int len = myString.length(); Object 9 Method Class

COMP 110: Spring Strings All objects of the same type have the same methods, but may have different data Example String s1 = “First!”; String s2 = “Second!”; s1.length() returns 6 s2.length() returns 7

COMP 110: Spring String Methods string.indexOf(A_String) Returns the index of the first occurrence of A_String in string Example String phrase = “UNC is Great”; int ind = phrase.indexOf(“Great”); UNCisGreat

COMP 110: Spring String Methods string.substring(Start) Returns a new string having the same characters as the substring of string beginning at index Start through the end of string Example String phrase = “UNC is Great”; String sub = phrase.substring(2); UNCisGreat

COMP 110: Spring String Methods Other methods of the String class include string.charAt(index) Returns the character at index in string string.toLowerCase() Returns a new String with the same characters as string, but with any uppercase characters changed to lowercase string.equals(A_String) Returns true if string and A_String are the same, otherwise returns false See tring.html

COMP 110: Spring Quotations Marks in Strings Problem Quotation marks in Java indicate the beginning and end of a String literal String phrase = “Enter a whole number from 1 to 99.”; How to put quotation marks into Strings?

COMP 110: Spring Quotation Marks in Strings Example We’d like to print the message This class is so "fun" But we cannot use System.out.println("This class is so "fun""); because the Java compiler sees it as System.out.println("This class is so "fun"");

COMP 110: Spring Quotation Marks in Strings The way to do this in Java is with the escape character '\' We can use the sequence \" inside a string to indicate a quotation mark Example System.out.println("This class is so \"fun\""); will print to screen This class is so "fun"

COMP 110: Spring Backslashes in Strings If backslash '\' is the escape character, how can I put backslashes into Strings? Use the sequence \\ for this Example System.out.println("This is a backslash \\"); will print to screen This is a backslash \

COMP 110: Spring New Line Sequence The sequence \n starts a new line of text Example System.out.println("Line 1\nLine 2"); will print to screen Line 1 Line 2

COMP 110: Spring Escape Characters \"\" Double quote \'\' Single quote \\Backslash \nNew line \rCarriage return \tTab

COMP 110: Spring String Exercise What is the output? System.out.println("abc\ndef"); abc def System.out.println("abc\\ndef"); abc\ndef

COMP 110: Spring Programming Demo Fahrenheit to Celsius Converter Problem Given a temperature in degrees Fahrenheit, convert to a temperature in degree Celsius

COMP 110: Spring Programming Demo Fahrenheit to Celsius Steps Pseudocode Programming Testing/Debugging

COMP 110: Spring The Algorithm Comes directly from the formula for conversion tempC = (5/9)(tempF – 32)

COMP 110: Spring Fahrenheit to Celsius Pseudocode Ask user for temp in Fahrenheit Convert temp in Fahrenheit to temp in Celsius Output the result