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