Download presentation
Presentation is loading. Please wait.
Published byLynne Cole Modified over 6 years ago
1
Yanal Alahmad Yanal.tg@gmail.com
Java Workshop Yanal Alahmad
2
Deitel&Deitel [ Java How to Program, 9th ]
3
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
4
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
5
Example for Student Class
public class Student { int id; String firstName; String lastName double gpa; public double getGPA() { } public void register(int id)
6
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()
7
Escape Character The backslash (\) is an escape character \n: new line
\t: horizontal tab \\: print \ \”: print “
8
printf() Displays formatted data
System.out.printf( "%s\n%s\n","Welcome to", "Java Programming!" ); Arguments are separated in a comma-separated list
9
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
10
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;
11
Object Every thing around us can be an object
Cars Humans Cats Each object has a state and behavior
12
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
13
Deitel&Deitel [ Java How to Program, 9th ]
Arithmetic Operators Deitel&Deitel [ Java How to Program, 9th ]
14
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 ]
15
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
16
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()
17
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 );
18
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
19
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
20
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
21
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
22
Initializing Objects with Constructors
23
If..else
24
Nested if..else
25
Dangling-else Problem
26
Solve Dangling Problem
27
While loop
28
Increment and Decrement Operators
29
For Repetition
30
Application
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.