eggbaskettrace_gray

eggbaskettrace_gray - import java.util.Scanner; public...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } CSE 201 – Elementary Computer Programming 45 CSE 201 – Elementary Computer Programming 46 •Sample Trace Through our Second Program •Notice the following things: •The Scanner has been imported. •The name of the class is EggBasket. •We start in the ‘main’ method. •Start with the first line in the program 45 46 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } numberOfBaskets import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } numberOfBaskets eggsPerBasket CSE 201 – Elementary Computer Programming 47 CSE 201 – Elementary Computer Programming 48 •We create a variable! •Attributes •Name: numberOfBaskets •Type: int •Value: -none- •We create another variable! •Attributes •Name: eggsPerBasket •Type: int •Value: -none- 47 48 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } numberOfBaskets import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } eggsPerBasket } numberOfBaskets totalEggs eggsPerBasket totalEggs CSE 201 – Elementary Computer Programming 49 CSE 201 – Elementary Computer Programming 50 •We create our final variable! •Attributes •Name: totalEggs •Type: int •Value: -none- •We now have three integer variables declared: •numberOfBaskets •eggsPerBasket •totalEggs 49 50 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } numberOfBaskets import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } eggsPerBasket } totalEggs keyboard numberOfBaskets eggsPerBasket totalEggs CSE 201 – Elementary Computer Programming 51 CSE 201 – Elementary Computer Programming 52 •Next Line… •We create a new Scanner class for our use •The name of our Scanner is “keyboard” •We will read from “System.in” •This will read the values given to us by the user of the program •For more information on the Scanner class: Section 2.13 51 52 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } keyboard numberOfBaskets eggsPerBasket totalEggs keyboard numberOfBaskets eggsPerBasket totalEggs CSE 201 – Elementary Computer Programming 53 CSE 201 – Elementary Computer Programming 54 •Now, we have created three integers and a Scanner for our use. •This will print “Enter the number of eggs in each basket: ” to the screen. 53 54 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } keyboard numberOfBaskets eggsPerBasket totalEggs keyboard numberOfBaskets eggsPerBasket totalEggs CSE 201 – Elementary Computer Programming 55 CSE 201 – Elementary Computer Programming 56 •Now, we have to read what the user entered! •The “keyboard.nextInt()” will go to our Scanner, and tell it that we are looking for an integer from the user. 55 56 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } keyboard numberOfBaskets eggsPerBasket totalEggs keyboard numberOfBaskets eggsPerBasket totalEggs CSE 201 – Elementary Computer Programming 57 CSE 201 – Elementary Computer Programming 58 •Our Scanner will wait for some input from the user. •The Scanner gets the user’s input. •The Scanner converts the input into an integer. 57 58 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } keyboard numberOfBaskets eggsPerBasket totalEggs keyboard numberOfBaskets eggsPerBasket totalEggs CSE 201 – Elementary Computer Programming 59 CSE 201 – Elementary Computer Programming 60 •The Scanner returns the value of the integer to our program. •“keyboard.nextInt()” has the value entered by the user. 59 60 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = 5; System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = 5; System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } keyboard numberOfBaskets eggsPerBasket totalEggs keyboard numberOfBaskets eggsPerBasket totalEggs CSE 201 – Elementary Computer Programming 61 CSE 201 – Elementary Computer Programming 62 •In our case, the user entered 5 •“keyboard.nextInt()” is conceptually converted to whatever the user enters… •NOTE: this is not what ACTUALLY happens, but is helpful for illustrative purposes! •Using the assignment operator ‘=’, we place the number entered b y the user into our variable named ‘eggsPerBasket’. 61 62 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = 5; System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } keyboard numberOfBaskets eggsPerBasket totalEggs keyboard numberOfBaskets eggsPerBasket totalEggs 5 5 CSE 201 – Elementary Computer Programming 63 CSE 201 – Elementary Computer Programming 64 •‘eggsPerBasket’ now contains the value entered by the user. •We have successfully stored the value entered by the user! 63 64 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } keyboard numberOfBaskets eggsPerBasket totalEggs keyboard numberOfBaskets eggsPerBasket totalEggs 5 5 CSE 201 – Elementary Computer Programming 65 CSE 201 – Elementary Computer Programming 66 •This will print “Enter the number of baskets: ” to the screen. •Now, we have to read the next number entered by the user! 65 66 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } keyboard numberOfBaskets eggsPerBasket totalEggs keyboard numberOfBaskets eggsPerBasket totalEggs 5 5 CSE 201 – Elementary Computer Programming 67 CSE 201 – Elementary Computer Programming 68 •The “keyboard.nextInt()” will go to our Scanner, and tell it that we are looking for an integer from the user. •Once again, our Scanner will wait for some input from the user. 67 68 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } keyboard numberOfBaskets eggsPerBasket totalEggs keyboard numberOfBaskets eggsPerBasket totalEggs 5 5 CSE 201 – Elementary Computer Programming 69 CSE 201 – Elementary Computer Programming 70 •The Scanner gets the user’s input. •The Scanner converts the input into an integer. •The Scanner returns the value of the integer to our program. 69 70 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = 10; totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } keyboard numberOfBaskets eggsPerBasket totalEggs keyboard numberOfBaskets eggsPerBasket totalEggs 5 5 CSE 201 – Elementary Computer Programming 71 CSE 201 – Elementary Computer Programming 72 •“keyboard.nextInt()” has the value entered by the user. •In our case, the user entered 10 •Once again, “keyboard.nextInt()” is conceptually converted to whatever the user enters… •NOTE: this is not what ACTUALLY happens, but is helpful for illustrative purposes! 71 72 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = 10; totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = 10; totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } keyboard numberOfBaskets eggsPerBasket totalEggs keyboard numberOfBaskets eggsPerBasket totalEggs 5 10 5 CSE 201 – Elementary Computer Programming 73 CSE 201 – Elementary Computer Programming 74 •Using the assignment operator ‘=’, we place the number entered b y the user into our variable named ‘numberOfBaskets’. •‘numberOfBaskets’ now contains the value entered by the user. 73 74 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } keyboard numberOfBaskets eggsPerBasket totalEggs keyboard numberOfBaskets eggsPerBasket totalEggs 10 5 10 5 CSE 201 – Elementary Computer Programming 75 CSE 201 – Elementary Computer Programming 76 •We have successfully stored the number of baskets! •Now, we need to compute the total number of eggs. 75 76 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = 10 * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } keyboard numberOfBaskets eggsPerBasket totalEggs keyboard numberOfBaskets eggsPerBasket totalEggs 10 5 10 5 CSE 201 – Elementary Computer Programming 77 CSE 201 – Elementary Computer Programming 78 •First, we grab whatever value is stored in ‘numberOfBaskets’. •In this case, the value of 10 is stored in the variable •Once again, we can conceptually replace the variable name with the value in that variable •Helps us with our execution trace. 77 78 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = 10 * eggsPerBasket; import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = 10 * 5 ; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } } System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } keyboard numberOfBaskets eggsPerBasket totalEggs keyboard numberOfBaskets eggsPerBasket totalEggs 10 5 10 5 CSE 201 – Elementary Computer Programming 79 CSE 201 – Elementary Computer Programming 80 •Now, we grab whatever value is stored in ‘eggsPerBasket’. •In this case, the value of 5 is stored in the variable •Once again, conceptually replace the variable name with the value. •Now, we can do the calculation! 79 80 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = 50 ; import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = 50 ; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } } System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } keyboard numberOfBaskets eggsPerBasket totalEggs keyboard numberOfBaskets eggsPerBasket totalEggs 10 5 10 5 CSE 201 – Elementary Computer Programming 81 CSE 201 – Elementary Computer Programming 82 •We find that 10 * 5 is 50 •Using the assignment operator ‘=’, we place the value of the expression into our variable ‘totalEggs’. 81 82 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = 50 ; import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } keyboard numberOfBaskets eggsPerBasket totalEggs 50 keyboard numberOfBaskets eggsPerBasket totalEggs 50 10 5 10 5 CSE 201 – Elementary Computer Programming 83 CSE 201 – Elementary Computer Programming 84 •Total eggs now contains the value 50. •Now, we have all of our variable initialized! 83 84 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } keyboard numberOfBaskets eggsPerBasket totalEggs 50 keyboard numberOfBaskets eggsPerBasket totalEggs 50 10 5 10 5 CSE 201 – Elementary Computer Programming 85 CSE 201 – Elementary Computer Programming 86 •This line will print the number of eggs per basket to the screen. •Grab the value contained in the variable ‘eggsPerBasket’. 85 86 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println( 5 + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } keyboard numberOfBaskets eggsPerBasket totalEggs 50 keyboard numberOfBaskets eggsPerBasket totalEggs 50 10 5 10 5 CSE 201 – Elementary Computer Programming 87 CSE 201 – Elementary Computer Programming 88 •The value contained in ‘eggsPerBasket’ is 5. •The string “5 eggs per basket.” is printed to the screen. 87 88 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } keyboard numberOfBaskets eggsPerBasket totalEggs 50 keyboard numberOfBaskets eggsPerBasket totalEggs 50 10 5 10 5 CSE 201 – Elementary Computer Programming 89 CSE 201 – Elementary Computer Programming 90 •This line will print the number of baskets. •Grab the value contained in the variable ‘numberOfBaskets’. 89 90 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println( 10 + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } keyboard numberOfBaskets eggsPerBasket totalEggs 50 keyboard numberOfBaskets eggsPerBasket totalEggs 50 10 5 10 5 CSE 201 – Elementary Computer Programming 91 CSE 201 – Elementary Computer Programming 92 •The value contained in ‘numberOfBaskets’ is 10. •The string “10 baskets.” is printed to the screen. 91 92 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } keyboard numberOfBaskets eggsPerBasket totalEggs 50 keyboard numberOfBaskets eggsPerBasket totalEggs 50 10 5 10 5 CSE 201 – Elementary Computer Programming 93 CSE 201 – Elementary Computer Programming 94 •This line will print the total number of eggs. •Grab the value contained in the variable ‘totalEggs’. 93 94 import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + 50 ); } } import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } keyboard numberOfBaskets eggsPerBasket totalEggs 50 keyboard numberOfBaskets eggsPerBasket totalEggs 50 10 5 10 5 CSE 201 – Elementary Computer Programming 95 CSE 201 – Elementary Computer Programming 96 •The value contained in ‘totalEggs’ is 50. •The string “Total number of eggs is 50.” is printed to the screen. •Our overall interaction is as follows (user input in bold): Enter the number of eggs in each basket: 5 Enter the number of baskets: 10 5 eggs per basket. 10 baskets. Total number of eggs is 50. 95 96 ...
View Full Document

This note was uploaded on 10/07/2009 for the course CSE 201 taught by Professor All during the Spring '09 term at Ohio State.

Ask a homework question - tutors are online