Java User Input
WHAT IS USER INPUT? - Collecting and acting on user input is important in many types of programs or applications. - User input includes typing on the keyboard, clicking of a mouse, tapping or swiping a touch screen device, etc. - After the user input event, your program must react to that event in some way.
JAVA SCANNER CLASS - In the following examples, we will be using the java.util.scanner class to handle keyboard input. - We will be retrieving values from the keyboard and storing those values in variables for future use.
GRADEBOOK PROGRAM NEW FEATURES We will be adding the following new features to the Gradebook program we started previously. - Build a loop and menu system to start and exit the program. - Allow a user to enter in a defined number of grades and have the program automatically calculate the grade percentage and letter grade.
1 2 GRADEBOOK PROJECT PART 2 - Now it is time to follow along with the code samples in the upcoming slides. - Open the previously created Gradebook.java file. - Import the java.util.scanner class to the gradebook.
GRADEBOOK PROJECT PART 2 (CONTINUED) - Create an instance of the Scanner class in order to collect the user keyboard input. This can go in the main method directly underneath the grades List. 3
GRADEBOOK PROJECT PART 2 (CONTINUED) - The entire Gradebook program is now going to be set up to be controlled by a loop. In this program we will use a while loop. - Many different types of applications and games are controlled by a loop. The program will continue running until a user event shuts it down. This could include clicking the exit button on an application, losing in a game, etc.
- Select the initial grade list that you created in the previous lesson and DELETE it. 4 GRADEBOOK PROJECT PART 2 (CONTINUED) - Yes, I said to delete it. We will be replacing this code with some different coding next.
- Add the code found on the next slide inside the main method below the Scanner input = new Scanner(System.in); statement. 5 GRADEBOOK PROJECT PART 2 (CONTINUED) - Now lets create the basic menu loop. This code is slightly more complicated than what we have worked with so far. However, we will walk through it step by step.
GRADEBOOK PROJECT PART 2 (CONTINUED)
GRADEBOOK PROJECT PART 2 (CONTINUED) - When you run the program, you will need to click down in the console output to enter in a number. After entering a number, press the enter key to send the number back to your program. - The program will run until you enter a 2. Here is an example of what the output of the program would look like until you enter a 2:
GRADEBOOK PROJECT PART 2 (CONTINUED) - As we learned previously, both an if statement and while loop are control structures. - These control structures can be placed inside of each other as seen in the previous example. - When a control structure is placed inside another, this is called nesting. - It is possible to nest multiple different types of control structures inside another and is a very important concept in programming.
GRADEBOOK PROJECT PART 2 (CONTINUED) - Inside the menuselection if statement we collect some additional user input. Add the following statements to the menuselection if statement: 6
GRADEBOOK PROJECT PART 2 (CONTINUED) - Continued adding the following code to the menuselection if statement: 7
- Run the program and input some grades. The console output will be similiar to the screenshot below. 8 GRADEBOOK PROJECT PART 2 (CONTINUED) - Did you notice anything wrong with this? - Hopefully you noticed that the total possible is incorrect. Which results in an incorrect letter grade. - Why is it 300 instead of 45 like it should be?
GRADEBOOK PROJECT PART 2 (CONTINUED) - The total possible is 300 because we set the initial variable of totalpossible to grades.size() * 100; 9 - To fix this error we need to replace the 100 with the maxgradescore statement. Replace 100 with maxgradescore.
GRADEBOOK PROJECT PART 2 (CONTINUED) - Here is what a portion of the program look like. - Did you run the program? If you did, you got an error message.
- Lets fix the error so we can run the program with no issues. 10 GRADEBOOK PROJECT PART 2 (CONTINUED) - Select the two curley brackets that are above the variables and move them both below your print statements.
GRADEBOOK PROJECT PART 2 (CONTINUED) - Now that you have moved the curley brackets, run the program and input some grades. 11
DEBUGGING - In a past lesson, we introduced the idea of debugging. Eclipse has a built in debugging tool that makes it easy to walk through your program step by step. - This is also useful for trying to understand how a program works. - When using the Eclipse debugger, you can see the values of your variables when each line of code is running.
DEBUGGING (CONTINUED) - The first step in debugging is to set a breakpoint. - A breakpoint is simply a place that your program will stop executing and turn control over to the Eclipse debugger. 12 Right click here and select "Toggle Breakpoint" You will see an icon to indicate a breakpoint has been set.
DEBUGGING (CONTINUED) - Eclipse comes with different views, that are called perspectives. Perspectives make it easier to do specific tasks. - In the top right corner of Eclipse there are links to switch back and forth between the Debug perspective and the Java perspective. - If you try to run debug mode in the Java perspective, it will provide you with a message asking if you want to switch.
- The next step is to go to the Eclipse menu and click Run > Debug (or use the F11 shortcut key) 13 DEBUGGING (CONTINUED) - If you were not on the Debug perspective, click "Yes". - You will notice that this perspective is split into a bunch of panels with different information. We will walk through the important ones now.
DEBUGGING (CONTINUED) - The top left panel is the Debug panel. This contains information about the Java classes and which method you are currently in. Currently in the main method
DEBUGGING (CONTINUED) - The variables panel allows you to track the value of each variable based on where you are at in the code. Variables show up here Variable values show up here
DEBUGGING (CONTINUED) - The outline panel simply gives you an outline of the methods within the class. In this case it shows the main and lettergrade methods that are inside the Gradebook class.
DEBUGGING (CONTINUED) - The panel on the left center of the Debug perspective contains the code that is currently running. It will display which line of code the program is currently on. It will also show you the breakpoints that you have set. Currently on this line of code
DEBUGGING (CONTINUED) - The console panel allows you to see the output of your program. This is the same as the console panel in the main Java perspective. You will be able to enter your input values into this panel when you are debugging your code. Output and input values are handled here. You can stop the program by clicking the red stop button
DEBUGGING (CONTINUED) - The debugging tool allows you to step through the code, line by line. You can use the icons near the top of the screen to control the execution of the program. Stop the currently running program Continue to the next breakpoint (or end of code if no other breakpoints exist) Used to step inside of a method. This can be used on the lettergrade call to move into that method. Used to step out of the current method. This can be used within the lettergrade method to step back out to the main method. Used to execute and step over the current line of code
DEBUGGING (CONTINUED) - This was the most complicated program you have written so far. Now it is time for you to use your newly learned debugging skills to walk through the program step by step and see exactly how it works. - Make sure to examine the variables at each step to see how things are getting processed. - Spend a lot of time getting used to the debugging tool, it will make programming much easier for you. - After working with the debugging tool, you will notice that although programming can be complex, there is no magic involved. The computer only performs the specific instructions you tell it to.
PRINTING 14 - Print the Gradebook.java file and turn in