TUNASAN NATIONAL HIGH SCHOOL SDO - Muntinlupa City ROWELL L. MARQUINA Senior High School Teacher TUNASAN NATIONAL HIGH SCHOOL Lesson 5 - Java Programming
LEARNING OBJECTIVES: At the end of the session, you will be able to: 1. discuss what is a Scanner class; 2. know how to import and make use of the Scanner class; 3. select the appropriate input methods in accommodating user’s input/s; and 4. create a program that will require input/s from the user.
The Scanner class is a method in Java that is used to capture data that is entered by the user. It is included in the java.util package. If the programmer intends to create a program that will accept inputs from the user, the scanner class must be imported at the very beginning of the code. JAVA SCANNER CLASS
After successfully importing a Scanner class the next thing, we must create is a Scanner object. The Scanner object is a representation of the scanner that is necessary when invoking the scanner for use. Create the scanner object at beginning of the program body to avoid conflicts when invoking the scanner at the later parts of the program. JAVA SCANNER CLASS
EXAMPLE 1: Create a program that will require the user to input their first name. The program will then display a greeting with the name of the user.
IMPORTING THE SCANNER As discussed earlier, to use the Scanner class we must first import it by invoking the Java statement:
DECLARING A VARIABLE We will declare a string variable called “name” . The “name” is the variable that will represent the data entered by the user.
CREATING A SCANNER OBJECT In this example, we initiated a scanner called “mitis”. The scanner “mitis” is being introduced here as a new scanner and as method of getting input from the user.
DISPLAYING TEXT ON SCREEN We displayed the question “What is your name?” using the print method to display the instruction for our user.
ASSIGNING DATA TO A VARIABLE Since the program is already familiar with the variable called “name”, we can now use it to represent any value that our scanner captured in the process.
ASSIGNING DATA TO A VARIABLE This statement instructs the program that the input will be captured using the scanner named “ mitis”. The “nextLine( )” statement tells the program that the incoming input is a String.
DISPLAYING TEXT ON SCREEN This statement instructs the program to display the greeting “Good morning” followed by whatever value contained in the variable name. The greeting will end with “!”.
SOURCE CODE AND OUTPUTS SOURCE CODE: SAMPLE OUTPUTS:
EXAMPLE 2: COMPUTING FOR AGE: Create a program that will require the user to input their birth year. The program will compute for the user’s age using the inputted birth year and display it on the screen.
SOURCE CODE:
EXAMPLE 3: SUM OF TWO NUMBERS: Create a program that will require the user to input two numbers. The program will compute for the sum of the numbers inputted by the user and display it on the screen.
SOURCE CODE:
EXAMPLE 4: KILOGRAM TO POUND CONVERSION: Create a program that will require the user to input a value in kilogram. The program will convert the value inputted by the user to pound by using the conversion 1 kg = 2.2 lbs.
SOURCE CODE:
PRACTICE TEST 1: COMPUTING FOR AREA: Create a flowchart for a program that will require the user to input the length and width of a rectangle. The program will compute for the area of the rectangle using the inputted length and with and display its results on the screen.
PRACTICE TEST 2: COMPUTING FOR AVERAGE Create a flowchart for a program that will require the user to input their grades in Mathematics, English, and Science. The program will compute for the average of the three grades and display its result on the computer screen.
PRACTICE TEST 3: PLAYING WITH NUMBERS: Create a flowchart for a program that will require the user to input four numbers. Using the inputs, the program will compute for: ▪ the sum of 1st and 2nd number, ▪ the product of the 3rd and 4th number ▪ the square of the 2nd number
GETTING INPUTS FROM USERS MR. ROWELL L. MARQUINA Tunasan National High School Senior High School Department Email Address: rowell.marquina001@deped.gov.ph sirrowellmarquina@gmail.com rmarquina@mitis.edu.ph

Lesson 5 - Getting Input from Users.pdf

  • 1.
    TUNASAN NATIONAL HIGHSCHOOL SDO - Muntinlupa City ROWELL L. MARQUINA Senior High School Teacher TUNASAN NATIONAL HIGH SCHOOL Lesson 5 - Java Programming
  • 2.
    LEARNING OBJECTIVES: At theend of the session, you will be able to: 1. discuss what is a Scanner class; 2. know how to import and make use of the Scanner class; 3. select the appropriate input methods in accommodating user’s input/s; and 4. create a program that will require input/s from the user.
  • 4.
    The Scanner classis a method in Java that is used to capture data that is entered by the user. It is included in the java.util package. If the programmer intends to create a program that will accept inputs from the user, the scanner class must be imported at the very beginning of the code. JAVA SCANNER CLASS
  • 5.
    After successfully importinga Scanner class the next thing, we must create is a Scanner object. The Scanner object is a representation of the scanner that is necessary when invoking the scanner for use. Create the scanner object at beginning of the program body to avoid conflicts when invoking the scanner at the later parts of the program. JAVA SCANNER CLASS
  • 7.
    EXAMPLE 1: Create aprogram that will require the user to input their first name. The program will then display a greeting with the name of the user.
  • 8.
    IMPORTING THE SCANNER Asdiscussed earlier, to use the Scanner class we must first import it by invoking the Java statement:
  • 9.
    DECLARING A VARIABLE Wewill declare a string variable called “name” . The “name” is the variable that will represent the data entered by the user.
  • 10.
    CREATING A SCANNEROBJECT In this example, we initiated a scanner called “mitis”. The scanner “mitis” is being introduced here as a new scanner and as method of getting input from the user.
  • 11.
    DISPLAYING TEXT ONSCREEN We displayed the question “What is your name?” using the print method to display the instruction for our user.
  • 12.
    ASSIGNING DATA TOA VARIABLE Since the program is already familiar with the variable called “name”, we can now use it to represent any value that our scanner captured in the process.
  • 13.
    ASSIGNING DATA TOA VARIABLE This statement instructs the program that the input will be captured using the scanner named “ mitis”. The “nextLine( )” statement tells the program that the incoming input is a String.
  • 14.
    DISPLAYING TEXT ONSCREEN This statement instructs the program to display the greeting “Good morning” followed by whatever value contained in the variable name. The greeting will end with “!”.
  • 15.
    SOURCE CODE ANDOUTPUTS SOURCE CODE: SAMPLE OUTPUTS:
  • 16.
    EXAMPLE 2: COMPUTING FORAGE: Create a program that will require the user to input their birth year. The program will compute for the user’s age using the inputted birth year and display it on the screen.
  • 17.
  • 18.
    EXAMPLE 3: SUM OFTWO NUMBERS: Create a program that will require the user to input two numbers. The program will compute for the sum of the numbers inputted by the user and display it on the screen.
  • 19.
  • 20.
    EXAMPLE 4: KILOGRAM TOPOUND CONVERSION: Create a program that will require the user to input a value in kilogram. The program will convert the value inputted by the user to pound by using the conversion 1 kg = 2.2 lbs.
  • 21.
  • 22.
    PRACTICE TEST 1: COMPUTINGFOR AREA: Create a flowchart for a program that will require the user to input the length and width of a rectangle. The program will compute for the area of the rectangle using the inputted length and with and display its results on the screen.
  • 23.
    PRACTICE TEST 2: COMPUTINGFOR AVERAGE Create a flowchart for a program that will require the user to input their grades in Mathematics, English, and Science. The program will compute for the average of the three grades and display its result on the computer screen.
  • 24.
    PRACTICE TEST 3: PLAYINGWITH NUMBERS: Create a flowchart for a program that will require the user to input four numbers. Using the inputs, the program will compute for: ▪ the sum of 1st and 2nd number, ▪ the product of the 3rd and 4th number ▪ the square of the 2nd number
  • 25.
    GETTING INPUTS FROMUSERS MR. ROWELL L. MARQUINA Tunasan National High School Senior High School Department Email Address: rowell.marquina001@deped.gov.ph sirrowellmarquina@gmail.com rmarquina@mitis.edu.ph