Documente Academic
Documente Profesional
Documente Cultură
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
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
Pure interpretation
Hybrid interpretation
COMPILATION
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
class Hello
{
//your program starts executing from main()
public static void main(String args[])
{
System.out.println(“Hello World”);
}
}
ENTERING THE CODE