University of Palestine software engineering department Introduction to data structures Introduction...

27
University of Palestine University of Palestine software engineering department software engineering department Introduction to data Introduction to data structures structures Introduction to java application Introduction to java application instructor: Tasneem Darwish instructor: Tasneem Darwish

Transcript of University of Palestine software engineering department Introduction to data structures Introduction...

Page 1: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

University of PalestineUniversity of Palestinesoftware engineering departmentsoftware engineering department

Introduction to data structuresIntroduction to data structures

Introduction to java applicationIntroduction to java application

instructor: Tasneem Darwishinstructor: Tasneem Darwish

Page 2: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

OutlinesOutlines

History of Java

Java Class Libraries

A First Program in Java: Printing a Line of Text

Modifying Our First Java Program

Displaying Text with printf

Page 3: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

History of JavaHistory of Java

Sun Microsystems in 1991 funded an internal corporateresearch project code-named Green, which resulted in a C++-based language that its creator, James Gosling, called Oak after an oak tree outside his window at Sun

It was later discovered that there already was a computer language by that name. When a group of Sun people visited a local coffee shop, the name Java was suggested, and it stuck.

Page 4: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

History of JavaHistory of Java

The Green project ran into some difficulties. The marketplace for intelligent consumer- electronic devices was not developing in the early 1990s as quickly as Sun had anticipated.

The project was in danger of being cancelled. but the World Wide Web exploded in popularity in 1993, and Sun people saw the immediate potential of using Java to add dynamic content, such as interactivity and animations, to web pages.

Page 5: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

Java Class LibrariesJava Class Libraries

Java programs consist of pieces called classes.

Classes include pieces called methods that perform tasks and return information when they complete them

Page 6: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

Java Class LibrariesJava Class Libraries

Programmers can create each piece they need to form Java programs.

However, most Java programmers take advantage of the rich collections of existing classes in the Java class libraries, which are also known as the Java APIs (Application Programming Interfaces).

Page 7: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

A First Program in Java: Printing a Line of A First Program in Java: Printing a Line of TextText

Every time you use a computer, you execute various applications that perform tasks for you. For example, your e-mail application helps you send and receive e-mail.

Computer programmers create such applications by writing computer programs.

Page 8: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

A First Program in Java: Printing a Line of A First Program in Java: Printing a Line of TextText

A Java application is a computer program that executes when you use the java command to launch the Java Virtual Machine (JVM).

Page 9: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

A First Program in Java: Printing a Line of A First Program in Java: Printing a Line of TextText

Page 10: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

A First Program in Java: Printing a Line of A First Program in Java: Printing a Line of TextText

Line 1 begins with //, indicating that the remainder of the line is a comment

This helps other people read and understand your programs.

The Java compiler ignores comments, so they do not cause the computer to perform any action when the program is run.

Page 11: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

A First Program in Java: Printing a Line of A First Program in Java: Printing a Line of TextText

A comment that begins with // is called an end-of-line (or single-line) comment, because the comment terminates at the end of the line on which it appears.

A // comment also can begin in the middle of a line and continue until the end of that line (as in lines 11 and 13).

Page 12: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

A First Program in Java: Printing a Line of A First Program in Java: Printing a Line of TextText

Traditional comments (also called multiple-line comments), such as

This type of comment can be spread over several lines. It begins with the delimiter /* and ends with */. All text between the delimiters is ignored by the compiler.

Page 13: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

A First Program in Java: Printing a Line of A First Program in Java: Printing a Line of TextText

Java also provides Javadoc comments that are delimited by /** and */. As with traditional comments, all text between the Javadoc comment delimiters is ignored by the compiler. Javadoc comments enable programmers to embed program documentation directly in their programs. The javadoc utility program (part of the Java SE Development Kit) reads Javadoc comments and uses them to prepare your program’s documentation in HTML format

Page 14: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

A First Program in Java: Printing a Line of A First Program in Java: Printing a Line of TextText

Line 3 is a blank line. Programmers use blank lines and space characters to make programs easier to read.

Together, blank lines, space characters and tab characters are known as white space. (Space characters and tabs are known specifically as white-space characters.)

White space is ignored by the compiler.

using white space enhance program readability.

Page 15: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

A First Program in Java: Printing a Line of A First Program in Java: Printing a Line of TextText

Page 16: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

A First Program in Java: Printing a Line of A First Program in Java: Printing a Line of TextText

Line 4 begins a class declaration for class Welcome1.

Every program in Java consists of at least one class declaration that is defined by you—the programmer. These are known as programmer defined classes or user-defined classes.The class keyword introduces a class declarationin Java and is immediately followed by the class name (Welcome1). Keywords (sometimes called reserved words) are reserved for use by Java and are always spelled with all lowercase letters. The complete list of Java keywords is shown in Appendix C.

Page 17: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

A First Program in Java: Printing a Line of A First Program in Java: Printing a Line of TextText

By convention, all class names in Java begin with a capital letter and capitalize the first letter of each word they include (e.g., SampleClassName).

A Java class name is an identifier

an identifier a series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ($), it does not begin with a digit and does not contain spaces.

Page 18: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

Class workClass work

Write five valid identifier and five invalid identifiers

Page 19: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

Class workClass workSome valid identifiers are:

Welcome1,

$value,

_value,

m_inputField1

button7.

Some invalid identifiers are:

1 welcome, 3button, input field, m 3 n, $ value

Page 20: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

A First Program in Java: Printing a Line of A First Program in Java: Printing a Line of TextText

Java is case sensitive—that is, uppercase and lowercase letters are distinct, so a1 and A1 are different (but both valid) identifiers.

A left brace (at line 5 in this program), {, begins the body of every class declaration.

A corresponding right brace (at line 13), }, must end each class declaration.

Page 21: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

A First Program in Java: Printing a Line of A First Program in Java: Printing a Line of TextText

Line 7 (the main method) is the starting point of every Java application. The parentheses after the identifier main indicate that it is a program building block called a method.

Java class declarations normally contain one or more methods. For a Java application, exactly one of the methods must be called main, otherwise, the JVM will not execute the application

Page 22: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

A First Program in Java: Printing a Line of A First Program in Java: Printing a Line of TextText

Methods are able to perform tasks and return information when they complete their tasks.

Keyword void indicates that this method will perform a task but will not return any information when it completes its task

The left brace, {, in line 8 begins the body of the method declaration.

A corresponding right brace, }, must end the method declaration’s body (line 11 of the program).

Page 23: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

A First Program in Java: Printing a Line of A First Program in Java: Printing a Line of TextText

Line 9

System.out.println( "Welcome to Java Programming!" );

instructs the computer to perform an action—namely, to print the string of characters contained between the double quotation marks (but not the quotation marks themselves).

Page 24: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

A First Program in Java: Printing a Line of A First Program in Java: Printing a Line of TextText

A string is sometimes called a character string, a message or a string literal.

We refer to characters between double quotation marks simply as strings.

White-space characters in strings are not ignored by the compiler.

Page 25: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

A First Program in Java: Printing a Line of A First Program in Java: Printing a Line of TextText

System.out is known as the standard output object. System.out allows Java applications to display sets of characters in the command window from which the Java application executes

Method System.out.println displays (or prints) a line of text in the command window. The string in the parentheses in line 9 is the argument to the method.

Page 26: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

A First Program in Java: Printing a Line of A First Program in Java: Printing a Line of TextText

Method System.out.println performs its task by displaying (also called outputting) its argument in the command window.

When System.out.println completes its task, it positions the output cursor (the location where the next character will be displayed) to the beginning of the next line in the command window.

Page 27: University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.

A First Program in Java: Printing a Line of A First Program in Java: Printing a Line of TextText

The entire line 9, including System.out.println, the argument "Welcome to Java Programming!" in the parentheses and the semicolon (;), is called a statement.