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

05_InputInJavaStatements

This document provides an overview of the basic structure of a Java program, including class and method definitions, various types of statements, and input methods. It explains how to declare variables, initialize values, and use the Scanner class for user input. Additionally, it covers error types in programming and the use of comments in Java code.

Uploaded by

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

05_InputInJavaStatements

This document provides an overview of the basic structure of a Java program, including class and method definitions, various types of statements, and input methods. It explains how to declare variables, initialize values, and use the Scanner class for user input. Additionally, it covers error types in programming and the use of comments in Java code.

Uploaded by

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

Class IX: Chapter 5

Input in Java: Various Inputs


Lesson 1
1. Basic Structure of a Java Program
1. Write the basic structure of a Java program with an example
[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.
Example:
class Sum
{
public static void main()
{
int a=5,b=3;
int s=a+b;
System.out.println("Sum: "+s);
}
}
2. Various Definitions and Statements
1. Which are two basic definitions in Java?
Class definition and method definitions are two basic definitions in Java. E.g.:
Class definition
class Sum
{
}
Method definition
public static void main()
{
}
2. What is a prototype?
First line of class defenition is known as class prototype, and first line of method definition
is known as method prototype. Examples:
1. class Sum
2. public static void main()
Some 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.*;

Raju Xavier | 9446748197 | www.rajuxavier.org 1 Input in Java


ICSE IX 2 Input in Java

3. Variable declaration statement


It is used to declare variable before using it. No value is assigned here.
E.g.: int a;
4. Initialization statement
It is used to initialize a value to a variable
E.g.: int a = 1;
5. Assignment statement
Assigning a value to a previously declared variable is an assignment statement.
E.g.: a = 5;
6. Object creation statement
Assigning something to a variable a class as its data type is an object creation statement.
E.g.: Scanner sc = new Scanner(System.in); //here sc is the object and Scanner is class.
7. Input statement
It is used to read/accept values at run time from user.
E.g.: String a = sc.nextLine();
8. Print statements
The built-in methods to get output are the print statements. E.g.:
System.out.println(“Hello”);
System.out.print(“dear”);
Difference between print and println() statements with an example:
System.out.print("Anu ");
System.out.println("George");
System.out.print("Chinu");
Output:
Anu George
Chinu
Explanation: The print statement does not end the line; so George is in the same line.
The println statement ends the line; so Chinu is in the next line.
Name the kind of statements or 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()
10. class Number
Answers
1. Initialization statement
2. Declaration statement
3. Assignment statement
4. Object creation statement
5. Input statement
6. Print statement
7. Method definition
8. Class definition
9. Method prototype
10. Class prototype

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


ICSE IX 3 Input in Java

5. Tokens Used in The Above Program


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
public
To define a class
class
public
static To define main method
void
int To define integer variables
Identifiers
Class names
method name To identify program parts
variable names
Literals
5 3 etc. Values stored in variables
Punctuators
To separate different elements: classes, methods, variables,
{} () . , ;
statements
Operators
= (Assignment) To store a value to a variable.
+ (Addition) To add two numbers.
+ (Concatenate) To join a value to a variable.

Raju Xavier | 9446748197 | www.rajuxavier.org 3 Input in Java


ICSE IX 4 Input in Java

Lesson 2
Various Inputs
Input means giving values to the program. There are three ways to give values to a program.:
1) Initialization (Giving values in the program itself)
2) Parameter input (User input in the beginning of program)
3) Input stream - Scanner class input (User input during program run)
Initialization of Values
Initialization means values are written to the variables when program writing.
Various Initializations
2. Initialize name, standard, division, mark of computer and maths of a student. Find average.
Print all details.
public class Student
{
public static void main()
{
String na="Anu";
int std=9;
char div='A';
double m1=100,m2=99.5;
double avg=(m1+m2)/2.0;
System.out.println("Name: "+na);
System.out.println("Class: "+std+" "+div);
System.out.println("Marks: "+m1+", "+m2);
System.out.println("Average: "+avg);

}
}
Input of Values Using Parameter
Values are accepted from the user when the program starts not during execution.
Variables are declared individully using comma separator with method prototype (header).
First line of a method definition is known as method prototype or header.
Various Parameter Elements
2. Input through parameter name, standard, division, mark of computer and maths of a
student. Find average. Print all details.
public class Student
{
public static void main(String name, int std, char div, double m1, double m2)
{
double avg=(m1+m2)/2.0;
System.out.println("Name: "+name);
System.out.println("Class: "+std+" "+div);
System.out.println("Marks: "+m1+", "+m2);
System.out.println("Average: "+avg);
}
}
Input Stream - Scanner Class Input
Using the input stream class Scanner values can input during program run. To use the Scanner
class the import java.util statement is required. The import is a keyword and java.util is a package.
Package
The java.util is a package. A package contain classes. The Scanner class resides in java.util package.
To input values methods are required. To use methods object of Scanner class is required. The sc is
an object of Scanner class.

Raju Xavier | 9446748197 | www.rajuxavier.org 4 Input in Java


ICSE IX 5 Input in Java

Input of Various Values Using Scanner Class


Input name, standard, division and mark of a student and print them in neat format.
import java.util.*;
public class Student
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter name, standard, division and two marks");
String na=sc.nextLine();
int std=sc.nextInt();
char div=sc.next().charAt(0);
double m1=sc.nextDouble();
double m2=sc.nextDouble();
double avg=(m1+m2)/2.0;
System.out.println("Name: "+na);
System.out.println("Class: "+std+“ ”+div);
System.out.println("Marks: "+m1+", "+m2);
System.out.println("Average: "+avg);
}
}
Statements to Input Various Types of Values
String a = sc.next(); To input single word byte a = sc.nextByte(); To input byte value
String a = sc.nextLine(); To input multiple words short a = sc.nextShort(); To input short value
int a = sc.nextInt(); To input int value long a = sc.nextLong(); To input long value
double a = sc.nextDouble(); To input double value float a = sc.nextFloat(); To input float value
char a = sc.next().charAt(0); To input a character boolean a=sc.nextBoolean(); To input boolean value
Errors: Three Types
Three types of errors are:
1) Compile time error (Syntax error):
The error happens when violating rules of a programming language.
E.g.1: Public class Sum (A keyword (public) should be in lowercase. This rule is violated.)
E.g.2: int s=a+b ( ; is required to terminate the statement at the end. It should be int s=a+b; )
2) Run time 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.1: int value = 5/0; (During runtime it happens an error. Division by 0 error.)
E.g.2: int a = sc.nextInt(); Input value is 1.5 (During runtime it happens an error. The input
value should be an integer 1.
3) Logical error:
If statements are without syntax error or run time error but the answer is error then it is logical error.
E.g.1: int sum=5-3; (The output is difference not sum.)
E.g.2: double average=6+4/2.0; (The output is 8.0. To get right answer it needs (6+4)/2.0;)
Find kind of error:
1) Int x=7, y=3;
2) int m=10, n=5, remainder=m/n;
3) int x=7, y=0, q=x/y;
Answer:
1) Syntax error
2) Logical error
3) Runtime error

Raju Xavier | 9446748197 | www.rajuxavier.org 5 Input in Java


ICSE IX 6 Input in Java

Comments
A comment is a remark on a program or part of a program. The compiler ignores the comment; it is not
considered as part of the program. It is used for the users.
Comments and their uses with examples.
1) // Single line comment
It is used write a comment in single line: E.g.:
s=a+b; // To find sum
2) /* */ Multi-line comment
It is used when there is more than one line to be remarked. E.g.:
/* The following lines are
to find result of students in a class */
3) /** */ Documentation comment
It is used to write description of a program. E.g.:
/** Automation of library management
© Raju Xavier */

Comments Used in Program


/*The following program is used
to find sum, difference, product, quotient and remainder*/
class Numbers
{
public static void main(int a,int b)
{
int s,d,p,q,r; //Variable declaration
}
}
/**Integer calculator
© James Gosling */

Select valid comments:


1) \\
2) /* \*
3) //
4) /* */
5) */ **/
6) /** */

Answer:

3) //
4) /* */
6) /** */

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

You might also like