INF 523Q Chapter 3: Program Statements (Examples).

Slides:



Advertisements
Similar presentations
Flow of Control Usually the order of statement execution through a method is linear: one after another flow of control: the order statements are executed.
Advertisements

Chapter 3: Program Statements
Loops – While, Do, For Repetition Statements Introduction to Arrays
INF 523Q Chapter 2: Objects and Primitive Data (Examples)
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Java Program Statements
Java Program Statements Selim Aksoy Bilkent University Department of Computer Engineering
Java Program Statements
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Chapter 4 Conditionals and Loops. Chapter Scope Flow of control Boolean expressions if and switch statements Comparing data while, do, and for loops Iterators.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Conditionals and Loops Now we will examine programming statements.
Conditionals and Loops Chapter 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 3: Program Statements
COS 312 DAY 4 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 1 Corrected – 7 A’s – Few minor issues with style and conventions Assignment 2 Posted.
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Chapter 3: Program Statements Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Copyright © 2014 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design 8 th Edition John.
Chapter 5 Loops.
1 5 Conditionals and Loops. 2 Now we will examine programming statements that allow us to: –make decisions –repeat processing steps in a loop Chapter.
Last time We covered: –primitive data types –declaration, initialization, assignment of variables –expressions and operator precedence –data conversions.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Copyright © 2012 Pearson Education, Inc. Lab 8: IF statement …. Conditionals and Loops.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
1 Chapter 3 Selections. 2 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
5-1 Chapter 5: Conditionals and Loops Topics: –Boolean expressions –Conditional statements –Increment and Decrement Operators (Chapter 2.4) –Repetition.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/29 The switch Statement The switch statement provides another way.
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 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 (c) elsaddik CSI 1102 Introduction to Software Design Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE.
Chapter 5 Conditionals and Loops
Loops Copyright © 2012 Pearson Education, Inc.. Conditionals and Loops (Chapter 5) So far, we’ve looked at: –making decisions with if –how to compare.
1 More about Flow of Control. Topics Debugging Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement.
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.
Feedback  Lab2, Hw1  Groups  Group Project Requirements.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
COS 312 DAY 5 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 2 Posted – Due Feb 22 prior to class – Any issues? Assignment 3 will be posted soon Capstones.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Conditionals and Loops Now we will examine programming statements that allow us to: – make decisions – repeat processing steps in a loop Chapter 5 focuses.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Java for Android is specific
CSC 1051 – Data Structures and Algorithms I
Chapter 5 Conditionals and Loops
Chapter 6 More Conditionals and Loops
More Conditionals and Loops
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Outline Boolean Expressions The if Statement Comparing Data
Flow of Control Unless specified otherwise, the order of statement execution through a method is linear: one after another Some programming statements.
Chapter 6 More Conditionals and Loops
Chapter 6 More Conditionals and Loops
Outline Boolean Expressions The if Statement Comparing Data
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
CSC 1051 – Data Structures and Algorithms I
Outline Boolean Expressions The if Statement Comparing Data
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

INF 523Q Chapter 3: Program Statements (Examples)

Age.java b //****************************************************************** b // Age.java Author: Lewis and Loftus b // Demonstrates the use of an if statement. b //****************************************************************** b import cs1.Keyboard; b public class Age b { b // Reads the user's age and prints comments accordingly. b public static void main (String[] args) b { b final int MINOR = 21; b System.out.print ("Enter your age: "); b int age = Keyboard.readInt(); b System.out.println ("You entered: " + age); b if (age < MINOR) b System.out.println ("Youth is a wonderful thing. Enjoy."); b System.out.println ("Age is a state of mind."); b }

Wages.java b //****************************************************************** b // Wages.java Author: Lewis and Loftus b // Demonstrates the use of an if-else statement. b //****************************************************************** b import java.text.NumberFormat; b import cs1.Keyboard; b public class Wages b { b // Reads the number of hours worked and calculates wages. b public static void main (String[] args) b { b final double RATE = 8.25; // regular pay rate b final int STANDARD = 40; // standard hours in a work week b double pay = 0.0; b System.out.print ("Enter the number of hours worked: "); b int hours = Keyboard.readInt(); b System.out.println ();

Wages.java (cont.) b // Pay overtime at "time and a half" b if (hours > STANDARD) b pay = STANDARD * RATE + (hours-STANDARD) * (RATE * 1.5); b else b pay = hours * RATE; b NumberFormat fmt = NumberFormat.getCurrencyInstance(); b System.out.println ("Gross earnings: " + fmt.format(pay)); b }

Guessing.java b //****************************************************************** b // Guessing.java Author: Lewis and Loftus b // Demonstrates the use of a block statement in an if-else. b //****************************************************************** b import cs1.Keyboard; b public class Guessing b { b // Plays a simple guessing game with the user. b public static void main (String[] args) b { b final int MAX = 10; b int answer, guess; b answer = (int) (Math.random() * MAX) + 1; b System.out.print ("I'm thinking of a number between 1 and " b + MAX + ". Guess what it is: "); b guess = Keyboard.readInt(); b

Guessing.java (cont.) b if (guess == answer) b System.out.println ("You got it! Good guessing!"); b else b { b System.out.println ("That is not correct, sorry."); b System.out.println ("The number was " + answer); b }

MinOfThree.java b //****************************************************************** b // MinOfThree.java Author: Lewis and Loftus b // Demonstrates the use of nested if statements. b //****************************************************************** b import cs1.Keyboard; b public class MinOfThree b { b // Reads three integers from the user and determines the smallest value. b public static void main (String[] args) b { b int num1, num2, num3, min = 0; b System.out.println ("Enter three integers: "); b num1 = Keyboard.readInt(); b num2 = Keyboard.readInt(); b num3 = Keyboard.readInt();

MinOfThree.java (cont.) b if (num1 < num2) b if (num1 < num3) b min = num1; b else b min = num3; b else b if (num2 < num3) b min = num2; b else b min = num3; b System.out.println ("Minimum value: " + min); b }

GradeReport.java b //****************************************************************** b // GradeReport.java Author: Lewis and Loftus b // Demonstrates the use of a switch statement. b //****************************************************************** b import cs1.Keyboard; b public class GradeReport b { b // Reads a grade from the user and prints comments accordingly. b public static void main (String[] args) b { b int grade, category; b System.out.print ("Enter a numeric grade (0 to 100): "); b grade = Keyboard.readInt(); b category = grade / 10; b System.out.print ("That grade is "); b switch (category) b { b case 10: b System.out.println ("a perfect score. Well done."); b break;

GradeReport.java (cont.) b case 9: b System.out.println ("well above average. Excellent."); b break; b case 8: b System.out.println ("above average. Nice job."); b break; b case 7: b System.out.println ("average."); b break; b case 6: b System.out.println ("below average. You should see the"); b System.out.println ("instructor to clarify the material " b + "presented in class."); b break; b default: b System.out.println ("not passing."); b }

Counter.java b //****************************************************************** b // Counter.java Author: Lewis and Loftus b // Demonstrates the use of a while loop. b //****************************************************************** b public class Counter b { b // Prints integer values from 1 to a specific limit. b public static void main (String[] args) b { b final int LIMIT = 5; b int count = 1; b while (count <= LIMIT) b { b System.out.println (count); b count = count + 1; b } b System.out.println ("Done"); b }

Average.java b //****************************************************************** b // Average.java Author: Lewis and Loftus b // Demonstrates the use of a while loop, a sentinel value, and a b // running sum. b //****************************************************************** b import java.text.DecimalFormat; b import cs1.Keyboard; b public class Average b { b // Computes the average of a set of values entered by the user. b // The running sum is printed as the numbers are entered. b public static void main (String[] args) b { b int sum = 0, value, count = 0; b double average; b System.out.print ("Enter an integer (0 to quit): "); b value = Keyboard.readInt();

Average.java (cont.) b while (value != 0) // sentinel value of 0 to terminate loop b { b count++; b sum += value; b System.out.println ("The sum so far is " + sum); b System.out.print ("Enter an integer (0 to quit): "); b value = Keyboard.readInt(); b } b System.out.println (); b System.out.println ("Number of values entered: " + count); b average = (double)sum / count; b DecimalFormat fmt = new DecimalFormat ("0.###"); b System.out.println ("The average is " + fmt.format(average)); b }

WinPercentage.java b //****************************************************************** b // WinPercentage.java Author: Lewis and Loftus b // Demonstrates the use of a while loop for input validation. b //****************************************************************** b import java.text.NumberFormat; b import cs1.Keyboard; b public class WinPercentage b { b // Computes the percentage of games won by a team. b public static void main (String[] args) b { b final int NUM_GAMES = 12; b int won; b double ratio; b System.out.print ("Enter the number of games won (0 to " b + NUM_GAMES + "): "); b won = Keyboard.readInt(); b

WinPercentage.java (cont.) b while (won NUM_GAMES) b { b System.out.print ("Invalid input. Please reenter: "); b won = Keyboard.readInt(); b } b ratio = (double)won / NUM_GAMES; b NumberFormat fmt = NumberFormat.getPercentInstance(); b System.out.println (); b System.out.println ("Winning percentage: " + fmt.format(ratio)); b }

Forever.java b //****************************************************************** b // Forever.java Author: Lewis and Loftus b // Demonstrates an INFINITE LOOP. WARNING!! b //****************************************************************** b public class Forever b { b // Prints ever decreasing integers in an INFINITE LOOP! b public static void main (String[] args) b { b int count = 1; b while (count <= 25) b { b System.out.println (count); b count = count - 1; b } b System.out.println ("Done"); // this statement is never reached b }

PalindromeTester.java b //****************************************************************** b // PalindromeTester.java Author: Lewis and Loftus b // Demonstrates the use of nested while loops. b //****************************************************************** b import cs1.Keyboard; b public class PalindromeTester b { b // Tests strings to see if they are palindromes. b public static void main (String[] args) b { b String str, another = "y"; b int left, right; b while (another.equalsIgnoreCase("y")) // allows y or Y b { b System.out.println ("Enter a potential palindrome:"); b str = Keyboard.readString(); b left = 0; b right = str.length() - 1;

PalindromeTester.java (cont.) b while (str.charAt(left) == str.charAt(right) && left < right) b { b left++; b right--; b } b System.out.println(); b if (left < right) b System.out.println ("That string is NOT a palindrome."); b else b System.out.println ("That string IS a palindrome."); b System.out.println(); b System.out.print ("Test another palindrome (y/n)? "); b another = Keyboard.readString(); b }

Counter2.java b //****************************************************************** b // Counter2.java Author: Lewis and Loftus b // Demonstrates the use of a do loop. b //****************************************************************** b public class Counter2 b { b // Prints integer values from 1 to a specific limit. b public static void main (String[] args) b { b final int LIMIT = 5; b int count = 0; b do b { b count = count + 1; b System.out.println (count); b } b while (count < LIMIT); b System.out.println ("Done"); b }

ReverseNumber.java b //****************************************************************** b // ReverseNumber.java Author: Lewis and Loftus b // Demonstrates the use of a do loop. b //****************************************************************** b import cs1.Keyboard; b public class ReverseNumber b { b // Reverses the digits of an integer mathematically. b public static void main (String[] args) b { b int number, lastDigit, reverse = 0; b System.out.print ("Enter a positive integer: "); b number = Keyboard.readInt(); b do b { b lastDigit = number % 10; b reverse = (reverse * 10) + lastDigit; b number = number / 10; b } b while (number > 0); b System.out.println ("That number reversed is " + reverse); b }

Counter3.java b //****************************************************************** b // Counter3.java Author: Lewis and Loftus b // Demonstrates the use of a for loop. b //****************************************************************** b b public class Counter3 b { b // Prints integer values from 1 to a specific limit. b public static void main (String[] args) b { b final int LIMIT = 5; b b for (int count=1; count <= LIMIT; count++) b System.out.println (count); b b System.out.println ("Done"); b }

Multiples.java b //****************************************************************** b // Multiples.java Author: Lewis and Loftus b // Demonstrates the use of a for loop. b //****************************************************************** b import cs1.Keyboard; b public class Multiples b { b // Prints multiples of a user-specified number up to a user-specified limit. b public static void main (String[] args) b { b final int PER_LINE = 5; b int value, limit, mult, count = 0; b System.out.print ("Enter a positive value: "); b value = Keyboard.readInt(); b System.out.print ("Enter an upper limit: "); b limit = Keyboard.readInt(); b System.out.println (); b System.out.println ("The multiples of " + value + " between " + b value + " and " + limit + " (inclusive) are:");

Multiples.java (cont.) b for (mult = value; mult <= limit; mult += value) b { b System.out.print (mult + "\t"); b // Print a specific number of values per line of output b count++; b if (count % PER_LINE == 0) b System.out.println(); b }

Stars.java b //****************************************************************** b // Stars.java Author: Lewis and Loftus b // Demonstrates the use of nested for loops. b //****************************************************************** b public class Stars b { b // Prints a triangle shape using asterisk (star) characters. b public static void main (String[] args) b { b final int MAX_ROWS = 10; b b for (int row = 1; row <= MAX_ROWS; row++) b { b for (int star = 1; star <= row; star++) b System.out.print ("*"); b b System.out.println(); b }

ExamGrades.java b //****************************************************************** b // ExamGrades.java Author: Lewis and Loftus b // Demonstrates the use of various control structures. b //****************************************************************** b import java.text.DecimalFormat; b import cs1.Keyboard; b public class ExamGrades b { b // Computes the average, minimum, and maximum of a set of exam b // scores entered by the user. b public static void main (String[] args) b { b int grade, count = 0, sum = 0, max, min; b double average; b // Get the first grade and give max and min that initial value b System.out.print ("Enter the first grade (-1 to quit): "); b grade = Keyboard.readInt(); b max = min = grade; b // Read and process the rest of the grades b while (grade >= 0) b { b count++; b sum += grade;

ExamGrades.java (cont.) b if (grade > max) b max = grade; b else b if (grade < min) b min = grade; b System.out.print ("Enter the next grade (-1 to quit): "); b grade = Keyboard.readInt (); b } b // Produce the final results b if (count == 0) b System.out.println ("No valid grades were entered."); b else b { b DecimalFormat fmt = new DecimalFormat ("0.##"); b average = (double)sum / count; b System.out.println(); b System.out.println ("Total number of students: " + count); b System.out.println ("Average grade: " + fmt.format(average)); b System.out.println ("Highest grade: " + max); b System.out.println ("Lowest grade: " + min); b }