Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Yanal Alahmad Yanal.tg@gmail.com Java Workshop Yanal Alahmad Yanal.tg@gmail.com."— Presentation transcript:

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

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

3 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

4 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()

5 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 );

6 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

7 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

8 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

9 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

10 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" );

11 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

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

13 Nested if..else

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

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

16 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

17 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

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

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

20 Switch Control Statement

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

22 Break and continue statements
Run BreakTest program Change Break to continue

23 Logical Operators AND NOT OR

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

25 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

26 Random Class


Download ppt "Yanal Alahmad Yanal.tg@gmail.com Java Workshop Yanal Alahmad Yanal.tg@gmail.com."

Similar presentations


Ads by Google