Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab.

Similar presentations


Presentation on theme: "CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab."— Presentation transcript:

1 CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab

2 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

3 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); }

4 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!");

5 Getting User Input import javax.swing.JOptionPane; class Main { public static void main (String [ ] args) { JOptionPane.showInputDialog("What is your name?"); }

6 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]

7 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?"); }

8 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.

9 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("...");

10 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("...");

11 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); }

12 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)); }

13 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)); }

14 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);

15 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!

16 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.

17 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!!!!!!"); }

18 Any Enquiry?


Download ppt "CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab."

Similar presentations


Ads by Google