0 Bewertungen0% fanden dieses Dokument nützlich (0 Abstimmungen)
42 Ansichten14 Seiten
COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES
UNIVERSITY MALAYSIA TERENGGANU 2009
COVER EARLY TOPIC OF JAVA PROGRAMMING
Disclaimer: I don't own this document. If you believe you do, and you don't want it to be published here, please let me know. I will remove it immediately.
COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES
UNIVERSITY MALAYSIA TERENGGANU 2009
COVER EARLY TOPIC OF JAVA PROGRAMMING
Disclaimer: I don't own this document. If you believe you do, and you don't want it to be published here, please let me know. I will remove it immediately.
Copyright:
Attribution Non-Commercial (BY-NC)
Verfügbare Formate
Als PDF, TXT herunterladen oder online auf Scribd lesen
COMPUTER PROGRAMMING (TMK 3102) LECTURE NOTES
UNIVERSITY MALAYSIA TERENGGANU 2009
COVER EARLY TOPIC OF JAVA PROGRAMMING
Disclaimer: I don't own this document. If you believe you do, and you don't want it to be published here, please let me know. I will remove it immediately.
Copyright:
Attribution Non-Commercial (BY-NC)
Verfügbare Formate
Als PDF, TXT herunterladen oder online auf Scribd lesen
To understand: the concept of input & output in JAVA
TMK 3102-Chap 4: Input & Output 1
System.out.println Statement public 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
TMK 3102-Chap 4: Input & Output 2
System.out.print Statement 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
TMK 3102-Chap 4: Input & Output 3
Escape Character 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
TMK 3102-Chap 4: Input & Output 4
Escape Character \n newline. Position the cursor to the beginning of next line \t horizontal tab. Move the cursor to the next tab stop \r carriage-return. Position the cursor to the beginning of the current line, do not advance to the next line \\ used to print a backslash character \’ Single quote. Used to print single quote character \” double quote. Used to print double quote character
TMK 3102-Chap 4: Input & Output 5
Using Scanner Class For input import java.util.*; public class UsingScannerClass { age 17 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
TMK 3102-Chap 4: Input & Output 6
Displaying in Graphics You can use the showMessageDialog method in the JOptionPane class. JOptionPane is one of the many predefined classes in the Java system, which can be reused rather than “reinventing the wheel.”
TMK 3102-Chap 4: Input & Output 7
The showMessageDialog Method JOptionPane.showMessageDialog(null, "Welcome to Java!", "Example 1.2 Output", JOptionPane.INFORMATION_MESSAGE));
TMK 3102-Chap 4: Input & Output 8
Two Ways to Invoke the Method There are several ways to use the showMessageDialog method. For the time being, all you need to know are two 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 is a 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.
TMK 3102-Chap 4: Input & Output 9
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"); } }
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");
} }
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("What is Your Name?"); JOptionPane.showMessageDialog(null,"Hi!!! "+name+ "\nWelcome\nTo\nJava\nWorld");
} }
TMK 3102-Chap 4: Input & Output 12
Exercise Write the java program using the given pseudo code. 1. Start 2. Get the value of radius 3. Calculate area where area = π x r x r 4. Display area 5. End