Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

Similar documents
Research Group. 2: More types, Methods, Conditionals

Data Structure and Programming Languages

Data Structure and Programming Languages

Visual Programming. Lecture 2: More types, Methods, Conditionals

Introduction to Programming Using Java (98-388)

More types, Methods, Conditionals. ARCS Lab.

6.092 Introduction to Software Engineering in Java January (IAP) 2009

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

PROGRAMMING FUNDAMENTALS

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Chapter 3 Selection Statements

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives

In this lab, you will learn more about selection statements. You will get familiar to

Introduction to Computer Science Unit 2. Notes

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Computer Science II (20082) Week 1: Review and Inheritance

Full file at

Chapter 2: Using Data

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Chapter 02: Using Data

Simple Java Reference

JAVA Programming Concepts

Array. Prepared By - Rifat Shahriyar

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

Software and Programming 1

Programming Basics. Digital Urban Visualization. People as Flows. ia

CS 231 Data Structures and Algorithms, Fall 2016

double float char In a method: final typename variablename = expression ;

CT 229 Fundamentals of Java Syntax

Building Java Programs

Pace University. Fundamental Concepts of CS121 1

Chapter 2: Using Data

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Building Java Programs

( &% class MyClass { }

Introduction to Computer Science Unit 2. Notes

AP CS Unit 3: Control Structures Notes

Chapter 2 Primitive Data Types and Operations

Chapter 2 ELEMENTARY PROGRAMMING

CSC 1214: Object-Oriented Programming

Programming with Java

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

CS313D: ADVANCED PROGRAMMING LANGUAGE

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Welcome to the Primitives and Expressions Lab!

Important Java terminology

CS260 Intro to Java & Android 03.Java Language Basics

CS111: PROGRAMMING LANGUAGE II

Java Bytecode (binary file)

Computer Components. Software{ User Programs. Operating System. Hardware

Object-Oriented Programming

Chapter 2 Elementary Programming

b. Suppose you enter input from the console, when you run the program. What is the output?

2/9/2012. Chapter Four: Fundamental Data Types. Chapter Goals

Expressions & Flow Control

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Java Basic Programming Constructs

Zheng-Liang Lu Java Programming 45 / 79

Chapter 2 Primitive Data Types and Operations. Objectives

Getting started with Java

Lecture Set 4: More About Methods and More About Operators

Date: Dr. Essam Halim

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

JAVA OPERATORS GENERAL

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Programming in Java

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Elementary Programming

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

Computer Science II (20073) Week 1: Review and Inheritance

Methods and Data (Savitch, Chapter 5)

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Introduction to Computer Science and Object-Oriented Programming

Eng. Mohammed S. Abdualal

Java is an objet-oriented programming language providing features that support

Programming Language Basics

Chapter 5: Methods. by Tony Gaddis. Starting Out with Java: From Control Structures through Objects. Fourth Edition

Datatypes, Variables, and Operations

CS 302: Introduction to Programming

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Oct Decision Structures cont d

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Computer Components. Software{ User Programs. Operating System. Hardware

Chapter 2 Elementary Programming

Chapter 2 Primitive Data Types and Operations

ECE 122. Engineering Problem Solving with Java

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Object Oriented Programming. Java-Lecture 1

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Transcription:

Java Tutorial Ashkan Taslimi Saarland University Tutorial 3 September 6, 2011 1

Outline Tutorial 2 Review Access Level Modifiers Methods Selection Statements 2

Review Programming Style and Documentation Comments Good Naming Balanced Brackets Space and Indent

Review Primitive Data Types Kinds of values that can be stored and manipulated Integral Types: byte, int, long (-30, 0, 4) Floating-Point Types: float, double (3.14, 2.0, -0.9) Constants final double PI = 3.14159; Casting Large range types to a smaller range conversion Int intvalue = (int) 10.1;

Review Variables Variables have different names according to their location Static Variable Instance Variables Local Variable Parameter

Review Operators Concatenating (+) Example: String a = Winter ; int b = 2012; String c = a + Semester + b; System.out.println(c); // Winter Semester 2012

Review Operators Division operator ( / ) operates differently on integers and doubles Example: 5 / 9 = 0; 5 and 9 are considered as integer 5.0 / 9 = 0.55; 5.0 is double so the result will be double

Review Input/Output classes Console: Output: System.out.println(some String) Input: Scanner Class and next() method for String Dialog Box - JOptionPane class Output: showinputdialog method Input: showinputdialog method Exercise

Outline Tutorial 2 Review Access Level Modifiers Methods Selection Statements Format Specifier Loops 9

Access Level Modifiers Determine whether other classes can use a particular field or invoke a particular method. public class MyClass { private int gear = 0; public void changegear(int newvalue){ gear = newvalue;

Access Level Modifiers public modifier the field is accessible from all classes. private modifier the field is accessible only within its own class.

Access Level Modifiers Visibility of Alpha variables Modifier Alpha Beta Alphasub Gamma public Y Y Y Y private Y N N N

Outline Tutorial 2 Review Access Level Modifiers Methods Selection Statements Format Specifier Loops 13

Methods Return Type Parameter public static void main(string[] arguments) { System.out.println( Welcome ); Method Body

Adding Method public static void NAME() { STATEMENTS To call Method: NAME();

Example public static void printhello{ System.out.println( Hello ); public static void main(string[] args) { printhello();

Example public static void printhello{ System.out.println( Hello ); public static void main(string[] args) { printhello();

Parameters public static void NAME(TYPE NAME) { STATEMENTS To call: NAME(ARGUMENT);

Example public static void printword(string x){ System.out.println(x); public static void main(string[] arguments){ printword( Students ); printword( Teachers );

Example public static void printword(string x){ System.out.println(x); public static void main(string[] arguments){ printword( Students ); printword( Teachers );

Multiple Parameters [ ] NAME(TYPE NAME, TYPE NAME) { STATEMENTS To call: NAME(arg1, arg2);

Example public static void addnumber(int x, int y){ System.out.println(x + y); public static void main(string[] arguments){ addnumber(22, 6);

Example public static void addnumber(int x, int y){ System.out.println(x + y); public static void main(string[] arguments){ addnumber (22, 6);

Return Values public static TYPE NAME() { STATEMENTS return EXPRESSION; void means no type so no return

Example public static int addnumber(int x, int y){ return (x + y); public static void main(string[] arguments){ System.out.println(addNumber(22, 6));

Example public static int addnumber(int x, int y){ return (x + y); public static void main(string[] arguments){ System.out.println(addNumber(22, 6));

Variable Scope Variables live in the block ({) where they are defined (scope) Method parameters are like defining a new variable in the method

Invoking an Object s Method public class NAME{ public void METHOD(){ STATEMENT public static void main(string[] arguments){ NAME object = new NAME(); object. METHOD();

Example public class Calculator{ public static int addnumber(int x, int y){ return (x + y); public static void main(string[] arguments){ Calculator object = new Calculator(); object.addnumber(22, 6));

Mathematical Methods Math.sin(x) Math.random() Math.pow(2, 3) Math.sqrt(4) Exercise

Outline Tutorial 2 Review Access Level Modifiers Methods Selection Statements Format Specifier Loops 31

Selection Statements If-then Statement If-then-else Statement Switch Statement Conditional Expressions

If-then Statement

Example public static void main(string[] arguments){ test(3); test(5); test(7); public static void test(int number){ if (number > 5){ System.out.println(number + " is > 5");

If-then-else

if (CONDITION) { STATEMENTS else if (CONDITION) { STATEMENTS else if (CONDITION) { STATEMENTS else { STATEMENTS else if

Example public static void main(string[] arguments){ test(4); public static void test(int x){ if (x > 5){ System.out.println(x + " is > 5"); else if (x == 5){ System.out.println(x + " equals 5"); else { System.out.println(x + " is < 5");

Switch Statement

Switch Syntax switch (switch-expression) { case value1: statement1; break; case value2: statement2; break; case valuen: statementn; break; default: statement(s)-for-default;

Switch Rules The switch-expression must yield a value of char, byte, short, or int type. The value1,..., and valuen must have the same data type as the value of the switch-expression. The keyword break is optional. The default case, which is optional. The case statements are checked in sequential order

Example switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); case 'c': System.out.println(ch); default: System.out.println( Not found! );

Conditional Expressions booleanexpression? expression1 : expression2; The result of this conditional expression is expression1 if booleanexpression is true; otherwise the result is expression2.

Example if (x > 0) y = 1; else y = -1; OR y = (x > 0)? 1 : -1; Exercise