Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "INF 523Q Chapter 3: Program Statements (Examples)."— Presentation transcript:

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

2 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 }

3 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 ();

4 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 }

5 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

6 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 }

7 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();

8 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 }

9 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;

10 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 }

11 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 }

12 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();

13 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 }

14 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

15 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 }

16 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 }

17 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;

18 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 }

19 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 }

20 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 }

21 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 }

22 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:");

23 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 }

24 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 }

25 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;

26 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 }


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

Similar presentations


Ads by Google