Tutorial 3 Lecturer: Ken Jackson Tutor: Andria Hunter ========== Friday Tutorial Section: 980123 @1pm (MP102) Tuesday Tutorial Section: 980127 @6pm (RW142) CSC 108, Spring 1998 Tutorial notes, T3 ================================================================== Tutorial Topics: - questions 3.16, 3.17, 3.18 and 3.19 on page 118 of the Lewis and Loftus textbook. - Assignment 1 is due Question 3.16 =============== Write a program that reads an integer value and prints the sum of all even integers between 2 and the input value, inclusive. Print an error message if the input value is less than 2. Prompt accordingly. Sample execution: ---------------- Enter an integer value (>=2): 9 The sum of all even numbers from 2 up to 9 is 20 Enter an integer value (>=2): 8 The sum of all even numbers from 2 up to 8 is 20 Enter an integer value (>=2): 7 The sum of all even numbers from 2 up to 7 is 12 Enter an integer value (>=2): 3 The sum of all even numbers from 2 up to 3 is 2 Enter an integer value (>=2): 2 The sum of all even numbers from 2 up to 2 is 2 Enter an integer value (>=2): 1 Invalid integer. Must be greater than or equal to 2 //package Chap3; import java.io.*; // CSC108 Chapter 3, Question 16 // Name: Iam Me Student ID: 555555555 // Tutor: Andria Hunter Prof: Ken Jackson // // Program Description: Reads an integer value and prints // the sum of all even integers between 2 and the input // value, inclusive. class q16 { // Main method to read the integer value and print the sum. public static void main (String[] args) throws IOException { // Declare stdin so data can be read from input. DataInputStream stdin = new DataInputStream (System.in); final int START = 2; // starting value of sum int value; // integer value read from input // Read the integer value from stdin System.out.println ("Enter an integer value (>="+START+"): "); value = Integer.parseInt(stdin.readLine()); // Report error if invalid integer is entered. if (value < START) { System.out.print ("Invalid integer. "); System.out.println ("Must be greater than or equal to "+START); } else { int count = 1; // counts each even number int sum = 0; // sums up each even number // Calculate sum of all even numbers up to the integer entered. while (count <= value/2) { sum = sum + count*2; count = count + 1; } System.out.print ("The sum of all even numbers from "+START); System.out.println (" up to "+value+" is "+sum); } } } Question 3.17 =============== Write a program that reads an integer value between 0 and 100 (inclusive), representing the amount of a purchase in cents. Produce an error message if the input value is not in that range. If the input is valid, determine the amount of change that would be received from one dollar, and print the number of quarters, dimes, nickels, and pennies that should be returned. Maximize the coins with the highest value. Follow the format below. The user input is shown below: Hint: 64/25 equals 2, and 64%25 equals 14. Sample execution: ---------------- Enter the purchase amount [0-100]: 36 Your change of 64 cents is given as: 2 Quarters 1 Dimes 0 Nickels 4 Pennies Enter the purchase amount [0-100]: 100 Your change of 0 cents is given as: 0 Quarters 0 Dimes 0 Nickels 0 Pennies Enter the purchase amount [0-100]: 1 Your change of 99 cents is given as: 3 Quarters 2 Dimes 0 Nickels 4 Pennies Enter the purchase amount [0-100]: 0 Your change of 100 cents is given as: 4 Quarters 0 Dimes 0 Nickels 0 Pennies Enter the purchase amount [0-100]: 200 Invalid purchase amount. Must be between 0 and 100. //package Chap3; import java.io.*; // CSC108 Chapter 3, Question 17 // Name: Iam Me Student ID: 555555555 // Tutor: Andria Hunter Prof: Ken Jackson // // Program Description: Reads an integer value (some dollar // amount between 0 and 100), and prints out the amount of // change that would be received from one dollar. The amount // is expressed in quarters, dimes, nickels, and pennies. class q17 { // Main method to read the integer purchase amount, and to // determine and print the change. public static void main (String[] args) throws IOException { // Declare stdin so data can be read from input. DataInputStream stdin = new DataInputStream (System.in); final int DOLLAR = 100; // One dollar in cents final int QUARTER = 25; // One quarter in cents final int DIME = 10; // One dime in cents final int NICKEL = 5; // One nickel in cents int value; // Integer value read from input // Read dollar amount from stdin. System.out.println ("Enter the purchase amount [0-100]: "); value = Integer.parseInt(stdin.readLine()); // Report error if invalid integer is entered. if (value<0 || value>DOLLAR) { System.out.print ("Invalid purchase amount. "); System.out.println ("Must be between 0 and 100."); } // Subtract the purchase amount from 1 dollar, and determine // the number of quarters, dimes, nickels, and pennies that // this change amount can be divided into. else { // Amount of change that would be received from one dollar int change = DOLLAR - value; int num_quarter=0; // Counts number of quarters int num_dime=0; // Counts number of dimes int num_nickel=0; // Counts number of nickels int num_penny=0; // Counts number of pennies // Use integer division to determine how many quarters, and // then use mod operation to determine the remaining change. // Repeat for dimes, nickels, and pennies. num_quarter = change / QUARTER; change = change % QUARTER; num_dime = change / DIME; change = change % DIME; num_nickel = change / NICKEL; change = change % NICKEL; num_penny = change; // Report change to standard out. System.out.println ("Your change of "+(DOLLAR-value)+ " cents is given as:"); System.out.println (" "+num_quarter+" Quarters"); System.out.println (" "+num_dime+" Dimes"); System.out.println (" "+num_nickel+" Nickels"); System.out.println (" "+num_penny+" Pennies"); } } } Question 3.18 =============== In problem 3.17, the coints were always expressed as plural even if there was only one (1 Dimes). Modify your answer to print the singular form of the word for each coin when only one is used. Sample execution: ---------------- Enter the purchase amount [0-100]: 36 Your change of 64 cents is given as: 2 Quarters 1 Dime 0 Nickels 4 Pennies Enter the purchase amount [0-100]: 99 Your change of 1 cent is given as: 0 Quarters 0 Dimes 0 Nickels 1 Penny //package Chap3; import java.io.*; // CSC108 Chapter 3, Question 18 // Name: Iam Me Student ID: 555555555 // Tutor: Andria Hunter Prof: Ken Jackson // // Program Description: Reads an integer value (some dollar // amount between 0 and 100), and prints out the amount of // change that would be received from one dollar. The amount // is expressed in quarters, dimes, nickels, and pennies. // This program is a modification of Question 3.17 that prints // the singular form of the word for each coin when necessary. class q18 { // Main method to read the integer purchase amount, and to // determine and print the change. public static void main (String[] args) throws IOException { // Declare stdin so data can be read from input. DataInputStream stdin = new DataInputStream (System.in); final int DOLLAR = 100; // One dollar in cents final int QUARTER = 25; // One quarter in cents final int DIME = 10; // One dime in cents final int NICKEL = 5; // One nickel in cents int value; // Integer value read from input // Read dollar amount from stdin. System.out.println ("Enter the purchase amount [0-100]: "); value = Integer.parseInt(stdin.readLine()); // Report error if invalid integer is entered. if (value<0 || value>DOLLAR) { System.out.print ("Invalid purchase amount. "); System.out.println ("Must be between 0 and 100."); } // Subtract the purchase amount from 1 dollar, and determine // the number of quarters, dimes, nickels, and pennies that // this change amount can be divided into. else { // Amount of change that would be received from one dollar int change = DOLLAR - value; int num_quarter=0; // Counts number of quarters int num_dime=0; // Counts number of dimes int num_nickel=0; // Counts number of nickels int num_penny=0; // Counts number of pennies // Use integer division to determine how many quarters, and // then use mod operation to determine the remaining change. // Repeat for dimes, nickels, and pennies. num_quarter = change / QUARTER; change = change % QUARTER; num_dime = change / DIME; change = change % DIME; num_nickel = change / NICKEL; change = change % NICKEL; num_penny = change; // Report change to standard out. if (DOLLAR-value == 1) System.out.println ("Your change of "+(DOLLAR-value)+ " cent is given as:"); else System.out.println ("Your change of "+(DOLLAR-value)+ " cents is given as:"); if (num_quarter == 1) System.out.println (" "+num_quarter+" Quarter"); else System.out.println (" "+num_quarter+" Quarters"); if (num_dime == 1) System.out.println (" "+num_dime+" Dime"); else System.out.println (" "+num_dime+" Dimes"); if (num_nickel == 1) System.out.println (" "+num_nickel+" Nickel"); else System.out.println (" "+num_nickel+" Nickels"); if (num_penny == 1) System.out.println (" "+num_penny+" Penny"); else System.out.println (" "+num_penny+" Pennies"); } } } Question 3.19 =============== Modify the answer to problem 3.17 to continue processing input values until a sentinel value of -1 is entered. Change the prompt accordingly and do not print an error message when the sentinel value is entered. Sample execution: ---------------- Enter the purchase amount [0-100]: 36 Your change of 64 cents is given as: 2 Quarters 1 Dime 0 Nickels 4 Pennies Enter next purchase amount [0-100]: 100 Your change of 0 cents is given as: 0 Quarters 0 Dimes 0 Nickels 0 Pennies Enter next purchase amount [0-100]: 1 Your change of 99 cents is given as: 3 Quarters 2 Dimes 0 Nickels 4 Pennies Enter next purchase amount [0-100]: 0 Your change of 100 cents is given as: 4 Quarters 0 Dimes 0 Nickels 0 Pennies Enter next purchase amount [0-100]: -1 //package Chap3; import java.io.*; // CSC108 Chapter 3, Question 19 // Name: Iam Me Student ID: 555555555 // Tutor: Andria Hunter Prof: Ken Jackson // // Program Description: Reads an integer value (some dollar // amount between 0 and 100), and prints out the amount of // change that would be received from one dollar. The amount // is expressed in quarters, dimes, nickels, and pennies. // This program is a modification of Question 3.18 that continues // processing until the user enters a sentinel value of -1. class q19 { // Main method to read the integer purchase amount, and to // determine and print the change. public static void main (String[] args) throws IOException { // Declare stdin so data can be read from input. DataInputStream stdin = new DataInputStream (System.in); final int SENTINEL = -1; // Stops processing purchases final int DOLLAR = 100; // One dollar in cents final int QUARTER = 25; // One quarter in cents final int DIME = 10; // One dime in cents final int NICKEL = 5; // One nickel in cents int value; // Integer value read from input // Read dollar amount from stdin. System.out.println ("Enter the purchase amount [0-100]: "); value = Integer.parseInt(stdin.readLine()); // Loop continues processing purchase amounts until a // purchase amount of -1 is entered. while (value != SENTINEL) { // Report error if invalid integer is entered. if (value<0 || value>DOLLAR) { System.out.print ("Invalid purchase amount. "); System.out.println ("Must be between 0 and 100."); break; } // Subtract the purchase amount from 1 dollar, and determine // the number of quarters, dimes, nickels, and pennies that // this change amount can be divided into. else { // Amount of change that would be received from one dollar int change = DOLLAR - value; int num_quarter=0; // Counts number of quarters int num_dime=0; // Counts number of dimes int num_nickel=0; // Counts number of nickels int num_penny=0; // Counts number of pennies // Use integer division to determine how many quarters, and // then use mod operation to determine the remaining change. // Repeat for dimes, nickels, and pennies. num_quarter = change / QUARTER; change = change % QUARTER; num_dime = change / DIME; change = change % DIME; num_nickel = change / NICKEL; change = change % NICKEL; num_penny = change; // Report change to standard out. if (DOLLAR-value == 1) System.out.println ("Your change of "+(DOLLAR-value)+ " cent is given as:"); else System.out.println ("Your change of "+(DOLLAR-value)+ " cents is given as:"); if (num_quarter == 1) System.out.println (" "+num_quarter+" Quarter"); else System.out.println (" "+num_quarter+" Quarters"); if (num_dime == 1) System.out.println (" "+num_dime+" Dime"); else System.out.println (" "+num_dime+" Dimes"); if (num_nickel == 1) System.out.println (" "+num_nickel+" Nickel"); else System.out.println (" "+num_nickel+" Nickels"); if (num_penny == 1) System.out.println (" "+num_penny+" Penny"); else System.out.println (" "+num_penny+" Pennies"); } // Read next dollar amount from stdin. System.out.println ("\nEnter next purchase amount [0-100]: "); value = Integer.parseInt(stdin.readLine()); } } }