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

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

Chapter 3 Introduction to Classes, Objects, Methods, and Strings
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved. 3 3 Introduction to Classes and Objects.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter3: Introduction to Classes and Objects 1.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Java: How to Program Methods Summary Yingcai Xiao.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
The switch Statement, DecimalFormat, and Introduction to Looping
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Introduction to Java Java SE 8 for Programmers Paul Deitel Harvey Deitel Deitel Developer Series 2014.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
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.
The Java Programming Language
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Java How to Program, Late Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
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.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
Classes Methods and Properties. Introduction to Classes and Objects In object-oriented programming terminology, a class is defined as a kind of programmer-defined.
Review for Nested loops & Math class methods & User defined methods.
 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.
A DVANCED P ROGRAMMING C HAPTER 4: I NTRODUCTION TO I NTRODUCTION TO C LASSES, O BJECTS, M ETHODS AND STRINGS Dr Shahriar Bijani Winter 2016.
Topics Instance variables, set and get methods Encapsulation
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 3 Introduction to Classes, Objects Methods and Strings
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Introduction to Modular Programming
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Control Statements: Part 1
Chapter 3: Using Methods, Classes, and Objects
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Dr Shahriar Bijani Winter 2017
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Advanced Programming Chapters 5 & 6: Control Structures
3-4-5 Introduction.
IFS410 Advanced Analysis and Design
3 Control Statements:.
Introduction to Classes and Objects
Classes, Objects, Methods and Strings
2.6 The if/else Selection Structure
Session 2: Introduction to Object Oriented Programming
Object Oriented Programming in java
3-4-5 Introduction.
Visual Programming Lecture 4.
Classes and Objects Systems Programming.
Presentation transcript:

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

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 public GradeBook( String name ) // constructor name is class name { courseName = name; // initializes courseName } GradeBook gradeBook1 = new GradeBook("CS101 Introduction to Java Programming" ); GradeBook gradeBook2 = new GradeBook("CS102 Data Structures in Java" );

Constructor A constructor must have the same name of the class Constructor is a special method that is called once the object is created Constructor can not return values, so it can not specify a return type

if Single-Selection if ….. else if ( studentGrade >= 50 ) System.out.println( "Passed" ); if ….. else if ( studentGrade >= 50 ) System.out.println( "Passed" ); else

Nested if..else

Execute Multiple Statements using Block{ } if ( studentGrade >= 50 ) { System.out.println( "Passed" ); System.out.println(“Can take next course”); }

Conditional Operator (?:) System.out.println( studentGrade >= 50 ? "Passed" : "Failed" );

While loop Example Explain determineClassAverage() method int x=1; while (x<=5) { System.out.println(x); x++; } Explain determineClassAverage() method Enter 10 grades and compute and print the class average

Compound, Increment and Decrement Operators Compound Operator: y= y+4 ; same as y+=4 Post Increment Operator: ++ z++ means z=z+1 Pre Increment Operator: ++ ++z means z=z+1 Show difference in Increment example Decrement Operator: -- K-- means k=k-1

do… while Repetition Statement { statement; } while ( condition ); Run DoWhileTest program Change the counter=0 while(counter>=1)

For Repetition Statement Run ForCounter program Q1) Print 10 to 1 in Decreasing order Q2) Summation of even numbers between 1-10

Switch Control Statement

Switch Example Enter 10 grades between 0-100 and report As, Bs, Cs, Ds, and Fs As the following: grade between 90-100 A grade between 80-89 B grade between 70-79 C grade between 60-69 D grade less than 60 F

Break and continue statements Run BreakTest program Change Break to continue

Logical Operators AND NOT OR

Static Method ClassName.methodName( arguments ) // called through the class name Math.sqrt( 900.0 ) Math is part of the java.lang package

Declaring Methods with Multiple Parameters Q) Write program to take three real numbers and return the maximum among them double result = maximum( number1, number2, number3 ); Note: A static method can call only other static methods of the same class directly

Random Class