Published on July 9, 2014
PowerPoint Presentation: 1 Writing Interactive Java Programs using Scanner class CBOP3203 – Object Oriented Programming Learning Outcomes: 2 Learning Outcomes By the end of this part, you should be able to: Write interactive Java program using Scanner class Introduction: 3 Introduction All the Java programs that presented to you earlier in other sessions are non-interactive Java programs. It means the data for the variables are fixed inside the program. If you want to change the value of the data, you need to go to program and do the necessary changes. Example of non-interactive program: Example of non-interactive program Interactive program using class Scanner : Interactive program using class Scanner Non-interactive programs are not practical. The best approach is to develop interactive programs. Interactive program means the program will accept the data for the variable when the program is executing, or sometime we call it “during the runtime”. In Java, interactive Java programs can be developed using a library class known as Scanner. If you want to use Scanner class in your program, make sure you have installed JDK version 1.5 or above. The class Scanner has various methods that allow you to read or accept data during the runtime of the program. Methods in Scanner class: Methods in Scanner class nextInt() to capture integer data type nextFloat() to capture float data type nextDouble() to capture double data type next() reads a string that ends before a white space nextLine() reads a line of character Problem: Problem Write a Java program that will accept three integer inputs during the runtime of the program and then display the sum of these inputs The solution for this problem is given in the next slide Answer: Answer 8 Homework: Homework Write a Java program that will accept three float data type inputs during the runtime of the program and then display the average of these inputs Common Programming Errors: Common Programming Errors Forget to inlude the statement “import java.util.*;” at the beginning of the program Forget to create the Scanner before calling the methods in the Scanner class Incorrect method names from the Scanner class are used when writing a program Summary: 11 Summary Capturing inputs from the user during the execution of the programs makes your program interactive In Java, interactive Java programs can be developed using the Scanner class If you want to use Scanner class in your program, make sure you have installed JDK version 1.5 or above.