Yanal Alahmad Yanal.tg@gmail.com Java Workshop Yanal Alahmad Yanal.tg@gmail.com.

Slides:



Advertisements
Similar presentations
Introduction to Java Applications
Advertisements

Chapter 3 Introduction to Classes, Objects, Methods, and Strings
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
IT151: Introduction to Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 2 - Introduction to Java Applications
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CMT Programming Software Applications
Chapter 2 - Introduction to Java Applications
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Java Java SE 8 for Programmers Paul Deitel Harvey Deitel Deitel Developer Series 2014.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Java Applications & Program Design
 2005 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming - Classes, Objects Methods, Strings 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 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.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Introduction to Java Applications
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 6 JavaScript: Introduction to Scripting
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Java Primer 1: Types, Classes and Operators
Yanal Alahmad Java Workshop Yanal Alahmad
2.5 Another Java Application: Adding Integers
Chapter 2 Introduction to Java Applications
Chapter 2 Introduction to Java Applications
Data types and variables
Java Programming: From Problem Analysis to Program Design, 4e
Fundamentals of Java Programs, Input/Output, Variables, and Arithmetic
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
Chapter 3 Introduction to Classes, Objects Methods and Strings
Starting JavaProgramming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 2 - Introduction to Java Applications
Chapter 2: Basic Elements of Java
MSIS 655 Advanced Business Applications Programming
elementary programming
Introduction to Java Applications
Object Oriented Programming in java
Primitive Types and Expressions
Introduction to C Programming
Presentation transcript:

Yanal Alahmad Yanal.tg@gmail.com Java Workshop Yanal Alahmad Yanal.tg@gmail.com

Deitel&Deitel [ Java How to Program, 9th ]

First Program Every Java program consists of at least one class A class is a blueprint from which individual objects are created class is a keyword (reserved word) By convention, class name begin with a capital letter A class name is an identifier Identifier: a series of characters consisting of letters, digits, underscores (_) and dollar signs ($) that does not begin with a digit and does not contain spaces Java is case sensitive—uppercase and lowercase letters are distinct

First Program For now: every class we define begins with the public keyword public class must be defined in file with the same name of the class .java extension Java class declarations normally contain one or more methods For a Java application, one of the methods must be called main() which JVM calls automatically public static void main(String args[]) Java program processing starts from the main() method which is a mandatory part of every Java program void return nothing

Example for Student Class public class Student { int id; String firstName; String lastName double gpa; public double getGPA() { } public void register(int id)

First Program The System.out object is known as the standard output object System.out.println method takes string as argument and display it on the command window Every Statement in java ends with semicolon ; Comments in Java End-of-line comment // Multiple lines comment /* …… */ Difference between System.out.print() and System.out.println()

Escape Character The backslash (\) is an escape character \n: new line \t: horizontal tab \\: print \ \”: print “

printf() Displays formatted data System.out.printf( "%s\n%s\n","Welcome to", "Java Programming!" ); Arguments are separated in a comma-separated list

Adding Integers A great strength of Java is its rich set of predefined classes that you can reuse These classes are grouped into packages import declaration that helps the compiler locate a class Package: java.util Class: Scanner

Variables A variable is a location in the computer’s memory where a value can be stored for use later in a program All Java variables must be declared with a name and a type before they can be used Other types of data include float and double, for holding real numbers and char, for holding character data String holds set of characters The types int, float, double, boolean and char are called primitive types Note they are keywords and must appear in all lowercase Several variables of the same type may be declared in a single declaration with the variable names separated by commas int number1, number2, sum;

Object Every thing around us can be an object Cars Humans Cats Each object has a state and behavior

Adding Integers Prompt the user for the input Note: class System is part of package java.lang Note: By default, package java.lang is imported in every Java program Scanner object nextInt() method to obtain an integer from the user at the keyboard assignment operator = is called a binary operator it has two operands Portions of statements that contain calculations are called expressions

Deitel&Deitel [ Java How to Program, 9th ] Arithmetic Operators Deitel&Deitel [ Java How to Program, 9th ]

Deitel&Deitel [ Java How to Program, 9th ] Relational Operators A condition is an expression that can be true or false Deitel&Deitel [ Java How to Program, 9th ]

Declare Class and Instantiating an Object of a Class Recall that main is a special method that’s always called automatically by the Java Virtual Machine (JVM) when you execute an application Method declared as public to indicate that the method is “available to the public” and can be called from any other class Return type: specifies the type of data the method returns to its caller after performing its task. Name of the method Parameters: method can take empty parameters Method header: access modifier + return type + method name + parameters including the parentheses

Declare Class and Instantiating an Object of a Class The body of a method contains one or more statements that perform the method’s task Class GradeBook is not an application because it does not contain main() static method is special, because you can call it without first creating an object of the class in which the method is declared Keyword new creates a new object of the class specified to the right of the keyword Call the method objectName.methodName()

Declaring a Method with a Parameter public void displayMessage(String coursName) { System.out.println( "Welcome to the Grade Book!" ); } Scanner input = new Scanner( System.in ); GradeBook myGradeBook = new GradeBook(); System.out.println( "Please enter the course name:" ); String nameOfCourse = input.nextLine(); System.out.println(); myGradeBook.displayMessage( nameOfCourse );

classes GradeBook and GradeBookTest are in the same default package classes GradeBook and GradeBookTest are in the same default package. Classes in the same package are implicitly imported

Instance Variables Variables declared in the body of a particular method are known as local variables and can be used only in that method When that method terminates, the values of its local variables are lost Attributes are represented as variables in a class declaration. Such variables are called fields (instance variables) and are declared inside a class declaration but outside the bodies of the class’s method declarations. Each object (instance) of the class has a separate instance of the variable in memory

Variables or methods declared with access modifier private are accessible only to methods of the class in which they’re declared Declaring instance variables with access modifier private is known as data hiding (encapsulation) Unlike local variables, which are not automatically initialized, every field has a default initial value The default value for a field of type String is null variables of types byte, char, short, int, long, float and double are initialized to 0, variables of type boolean are initialized to false

Programs use variables of reference types (normally called references) to store the locations of objects in the computer’s memory Reference-type instance variables are initialized by default to the value null

Initializing Objects with Constructors

If..else

Nested if..else

Dangling-else Problem

Solve Dangling Problem

While loop

Increment and Decrement Operators

For Repetition

Application