0% found this document useful (0 votes)
12 views

L6a structure of Java Prgm

Uploaded by

suiiisimon0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

L6a structure of Java Prgm

Uploaded by

suiiisimon0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

A typical structure of a Java program contains the following elements:

1. Documentation Section
2. Package Declaration
3. Import Statements
4. Interface Section
5. Class Definition
6. Class Variables and Variables
7. Main Method Class
8. Methods and Behaviors
1. Documentation Section

o The documentation section is important but optional for a Java program.


o It includes basic information about a Java program.

o The information includes the author's name, date of creation, version, program name, company
name, and description of the program. It improves the readability of the program.

o Whatever we write in the documentation section, the Java compiler ignores the statements during the
execution of the program.

o To write the statements in the documentation section, we use comments.


o The comments may be single-line, multi-line, and documentation comments.
o Single-line Comment: It starts with a pair of forward slashes (//).
o For example:
//First Java Program

o Multi-line Comment: It starts with a /* and ends with */. We write between these two symbols.
o For example:
/*It is an example of
multiline comment*/

o Documentation Comment: It starts with the delimiter (/**) and ends with */.
o For example:
/**It is an example of a documentation comment*/
Package Declaration

o The package declaration is optional.


o It is placed just after the documentation section.
o we declare the package name in which the class is placed.
o Note that there can be only one package statement in a Java program.
o It must be defined before any class and interface declaration.
o It is necessary because a Java class can be placed in different packages and directories based on the
module they are used.
o For all these classes package belongs to a single parent directory. We use the keyword package to
declare the package name.

o For example:
1. package javatpoint; //where javatpoint is the package name
2. package com.javatpoint; //where com is the root directory and javatpoint is the subdirectory
Import Statements
o The package contains many predefined classes and interfaces.
o If we want to use any class of a particular package, we need to import that class.
o The import statement represents the class stored in the other package.
o We use the import keyword to import the class. It is written before the class declaration and after the
package statement.
o We use the import statement in two ways, either import a specific class or import all classes of a
particular package.
o In a Java program, we can use multiple import statements.

o For example:
1. import java.util.Scanner; //it imports the Scanner class only
2. import java.util.*; //it imports all the class of the java.util package
Interface Section
o It is an optional section. We can create an interface in this section if required.
o We use the interface keyword to create an interface.
o An interface is slightly different from the class. It contains only constants and method declarations.
o Another difference is that it cannot be instantiated.
o We can use interface in classes by using the implements keyword.
o An interface can also be used with other interfaces by using the extends keyword.
For example:
interface car
{
void start();
void stop();
}
Class Definition
o In this section, we define the class.
o It is a vital part of a Java program. Without the class, we cannot create any Java program.
o A Java program may contain more than one class definition.
o We use the class keyword to define the class.
o The class is a blueprint of a Java program. It contains information about user-defined methods,
variables, and constants.
o Every Java program has at least one class that contains the main() method.
For example:
class Student //class definition
{
}
Class Variables and Constants
o In this section, we define variables and constants that are to be used later in the program.
o In a Java program, the variables and constants are defined just after the class definition.
o The variables and constants store values of the parameters. It is used during the execution of the
program.
o We can also decide and define the scope of variables by using the modifiers. It defines the life of the
variables.
For example:
class Student //class definition
{
String sname; //variable
int id;
double percentage;
}
Main Method Class
o In this section, we define the main() method.
o It is essential for all Java programs. Because the execution of all Java programs starts from the main()
method.
o In other words, it is an entry point of the class. It must be inside the class.
o Inside the main method, we create objects and call the methods.

We use the following statement to define the main() method:


public static void main(String args[])
{
}
For example:
public class Student //class definition
{
public static void main(String args[])
{
//statements
}
}
Methods and behavior
o In this section, we define the functionality of the program by using the methods.
o The methods are the set of instructions that we want to perform.
o These instructions execute at runtime and perform the specified task.

o For example:
public class Demo //class definition
{
public static void main(String args[])
{
void display()
{
System.out.println("Welcome to javatpoint");
}
//statements
}
}
When we follow and use the above elements in a Java program, the program looks like the following.

CheckPalindromeNumber.java
/*Program name: Palindrome*/
//Author's name:
/*Palindrome is number or string that will remains the same When we write that in reverse order. Some e
xample of palindrome is 393, 010, madam, etc.*/

//imports the Scanner class of the java.util package


import java.util.Scanner;

//class definition
public class CheckPalindromeNumber
{
//main method
public static void main(String args[])
{
//variables to be used in program
int r, s=0, temp;
int x; //It is the number variable to be checked for palindrome
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number to check: ");
//reading a number from the user
x=sc.nextInt();
//logic to check if the number id palindrome or not
temp=x;
while(x>0)
{
r=x%10; //finds remainder
s=(s*10)+r;
x=x/10;
}
if(temp==s)
System.out.println("The given number is palindrome.");
else
System.out.println("The given number is not palindrome.");
}
}
Output:

You might also like