csc1030 hands-on introduction to java introductory lab

18
CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab

Upload: morris-ball

Post on 14-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab

CSC1030HANDS-ON INTRODUCTION

TO JAVA Introductory Lab

Page 2: 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

Page 3: CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab

The Second Java Programclass 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); }}

Page 4: CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab

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

Page 5: CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab

Getting User Input

import javax.swing.JOptionPane;

class Main{

public static void main (String [ ] args) {

JOptionPane.showInputDialog("What is your name?"); }

}

Page 6: CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab

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]

Page 7: CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab

Storing the User Input Textimport javax.swing.JOptionPane;

class Main{

public static void main (String [ ] args) { String answer; // declare a variable

answer = JOptionPane.showInputDialog( "What is your name?"); }

}

Page 8: CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab

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.

Page 9: CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab

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

Page 10: CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab

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

Page 11: CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab

Processing the User Input Textimport 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); }

}

Page 12: CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab

Converting the User Input Textimport 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)); }}

Page 13: CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab

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

Page 14: CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab

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

Page 15: CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab

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 < 1) || (b > 9)

Logical Operator NOT !(a > b)

LHS is a storage!

Page 16: CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab

Syntax of if…else statementif (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.

Page 17: CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab

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

Page 18: CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab

Any Enquiry?