0% found this document useful (0 votes)
4 views17 pages

Chapter 5 - Input in Java

This document covers the concept of packages in Java, explaining their purpose in organizing classes and preventing naming conflicts. It details the Scanner class for reading user input, including methods for various data types. Examples are provided to illustrate how to create and access packages, as well as how to use the Scanner class to obtain user input.

Uploaded by

Sonal Deshpande
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)
4 views17 pages

Chapter 5 - Input in Java

This document covers the concept of packages in Java, explaining their purpose in organizing classes and preventing naming conflicts. It details the Scanner class for reading user input, including methods for various data types. Examples are provided to illustrate how to create and access packages, as well as how to use the Scanner class to obtain user input.

Uploaded by

Sonal Deshpande
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

Blue Ridge Public School

Blue Ridge Public School


Standard IX
Computer Applications
Ch 5 – Input in Java
1
Blue Ridge Public School
Topics
• Packages

• Scanner Class
• Reading User Input

2
Blue Ridge Public School
Packages
• A package is a collection of related classes
• Conceptually you can think of packages as being similar to
different folders on your computer. You might keep HTML
pages in one folder, images in another, and scripts or
applications in yet another
• Packages help organize your classes into a folder structure
and make it easy to locate and use them
• Software written in the Java programming language can be
composed of hundreds or thousands of individual classes
• It makes sense to keep things organized by placing related
classes and interfaces into packages

3
Blue Ridge Public School
Packages – cont.
• Each package in Java has its unique name and organizes its
classes and interfaces into a separate namespace, or name
group
• Although interfaces and classes with the same name cannot
appear in the same package, they can appear in different
packages. This is possible by assigning a separate namespace
to each package
• Advantage of Package
• Java package is used to categorize the classes and interfaces so
that they can be easily maintained
• Packages are used in Java in order to prevent naming conflicts,
to control access, to make searching/locating and usage of
classes, interfaces etc. easier
4
Packages – cont.

Blue Ridge Public School


• Package in java can be categorized in two form, built-in package and user-
defined package
• Built-in packages
• The Java platform provides an enormous class library (a set of
packages) suitable for use in applications
• This library is known as the "Application Programming Interface", or
"API" in short
• Its packages represent the tasks most commonly associated with
general-purpose programming
• For example, a String object contains state and behaviour for character
strings; a Fileobject allows a programmer to easily create, delete,
inspect, compare, or modify a file on the filesystem; various GUI objects
control buttons and checkboxes and anything else related to graphical
user interfaces
• There are literally thousands of classes to choose from. This allows you
to focus on the design of your particular application, rather than the
infrastructure required to make it work 5
• The Java Platform API Specification contains the complete listing for all
packages, interfaces, classes, fields, and methods supplied by the Java
SE platform
Blue Ridge Public School
Packages – cont.
• User-Defined Packages
• Programmers can define their own packages to bundle group of
classes/interfaces, etc.
• It is a good practice to group related classes implemented by you
so that a programmer can easily determine the related classes
and interfaces

6
Blue Ridge Public School
Packages – cont.
• Creating a Package
• While creating a package, a package statement along with a
suitable name of the package needs to be mentioned at the top
of every source file that contains the classes and interfaces that
you want to include in the package
• The package statement should be the first line in the source file.
There can be only one package statement in each source file and
it applies to all types in the file
• If a package statement is not used then the class, interfaces,
enumerations, and annotation types will be placed in the
current default package

7
Blue Ridge Public School
Packages – cont.
• Let us look at an example that creates a package
called mypack. It is a good practice to use names of packages
with lower case letters to avoid any conflicts with the names
of classes and interfaces
package mypack;
public class Simple{
public static void main(String args[]){
[Link]("Welcome to package");
}
}

8
Packages – cont.

Blue Ridge Public School


• How to access package from another package
• There are three ways to access the package from outside the
package
• import package.*;
• import [Link];
• fully qualified name

• Using packagename.*
• If you use package.* then all the classes and interfaces of this
package will be accessible but not subpackages
• The import keyword is used to make the classes and interface of
another package accessible to the current package

9
Packages – cont.

Blue Ridge Public School


• Example of package that import the packagename.*
//save by [Link]
package pack;
public class A{
public void msg(){[Link]("Hello");}
}

//save by [Link]
package mypack;
import pack.*;

class B{
public static void main(String args[]){
A obj = new A();
[Link]();
} 10

}
Output:Hello
Blue Ridge Public School
Packages – cont.
• Using [Link]
• If you import [Link] then only declared class of this
package will be accessible
//save by [Link]

package pack;
public class A{
public void msg(){[Link]("Hello");}
}

//save by [Link]
package mypack;
import pack.A;

class B{
public static void main(String args[]){
A obj = new A();
[Link]();
11
}
}
• Output:Hello
Packages – cont.

Blue Ridge Public School


• Using fully qualified name
• If you use fully qualified name then only declared class of this
package will be accessible
• Now there is no need to import. But you need to use fully
qualified name every time when you are accessing the class or
interface
//save by [Link]
package pack;
public class A{
public void msg(){[Link]("Hello");}
}

//save by [Link]
package mypack;
class B{
public static void main(String args[]){
pack.A obj = new pack.A();//using fully qualified name
[Link](); 12
}
}
• Output:Hello
Blue Ridge Public School
Scanner Class – cont.
• The [Link] class is a simple text scanner which can
parse primitive types and strings using regular expressions
• The Scanner looks for tokens in the input
• A token is a series of characters that ends with what Java calls
whitespace
• A whitespace can be a blank, a tab character, a carriage
return, or the end of file
• The Scanner splits input into substrings, or tokens, separated
by delimiters
• The resulting tokens may then be converted into values of
different types using the various methods
• Scanner in = new Scanner([Link]); // [Link] is an
13
InputStream
Scanner Class – cont.

Blue Ridge Public School


• Let's see an example in which we will input an integer value
from the user
import [Link].*;
class I1{
public static void main(String[] args){
Scanner s = new Scanner([Link]);
[Link]("Enter an integer");
int n;
n = [Link]();
[Link]("The entered integer is" + n);
}
}
• First it is necessary to create an object of type Scanner and
then an argument of object [Link] to the Scanner
constructor
• Here by writing Scanner s, we are declaring s as an object
of Scanner class. [Link] within the round brackets tells
Java that this will be System Input i.e. input will be given to 14
the system
Scanner Class – cont.

Blue Ridge Public School


• Statement n = [Link](); is used to input the value of
an integer variable 'n' from the user
• Here, nextInt() is a method of the object s of the
Scanner class
• Same as nextInt() is used to input an integer value,
methods to input values of other data types are listed
below
Method Inputs
nextInt() Integer
nextFloat() Float
nextDouble() Double
nextLong() Long
nextShort() Short
next() Single word 15

nextLine() Line of Strings


nextBoolean() Boolean
Scanner Class – cont.

Blue Ridge Public School


• Let's see another example which reads the int, string and double value
as an input:
import [Link];
class ScannerTest{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);

[Link]("Enter your rollno");


int rollno=[Link]();
[Link]("Enter your name");
String name=[Link]();
[Link]("Enter your fee");
double fee=[Link]();
[Link]("Rollno:"+rollno+" name:"+name+" fee:"+fe
e);
[Link]();
} 16
}
Thank You!!

17

Blue Ridge Public School

You might also like