0% found this document useful (0 votes)
24 views8 pages

Unit - III Computer Notes

The document discusses the basic structure of a Java program, including comments, classes, and the main method. It also covers taking input using various methods like assignment statements, function arguments, the Scanner class, and Stream Reader class. Finally, it discusses flow of control in programs, including normal sequential flow and using statements to change the flow.

Uploaded by

fAwx aRt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views8 pages

Unit - III Computer Notes

The document discusses the basic structure of a Java program, including comments, classes, and the main method. It also covers taking input using various methods like assignment statements, function arguments, the Scanner class, and Stream Reader class. Finally, it discusses flow of control in programs, including normal sequential flow and using statements to change the flow.

Uploaded by

fAwx aRt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Unit III: Compiling and Executing Programs

Basic structure in Java programming:

1. Documentation section/comments suggested, optional

2. Declaration of a classcompulsory

[access specifier] class <classname>

{ beginning of the class

Declaration of main() function

public static void main(String args[])

{ beginning of main from where program starts.

Body of main()

} end of main

}end of class.

Here, access specifier may be public or private. But it is optional.

Comment statement in java programming:

Comments are the non-executable statements that are not read by a


compiler. It is written just to improve the readability of the program.

Types of Comments:

1. Single-Line comment: used to write a comment in single line only.

Syntax: -
// comments.

e.g. // program to add two numbers.

2. Multiline Comment: used to write comments in multiple lines.

Syntax: -

/*opening

Statements

*/closing

e.g. /* program to find sum

of two numbers and store in

third variable*/

3. Documentation Comment: used for preparing documentation of


the software.

Syntax: -

/**-------

-----------*/

It is also multiline but it is specially designed to write documentation


of the software.

Taking input:

There are various ways to input the values on an operation. This can
be done by the following ways:

1. By using assignment statement.

2. By using function argument.


3. By using Scanner class.

4. By using Input Stream Reader class.

1. By using assignment statement.

// to find the sum of two numbers by using assignment statement

class sum

{ public static void main(String args[])

{ int a=20,b=30,c;

c=a+b;

System.out.println(“sum=”+c);

}}

Output: sum=50

2. By using function argument:

This is one of the methods to accept the value from the user at the
time of execution of the program. The variables must be defined
within main() as arguments.

Example: public static void main(int a,int b)

/* program to find sum and average of two numbers by using


function arguments.*/

class sum

public static void main(int a,int b)

{
int sum,avg;

sum=a+b;

avg=sum/2;

System.out.println(“sum=”+sum);

System.out.println(“average=”+avg);

}}

3. By using Scanner class:

There are some steps that you all must follow in order to take input
from Scanner class.

Step1: import java.util.*;

This must be the first line in your program after comments.

Step2: Now make object of Scanner class which is defined in java.util


package.

Syntax: Scanner objectname=new Scanner(System.in);

e.g.: Scanner sc=new Scanner(System.in);

Step3: Scanner class provides us various predefined functions to take


different types of inputs from users. Various functions are as follows:

(1). sc.nextInt() used to take integer input.

e.g. int x;

x=sc.nextInt();

(2) . sc.nextFloat() used to take floating type of input from users.

e.g. float x;
x=sc.nextFloat();

(3). sc.nextDouble() used to take double type of input from users.

e.g. double x;

x=sc.nextDouble();

(4). sc.next()used to take single string as an input.

e.g. String str;

str=sc.next();

(5). sc.nextLine() used to take multiple strings as input.

e.g. String str;

str=sc.nextLine();

// example of taking various types of inputs by using Scanner class.

import java.util.*;

class Input

public static void main(String args[])

Scanner sc=new Scanner(System.in);

int x;

float y;

double z;

String str;
System.out.println(“enter an integer”);

x=sc.nextInt();

System.out.println(“enter a floating type of value”);

y=sc.nextFloat();

System.out.println(“enter a double type of value”);

z=sc.nextDouble();

System.out.println(“enter any single string”);

str=sc.next();

System.out.println(“x=”+x+”\ty=”+y+”\tz=”+z+”\tstr=”+str);

}}

4. By using Stream Reader class:

This system uses two types of classes, viz. ‘InputStream’ and


‘BufferedReader’.

1.InputStream class: - the streamlining of bits are done by using this


class.

2. BufferedReader class: - a computer system has a high speed


buffer device in between input unit and processor. In order to
synchronise the speeds of I/O devices and the processor,
BufferedReader class is used so that instant supply of data can be
done to the CPU without any delay.

Steps: -

1. import java.io.*;

2. class <classname>
{

public static void main(String args[]) throws IOException

Body

Where throwskeyword

IOExceptionpredefined class.

Sometimes, the program experiences I/O errors. To overcome errors


related with I/O throws IOException is required.

3. create an object of InputStream type.

InputStreamReader read=new InputStreamReader(System.in);

4.BufferedReader in=new BufferedReader(read);

This statement is used to create an object ‘in’ to store the input data
from the keyboard temporarily.

Syntax to accept numeric data:

 To accept an integer
int n;
n=Integer.parseInt(in.readLine());
 To accept float value
float n;
n=Float.parseFloat(in.readLine());
 To accept double type value
double n;
n=Double.parseDouble(in.readLine());
 To accept character
char ch;
ch=(char)(in.read());
 To accept a string
String str;
str=in.readLine();

FLOW OF CONTROL: -

In the normal course of execution the control reaches the main()


function straightway and goes on operating the statements
sequentially from beginning to end.

But sometimes we can change the normal flow of control by using


various control statements. The flow of control can be classified into
various ways:

1. Normal flow of control.

2. Bi-directional flow of control.

3. Multiple branching of control.

Normal flow of control

In this system all lines of programs runs sequentially from beginning


to end. As soon as one line has been executed, the next line is
followed for further proceeding.

Bi-directional flow of control

You might also like