Chapter 4 Input and Output.pptx - Chapter 4 Input and...

This preview shows page 1 - 7 out of 24 pages.

Chapter 4 Input and Output
Image of page 1

Subscribe to view the full document.

Objectives At the end of this chapter, student should be able to: understand the concept of input and output representation in java. obtain input from the console using the Scanner class. obtain input using the JOptionPane input dialog boxes examine ways to output results using output statements. 2
Image of page 2
Program Without Input public class WithoutInput { public static void main(String[] args){ System.out.println("How old are you?"); System.out.println("Hi!!, You are 20 years old"); } } 3 C:\> javac WithoutInput.java C:\> java WithoutInput How old are you? Hi!!, you are 20 years Old
Image of page 3

Subscribe to view the full document.

Program With Static Input public class UsingStaticInput { public static void main(String[] args){ int age; System.out.print("How old are you?"); age = 20; System.out.println("Hi!!, You are "+age+ " years old"); } } 4 How old are you? Hi!!, you are 20 years Old How to make an interactive program?
Image of page 4
Input Input can be obtained from: console input dialog To read input from the console, Java uses System.in to refer to the standard input device. By default, the input device is the keyboard. When the computer gets the data from the keyboard, the user is said to be acting interactively. 5
Image of page 5

Subscribe to view the full document.

Scanner Class Scanner class can be used to create an object to read input from System.in. Scanner is a predefined Java class. The Scanner class is in the java.util package To read data: Create an input stream object of the class Scanner and associate it with the standard input device as the following statements: static Scanner console = new Scanner(System.in); Use the methods such as next() , nextLine() , nextInt() , nextShort() , nextByte( ), nextLong() , nextFloat() and nextDouble() 6
Image of page 6
Methods for
Image of page 7
You've reached the end of this preview.
  • Spring '17
  • DR. NOORHAFHIZAH

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern