View
223
Download
0
Category
Preview:
Citation preview
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 4
1/14
TMK 3102-Chap 4: Input & Output 1
Chapt er 4 : Input & Out put
Session Plan (Week 4):
To understand:the concept of input & output in JAVA
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 4
2/14
TMK 3102-Chap 4: Input & Output 2
Syst em .out .pr in t ln St a t em entpublic class Hello {
public static void main(String[] args){
System.out.println(Hello World);
System.out.println(Welcome to Java );}
}
C:\> javac Hello.java
Hello World
Welcome to Java
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 4
3/14
TMK 3102-Chap 4: Input & Output 3
Syst em .out .pr in t St a t em ent
public class Hello {
public static void main(String[] args){
System.out.print(Hello World);
System.out.println(Welcome to Java );}
}
C:\> javac Hello.java
C:\> java Hello
Hello World Welcome to Java
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 4
4/14
TMK 3102-Chap 4: Input & Output 4
Esc ape Charac t er
public class Hello {
public static void main(String[] args){
System.out.println(Hello World!! \n Welcome to Java );
}}
C:\> javac Hello.java
Hello World!!
Welcome to Java
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 4
5/14
TMK 3102-Chap 4: Input & Output 5
Esc ape Charac t er
\n newline. Position the cursor to the beginning of nextline
\t horizontal tab. Move the cursor to the next tabstop
\r carriage-return. Position the cursor to thebeginning of the current line, do not advance tothe next line
\\ used to print a backslash character
\ Single quote. Used to print single quote character\ double quote. Used to print double quote character
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 4
6/14
TMK 3102-Chap 4: Input & Output 6
Using Sc anner Class For input
import java.util.*;
public class UsingScannerClass {
public static void main(String[] args){
Scanner input = new Scanner(System.in);int age;
System.out.print("How old are you?");
age = input.nextInt();
System.out.println("Hi!!, You are "+age+ " years old");
}}
C:\> javac Hello.java
C:\> java Hello
How old are you? 17
Hi!!, you are 17 years Old
age 17
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 4
7/14
TMK 3102-Chap 4: Input & Output 7
Display ing i n Graphic s
You can use the showMessageDialogmethod in the JOptionPane class.
JOptionPane is one of the manypredefined classes in the Javasystem, which can be reused ratherthan reinventing the wheel.
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 4
8/14
TMK 3102-Chap 4: Input & Output 8
The show MessageDia logMethod
JOptionPane.showMessageDialog(null,
"Welcome to Java!",
"Example 1.2 Output",
JOptionPane.INFORMATION_MESSAGE));
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 4
9/14
TMK 3102-Chap 4: Input & Output 9
Tw o Ways t o Invoke t he Met hod
There are several ways to use the showMessageDialogmethod. For the time being, all you need to know aretwo ways to invoke it.
One is to use a statement as shown in the example:JOptionPane.showMessageDialog(null, x,y, JOptionPane.INFORMATION_MESSAGE));
where x is a string for the text to be displayed, and y isa string for the title of the message dialog box.
The other is to use a statement like this:JOptionPane.showMessageDialog(null, x);
where x is a string for the text to be displayed.
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 4
10/14
TMK 3102-Chap 4: Input & Output 10
Example
import javax.swing.JOptionPane;
public class Welcome {
public static void main(String[] args) {
String name;name = JOptionPane.showInputDialog("What is Your
Name?");
JOptionPane.showMessageDialog(null,"Hi!!! "+name+"\nWelcome\nTo\nJava\nWorld");
}
}
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 4
11/14
TMK 3102-Chap 4: Input & Output 11
Example
import javax.swing.JOptionPane;
public class Welcome {
public static void main(String[] args) {
String name;name = JOptionPane.showInputDialog("Whatis Your Name?");
JOptionPane.showMessageDialog(null,
"Hi!!! "+name+"\nWelcome\nTo\nJava\nWorld");
}
}
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 4
12/14
TMK 3102-Chap 4: Input & Output 12
Example
import javax.swing.JOptionPane;
public class Welcome {
public static void main(String[] args) {
String name;
name = JOptionPane.showInputDialog("What is YourName?");
JOptionPane.showMessageDialog(null,"Hi!!! "+name+"\nWelcome\nTo\nJava\nWorld");
}}
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 4
13/14
TMK 3102-Chap 4: Input & Output 13
Exerc ise
Write the java program using the givenpseudo code.
1. Start
2. Get the value of radius
3. Calculate area where area = x r x r
4. Display area
5. End
8/9/2019 COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES 4
14/14
TMK 3102-Chap 4: Input & Output 14
Thank You
Recommended