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

Slides:



Advertisements
Similar presentations
CSC 1051 – Algorithms and Data Structures I
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
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.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
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.
© 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.
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:
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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:
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.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
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:
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:
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:
Using Classes and Objects Chapters 3 Creating Objects – Section 3.1 The String Class – Section 3.2 The Scanner Class – Section 2.6 Instructor: Scott Kristjanson.
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.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
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.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures.
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.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
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.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
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.
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)
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?
Introduction to Classes and Methods
Fundamentals 2.
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 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus CSC 1051 M.A. Papalaskari, Villanova University Basics of Java Programming variables, assignment, and input

int sum; double milesPerGallon; String name, petName; 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 data type variable name CSC 1051 M.A. Papalaskari, Villanova University

Java Primitive Data Types integers: –byte, short, int, long floating point numbers: –float, double characters: –char Logical values (true/false): –boolean CSC 1051 M.A. Papalaskari, Villanova University

Assignment Statement Changes the value of a variable The assignment operator is the = sign total = 55 - discount; 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

Combined declaration and assignment A variable can be given an initial value in the declaration int age = 18; double x = 3.2, y = -0.80; String name = scan.nextLine(); CSC 1051 M.A. Papalaskari, Villanova University

Combined declaration and assignment A variable can be given an initial value in the declaration - a new value can be assigned later: int age = 18; double x = 3.2, y = -0.80; String name = scan.nextLine(); age = 19; x = x + 0.5; name = scan.nextLine(); CSC 1051 M.A. Papalaskari, Villanova University

Combined declaration and assignment – CANNOT declare twice A variable can be given an initial value in the declaration - a new value can be assigned later: int age = 18; double x = 3.2, y = -0.80; String name = scan.nextLine(); int age = 19; CSC 1051 M.A. Papalaskari, Villanova University Error: declaring variable age again

CONSTANTS: like variables, but value cannot change – declare using final modifier: final int INCHES_PER_FOOT = 12; final double LBS_PER_KG = 2.2; CSC 1051 M.A. Papalaskari, Villanova University Convention: Use UPPER_CASE identifiers

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

Division and Remainder If both operands are integers, the division result is an integer (the fractional part is discarded): % gives the remainder of the division: 14 / 3 8 / % 3 8 % 12 CSC 1051 M.A. Papalaskari, Villanova University 143 / / % % 16

Operator Precedence result = total + count / max - offset; Order of evaluation: 1.Multiplication, division, remainder 2.addition, subtraction, string concatenation –Operators with the same precedence: left  right –Use parentheses to override default order CSC 1051 M.A. Papalaskari, Villanova University

Examples int x = 5; System.out.println ("Ans= " + (x + 1)); System.out.println ("Ans= " + x + 1); System.out.println ("Ans= " + (x + 1 * 3)); System.out.println ("Ans= " + ((x + 1) * 3)); System.out.println ("Ans= " + (x / 3 / 2)); System.out.println ("Ans= " + (x / (3 / 2))); CSC 1051 M.A. Papalaskari, Villanova University

More examples a + b + c + d + e a – b / c + d * e a / (b + c) - d % e a / (b * (c + (d - e))) CSC 1051 M.A. Papalaskari, Villanova University

Assignment Revisited lower precedence than the arithmetic operators evaluated right  left 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

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 operator ( ++ ) adds one to its operand The decrement operator ( -- ) subtracts one from its operand The statement count++; is functionally equivalent to count = count + 1; CSC 1051 M.A. Papalaskari, Villanova University

Assignment operator Assignment ( = ) copies the value of the right side into the memory location associated with the left side It does not set up an ongoing equivalence int davesAge = 21; int suesAge = davesAge; davesAge = 22; System.out.println (davesAge); // prints 22 System.out.println (suesAge); // prints 21 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); Copyright © 2012 Pearson Education, Inc. Scanner object keyboard input

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 Copyright © 2012 Pearson Education, Inc. Scanner object

Reading Input 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 ) 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 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:

Homework Read Sections 2.1 – 2.4 and 2.6 –Always do all self-review exercises when you review material Do Exercises EX 2.2 – 2.7 and 2.11 CSC 1051 M.A. Papalaskari, Villanova University