CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

Slides:



Advertisements
Similar presentations
CSC 1051 – Algorithms and Data Structures I
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.
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.
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.
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
String Escape Sequences
Expressions, Data Conversion, and Input
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyCopyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 M.A. Papalaskari, Villanova University Conditional Statements Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
Mathematical Calculations in Java Mrs. C. Furman.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CSC 1051 M.A. Papalaskari, Villanova University Algorithms Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Doing math In java.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
© 2006 Pearson Education Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
CSC 1051 – Data Structures and Algorithms I
Data Conversion & Scanner Class
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?
Lecture 3 Expressions Richard Gesick.
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Building Java Programs
CSC 1051 – Data Structures and Algorithms I
Data Types and Expressions
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: CSC 1051 M.A. Papalaskari, Villanova University Lecture 3: Variables and assignment

Last time: Lab 1: –Get familiar with jGrasp programming environment –Try some example programs –Modify programs to create your own –Learn about string literals, concatenation, and escape sequences –Explore Java syntax –Experience some errors! CSC 1051 M.A. Papalaskari, Villanova University

3 Today: Problem Solving Create a program that will help us calculate a grade point average (GPA) given the number of quality points (QP) and the number of credits. The appropriate formula is GPA = QP / credits We assume A, B, C, D, F grading system. CSC 1051 M.A. Papalaskari, Villanova University

4 For Example CourseCreditsGradeQPs Underwater Basket Weaving3A = 412 Main Line Boutiques3B = 3 9 Winning the Hoops Lottery3C = 2 6 Web Surfing3B = 3 9 Alg and Data Structures4A = 416 Totals1652 GPA = 52 / 16 = 3.25 CSC 1051 M.A. Papalaskari, Villanova University

5 Topic Thread 2.1 Character Strings 2.2 Variables, Assignment 2.3 Data Types, in particular int, double 2.4 Expressions (simple) 2.5 Data Conversion 2.6 Interactive Programs 5.1 Boolean Expressions 5.2 The if Statement 5.4 The while Statement CSC 1051 M.A. Papalaskari, Villanova University

6 The GPA Problem Solution 1 Not very exciting, is it? Let’s add some storage (remember our model of computing) //********************************************** // GPA01.java // // Prints out a QPA //********************************************** public class GPA01 { public static void main (String[ ] args) { System.out.println ("Quality Points: 52"); System.out.println ("Credits: 16"); System.out.println (“\n\tGPA: 3.25"); } CSC 1051 M.A. Papalaskari, Villanova University

7 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 credits; int count, temp, result; Multiple variables can be created in one declaration data type variable name CSC 1051 M.A. Papalaskari, Villanova University

8 Variable Initialization 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; CSC 1051 M.A. Papalaskari, Villanova University

9 Assignment Statement Changes the value of a variable The assignment operator is the = sign total = 55 - discount; The old value that was in total is overwritten See Geometry.java (page 68)Geometry.java The expression on the right is evaluated and the result is stored in the variable on the left CSC 1051 M.A. Papalaskari, Villanova University

10 Assignment operator Assignment ( = ) copies the value of the right side into the memory location associated with the left side It does not (for primitive types) set up an ongoing equivalence int davesAge = 21; int suesAge; suesAge = davesAge; davesAge = 22; System.out.println (davesAge); // prints 22 System.out.println (suesAge); // prints 21 Tracing program code is an important skill !! CSC 1051 M.A. Papalaskari, Villanova University

11 Primitive Data There are eight primitive data types in Java Four of them represent integers: –byte, short, int, long Two of them represent floating point numbers: –float, double One of them represents characters: –char And one of them represents boolean values: –boolean CSC 1051 M.A. Papalaskari, Villanova University

12 Numeric Primitive Data 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 CSC 1051 M.A. Papalaskari, Villanova University

13 Solution 2 Still not very exciting, is it? Let’s add some processing //******************************************** // GPA02.java // // Prints out a GPA //******************************************** public class GPA02 { public static void main (String[ ] args) { int qp = 52; int credits = 16; double gpa = 3.25; System.out.println ("Quality Points: " + qp); System.out.println ("Credits: " + credits); System.out.println (); System.out.println (“\tGPA: " + gpa); } CSC 1051 M.A. Papalaskari, Villanova University

14 Expressions An expression is a combination of one or more operators and operands Arithmetic expressions compute numeric results and make use of the arithmetic operators: If either or both operands used by an arithmetic operator are floating point, then the result is a floating point Addition Subtraction Multiplication Division Remainder +-*/%+-*/% CSC 1051 M.A. Papalaskari, Villanova University

15 Division and Remainder If both operands to the division operator ( / ) are integers, the result is an integer (the fractional part is discarded) The remainder operator (%) returns the remainder after dividing the second operand into the first 14 / 3 equals 8 / 12 equals % 3 equals 8 % 12 equals 2 8 CSC 1051 M.A. Papalaskari, Villanova University

16 Operator Precedence Operators can be combined into complex expressions result = total + count / max - offset; Operators have a well-defined precedence which determines the order in which they are evaluated Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation Arithmetic operators with the same precedence are evaluated from left to right, but parentheses can be used to force the evaluation order CSC 1051 M.A. Papalaskari, Villanova University

17 Operator Precedence What is the order of evaluation in the following expressions? a + b + c + d + e 1432 a + b * c - d / e 3241 a / (b + c) - d % e 2341 a / (b * (c + (d - e))) 4123 CSC 1051 M.A. Papalaskari, Villanova University

18 Assignment Revisited The assignment operator has a lower precedence than the arithmetic operators First the expression on the right hand side of the = operator is evaluated Then the result is stored in the variable on the left hand side answer = sum / 4 + MAX * lowest; 1432 CSC 1051 M.A. Papalaskari, Villanova University

19 Assignment Revisited The right and left hand sides of an assignment statement can contain the same variable First, one is added to the original value of count Then the result is stored back into count (overwriting the original value) count = count + 1; CSC 1051 M.A. Papalaskari, Villanova University

Increment and Decrement The increment and decrement operators use only one operand The increment operator ( ++ ) adds one to its operand The decrement operator ( -- ) subtracts one from its operand The statement count++; is functionally equivalent to count = count + 1; 20 CSC 1051 M.A. Papalaskari, Villanova University

Increment and Decrement The increment and decrement operators can be applied in postfix form: count++ or prefix form: ++count When used as part of a larger expression, the two forms can have different effects Because of their subtleties, the increment and decrement operators should be used with care 21 CSC 1051 M.A. Papalaskari, Villanova University

22 Solution 3 A little more interesting but... What happened to the output? //******************************************** // GPA03.java // // Prints out a GPA //******************************************** public class GPA03 { public static void main (String[ ] args) { int qp = 52; int credits = 16; double gpa = qp / credits; System.out.println ("Quality Points: " + qp); System.out.println ("Credits: " + credits); System.out.println (); System.out.println (“\tGPA: " + gpa); } CSC 1051 M.A. Papalaskari, Villanova University

Summary Variables Data types Assignment operator Arithmetic operators Operator precedence CSC 1051 M.A. Papalaskari, Villanova University

Homework Review Sections –Always do all self-review exercises when you review material Do Exercises EX 2.6, 2.7, 2.8, 2.11 Read Sections 2.5, 2.6 to prepare for next class Some slides adapted from a presentation by Daniel Joyce and from the slides accompanying Java Software Solutions by Lewis & Loftus Caveman image is from CSC 1051 M.A. Papalaskari, Villanova University