computer programming 2 lab(1) i.fatimah alzahrani

51
Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Upload: ambrose-banks

Post on 02-Jan-2016

232 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Computer Programming 2Lab(1)I.Fatimah Alzahrani

Page 2: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Program Components

•The first sample application program Ch2Sample1 is composed of three parts: comment, import statement, and class declaration. These three parts are included universally in Java program.

Page 3: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Comments • In addition to the instructions for computers

to follow, programs contain comments •Comments can explain the purpose of the

program •Comments can explain the meaning of code •Comments can provide any other

descriptions to help programmers understand the program

Page 4: Computer Programming 2 Lab(1) I.Fatimah Alzahrani
Page 5: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

• A comment is any sequence of text that begins with the marker /* and terminates with another marker */ .The beginning and ending comment markers are matched in pairs. You cannot put a comment inside another comment.

• Another marker for a comment is double slashes //. This marker is used for a single-line comment marker. Any text between the double-slash marker and the end of a line is a comment.

• Example: • Multi line comment : /* */ • Single Line comment : //

Page 6: Computer Programming 2 Lab(1) I.Fatimah Alzahrani
Page 7: Computer Programming 2 Lab(1) I.Fatimah Alzahrani
Page 8: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

• The third type of comment is called a javadoc comment. It is a specialized Comment that can appear before the class declaration and other program elements. Comments are intended for the programmers only and are ignored by the computer. Therefore, comments are really not necessary in making a program executable, but they are an important aspect of documenting the program. It is not enough to write a program that executes correctly. We need to document the program, and commenting the program is an important part of program documentation.

Page 9: Computer Programming 2 Lab(1) I.Fatimah Alzahrani
Page 10: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Import Statement • We develop object-oriented programs by using

predefined classes, both system and programmer-defined. In Java, classes are grouped into packages, and the Java system comes with numerous packages. We also can logically group our own classes into a package so they can be reused conveniently by other programs.

• To use a class from a package, we refer to the class in our program by using the following format:

Page 11: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Which we read as “dorm dot Resident.” This notation is called dot notation

Page 12: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

•A package can include subpackages, forming a hierarchy of packages. In referring to a class in a deeply nested package, we use multiple dots. For example, we write:

Javax.swing.JFrame;

Page 13: Computer Programming 2 Lab(1) I.Fatimah Alzahrani
Page 14: Computer Programming 2 Lab(1) I.Fatimah Alzahrani
Page 15: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

•Note: add import statement at the beginning of the program, and import statement is terminated by a semicolon. If we need to import more than one class from the same package, then instead of using an import statement for every class, we can import them all by using asterisk notation *.

Page 16: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Class Declaration •A Java program is composed of one or

more classes; some are predefined classes, while others are defined by us. To define a new class, we must declare it in the program, or make a class declaration. The syntax for declaring the class is

Page 17: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

•Where <class name> is the name of the class and <class member declarations> is a sequence of class member declarations. The word class is a reserved word used to mark the beginning of a class declaration. A class member is either a data value or a method. We can use any valid identifier that is not reserved to name the class.

Page 18: Computer Programming 2 Lab(1) I.Fatimah Alzahrani
Page 19: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

•Note: one of the classes in a program must be designated as the main class.

•If we designate a class as the main class, then we must define a method called main, because when a Java program is executed, the main method of a main class is executed first. To define a method, we must declare it in a class.

Page 20: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Method Declaration

Where <modifiers> is a sequence of terms designating different kinds of methods, <return type> is the type of data value returned by a method, <method name> is the name of a method, <parameters> is a sequence of values passed to a method, and <method body> is a sequence of instructions. Here’s the method declaration for the main method

Page 21: Computer Programming 2 Lab(1) I.Fatimah Alzahrani
Page 22: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Object Declaration •Every object we use in a program must be

declared. An object declaration designates the name of an object and the class to which the object belongs. Its syntax is

Page 23: Computer Programming 2 Lab(1) I.Fatimah Alzahrani
Page 24: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

The first declaration declares an Account object named checking, and the second declaration declares three Customer objects. To declare an object as an instance of some class, the class must be defined already.

Page 25: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Identifier •Any valid identifier that is not reserved

for other uses can be used as an object name. A Java identifier is a sequence of letters, digits, underscores (_), and dollar signs ($) with the first one being a letter. We use an identifier to name a class, object, method, and others.

Page 26: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

The following words are all valid identifiers:

Page 27: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Reserved word •It is an identifier that is used for a specific

purpose and cannot be used for any other purpose, such as for the name of an object, class, method and variables.

•Standard naming convention for Java

Page 28: Computer Programming 2 Lab(1) I.Fatimah Alzahrani
Page 29: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

A Program Template for Simple Java Applications

•The following diagram shows a program template for simple Java applications. You can follow this program template to write very simple Java applications. The structure of the sample program Ch2Sample1 follows this template.

Page 30: Computer Programming 2 Lab(1) I.Fatimah Alzahrani
Page 31: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Sample Java Standard Classes •Eventually, you must learn how to

define your own classes, the classes you will reuse in writing programs. But before you can become adept at defining your own classes, you must learn how to use existing classes.

Page 32: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Standard Output •When a program computes a result, we

need a way to display this result to the user of the program. We output data such as the computation results or messages to the console window (Standard Output Window) via System.out

•We use the print() method to output a value. For example, executing the cod

System.out.print("Hello, Dr. Caffeine.");

Page 33: Computer Programming 2 Lab(1) I.Fatimah Alzahrani
Page 34: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

•Notice that they all appear on the same line. If we want them to appear on individual lines, we can use the println() method instead of print

Page 35: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Standard Input • We have System.in for input. We call the

technique to input data using System.in standard input. System.in accepts input from the keyboard.

• Scanner Class The Scanner class from the java.util package

provides a necessary input facility to accommodate various input routines

To input data from the standard input by using a Scanner object, we first create it by passing System.in

Page 36: Computer Programming 2 Lab(1) I.Fatimah Alzahrani
Page 37: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

•Once we have a Scanner object, then we can input a single word by using its next() method.

•Here’s code to input the first name of a person:

Page 38: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

•Now let’s consider the case in which we want to input both the first name and the last name. We can follow the sample code and input them one by one as follows:

Page 39: Computer Programming 2 Lab(1) I.Fatimah Alzahrani
Page 40: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

•Exercise: Write a programme to input both the first name and the last name.

*Using nextLine() to input String method

Page 41: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Output:

Page 42: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Getting Numerical Input (Scanner Class)

*To input an int value, we use the nextInt() method. Here’s an example of inputting a person’s age

Page 43: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

•In addition to the int data type, we have five input methods that correspond to the other numerical data types

Page 44: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

•Note: The nextDouble() method accepts the value 35 and then converts it to a double data type. The method returns a double value, so even if the user enters an integer, you cannot assign the input to an int variable. See the following codes

Page 45: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Valid

Invalid

Page 46: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Input String and numerical value •Now let’s study how we can mix the input

of strings and numerical values. Consider inputting a horse’s name and age.

Page 47: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

•Note: Everything seems to be working okay. What will happen if the name of a horse has more than one word, such as Sea Biscuit?

The code will not work

Page 48: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

•Because only the first word is assigned to the String variable horseName. Remember that the default delimiter is the white space, so the blank space after the first word is treated as the end of the first input.

The Solution you can use nextLine() method to input String.

Page 49: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Converting String number to Specific data type

We can use class method parseInt( ) of the Integer class to convert a string to an int. Other common conversion methods are parseDouble, parseFloat, and parseLong of the Double, Float, and Long classes, respectively.

• Example : converts "14" to an int value 14 Note: Passing a string that cannot be converted

to an int (e.g., "12b") will result in an error.

Page 50: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Example: 1. Write a code to input the height of a user

in meter (int) and centimetre (float).

2. Write a code to input the full name of a person and his or her age. The full name of a person includes the first name and the last name

Page 51: Computer Programming 2 Lab(1) I.Fatimah Alzahrani

Reference (BOOK) : A Comprehensive Introduction to Object-

Oriented Programming with Java, C.Thomas Wu