Introduction to Java



Similar documents
Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Chapter 2: Elements of Java

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Topic 11 Scanner object, conditional execution

Introduction to Java. CS 3: Computer Programming in Java

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Building Java Programs

Building Java Programs

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Building Java Programs

Object-Oriented Programming in Java

Chapter 2 Introduction to Java programming

Chapter 2 Elementary Programming

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Java Crash Course Part I

JAVA.UTIL.SCANNER CLASS

Lecture 1 Introduction to Java

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

CS 106 Introduction to Computer Science I

6.1. Example: A Tip Calculator 6-1

Programming in Java. What is in This Chapter? Chapter 1

Basics of Java Programming Input and the Scanner class

Chapter 1 Java Program Design and Development

Using Files as Input/Output in Java 5.0 Applications

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Install Java Development Kit (JDK) 1.8

Sources: On the Web: Slides will be available on:

Part I. Multiple Choice Questions (2 points each):

Example of a Java program

Introduction to Java Lecture Notes. Ryan Dougherty

CS 106 Introduction to Computer Science I

AP Computer Science File Input with Scanner

Sample CSE8A midterm Multiple Choice (circle one)

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Java Programming Fundamentals

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

J a v a Quiz (Unit 3, Test 0 Practice)

An Overview of Java. overview-1

Chapter 3. Input and output. 3.1 The System class

Object Oriented Software Design

VB.NET Programming Fundamentals

Programming and Data Structures with Java and JUnit. Rick Mercer

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Pemrograman Dasar. Basic Elements Of Java

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Line-based file processing

AP Computer Science Static Methods, Strings, User Input

Programming Languages CIS 443

Explain the relationship between a class and an object. Which is general and which is specific?

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

13 File Output and Input

Informatica e Sistemi in Tempo Reale

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

JAVA ARRAY EXAMPLE PDF

Java Interview Questions and Answers

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Programming in Java Course Technology, a part of Cengage Learning.

Introduction to Object-Oriented Programming

Week 1: Review of Java Programming Basics

Data Structures Lecture 1

Visit us at

1001ICT Introduction To Programming Lecture Notes

Installing Java (Windows) and Writing your First Program

Computer Programming I

Statements and Control Flow

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Object Oriented Software Design

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Crash Course in Java

Java Programming Unit 1. Your first Java Program Eclipse IDE

Programmierpraktikum

Comp 248 Introduction to Programming

Building Java Programs

Course Intro Instructor Intro Java Intro, Continued

Introduction to Visual C++.NET Programming. Using.NET Environment

Moving from CS 61A Scheme to CS 61B Java

The following program is aiming to extract from a simple text file an analysis of the content such as:

AP Computer Science Java Mr. Clausen Program 9A, 9B

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Quick Introduction to Java

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Introduction to Programming

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

CS 111 Classes I 1. Software Organization View to this point:

WRITING DATA TO A BINARY FILE

1.00/ Session 2 Fall Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types

In this Chapter you ll learn:

Java Basics: Data Types, Variables, and Loops

Transcription:

Introduction to Java The HelloWorld program Primitive data types Assignment and arithmetic operations User input Conditional statements Looping Arrays CSA0011 Matthew Xuereb 2008 1

Java Overview A high level programming language An object-oriented language A platform-independent language Java code is compiled into byte-code The byte-code is interpreted into machine code (Just-in-time compilation) using the JVM Java provides automatic memory allocation and garbage-collection CSA0011 - Matthew Xuereb 2008 2

The HelloWorld program[1/2] /* /* * * First First program program in in Java Java */ */ class class HelloWorld HelloWorld static static void void main(string[] main(string[] args) args) System.out.println("Hello System.out.println("Hello World!!"); World!!"); // // End End of of main main method method Save the code in a file named HelloWorld.java Java is a case sensitive language Comments are closed within /* */ Inline comments starts with a // CSA0011 - Matthew Xuereb 2008 3

The HelloWorld program[2/2] Compiling and running the HelloWorld program Using the OS shell Make sure that the JDK is properly installed and the class path is correctly configured. I recommend to use an IDE Eclipse, NetBeans, JCreator, BlueJ, CSA0011 - Matthew Xuereb 2008 4

Primitive data types There are 8 primitive data types Data Type Values boolean byte short int long float double Char true or false -128..127 [8-bit signed] -32,768..32,767 [16-bit signed] -2,147,483,648..2,147,483,647 [32-bit signed] -9,223,372,036,854,775,808..9,223,372,036,854,775,807 [64-bit signed] 1.40129846432481707e-45..3.40282346638528860e+38 [32-bit signed] 4.94065645841246544e-324d..1.79769313486231570e+308d [64-bit signed] 16-bit Unicode character A string is not a primitive data type String Java has special support for strings and many times they can used just as primitive data types. CSA0011 - Matthew Xuereb 2008 5

Variables declarations class class Variables Variables static static void void main(string[] main(string[] args) args) boolean boolean flag flag true; true; int int x, x, y y,, z; z; double double d d 4.346; 4.346; char char c c 'x'; 'x'; String String s s "Hello"; "Hello"; Identifiers: Must be composed of letters, numbers, the underscore _ and the dollar sign $ May only begin with a letter, the underscore or a dollar sign Reserved words cannot be used as identifiers CSA0011 - Matthew Xuereb 2008 6

Assignment statements The equal sign () is used as the assignment statement. int x 65; y x - 9 + (x * 4); // Assuming that y is an integer String s Hello + World!! ; CSA0011 - Matthew Xuereb 2008 7

Arithmetic Operators Arithmetic operation Addition Subtraction Multiplication Division Remainder Operator Rules of operator precedence + - * / % Algebraic expression f + 7 p - c bm x y or x / y r mod s Java expression F + 7 p c b * m x / y r % s *, / and % operators are applied first. If an expression contains several such operations, the operators are applied from left to right. + and operations are applied next. If an expression contains several such operations, the operators are applied from left to right. CSA0011 - Matthew Xuereb 2008 8

Getting input from console[1/3] Class Scanner is used A Scanner enables a program to read data for use in a program. The data can come from many sources, e.g. file on disk or the user at the keyboard. Syntax: Scanner input new Scanner(System.in); int num1 input.nextint(); // read an integer from user String str input.nextline(); // read a string from user CSA0011 - Matthew Xuereb 2008 9

Getting input from console[2/3] import java.util.scanner; class UserInput static void main(string[] args) // create Scanner to get user input from console Scanner input new Scanner(System.in); // Prompt user to enter his name System.out.println("What is your name?"); // Read name from console String name input.nextline(); // Output welcome message System.out.println("Hello " + name + " :-) Welcome to CSA 0011!!"); CSA0011 - Matthew Xuereb 2008 10

Getting input from console[3/3] Basic numeric and string methods for Scanner Method int nextint() long nextlong() float nextfloat() double nextdouble() String next() String nextline() void close() Returns Returns the next token as an int. If the next token is not an integer, InputMismatchException is thrown. Returns the next token as a long. If the next token is not an integer, InputMismatchException is thrown. Returns the next token as a float. If the next token is not a float or is out of range, InputMismatchException is thrown. Returns the next token as a long. If the next token is not a float or is out of range, InputMismatchException is thrown. Finds and returns the next complete token from this scanner and returns it as a string; a token is usually ended by whitespace such as a blank or line break. If not token exists, NoSuchElementException is thrown. Returns the rest of the current line, excluding any line separator at the end. Closes the scanner. CSA0011 - Matthew Xuereb 2008 11

Conditional statements[1/2] Equality and relational operators Algebraic equality or relational operator Java equality or relational operator Java example Meaning Equality operators x y x is equal to y! x! y x is not equal to y Relational operators > > x > y x is greater than y < < x < y x is less than y > x > y x is greater than or equal to y < x < y x is less than or equal to y Syntax if / if..else / if.. else if.. else switch.. case.. default CSA0011 - Matthew Xuereb 2008 12

Conditional statements[2/2] static void main(string[] args) // create Scanner to get user input from console Scanner input new Scanner(System.in); // Prompt user to enter his age System.out.println("What is your age?"); int age input.nextint(); if(age > 18) System.out.println("You can drive a car!"); else System.out.println("You cannot drive a car!"); CSA0011 - Matthew Xuereb 2008 13

Looping: A loop is a sequence of statements which is specified once but which may be carried out several times in succession. 3 types of loops: for loop while loop do.. while loop CSA0011 - Matthew Xuereb 2008 14

For loop An iteration statement with a counter Example: /* /* * For For loop loop example example */ */ class class ForLoop ForLoop static static void void main(string[] main(string[] args) args) // // A for for loop loop to to display display the the numbers numbers 1-10 1-10 for(int for(int i 1;i 1;i <10; <10; i++) i++) System.out.println(i); CSA0011 - Matthew Xuereb 2008 15

While loop A control flow statement that allows code to be executed repeatedly based on a given boolean condition. The condition is first evaluated - if the condition is true the code within the block is then executed static static void void main(string[] main(string[] args) args) Scanner Scanner input input new new Scanner(System.in); Scanner(System.in); System.out.println("Enter System.out.println("Enter an an integer integer less less then then 100"); 100"); int int num num input.nextint(); input.nextint(); while(num while(num > > 100) 100) System.out.println("Enter System.out.println("Enter an an integer integer less less then then 100"); 100"); num num input.nextint(); input.nextint(); CSA0011 - Matthew Xuereb 2008 16

Do.. While loop A control flow statement that allows code to be executed repeatedly based on a given boolean condition. First, the code within the block is executed, and then the condition is evaluated. If the condition is true the code within the block is executed again static static void void main(string[] main(string[] args) args) Scanner Scanner input input new new Scanner(System.in); Scanner(System.in); int int num; num; do do System.out.println("Enter System.out.println("Enter an an integer integer less less then then 100"); 100"); num num input.nextint(); input.nextint(); while(num while(num > > 100); 100); CSA0011 - Matthew Xuereb 2008 17

Arrays[1/3] A data structure consisting of a group of elements that are accessed by indexing. A collection of related data items. A group of variables containing values that all have the same type. CSA0011 - Matthew Xuereb 2008 18

Arrays[2/3] Declaring and creating arrays int nums[]; nums new int[10]; -- OR - int num[] new int[10]; Declaring array Creating array Declaring and creating array Accessing arrays nums[2] 36; System.out.println(nums[5]); An array of size 10 has an index range from 0 to 9 CSA0011 - Matthew Xuereb 2008 19

Arrays[3/3] Example class class Arrays Arrays static static void void main(string[] main(string[] args) args) Scanner Scanner input input new new Scanner(System.in); Scanner(System.in); // // Create Create an an array array of of size size 10 10 of of type type integer integer int int numbers[] numbers[] new new int[10]; int[10]; // // Ask Ask the the user user to to enter enter 10 10 integers integers to to fill fill the the array array for(int for(int i i 0; 0; i i < < 9;i++) 9;i++) System.out.println("Enter System.out.println("Enter an an integer integer - - "); "); numbers[i] numbers[i] input.nextint(); input.nextint(); // // Now Now display display the the whole whole array array for(int for(int i i 0;i 0;i < < 9;i++) 9;i++) System.out.println(numbers[i]); System.out.println(numbers[i]); CSA0011 - Matthew Xuereb 2008 20