[Go to site: main page, start]

0% found this document useful (0 votes)
17 views6 pages

Java Input Basics for Class IX Students

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

Java Input Basics for Class IX Students

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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;
[Link]("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 [Link].*;

Raju Xavier | 9446748197 | [Link] 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([Link]); //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 = [Link]();
8. Print statements
The built-in methods to get output are the print statements. E.g.:
[Link](“Hello”);
[Link](“dear”);
Difference between print and println() statements with an example:
[Link]("Anu ");
[Link]("George");
[Link]("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([Link]);
5. char c = [Link]().charAt(0);
6. [Link](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 | [Link] 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 | [Link] 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;
[Link]("Name: "+na);
[Link]("Class: "+std+" "+div);
[Link]("Marks: "+m1+", "+m2);
[Link]("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;
[Link]("Name: "+name);
[Link]("Class: "+std+" "+div);
[Link]("Marks: "+m1+", "+m2);
[Link]("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 [Link] statement is required. The import is a keyword and [Link] is a package.
Package
The [Link] is a package. A package contain classes. The Scanner class resides in [Link] 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 | [Link] 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 [Link].*;
public class Student
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter name, standard, division and two marks");
String na=[Link]();
int std=[Link]();
char div=[Link]().charAt(0);
double m1=[Link]();
double m2=[Link]();
double avg=(m1+m2)/2.0;
[Link]("Name: "+na);
[Link]("Class: "+std+“ ”+div);
[Link]("Marks: "+m1+", "+m2);
[Link]("Average: "+avg);
}
}
Statements to Input Various Types of Values
String a = [Link](); To input single word byte a = [Link](); To input byte value
String a = [Link](); To input multiple words short a = [Link](); To input short value
int a = [Link](); To input int value long a = [Link](); To input long value
double a = [Link](); To input double value float a = [Link](); To input float value
char a = [Link]().charAt(0); To input a character boolean a=[Link](); 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 = [Link](); 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 | [Link] 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 | [Link] 6 Input in Java

Common questions

Powered by AI

The 'import' statement in Java is necessary for including classes from packages that are not part of the java.lang package, which is imported by default. It allows the reuse of existing code, promoting modularity. An example where it is essential is when using the Scanner class for input operations, requiring 'import java.util.Scanner;'. This facilitates utilizing methods of the Scanner class for efficient input handling from the console .

Parameter input in Java allows values to be provided at the start of program execution, influencing its behavior dynamically as these values are passed as arguments to methods. An example is a program recording student details: 'public static void main(String name, int std, char div, double m1, double m2)' reads student information via parameters, computes average marks, and prints the details. This method makes the program adaptable to different inputs without code modification .

In Java, a class prototype refers to the first line of the class definition, and a method prototype is the first line of the method definition. These prototypes declare the class or method and its accessibility. An example of a class prototype is 'class Sum', and an example of a method prototype is 'public static void main()'. These prototypes serve as blueprints, defining the basic structure and entry points of the code .

The Scanner class in Java provides methods to read different types of data from user input. For strings, 'sc.nextLine()' is used, while 'sc.next()' handles single words. Numeric types have specific methods: 'sc.nextInt()' for integers, 'sc.nextDouble()' for double values, and 'sc.nextFloat()' for floats. Characters can be read with 'sc.next().charAt(0)', and boolean values can be captured using 'sc.nextBoolean()'. Each method ensures that the input is correctly parsed and assigned to the appropriate variable type .

Comments are crucial in Java programming as they provide explanations or notes about the code, helping developers understand and maintain programs more easily. There are three types of comments: single-line (//), which annotates short remarks; multi-line (/* */), for longer statements spanning multiple lines; and documentation comments (/** */), used for generating external documentation with tools like Javadoc .

Java programming errors can be classified into three types: Syntax errors, Runtime errors, and Logical errors. Syntax errors occur when the programmer violates the language's grammatical rules, such as missing semicolons (';'). Runtime errors occur during execution, such as dividing by zero. Logical errors occur when a program runs without crashing but produces incorrect results, due to errors in the logic, such as adding two numbers but intending to subtract them. Examples include 'int a = 5 / 0;' for Runtime error and 'int sum = 5 - 3;' for Logical error .

The three major ways to input values in a Java program are Initialization, Parameter input, and Input stream using the Scanner class. Initialization involves assigning values directly in the program before it runs, meaning the values are hardcoded. Parameter input allows users to provide values at the start of the program, using variables declared in the method prototype. Input stream with Scanner class allows users to input values during the program's execution, making the program interactive and dynamic .

Tokens in Java are the smallest units of a program, similar to words in a sentence. They include keywords (such as 'public', 'class'), identifiers (names for variables and methods), literals (constant values like numbers), punctuators (symbols such as '{}', '(', ')'), and operators (such as '=', '+'). Each type of token serves a specific role in syntax and semantics, facilitating the structure and function of the program .

Java programs utilize a variety of statement types. For instance, consider: 'int a = 5;' as an initialization; 'int b;' as a declaration; 'b = 10;' as an assignment; 'Scanner sc = new Scanner(System.in);' as an object creation; and 'System.out.println("Hello, World!");' as a print statement. These statements perform distinct functions within a program: defining values, creating objects, and managing input/output .

In Java, keywords 'public' and 'static' play crucial roles in method definitions. The keyword 'public' indicates that the method is accessible from any other class, serving as an access modifier. 'Static' means the method belongs to the class, rather than instances of it, and can be called without creating an object of the class, useful for defining the 'main' method which is the entry point of a Java application .

You might also like