Sunteți pe pagina 1din 27

DEVELOPING AND

TESTING
A
JAVA PROGRAM
OBJECTIVES
After completing this chapter, you will be able to:
 Identify the components of a class in the Java
Programming Language
 Learn the compilation techniques for creation of
“.class” files
 Learn debugging techniques
 Write a simple Java program
 Dissect the first Java program
 Compile and execute a Java program
COMPONENTS OF A CLASS
 Class is a collection of homogeneous real world objects.
 A single class can be divided into four separate sections,
namely:
 Declaration of a class
 Variable declaration and initialization (optional)
 Methods (optional)
Ordinary Methods
Constructors

main method
 Comments (optional)
EXAMPLE
Filename: Book.java
In order to execute this
public class Book //Declaration of the class
program, you need a
{ main method, within
public int BookId = 0; which you have to create
public String BookName = “Intro to Java”; an object for the Book
public String Author = “Sarada Satapathy”; class and call the
public Double Price = “100.00”; BookInfo() method.
public int Quantityinstock = 0;
//This method displays the information about the
Book. Main Method
public void BookInfo() //Method class CallBook
{ //Declaration of the class
System.out.println(“Book Id : “+BookId); {
System.out.println(“Book Name : “+BookName);
public static void main
System.out.println(“Author : “+Author); (String args[]) //Main Method
System.out.println(“Price of the Book : “+Price);
{
System.out.println(“Quantity in Hand :
“+Quantityinstock); Book ob1 = new
Book(); //Object Creation
}
} ob1.BookInfo();
//Calling Method
}}
CLASS DECLARATION
 Java is purely object oriented - anything we write to solve a
problem has to be written within a class.
 Before you use, a class must be declared, for each class you
designed for the problem domain.
 Syntax for declaring the class is:
[access modifier] class class_identifier
 The class definition is followed by an open curly brace ({),
indicating the beginning of the class_body, the attribute variables
and the methods that compose the class.
 The braces “{ }” around the class_body define where the class
starts and ends.
CLASS NAMING GUIDELINES

 Class names must be meaningful, in mixed case with the first


letter of each word capitalized.
 For example, MyClass.
 They must contain whole words.
 Acronyms and abbreviations are avoided (unless the
abbreviation is much more widely used than the long form,
such as JVM or UML).
 It is not advisable to use any reserve word for declaration of
class name.
VARIABLE DECLARATIONS
AND ASSIGNMENTS
 The declaration of the attributes and its assignment of values is
done just after the first curly brace ({).
 The code for Book class example contains five attribute variable
declarations, and is also assigned some values:

public int BookId = 0; //Attributes


public String BookName = “Intro to Java”; //Attributes
public String Author = “Sarada Satapathy”; //Attributes
public Double Price = “100.00”; //Attributes
public int Quantityinstock = 0; //Attributes
COMMENTS
 Inserting comments is nothing but documentation within
programs.
 It is advisable to put comments in every class that you create
to make it easier to determine the objective and behavior of
the program.
 Commenting is very much useful in longer programs
developed by large teams, where several programmers need
to work on the same code by reading it.
 It helps with the maintenance of a program when new
programmers need to determine what the code is doing.
Comments continued…
 Single-line comments
 A “//” marker tells the compiler to ignore everything to the end of
the current line.
 Example:

public int BookId = 0; //Attributes


//This method displays the information about the Book.

 Multi Line Comments


 A “/*” character combination tells the compiler to ignore everything
on all lines up to, and including, a comment terminating marker
(“*/”).
 Example:
public int BookId = 0;
/*This method ……..
……….. */
Comments continued…

 Documentation Comment
 Thistype of comment must begin with a forward slash
and two asterisks (/**), and end with an asterisk and a
forward slash (*/).
A Java Technology Tool, the Javadoc software, can be
used to create documentation of your class.
METHODS
 Syntax:
[access modifiers] return_type method_name
([arguments],[arguments],[...])
{ method_code_block
............. }
 [access modifiers] specifies the accessibility of the current method
depending on which the other classes/methods/package can have the
accessibility.
 The return_type indicates the type of value that the method returns.
 The method_name is the name of the method.
 The ([arguments]) represents a list of variables whose values are
passed to the method for use by the method.
 The method_code_block is a sequence of statements that the method
performs.
EXAMPLE

public void BookInfo() //Method


{
System.out.println(“Book Id : “+BookId);
System.out.println(“Book Name : “+BookName);
System.out.println(“Author : “+Author);
System.out.println(“Price of the Book : “+Price);
System.out.println(“Quantity in Hand : “+Quantityinstock);
}
COMPILING TECHNIQUES
 All high level languages need to be translated to machine
language before the machine can execute it.
 Typically, a programmer writes language statements in a
language, such as Java or 'C', one line at a time using an 'editor'
(like notepad/textpad).
 The file that is created contains what are called the source
statements. The programmer then runs the appropriate
language translator, specifying the name of the file that
contains the source statements.
 There are three methods of translation available:
Compilation

Pure interpretation
Hybrid interpretation
COMPILATION

 The programs are translated into machine level language and


stored in the machine language or object code form.
 The compiler first parses (or analyzes) all the statements -
one after the other - and the output code, making sure that
statements that refer to other statements are referred to
correctly in the final code.
 Traditionally, the output of the compilation has been called
'object code' or sometimes an 'object module'.
 The object code is the machine code that the processor can
process or execute - one instruction at a time.
PURE INTERPRETATION

 The source code is translated into machine code and


simultaneously executed, one line at a time, by the
software called interpreter.
 The program can be executed only if the interpreter
is present on the machine.
 Execution is much slower than in compiled systems.
HYBRID INTERPRETATION

 The Java compiler converts the source code program into


a generalized machine instruction (the intermediate
code) called byte code, which is then translated by the
Java virtual machine (interpreter) present on the
machine.
 The VM converts each generalized machine instruction
into a specific machine instruction or instructions that
the computer's processor understands.
 This enables Java to be platform independent.
DEBUGGING TECHNIQUES
 Debugging is a method used by software developers to figure
out where the problems (bugs) lie in their code.
 Some common bugs are:
 Typographical errors - <SPIN> instead of <SPAN>
 Omissions – Forgetting to close off a section with a slash,
such as <DIV> followed by a </DIV>
 Syntax errors – Not using the correct syntax for a method
or property
 Logical errors – Can be as simple as forgetting to take into
account what happens to your script when cookies are
turned off, or using the wrong method or property.
Debugging techniques continued…

 Detecting Bugs
 Detecting bugs is a fairly simple task during the early stages of
software development.
 However, the more time you spend looking for bugs, the harder it
becomes to find them.
 Java 2 SDK contains a debugger called jdb, which can be used for
this purpose.
 Repairing Bugs
 The task of repairing bugs, also known as implementing a “bug-
fix”, can be a tiresome and frustrating process.
 Once repaired, however, you need to distribute the changed code
to all of your users.
YOUR FIRST JAVA
PROGRAM
Program name: Hello.java

/* This is a simple Java program. Call this file Hello.java */

class Hello
{
//your program starts executing from main()
public static void main(String args[])
{
System.out.println(“Hello World”);
}
}
ENTERING THE CODE

 In Java, a unit of code or a program must reside in a


class.
 By convention, the name of that class should match
the name of the file that holds the program.
 You should also make sure that the capitalization of the
filename matches the class name.
 The reason for this is that Java is case-sensitive.
DISSECTING THE JAVA PROGRAM
a) The program begins with the following:
/* This is a simple Java program. Call this file Hello.java */
This is a comment. Like most programming languages, Java lets
you enter a remark into a program's source file. The contents of a
comment are ignored by the compiler.

b) The next line of code in the program is shown below:


class Hello {
This line uses the keyword “class” to declare that a new class is
being defined. “Hello” is an identifier that is the name of the
class. The entire class declaration, including all of its members
(data and methods), will be between the opening curly brace ({)
and the closing curly brace (}).
Dissecting continued…

c) The next line in the program is the single-line comment, shown


below:
//Your program starts executing at main:
This is the second style of comment supported by Java. A single-
line comment begins with a “//” and concludes at the end of the
line.

d)The next line of code is shown below:


public static void main(String args[])
This line begins the main() method. As the comment preceding it
indicates, this is the line at which the program will begin
executing. All Java applications begin execution by calling main().
Dissecting continued…

e) The next line of code is the one which occurs within


main():
System.out.println(“Hello World”);
 This line outputs the string “Hello World” followed by a new
line character to screen.
 Output is accomplished by the built-in println() method.
 The first closing brace (}) after the println() statement ends
main(), and the last closing brace (}) ends the Hello class
definition.
COMPILING A JAVA
PROGRAM

 To compile the “Hello” program, execute the compiler,


javac, specifying the name of the source file.
 The javac compiler creates a file called “Hello.class”
that contains the bytecode version of the program.
 To run the program, you must run the Java interpreter,
called java.
 To do so, pass the Hello.class file (it is enough to specify
only the primary name; the java interpreter will anyway
look for a Hello.class file) as a command line argument
to the java interpreter.
Compilation continued…

 To run the program, you must run the Java interpreter,


called java.
 To do so, pass the Hello.class file (it is enough to specify
only the primary name; the java interpreter will anyway
look for a Hello.class file) as a command line argument to
the java interpreter.
EXECUTING A JAVA
PROGRAM

 To execute the program, enter the following at the


prompt:
c:\YourName> java classname.class
 To run your application, a user needs to install the
Java virtual machine (JVM), the Java platform core
classes and various support programs and files.
 This collection of software is known as runtime
environment.
SUMMARY
You now should be able to:
 Identify the components of a class in the Java
Programming Language
 Learn the compilation techniques for creation of
“.class” files
 Learn debugging techniques
 Write a simple Java program
 Dissect the first Java program
 Compile and execute a Java program

S-ar putea să vă placă și