CSC 1051 M.A. Papalaskari, Villanova University Algorithms Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:

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.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Introduction to Computers and Programming Lecture 5 New York University.
Introduction to Computer Programming Decisions If/Else Booleans.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Algorithm development. The invention of the computer  Programming language developments: 1. Machine code 2. Assembler  easier to write, debug, and update.
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:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 5 Loops.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
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 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:
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
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:
Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
5-1 Chapter 5: Conditionals and Loops Topics: –Boolean expressions –Conditional statements –Increment and Decrement Operators (Chapter 2.4) –Repetition.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution,
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 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
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:
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 Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
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:
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.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
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:
Testing and Debugging UCT Department of Computer Science Computer Science 1015F Hussein Suleman March 2009.
CSC111 Quick Revision.
CSC1401 Input and Output (and we’ll do a bit more on class creation)
CSC 1051 – Data Structures and Algorithms I
CSC 1051 – Data Structures and Algorithms I
Chapter 6 More Conditionals and Loops
CSC 1051 – Data Structures and Algorithms I
SELECTION STATEMENTS (1)
Chapter 6 More Conditionals and Loops
1) C program development 2) Selection structure
Repetition Statements
CSC 1051 – Data Structures and Algorithms I
Outline Software Development Activities
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

CSC 1051 M.A. Papalaskari, Villanova University Algorithms 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 or from Dr. Daniel Joyce’s slides for this course. CSC 1051 – Algorithms and Data Structures I

Source: CSC 1051 M.A. Papalaskari, Villanova University Algorithms in everyday life

Algorithm Example Statement of GPA problem: Write a program that inputs the credits and quality points earned and outputs the gpa. CSC 1051 M.A. Papalaskari, Villanova University variables: qp, credits, gpa Algorithm: 1.Input qp 2.Input credits 3.gpa = qp / credits 4.Print gpa

Algorithms An algorithm is a specific set of instructions for carrying out a procedure or solving a problem, usually with the requirement that the procedure terminate at some point. Specific algorithms sometimes also go by the name method, procedure, or technique. The word "algorithm" is a distortion of al-Khwārizmī, a Persian mathematician who wrote an influential treatise about algebraic methods.methodproceduretechnique Sources: and Wikipedia ( ) CSC 1051 M.A. Papalaskari, Villanova University

Java Program  //************************************************************* // GPA.java Author: Joyce/Papalaskari // Demonstrates the use of Scanner input and simple computation. //************************************************************* import java.util.Scanner; public class GPA { public static void main (String[] args) // // Inputs the quality points and credits and calculates GPA. // { double qp, credits, 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(); // output information entered System.out.println ("\nQuality Points: " + qp); System.out.println ("Credits: " + credits); // calculate and output GPA gpa = qp / credits; System.out.println ("\n\tGPA: " + gpa); } variables: qp, credits, gpa Algorithm: 1.Input qp 2.Input credits 3.gpa = qp / credits 4.Print gpa CSC 1051 M.A. Papalaskari, Villanova University

Pseudocode: a way to describe what an algorithm does without writing a program. CSC 1051 M.A. Papalaskari, Villanova University variables: qp, credits, gpa Algorithm: 1.Input qp 2.Input credits 3.gpa = qp / credits 4.Print gpa

Writing an algorithm in pseudocode List the variables used. List the steps for solving the problem, in order. Try to be brief and unambiguous; use Java expressions only when it is simpler to specify a step in java than in English. CSC 1051 M.A. Papalaskari, Villanova University variables: qp, credits, gpa Algorithm: 1.Input qp 1.Input credits 1.gpa = qp / credits 1.Print gpa

Writing an algorithm in pseudocode List the variables used. List the steps for solving the problem, in order. Try to be brief and unambiguous; use Java expressions only when it is simpler to specify a step in java than in English. CSC 1051 M.A. Papalaskari, Villanova University variables: qp, credits, gpa (use floating point) Algorithm: 1.Input qp 1.Input credits 1.gpa = qp / credits (Note: use floating point division) 2.Print gpa When the type is not obvious you can add a note.

Another example: CSC 1051 M.A. Papalaskari, Villanova University Write an application that reads values representing a time duration in hours, minutes, and seconds and then prints the equivalent total number of seconds. (For example, 1 hour, 28 minutes, and 42 seconds is equivalent to 5322 seconds.) PP 2.8 (textbook, Chapter 2 Programming projects)

Can we reverse this calculation? CSC 1051 M.A. Papalaskari, Villanova University Create a version of the previous project that reverses the computation. That is, read a value representing a number of seconds, then print the equivalent amount of time as a combination of hours, minutes, and seconds. (For example, 9999 seconds is equivalent to 2 hours, 46 minutes, and 39 seconds.) The next 3 slides will help us visualize this problem. PP 2.9 (textbook, Chapter 2 Programming projects)

Algorithm for PP 2.9 CSC 1051 M.A. Papalaskari, Villanova University

How many of each can you pack in the black box?

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

Java Program  //************************************************************* // GPA.java Author: Joyce/Papalaskari // Demonstrates the use of Scanner input and simple computation. //************************************************************* import java.util.Scanner; public class GPA { public static void main (String[] args) // // Inputs the quality points and credits and calculates GPA. // { double qp, credits, 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(); // output information entered System.out.println ("\nQuality Points: " + qp); System.out.println ("Credits: " + credits); // calculate and output GPA gpa = qp / credits; System.out.println ("\n\tGPA: " + gpa); } variables: qp, credits, gpa Algorithm: 1.Input qp 2.Input credits 3.gpa = qp / credits 4.Print gpa CSC 1051 M.A. Papalaskari, Villanova University What if credits = 0 ???? Previous Example

Updated Algorithm CSC 1051 M.A. Papalaskari, Villanova University variables: qp, credits, gpa Algorithm: 1.Input qp 2.Input credits 3.if credits = 0 Print “No gpa yet” else gpa = qp / credits Print gpa 4.Print gpa goodbye message

variables: qp, credits, gpa Algorithm: 1.Input qp 2.Input credits 3.if credits = 0 Print “No gpa yet” else gpa = qp / credits Print gpa 4.Print gpa goodbye message Java code if (credits == 0) System.out.println (“\n\tGPA: None"); else { gpa = qp / credits; System.out.println (“\n\tGPA: " + gpa); } CSC 1051 M.A. Papalaskari, Villanova University

//************************************************************* // GPA_updated.java Author: Joyce/Papalaskari // // Demonstrates the use of conditional statements. //************************************************************* import java.util.Scanner; public class GPA_Updated { public static void main (String[] args) // // Reads the quality points and credits and calculates GPA. // { double qp, credits, 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(); // output information entered System.out.println ("\nQuality Points: " + qp); System.out.println ("Credits: " + credits); // calculate and output GPA, if possible if (credits == 0) System.out.println ("\n\tGPA: None"); else { gpa = qp / credits; System.out.println ("\n\tGPA: " + gpa); } // Print goodbye message System.out.println ("Goodbye and thank you for using my GPA calculator."); } CSC 1051 M.A. Papalaskari, Villanova University Updated program  variables: qp, credits, gpa Algorithm: 1.Input qp 2.Input credits 3.if credits = 0 Print “No gpa yet” else gpa = qp / credits Print gpa 4.Print goodbye message

Conditional statements alter the linear flow of control. They use boolean expressions to determine what to do next. Example: if (credits == 0) System.out.println ("GPA: None"); else { gpa = qp / credits; System.out.println ("\n\tGPA: " + gpa); } CSC 1051 M.A. Papalaskari, Villanova University A boolean expression

Control flow Sequence of statements that are actually executed in a program Conditional and Repetition statements: enable us to alter control flow CSC 1051 M.A. Papalaskari, Villanova University statement 1 statement 2 statement 3 statement 4 boolean 1boolean 2 statement 1 statement 2 true false statement 3 This slide dapted from Doug Clark’s course

Java relational operators relational operators can be used with numeric types and produce boolean results: == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to Note the difference between the equality operator ( == ) and the assignment operator ( = ) CSC 1051 M.A. Papalaskari, Villanova University

Boolean Expressions The reserved words true and false are the only valid values for a boolean type Example boolean declarations: A boolean expression using a relational operator CSC 1051 M.A. Papalaskari, Villanova University boolean aboveAgeLimit = false; boolean usePlural = hours > 1;

Example An if statement with its boolean condition: if (hours != 1) System.out.print("s"); Another way, using a boolean variable: boolean usePlural = hours != 1; if (usePlural) System.out.print("s"); See also Age.javaAge.java CSC 1051 M.A. Papalaskari, Villanova University