CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:

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.
2 Data and Expressions Software Solutions Lewis & Loftus java
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.
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.
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.
CSE 1201 Object Oriented Programming Using Classes and Objects.
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.
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:
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Variables in Java A variable holds either
Project 1.
Chapter 2 Data and Expressions
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
Chapter 2 Data and Expressions
Escape Sequences What if we wanted to print the quote character?
Increment and Decrement
Chapter 2 Data and Expressions.
Chapter 2 Data and Expressions.
CSC 1051 – Data Structures and Algorithms I
Reading Input and Scanner Class
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
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 4: Interactive programs

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

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

4 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

5 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

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

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

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

9 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

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

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

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 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 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 For example, in a particular situation we may want to treat an integer as a floating point value These conversions do not change the type of a variable or the value that's stored in it – they only convert a value as part of a computation Copyright © 2012 Pearson Education, Inc.

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

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

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 = ; 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.

23 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); } First casting occurs, copy of value in qp changed to double Then promotion occurs to credits, i.e. copy of value in credits is changed to double

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

Interactive Programs The Scanner class has methods for reading input We set up a “ Scanner object” to read input from various sources, including the user typing values on the keyboard Scanner scan = new Scanner (System.in); –Keyboard input is represented by the System.in object Copyright © 2012 Pearson Education, Inc.

Reading Input Once created, the Scanner object can be used to invoke various input methods, such as: answer = scan.nextLine(); Copyright © 2012 Pearson Education, Inc.

Reading Input The Scanner class is part of the java.util class library, and must be imported into a program to be used The nextLine method reads all of the input until the end of the line is found See Echo.java 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. //******************************************************************** // 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 + "\""); } Sample Run Enter a line of text: You want fries with that? You entered: "You want fries with that?"

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 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. 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. 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); } Sample Run Enter the number of miles: 328 Enter the gallons of fuel used: 11.2 Miles Per Gallon:

34 Solution 5 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. 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); }

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 as you can Next class we will have a quiz and continue working on interactive programs (section 2.6) Optional: me a 10 min quiz for next Wednesday CSC 1051 M.A. Papalaskari, Villanova University