You are on page 1of 5

COMPUTER APPLICATIONS Class X

TOPIC :- Input in Java (Lesson 10)

COMPUTER APPLICATIONS
CLASS – X

TOPIC :- Input in Java (Lesson 10)

Initialization:

Java designers believe every variable should be properly initialized. To initialize


a variable is to give it a correct initial value.

Syntax:
<variable>= <expression / constant value>

Example :
int A,B;
A=10;
B=5+A;

Initialization takes place in following two ways:


• Static initialization
• Dynamic initialization

Static initialization
The process uses direct assignment of a constant to a declared variable. The
variable is initialized by the programmer in the coding.
Example:
int x;
x=5;
double y;
y=9.4;

Dynamic initialization

When a variable gets initialised at runtime, i.e, during the time of execution of
program logic is termed as dynamic initialization. Under this situation a
variable is assigned with the outcome of any arithmetical operation or function.

Example:

int x=5,y=9,z;

z=x+y*10;

Page 1
COMPUTER APPLICATIONS Class X
TOPIC :- Input in Java (Lesson 10)

Parameters

Information can be passed to methods as parameter. Parameters act as


variables inside the method.

Parameters are specified after the method name, inside the parentheses. You
can add as many parameters as you want, just separate them with a comma.

The following example has a method that takes two int variables called a,b as
parameters. When the method is called, we pass the values, which is used
inside the method to print the sum of two values.

Example

public class Cal{

static void myMethod(int a, int b) {

System.out.println(“ sum is" + (a+b));

Java Package

A java package is a group of similar types of classes, interfaces and sub-


packages.

Package in java can be categorized in two form, built-in package and user-
defined package.

Packages are used for:


• Preventing naming conflicts. For example there can be two classes
with name Employee in two packages, college.staff.cse.Employee and
college.staff.ee.Employee
• Making searching/locating and usage of classes.
• Providing controlled access: protected and default have package level
access control. A protected member is accessible by classes in the
same package and its subclasses. A default member (without any
access specifier) is accessible by classes in the same package only.
• Packages can be considered as data encapsulation (or data-hiding).

Page 2
COMPUTER APPLICATIONS Class X
TOPIC :- Input in Java (Lesson 10)

Types of packag

Built-in Packages
These packages consist of a large number of classes which are a part of
Java API.Some of the commonly used built-in packages are:
1) java.lang: Contains language support classes(e.g classed which defines
primitive data types, math operations). This package is automatically
imported.
2) java.io: Contains classed for supporting input / output operations.
3) java.util: Contains utility classes which implement data structures like
Linked List, Dictionary and support ; for Date / Time operations.
4) java.applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the components for graphical
user interfaces (like button , ;menus etc).
6) java.net: Contain classes for supporting networking operations.

In Java, the import statement is used to bring certain classes or the entire
packages, into visibility. As soon as imported, a class can be referred to directly
by using only its name.

Below is the general form of the import statement :


import pkg1[.pkg2].(classname|*);

Here, pkg1 is the name of top-level package, and pkg2 is the name of
subordinate package inside outer package separated by dot (.). There is no
practical limit on the depth of a package hierarchy, except that imposed by the
file system. Now, you specify either an explicit classname or a star (*), indicates
that the Java compiler should import the full package.

Below code fragment shows both forms in use:


import java.util.Scanner;
import java.io.*;

Page 3
COMPUTER APPLICATIONS Class X
TOPIC :- Input in Java (Lesson 10)

User-defined packages
These are the packages that are defined by the user. First we create a
directory myPackage (name should be same as the name of the package).
Then create the MyClass inside the directory with the first statement being
the package names.

// Name of the package must be same as the directory


// under which this file is saved
package myPackage;
public class MyClass
{
public void getNames(String s)
{
System.out.println(s);
}
}

Scanner Class :

Scanner is a class in java.util package used for obtaining the input of the
primitive types like int, double, etc. and strings. It is the easiest way to read
input in a Java program.

• To create an object of Scanner class, we usually pass the predefined


object System.in, which represents the standard input stream.
• To read numerical values of a certain data type XYZ, the function to use
is nextXYZ(). For example, to read a value of type short, we can use
nextShort()
• To read strings, we use nextLine().
• To read a single character, we use next().charAt(0). next() function
returns the next token/word in the input as a string and charAt(0)
function returns the first character in that string.

Method Description
nextBoolean() Reads a boolean value from the user
nextByte() Reads a byte value from the user
nextDouble() Reads a double value from the user
nextFloat() Reads a float value from the user
nextInt() Reads a int value from the user
nextLine() Reads a String value from the user
nextLong() Reads a long value from the user
nextShort() Reads a short value from the use

Page 4
COMPUTER APPLICATIONS Class X
TOPIC :- Input in Java (Lesson 10)

Example:

import java.util.Scanner;

class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);

System.out.println("Enter name, age and salary:");

// String input
String name = myObj.nextLine();

// Numerical input
int age = myObj.nextInt();
double salary = myObj.nextDouble();

// Output input by user

System.out.println("Name: " + name);


System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
}

Page 5

You might also like