CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab
Outline Introduction to Java a modern programming language Java Syntax: Variables and Operations Basic Output Getting User Input Storing Text Using String String Processing and Integer Java Programming Lab and Exercise
The Second Java Program class Main { /*... */ public static void main (String [ ] args) { double radius; // declare a variable radius = 5.7; double area; // declare another variable area = radius * radius * 3.14; System.out.println("Circle Area = " + area); }
Output: Printing We often print some text on the screen. The command is awfully long in Java… Do NOT miss the punctuations. Note also the letter cases. Examples: System.out.println("Hello world!"); System.out.println("2 x 3 = " + 2*3); System.out.println("H\nE\nY\n!");
Getting User Input import javax.swing.JOptionPane; class Main { public static void main (String [ ] args) { JOptionPane.showInputDialog("What is your name?"); }
JOptionPane.showInputDialog("…"); It pops up a dialog box and waits for response from the user. The user can type some text. Notice the first line in the program: import javax.swing.JOptionPane; This line is mandatory. NetBeans can help filling in this line for us using [Source] [Fix Imports]
Storing the User Input Text import javax.swing.JOptionPane; class Main { public static void main (String [ ] args) { String answer; // declare a variable answer = JOptionPane.showInputDialog( "What is your name?"); }
String Data Type Recall that double is a data type defined in Java for storing numbers. String is another data type defined in Java for storing text. We use the String type to declare a variable named answer.
Storing a String We use the assignment operator to store a String: String address; address = "SHB12X, HSH Engg Bldg, CUHK"; String full_address; full_address = address + ", HONG KONG"; String answer; answer = JOptionPane.showInputDialog("...");
Storing a String from the User The instruction JOptionPane.showInputDialog("...") returns a String result that we can store. So, we have this: // store the result (text from the user) answer = JOptionPane.showInputDialog("...");
Processing the User Input Text import javax.swing.JOptionPane; class Main { public static void main (String [ ] args) { String answer; // declare a variable answer = JOptionPane.showInputDialog( "What is your name?"); // say Hello to the user System.out.println("Hello, " + answer); }
Converting the User Input Text import javax.swing.JOptionPane; class Main { public static void main (String [ ] args) { String answer; // declare a variable answer = JOptionPane.showInputDialog( "How old are you?"); int age; // declare an integer variable age = Integer.parseInt(answer); // do a calculation System.out.println("You will die at " + (age + 1)); }
What's the difference? import javax.swing.JOptionPane; class Main { public static void main (String [ ] args) { String answer; // declare a variable answer = JOptionPane.showInputDialog( "How old are you?"); System.out.println("Next year you will be " + (answer + 1)); }
Converting the User Input Text int is another data type in Java. An integer variable stores integral number. We cannot do calculation with text/ String. We convert a String to an integer by the magic: age = Integer.parseInt(answer);
Relational and Boolean Operators Assignment operator x = 7 Equality operator (a + b) == 7 Inequality operator (b * b) != 9 Comparison operators > = <= Logical Operator AND (a > 3) && (a < 5) Logical Operator OR (a 9) Logical Operator NOT !(a > b) LHS is a storage!
Syntax of if…else statement if (condition) statement1; else statement2; statement3; If condition is true, then execute statement1. If condition is false, then execute statement2. statement3 is executed regardless of the value of condition.
if … else … class Main { public static void main (String [ ] args) { double UV_level = 12; // declare a variable // make a decision if ( UV_level > 10 ) { System.out.println("UV is high!"); System.out.println("UV is high!!!!!"); } else { System.out.println("UV is low!"); System.out.println("UV is low!!!!!!"); }
Any Enquiry?