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

05 InputInJava9

Best

Uploaded by

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

05 InputInJava9

Best

Uploaded by

rafeekher.m
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ICSE Computer Applications 1 Input In Java

5. Input in Java
Introduction
Input in general means giving values to a program. In this lesson two ways of input are discussed:
1. Initialising values: Giving values when program writing: E.g.: int a = 5;
2. Input using methods of an Input Stream class Scanner: E.g.: int a = sc.nextInt();
1. Basic Structure of a Program
[import statement]
class definition
{
method definition
{
statements;
}
}
Note: The [ ] means the import statement is optional, i.e., if it is required use it, otherwise
no need. In the following example it is not used as it is not necessary.
An Example Program: Initializing Values
public class Square
{
public static void main()
{
int n = 5;
int sq=n*n;
System.out.println("Square = "+sq);
}
}
Note: In this program no need of import statement, so it is not written.
Description of the Above Program
The whole program part is class definition. A class definition includes method definition,
and a method includes statements in it.
Syntax of Class Definition
[access specifier] class classname
{
method definition
{
statements;
}
}
Note 1: The [ ] means optional, it is not necessary to write the access specifier. The public
keyword is an access specifier. In simple programs it is not necessary to write public.
Instead of public class Sum we can write class Square
Class Definition
public class Square
It is known as class prototype or class header. A class prototype is first line of a class
definition.
public
The public keyword is used to declare that a class can be executed by all other classes (A
program can contain more than one classes). Keywords are words that are reserved for
special purposes. Keywords should be written in small letters; otherwise, syntax errors
will happen. Keywords should not be used as identifiers such as class names, method
names, variable names, etc.
Raju Xavier | 9446748197 | www.rajuxavier.com 1 Input in Java
ICSE Computer Applications 2 Input In Java
class
The class also is a keyword. The class keyword is reserved to define classes only.
Square
The Square is the name that we have given to the class, i.e., the class name. You can give
any name, but to write a meaningful name is better. Writing the first letter of a class name in
capital letter is a better practice, not a rule. Names given to program parts are known as
identifiers. Class names are identifiers.
{
}
The pair of curly braces are known as punctuators. The { } are essential for a class definition.
The main method and statements inside the braces are known as class body.
Method Definition
public static void main()
It is method prototype or method header. A method prototype is first line of a method definition.
public static void
These are keywords that are essential for the main method definition.
main()
It is the name of the method. Every Java program requires main method. The
puntuator () indicates that it is a method.
{}
The pair of curly braces is essential for a method definition. The statements inside the
braces are known as method body.
Statements
int n = 5;
It is an initialisation statement that means value is given to the program when the program
is written. The value 5 will be stored in the memory cell named n when program compiles.
int
The int means a memory cell named n can store only integers. The int is known as
keyword.
n
The n is a variable name. Variables are identifiers. The names given to various program
parts such as class, method, variables etc. are known as Identifiers.
=
The = is known as assignment operator. This operator stores a value in a variable.
5
The 5 is a value. It is termd as literal, constant, data etc.
;
The ; is a punctuator that is used to separate two statements. So it is also known as
separator.
int sq=n*n;
This is a process statement to calculate square of the input number.
*
The * is multiplication operator.
System.out.println("Square: "+sq);
The System.out.println( ); is a print (output) statement to display output on monitor.
“Square: ”
It is a string value. The sequence of characters in “” (double quotes) is a string value or
string literal. The Square: is printed on the monitor without “”.
+
The + with a string is concatenate operator. The concatenate operator is used to join a
value to the string. Here the value in sq is joined to Square:

Raju Xavier | 9446748197 | www.rajuxavier.com 2 Input in Java


ICSE Computer Applications 3 Input In Java

Some Other Initialization Statements


String name = “Anu”; For words and sentences (strings)
char c = ‘A’; For a single character
long mob = 9446748197L; For 10 digit numbers (example: mobile numbers)
double m = 98.5; For decimal pointed numbers
An Example Program: Input Using a Scanner Class Method
import java.util.*;
public class Square
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter an integer");
int n = sc.nextInt();
int sq=n*n;
System.out.println("Square = "+sq);
}
}
Description of the Above Program
Import Statement
The import java.util.*; is known as an import statement.
The import is a keyword. It means connect or link.
The java.util is a package. This packge contains classes like Scanner. The Scanner class
has some methods to input values at runtime.
The * represents all classes in a package including Scanner.
The import java.util.*; connects the program written by the user to the Scanner class in
the java.util package.
Statements For Runtime Input
Scanner sc = new Scanner(System.in);
It is an object creation statement. An object named sc is created for Scanner class.
System.out.println("Enter an integer");
This print statement is a user request to input an integer. This statement is also known as
user prompt.
int n = sc.nextInt();
It is a runtime input statement. It accepts an integer number from the user through the
keyboard and stores it in a variable named n.
sc.nextInt()
The sc is an object created by the user and nextInt() is a method to accept a value at
runtime from keyboard.
Some Other Input Statements
String name = sc.nextLine(); For words and sentences (strings)
char c = sc.next().charAt(0); For a single character
long mob = sc.nextLong(); For 10 digit numbers (example: mobile numbers)
double m = sc.nextDouble(); For decimal pointed numbers
Raju Xavier | 9446748197 | www.rajuxavier.com 3 Input in Java
ICSE Computer Applications 4 Input In Java
2. Various Definitions and Statements
Basic Defenitions
1. Which are two basic definitions in Java?
Class definition and method definitions are two basic definitions in Java.
Class definition example:
public class Sum
{
}
Method definition example:
public static void main()
{
}
Basic Statements
1. What is a statement?
A statement is a group of tokens that convey a complete instruction to the compiler.
E.g.: int a = 5;
1. Import statement
It is used to link classes in a predefined package to a running program.
E.g.: import java.util.*;
2. Variable declaration statement
It is used to declare variable before using it. No value is assigned here.
E.g.: int a;
3. Initialization statement
It is used to initialize a value to a variable.
E.g.: int a = 1;
4. Assignment statement
Assigning a value to a previously declared variable is an assignment statement.
E.g.: a = 5;
5. Object creation statement
Creating an object class as its data type is an object creation statement.
E.g.: Scanner sc = new Scanner(System.in);
The sc is an object and Scanner is a class as data type.
6. Input statement
It is used to read/accept values at run time from user.
E.g.: String a = sc.nextLine();
7. Print statements
The built-in methods to get output are the print statements. E.g.:
System.out.println(“Hello”);
System.out.print(“dear”);
Name the Statements / Definition
1. double m = 99.5;
2. String s;
3. s = “Anu”;
4. Scanner sc=new Scanner(System.in);
5. char c = sc.next().charAt(0);
6. System.out.println(a);
7. public static void main()
8. public class Number
9. public static void main()
{
System.out.println("Hello");
}
10. public class Number
{
public static void main()
{
System.out.println("Hello");
}
}
Raju Xavier | 9446748197 | www.rajuxavier.com 4 Input in Java
ICSE Computer Applications 5 Input In Java
Answer:
1. Initialization statement 6. Print statement
2. Declaration statement 7. Method prototype
3. Assignment statement 8. Class prototype
4. Object creation statement 9. Method definition
5. Input statement 10. Class definition
3. Two Kinds of Print Statements
There are two kinds of print statements in Java.
1) System.out.println()
2) System.out.print()
Example:
System.out.print("A");
System.out.println("B");
System.out.print("C");
Output:
AB
C
Write Output
System.out.print("Dear ");
System.out.println("friend,");
System.out.println("Hope you are fine.");
Output:
Dear friend,
Hope you are fine.
4. Some Basic Tokens
Tokens are smallest individual units in a program. It is like words in a sentence. These are:
Keywords, Identifiers, Literals and Punctators and Operators (KILPO).
Keywords: Words that have special meanings to define/declare classes, methods, variables etc.
public class To define a class
public static void To define main method
int long To declare integer variables
double To declare variables to store numbers with decimal point
char To declare variables to store a single character
Identifiers: To identify program parts
Square Class names:
main() Method name:
n sq Variable names:
sc Object name:
Literals: The values that are stored in variables
5 integer literal
98.5 real literal / floating point literal
“Anu” String literal
‘A’ Character literal
Punctuators: To separate or to group classes, methods, variables, statements etc.
{} For class definition, method definition etc.
() For method definition
. In input statements and print statements
, To separate variables
; To separate statements
Operators: To do an operation.
= (Assignment) To store a value to a variable.
+ (Addition) To add two numbers.
+ (Concatenate) To join a value to a variable.
- (Subtraction) To find difference
* Multiplication To find product
/ Division To find quotient
% Modulus To find remainder
Raju Xavier | 9446748197 | www.rajuxavier.com 5 Input in Java
ICSE Computer Applications 6 Input In Java
Identify the tokens in the statements below:
1. import java.util.*;
Answer:
import Keyword
java.util Identifier - pre-defined package
* A symbol to represent all classes (identifiers) in a package
. ; Punctuators
2. public class Student
{
}
Answer:
public class Keywords
Student Identifier
{} Punctuator
3. public static void main()
Answer:
public static void Keywords
main Identifier
() Punctuator
4. Scanner sc = new Scanner(System.in);
Answer:
Scanner Identifier - Pre-defined class
sc Identifier - user-defined object
= Operator
new Keyword
System.in Identifier - Pre-defined object
() ; Punctuators
5. int a=5,b=3;
Answer:
int Keyword
ab Identifiers (variables)
= Operator
, ; Punctuators
53 Literals
6. int s=a+b;
Answer:
int Keyword
sab Identifiers
= + Operators
; Punctuator
7. sc.nextInt()
Answer:
sc Identifier - user-defined object
. Punctuator – to connect sc and nextInt()
nextInt Identifier - pre-defined method. Method name are with ()
() Punctuator - here to identify a method name
8. System.out.println("Sum = "+s);
Answer:
System.out.println Identifier (Pre-defined)
"Sum = " Literal (String)
+ Operator (Concatenate)
. () ; Punctuators

Raju Xavier | 9446748197 | www.rajuxavier.com 6 Input in Java


ICSE Computer Applications 7 Input In Java

7. Three Kinds of Errors


There are three kinds of errors may happen in a program.
1. Compile time error (Syntax error)
2. Runtime error
3. Logical error
Examples:
Compile time error (Syntax error)
The error happens when violating the rules of a programming language.
E.g.: Public class Sum. A keyword (public) should be in lowercase –this rule is violated.
Runtime error
If the user writes statements without syntax error but an error happens during runtime and
program stops suddenly is a run time error.
E.g.: int n = 5/0; When an integer is divided by 0 the program stops when this statement
executes.
Logical error
If the user writes statements without syntax error or run time error but he gets wrong answer
then it is logical error.
E.g.: To find sum of two values if the user write statement as int sum=5-3, then the output
will be difference (2) not sum (8).
Answer the following:
Find the type of error in the following and write breif explanations:
1. int square=2*n;
2. int a=6,b=0; int n=a/b;
3. Int n=2;
Answer:
1. Logical error. To find square it requires n*n not 2*n.
2. Runtime error. Because the devisor is 0. An integer cannot be divided by 0.
3. Syntax error. The first letter is in caps, all letters should be small letters
8. Comments in a Program
Comments in a program are used to describe or remark, a program or program part to the
reader. There are three comments. These are
1. Single line comment:
//
2. Multi-line comment:
/* */
3. Documentation comment:
/** */

Raju Xavier | 9446748197 | www.rajuxavier.com 7 Input in Java


ICSE Computer Applications 8 Input In Java
Example for Using Various Comments
/* The following program is used to find difference of two integers
and display the output with heading */
public class Difference //Class prototype
{
public static void main()
{
int a=5,b=3; //It is an initialization statement
int d=a-b; //To find difference
System.out.println(“Difference=”+d); //To display output
}
}
/** Written by Hima
Year: 2018 */
Answer the following:
Choose the valid comments:
1. \\to find sum
2. //input statement
3. /* Program to reverse an integer /*
4. /*Example program to convert
meter to centimeter*/
5. /**Version 5.01*/
Answer:
2. //input statement
4. /*Example program to
convert meter to centimeter*/
5. /**Version 5.01*/

Raju Xavier | 9446748197 | www.rajuxavier.com 8 Input in Java

You might also like