CSC 1051 – Data Structures and Algorithms I

Slides:



Advertisements
Similar presentations
CSci 1130 Intro to Programming in Java
Advertisements

CSC 1051 – Algorithms and Data Structures I
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.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
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.
Chapter Day 4. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 4 Return Signed Contracts Questions from last Class?? Problem set 1 Posted.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
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.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
© 2004 Pearson Addison-Wesley. All rights reserved Lecture 2  Chapter 2 CS0401: Intermediate Java Programming.
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.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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:
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
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.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 2 Data and Expressions Java Software Solutions Foundations of Program Design 1.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs.
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.
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:
Data and Expressions Let's explore some other fundamental programming concepts Chapter 2 focuses on: –character strings –primitive data –the declaration.
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
2-1 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.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Primitive Data Types 1 In PowerPoint, point at the speaker icon, then click the "Play" button.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
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:
Chapter 2 Data and Expressions 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights.
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
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:
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.
CompSci 230 S Programming Techniques
Project 1.
Chapter 2 Data and Expressions
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Data Conversion & Scanner Class
CSC 1051 – Data Structures and Algorithms I
Multiple variables can be created in one declaration
Chapter 2 Data and Expressions
Escape Sequences What if we wanted to print the quote character?
Increment and Decrement
Chapter 2 Data and Expressions.
Escape Sequences Some Java escape sequences: See Roses.java (page 68)
Objects and Primitive Data
Chapter 2 Data and Expressions.
CSC 1051 – Data Structures and Algorithms I
Chap 2. Identifiers, Keywords, and Types
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

CSC 1051 – Data Structures and Algorithms I Lecture 4: Interactive programs 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/ CSC 1051 M.A. Papalaskari, Villanova University

Last time: Variables Assignment operator Primitive data types Arithmetic operators 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. CSC 1051 M.A. Papalaskari, Villanova University

Today: More on: data types assignment operators Interactive programs Input using the Scanner class CSC 1051 M.A. Papalaskari, Villanova University

Topic Thread 2.1 Character Strings 2.2 Variables, Assignment 2.3 Data Types 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

The GPA Problem Solution 1 //********************************************** // 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"); } The GPA Problem Solution 1 Not very exciting, is it? Let’s add some storage (remember our model of computing) CSC 1051 M.A. Papalaskari, Villanova University

Solution 3 //******************************************** // 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); } Solution 3 CSC 1051 M.A. Papalaskari, Villanova University

Quick Check What are the results of the following expressions? 12 / 2 12.0 / 2.0 10 / 4 10 / 4.0 4 / 10 4.0 / 10 12 % 3 10 % 3 3 % 10 Copyright © 2012 Pearson Education, Inc.

Quick Check What are the results of the following expressions? 12 / 2 12.0 / 2.0 10 / 4 10 / 4.0 4 / 10 4.0 / 10 12 % 3 10 % 3 3 % 10 = 6 = 6.0 = 2 = 2.5 = 0 = 0.4 = 1 = 3 Copyright © 2012 Pearson Education, Inc.

Solution 3 A little more interesting but ... //******************************************** // 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); } Solution 3 A little more interesting but ... What happened to the output? CSC 1051 M.A. Papalaskari, Villanova University

Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes Copyright © 2012 Pearson Education, Inc.

Assignment Conversion Assignment conversion occurs when a value of one type is assigned to a variable of another Example: int dollars = 20; double money = dollars; Only widening conversions can happen via assignment Copyright © 2012 Pearson Education, Inc.

Converting from one type to another Widening conversions small data type  larger one (eg int to an double) Narrowing conversions large data type  smaller one (eg double to a int) can lose information! Copyright © 2012 Pearson Education, Inc.

Converting from one type to another Widening conversions small data type  larger one (eg int to an double) Narrowing conversions large data type  smaller one (eg double to a int) can lose information! In Java, data conversions can occur in three ways: assignment conversion promotion casting Copyright © 2012 Pearson Education, Inc.

Converting from one type to another Widening conversions small data type  larger one (eg int to an double) Narrowing conversions large data type  smaller one (eg double to a int) can lose information! In Java, data conversions can occur in three ways: assignment conversion promotion casting double money; money = 3; money = 5 / 2.0; money = (double) 8; Copyright © 2012 Pearson Education, Inc.

Assignment Conversion Assignment conversion occurs when a value of one type is assigned to a variable of another Example: int dollars = 20; double money = dollars; Only widening conversions can happen via assignment Copyright © 2012 Pearson Education, Inc.

Promotion Promotion happens automatically when operators in expressions convert their operands Example: int count = 12; double sum = 490.27; result = sum / count; The value of count is converted to a floating point value to perform the division calculation Copyright © 2012 Pearson Education, Inc.

Casting Casting forces a change of type, even if information is lost Both widening and narrowing conversions To cast, the type is put in parentheses in front of the value being converted: int total = 50; float result = (float) total / 6; Without the cast, the fractional part of the answer would be lost Copyright © 2012 Pearson Education, Inc.

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

Numeric Primitive Data The difference between the numeric primitive types is their size and the values they can store: Type byte short int long float double Storage 8 bits 16 bits 32 bits 64 bits Min Value -128 -32,768 -2,147,483,648 < -9 x 1018 +/- 3.4 x 1038 with 7 significant digits +/- 1.7 x 10308 with 15 significant digits Max Value 127 32,767 2,147,483,647 > 9 x 1018 Copyright © 2012 Pearson Education, Inc.

Characters A char variable stores a single character Character literals are delimited by single quotes: 'a' 'X' '7' '$' ',' '\n' Example declarations: char topGrade = 'A'; char terminator = ';', separator = ' '; Note the difference between a primitive character variable, which holds only one character, and a String object, which can hold multiple characters Copyright © 2012 Pearson Education, Inc.

Character Sets A character set is an ordered list of characters, with each character corresponding to a unique number A char variable in Java can store any character from the Unicode character set 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 Copyright © 2012 Pearson Education, Inc.

Characters The ASCII character set is older and smaller than Unicode, but is still quite popular 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, ... Copyright © 2012 Pearson Education, Inc.

Boolean A boolean value represents a true or false condition The reserved words true and false are the only valid values for a boolean type boolean done = false; A boolean variable can also be used to represent any two states, such as a light bulb being on or off Copyright © 2012 Pearson Education, Inc.

Data Conversion Widening Conversions Narrowing Conversions Copyright © 2012 Pearson Education, Inc.

//******************************************************************** // GPA04.java Author: Joyce // // Prints out a GPA public class GPA04 { public static void main (String[] args) int qp = 52; int credits = 16; double gpa = (double) qp / credits; System.out.println ("Quality Points: " + qp); System.out.println ("Credits: " + credits); System.out.println (); System.out.println (“\tGPA: " + gpa); } Solution 4

double gpa = (double) qp / credits; . Solution 4 int qp = 52; int credits = 16; double gpa = (double) qp / credits; . System.out.println ("Quality Points: " + qp); System.out.println ("Credits: " + credits); System.out.println (); Then promotion occurs to make both expressions the same type - so, value from credits is changed to double First casting occurs, value from qp changed to double

double gpa = qp / credits; . Solution 3 int qp = 52; int credits = 16; double gpa = qp / credits; . Previous version No casting int division Promotion occurs after the division --too late!

Solution 4 That’s better ... Let’s add input //******************************************************************** // GPA04.java Author: Joyce // // Prints out a GPA public class GPA04 { public static void main (String[] args) int qp = 52; int credits = 16; double gpa = (double) qp / credits; System.out.println ("Quality Points: " + qp); System.out.println ("Credits: " + credits); System.out.println (); System.out.println (“\tGPA: " + gpa); } Solution 4 That’s better ... Let’s add input

Topic Thread 2.1 Character Strings 2.2 Variables, Assignment 2.3 Data Types 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

Interactive Programs The Scanner class has methods for reading input We declare a Scanner object to read input from the keyboard: Scanner scan = new Scanner (System.in); keyboard input Scanner object Copyright © 2012 Pearson Education, Inc.

answer = scan.nextLine(); Reading Input Once created, the Scanner object can be used to invoke various input methods, such as: answer = scan.nextLine(); The nextLine method reads all of the input until the end of the line is found Scanner object Copyright © 2012 Pearson Education, Inc.

Reading Input import java.util.Scanner; The Scanner class is part of the java.util class library, and must be imported into a program to be used The import statement goes at beginning of your program (above class definition) import java.util.Scanner; (See Echo.java ) The details of object creation and class libraries are discussed further in Chapter 3 Copyright © 2012 Pearson Education, Inc.

//******************************************************************** // Echo.java Author: Lewis/Loftus // // Demonstrates the use of the nextLine method of the Scanner class // to read a string from the user. import java.util.Scanner; public class Echo { //----------------------------------------------------------------- // Reads a character string from the user and prints it. public static void main (String[] args) String message; Scanner scan = new Scanner (System.in); System.out.println ("Enter a line of text:"); message = scan.nextLine(); System.out.println ("You entered: \"" + message + "\""); } Copyright © 2012 Pearson Education, Inc.

Sample Run Enter a line of text: You want fries with that? You entered: "You want fries with that?" //******************************************************************** // Echo.java Author: Lewis/Loftus // // Demonstrates the use of the nextLine method of the Scanner class // to read a string from the user. import java.util.Scanner; public class Echo { //----------------------------------------------------------------- // Reads a character string from the user and prints it. public static void main (String[] args) String message; Scanner scan = new Scanner (System.in); System.out.println ("Enter a line of text:"); message = scan.nextLine(); System.out.println ("You entered: \"" + message + "\""); } Copyright © 2012 Pearson Education, Inc.

Reading in numbers nextInt and reads in an integer: Example: age = scan.nextInt(); nextDouble similar method for type double White space (space, tab, new line) can be used to separate input tokens next reads the next input token and returns it as a string See GasMileage.java Copyright © 2012 Pearson Education, Inc.

//******************************************************************** // GasMileage.java Author: Lewis/Loftus // // Demonstrates the use of the Scanner class to read numeric data. import java.util.Scanner; public class GasMileage { //----------------------------------------------------------------- // Calculates fuel efficiency based on values entered by the // user. public static void main (String[] args) int miles; double gallons, mpg; Scanner scan = new Scanner (System.in); continue Copyright © 2012 Pearson Education, Inc.

System.out.print ("Enter the number of miles: "); continue System.out.print ("Enter the number of miles: "); miles = scan.nextInt(); System.out.print ("Enter the gallons of fuel used: "); gallons = scan.nextDouble(); mpg = miles / gallons; System.out.println ("Miles Per Gallon: " + mpg); } Copyright © 2012 Pearson Education, Inc.

Sample Run Enter the number of miles: 328 Enter the gallons of fuel used: 11.2 Miles Per Gallon: 29.28571428571429 continue System.out.print ("Enter the number of miles: "); miles = scan.nextInt(); System.out.print ("Enter the gallons of fuel used: "); gallons = scan.nextDouble(); mpg = miles / gallons; System.out.println ("Miles Per Gallon: " + mpg); } Copyright © 2012 Pearson Education, Inc.

Solution 5 Note: Opening comment removed to allow better viewing. import java.util.Scanner; public class GPA05 { public static void main (String[] args) int qp; int credits; double gpa; Scanner scan = new Scanner(System.in); // get input System.out.print ("Enter Quality Points > "); qp = scan.nextInt(); System.out.print ("Enter Credits > "); credits = scan.nextInt(); // calculate gpa gpa = (double) qp / credits; // output information System.out.println (“\nQuality Points: " + qp); System.out.println ("Credits: " + credits); System.out.println (“\n\tGPA: " + gpa); } Note: Opening comment removed to allow better viewing. Now we are cooking. But, what if you haven’t taken any classes? Next we will add some control, i.e. some decision making capability.

Homework Read Sections 2.5, 2.6 Always do all self-review exercises when you review material Do Exercises EX 2.10 and as many of PP 2.2-2.8 as you can CSC 1051 M.A. Papalaskari, Villanova University