CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab.

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Chapter 2 - Introduction to Java Applications
Shlomo Hershkop1 Introduction to java Class 1 Fall 2003 Shlomo Hershkop.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
Introduction to Computer Programming Decisions If/Else Booleans.
Control structures: if-else statements and switch statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
IC211 Lecture 8 I/O: Command Line And JOptionPane.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
CS107 Introduction to Computer Science Java Basics.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
Intro to Java Programming  A computer follows the instruction precisely and exactly.  Anything has to be declared and defined before it can be used.
CS107 Introduction to Computer Science Java Basics.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Review, Pseudocode, Flow Charting, and Storyboarding.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Decisions, Decisions, Decisions Conditional Statements In Java.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
Java Review if Online Time For loop Quiz on Thursday.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
JAVA 1.COMPARISON: JAVA AND C++ 2.KEYBOARD INPUT 3.OUTPUT 4.CONVERSION FROM STRING 5.OPERATORS AND EXPRESSIONS 6.DIALOG BOX – USER PROMPT January
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
Midterm preview.
CSC111 Quick Revision.
Crash course in the Java Programming Language
Introduction to Computer Science / Procedural – 67130
Lecture 2: Data Types, Variables, Operators, and Expressions
2.5 Another Java Application: Adding Integers
Chapter 6 More Conditionals and Loops
SELECTION STATEMENTS (1)
Chapter 2.
Introduction to Programming in Java
OPERATORS (2) CSC 111.
Introduction to C++ Programming
An Introduction to Java – Part I, language basics
Chapter 2 - Introduction to Java Applications
MSIS 655 Advanced Business Applications Programming
AP Java Review If else.
CS110D Programming Language I
Programming 2 Decision-Making.
COM-152: Computer Programming Types, Variables, Operators Part 1 of 2
Chapter 2 Programming Basics.
AP Java Review If else.
Java: Variables, Input and Arrays
F II 2. Simple Java Programs Objectives
Introduction to java Part I By Shenglan Zhang.
Presentation transcript:

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?