Download presentation
Presentation is loading. Please wait.
Published byAmbrose Banks Modified over 9 years ago
1
Computer Programming 2 Lab(1) I.Fatimah Alzahrani
2
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.
3
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
5
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 : //
8
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.
10
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:
11
Which we read as “dorm dot Resident.” This notation is called dot notation
12
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;
15
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 *.
16
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
17
Where is the name of the class and 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.
19
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.
20
Method Declaration Where is a sequence of terms designating different kinds of methods, is the type of data value returned by a method, is the name of a method, is a sequence of values passed to a method, and is a sequence of instructions. Here’s the method declaration for the main method
22
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
24
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.
25
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.
26
The following words are all valid identifiers:
27
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
29
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.
31
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.
32
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.");
34
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
35
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
37
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:
38
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:
40
Exercise: Write a programme to input both the first name and the last name. *Using nextLine() to input String method
41
Output:
42
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
43
In addition to the int data type, we have five input methods that correspond to the other numerical data types
44
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
45
Valid Invalid
46
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.
47
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
48
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.
49
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.
50
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
51
Reference (BOOK) : A Comprehensive Introduction to Object- Oriented Programming with Java, C.Thomas Wu
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.