9.
Arrays, Strings and Vectors
Array:
An Array is a group of contiguous or related data items that share a common name
and different subscript.
It is a set of homogenous data.
Each item in an array is called as element.
To refer an individual element subscript can be used .Subscript must be a positive
integer; it must be enclosed in square brackets and preceded by array name.
Subscript may be a positive integer or a variable or any expression.
If there are n elements in the array, then the subscript ranges from 0 to n-1.
Types of Array:
Arrays are classified into two types:
1. Single/One –Dimensional Arrays
2. Multi-Dimensional Arrays(2D, 3D,…Arrays )
One -Dimensional Array :
A list of items can be given one variable name using only one subscript and such a
variable is called as single-subscripted variable or a one-dimensional array.
0 1 2 3 4
23 30 5 8 10
------------------array length is 5---------------------
Creating an Array:
1. Like any other variables, arrays must be declared and created in the computer memory
before they are used.
2. Creation of array involve three steps
Declaring the array
Creating memory locations
Putting values into the memory locations
Declaring the Array :
Arrays in java may be declared in two forms
Form 1 :
type arrayname[ ];
Form 2 :
type[ ] arrayname;
ex: int a[ ];
int rollno[ ];
float[ ] avg;
Note: we don’t enter the size of the arrays in the declaration
Creation of Arrays:
After declaring an array, we need to allocate memory for it.
Java allows us to create arrays using “new” operator only
arrayname = new type[size];
Ex:
rollno =new int[5];
avg= new float[6];
It is possible to combine the two steps i.e., declaration and creation into one step as:
type arrayname[ ] = new type[size];
ex: int rollno [ ]= new int[ 5] ;
Initialization of arrays :
Page 1 of 17
After creating an array we have to store values into the array. This process is known as
initialization. The syntax for initializing values into the array is
arrayname[subscript] = value;
Ex: rollno[0]=1;
rollno[1]=2;
Arrays can be initialized in the declaration itself. The values must be enclosed within the
braces and separated by commas
type arrayname[ ] ={list of values};
Ex: int rollno[ ] ={10,20,30,40,50};
Array Length:
In Java, all arrays store the allocated size in a variable named length. We can obtain the
length of the array by using the syntax
int variablename= [Link];
if want to obtain the length of array ‘a ‘ using [Link] is
int assize= [Link] ;
Write a program to enter elements into an array and display them
import [Link].*;
class ArrayInsert
{
public static void main(String args[ ]) throws Exception
{
DataInputStream dis=new DataInputStream([Link]);
int a[ ] = new int[10];
[Link]("enter elements");
for(int i=0 ; i<10 ; i++)
a[i]=[Link]([Link]());
[Link]("elements are");
for(int i=0;i<10;i++)
[Link](a[i]);
}
}
Write a program to sort list of array items?
import [Link].*;
class ArraySort
{
public static void main(String args[ ]) throws Exception
{
DataInputStream dis=new DataInputStream([Link]);
int a[ ] = new int [10];
[Link]("enter elements");
int len=[Link];
for(int i=0 ; i<len ; i++)
a[i]=[Link]([Link]());
[Link](" elements before sorting are ");
for(int i=0 ; i<len ; i++)
[Link](a[i]);
for(int i=0;i<len;i++) //sorting begins
{
for(int j=i+1;j<len;j++)
if( a[i] > a[j] )
{
Page 2 of 17
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}//sorting ends
[Link](" elements after sorting are ");
for(int i=0 ; i<len ; i++)
[Link](a[i]);
}
}
Multi-Dimensional Array:
In Java, multi-dimensional arrays are actually arrays of array .To declare a multi-dimensional
array variable, specify each additional index using another set of square brackets.
Two-Dimensional Array:
A two–dimensional array can be like a table of rows and columns.
Syntax: Datatype variable[ ][ ] = new datatype[n][m];
Here first index selects the row and the second index selects the column within that row
Here n rows and m columns totally n*m elements can be stored in the array
Example: int a[ ][ ]= new int[5][4];
In this example, this creates a table that can store 20 integer values 5 across and 4 down .
Initializing a two-dimensional array :
Like one-dimensional arrays, two-dimensional arrays can be initialized.
The only difference is that since it is an array of arrays. Its initialization list has to be a list of
initialization list.
Example:
1. int a1[2 ][3 ]={0,0,0,1,1,1};
Here it will create an array of 2 rows and 3 columns. First row is initialized with 0 and the
second row with 1. The initialization is done row by row. (or)
2. int array1[ ][ ]={ {1,2,3}, {4,5,6},{7,8,9} };
here elements of each row are surrounded by braces
We can refer to a value stored in a two –dimensional array by using subscripts for both the
column and row of the corresponding element
Example : int value=array1[1][2];
This retrieves the value stored in the 2nd row and 3rd column of the table matrix.
Example: Write a program to enter elements into a 3X3 matrix and display them?
import [Link].*;
class Array1
{
public static void main(String args[ ]) throws Exception
{
DataInputStream dis =new DataInputStream([Link]);
int a[ ][ ] = new int[3][3];
[Link]("Enter elements"+ 3*3 + " in to the array");
for(int i=0; i<3; i++)
for(int j=0;j<3;j++)
a[i][j]=[Link]([Link]());
[Link]("The Elements are ");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
[Link](a[i][j] +"" );
Page 3 of 17
}
[Link]();
}
}
}
Variable Size Arrays:
Java Supports rows with variable columns of array .That are each row may contain variable
no. of columns.
Example:
int x[ ][ ]=new int[4][ ];
x[0]= new int[3];
x[1]= new int[2];
x[2]= new int[1];
x[3]= new int[4];
This feature is not supported in c or c++
Note : [Link] varying columns don’t specify the size of the column at
array declaration .
2. Initialization is done for each row
Strings:
Strings represent a sequence of characters.
In Java , strings are class objects and implemented using two classes, namely,
String and StringBuffer
A Java string is an object of String class.
It is not a character array and is not NULL terminated.
String class is a class in [Link] package
Strings may be declared as follows
String stringname ;
stringname = new String(“string”);
( or )
Both these statements can be combined as
String stringname =new String( “ String “)
Example :
String name ;
name = new String(“sasidhar”);
(or)
String name = new String(“sashank”);
Like arrays it is also possible to get the length of the string using the length
method of String class
int n =[Link]( );
String Arrays:
We can also create and use arrays that contain strings. The statement
String names[ ] =new String[3];
Will create names array of size 3 to hold three string constants. We can assign the strings to
the names element by element using three different statements or more efficiently using a for
loop
String Methods :
Page 4 of 17
The String class defines a number of methods that allow us to accomplish a variety of string
manipulation tasks. The most commonly used string methods are:
s2=[Link]; --Converts the string s1 to all lowercase
s2=[Link]; -- Converts the string s1 to all uppercase
s2=[Link](‘x’,’y’); -- Replaces all appearances of x with y
s2=[Link]( ); -- Remove white spaces at the beginning and end of the string s1
[Link](s2); --Returns ‘true’ if s1 = s2
[Link](s2); --Returns ‘true’ if s1 =s2 ,ignoring case of characters
[Link]( ); --Gives the length of s1
[Link](n); --Gives nth character of s1
[Link](s2); -- Returns negative if s1<s2, positive if s1>s2, and zero s1=s2
[Link](s2); -- Concatenates s1 and s2
[Link](n); --Gives substring starting from nth character
[Link](n, m); --Gives substring starting from nth character upto mth
[Link](‘x’) ; ---Gives the position of the first occurrence of ‘x’ in the string s1
[Link] Of (variable); -- Converts the parameter value to string representation
Write a program to sort the given strings?
class StringSort
{
public static void main(String args[ ])
{
String name[]= {"zebra","giraffe","donkey","cow", "bear", "tiger"};
int size=[Link];
String temp=null;
for(int i = 0 ; i<size ; i++)
{
for(int j=i+1; j<size ; j++)
{
if(name[j].compareTo(name[i])< 0)
{
temp = name[i];
name[i] = name[j];
name[j] = temp;
}
}
}
for(int i = 0 ; i<size ; i++)
{
[Link](name[i]);
}
}
}}
Write a program to perform some String Manipulations ?
import [Link];
class StringManip
{
public static void main(String args[])
{
String s= "Java is a general purpose object oriented programming language";
String s1= "Hello World";
String s2="WELCOME";
String s3= "java";
String s4="java";
Page 5 of 17
[Link]("index of t :"+[Link]('t'));
[Link]([Link](6));
[Link]([Link](3,8));
[Link]([Link]("Students"));
[Link]([Link]());
[Link](s3 + " equals " + s4 + "is" +[Link](s4));
}
}
Write a program to count [Link] vowels in a given String ?
import [Link].*;
import [Link];
public class VowelsCount
{
public static void main(String Args[ ]) throws IOException
{
DataInputStream dis =new DataInputStream([Link]);
[Link]("enter the string");
String s= [Link]( );
int count=0;
for(int i =0;i<[Link]();i++)
{
char c=[Link](i);
if(c=='a'|| c=='e'||c =='i'|| c=='o'|| c=='u' )
{
count++;
}
}
[Link](" [Link] vowels : " +count );
}
}
StringBuffer class:
String creates strings of fixed length, StringBuffer creates strings of flexible
length that can be modified in terms of both length and content .We can insert characters and
substrings in the middle of a string, or append another string to the end
Methods of StringBuffer class :
[Link]( n,’x’); ---Modifies the nth character to x
[Link](s2) ; --Appends the string s2 to s1 at the end
[Link](n,s2); ---inserts string s2 at the position n of the string s1
[Link](n); --- sets the length of the string s1 to n .if n<[Link] ( ) s1 is
truncated . if n>[Link]( ) zeros are added to s1
Vectors:
The [Link] package has a class called Vector, which permits an array to store
different elements that are of different class.
Vectors are commonly used instead of arrays, because they expand automatically
when new data is added to them.
Vector is also an expandable array of objects.
The array grows larger as more elements are added to it. The array may also be
reduced in size after some of its elements have been deleted.
We cannot store primitive data types in a vector, we can only store objects.
Vector automatically increases its size when needed .ordinary array cannot do that
Arrays can be easily implemented as vectors
Vectors are created like arrays by using the following constructors:
Creating vector with default initial size:
Page 6 of 17
Vector objname = new Vector( ); // declaring without size
Creating vector with specifying size
Vector objname = new Vector(int size );// declaring with size
Example:
Vector v1=new Vector( );
Vector v2 = new Vector(3);
Note :
We can declare a vector without specifying any size explicitly .Then it can
accommodate an unknown [Link] items.
Even though when size is specified ,we can store different [Link] items in to
the vector .But array must have its size specified
Advantages of Vectors over Arrays: Vectors possess a number of advantages over arrays
It is convenient to use vectors to store objects
A vector can be used to store a list objects that may vary in size
We can add and delete objects from the list as and when required
Methods In Vector:
addElement( ) :-This method is used to add element into the vector at the last
position.
elementAt(int i ) :-This method returns the object at position i in the vector .
size( ):- gives the number of objects present
removeElement( item ):-Removes the specified item from the list
removeElementAt(n):- Removes the element in the stored in the nth position of
the list
removeAllElements( ) :-Removes all the elements in the list
copyInto (array) :-copies all items from list to array
insertElementAt( item , n ) ;-Inserts the item at nth position
Program to add elements into the vector and display them ?
import [Link].*;
import [Link].*;
class VectorDemo
{
public static void main(String args[ ])
{
Vector v= new Vector( );
[Link]("C");
[Link]("C++");
[Link]("ADA");
[Link]("JAVA");
[Link]("Dot Net");
[Link]("COBOL",2);
for(int i=0;i<[Link]();i++)
{
[Link](i + "" +[Link](i));
}
}
}
Write a program to store different data items in a vector ?
import [Link].*;
import [Link].*;
class Vector1
{
public static void main(String args[ ] )
{
Page 7 of 17
Vector v=new Vector( );
[Link](new Integer(10));
[Link](new Float(10.4));
[Link](new Double(10.1345));
[Link](new Character('K'));
[Link](new Boolean(true));
[Link]("First Element" +(Integer)[Link]());
[Link]("Last Element"+(Boolean)[Link]());
}
}
Wrapper Classes
Wrapper classes are used to provide object versions of primitive types
That is, Primitive data types can be converted into object types using the wrapper
classes contained in the [Link] package.
All of the primitive wrapper classes in java are immutable .
Simple data types and their corresponding wrapper class types are specified below
Simple type Wrapper class Constructor Arguments
Boolean Boolean byte or String
char Character char
double Double double or String
float Float float or double or String
int Integer int or String
long Long long or String
short Short short or String
byte Byte byte or String
The Byte, Short, Integer, Long, Float and Double wrapper classes are all subclasses of
the Number class.
The Boolean Class:
The Boolean class is a Wrapper for the boolean values. Key methods provided by this
class are booleanValue( ),getBoolean( ), toString( ), and valueOf( ).These methods
support type and class conversion . The class has two constructors :
public Boolean(Boolean value);
public Boolean(String s);
Example :
boolean primbool =false;
Boolean wrapbool =new Boolean(primbool);
Or
Boolean wrapbool =new Boolean(“false “);
The Character Class :
The Character class is a wrapper for char values .The class provides many
different methods for working with char values. The class has one constructor
public Character(char value);
Example :
char primchar = “A”;
Character wrapchar = new Character(primchar);
The Byte, Short, Long, Integer Classes:
The byte, short, int and long primitive data types all have different wrapper classes.
These classes provide methods for working with and converting the values.
Page 8 of 17
These classes all have two constructors, one that expects to be passed a primitive value
and another that expects to be passed a String. You could construct these classes using
primitive values as follows:
Example: byte primbyte = 16;
Byte wrapbyte =new Byte(primbyte);
short primshort =1234;
Short wrapshort =new Short(primshort);
int primint =98765;
Integer wrapint =new Integer(primint);
long primlong =98986789L;
Long wraplong = new Long(primlong);
If you want to construct the classes from strings we can use :
Byte wrapbyte =new Byte(“18”);
Short wrapshort =new Short(“1234”);
Integer wrapint =new Integer(“9898567”);
Long wraplong = new Long(“99967899”);.
The Float and Double Classes :
The Float and Double classes wrap the float and double primitive data types. These
classes provide methods for working with, converting the values. They also have two
constructors, one that expects to be passed a primitive value and another that expects to
be passed a string. To construct these classes using primitive values, we can use:
float primfloat =1.678F;
Float wrapfloat = new Float(primfloat);
double primdouble =1.87514567;
Double wrapdouble =new Double(primdouble);
To construct these classes using Strings, we could use
Float wrapfloat = new Float(“1.678F”);
Double wrapdouble = new Double(“1.87514567”);
1. Converting Primitive Numbers to Object numbers using Constructor methods :
Constructor Calling Conversion action
Integer intval = new Integer(i); Primitive integer to Integer Object
Float floatval = new Float(f) ; Primitive float to Float Object
Double doubleval=new Double( d); Primitive double to Double Object
Long longval= new Long(l) ; Primitive long to Long Object
Note : i , f, d, l are primitive data values denoting int , float , double and long data types .
they may be constants or variables .
[Link] Object numbers to primitive Numbers using typeValue() method :
Method calling Conversion Action
int i =intval .intValue( ) ; Object to primitive integer
float f =floatval .floatValue( ) ; Object to primitive float
long l =longval .longValue( ) ; Object to primitive long
double d =doubleval .doubleValue( ); Object to primitive double
3. Converting Numbers to Strings Using toString( ) method :-
Method calling Conversion Action
str=[Link](i); Primitive integer to String
str=[Link](f); Primitive float to String
str=[Link](d); Primitive double to String
str=[Link](l); Primitive long to String
Page 9 of 17
4. Converting Strings objects to Numeric Objects Using static method valueOf( ):
Method Calling Conversion Action
Doubleval=[Link](str); Converts String to Double object
Floatval=[Link](str); Converts String to Float object
intval=[Link](str) ; Converts String to Integer object
longval=[Link](str) ; Converts String to Long object
Note : These numeric values can be converted to primitive numbers using the typeValue( )
method as shown in table 2
[Link] Numeric Strings to Primitive Numbers Using Parsing Methods :
Method calling Conversion Action
int i = [Link](str); Converts String to Primitive Integer
long l=[Link](str) ; Converts String to Primitive Long
float f =[Link](str); Converts String to Primitive Float
Note :
parseInt( ) , parseFloat ( ),parseLong( ) methods throws a NumberFormatException if the value
of the str does not represent an integer .
Enumerated Types:
J2SE 5.0 allows us to use the enumerated type in Java using the enum keyword. This keyword
can be used similar to the static final constants in the earlier versions of java .
An enum type is a type whose fields consist of a fixed set of constants. Common examples
include compass direction(North, south , east ,west) and the days of the week .
Because they are constants, the names of an enum type’s fields are in uppercase letters
Example:
public enum DAY
{
SUNDAY , MONDAY , TUESDAY , WEDNESDAY , THURSDAY , FRIDAY ,
SATURDAY
}
An enumerated type is a type whose instances describe values, where the set of possible values is
finite. Typically, an enumerated type is used when the most important information is the
existence of the value.
A static enumerated type is an enumerated type whose set of possible values is fixed, and does
not vary at run-time.
A dynamic enumerated type is an enumerated type that can gain or lose values at run-time. For
example, enumerated type of car models which may vary according to the need.
Enumerated types may be ordered or unordered. Ordered types may be singly or multiply
ordered. Unordered have no logical order. For example, the standard Boolean type may be
considered an unordered enumerated type: there is no logical reason to list the value true before
or after the value false.
Advantages: The advantages of using enumerated types are:
Compile-time type safety.
We can use the “enum” keyword in switch statements.
Example:
public class Workingdays
{
enum days
{
Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday
}
Private static void weekend(days d)
Page 10 of 17
{
If([Link]([Link]))
[Link](“value=”+d+” is a holiday”);
Else
[Link](“value=”+d+” is working day”);
}
Public static void main(String args[])
{
for(days d:[Link]())
{
weekend (d);
}
}
*****
10. INTERFACES
Java does not support the concept of multiple inheritances but it provides an alternate
approach known as Interfaces to support the concept of multiple Inheritances. Although a
Java class cannot be a subclass of more than one super class, it can implement more than one
interface.
Defining Interfaces:
An interface is similar to class except that
Interfaces don’t have instance variables
All the methods of interface are abstract methods that means the methods in the
interface have only declarations without body
Interfaces can have variables but they are implicitly final static variables
Interfaces can be created by using the keyword interface. when we use the keyword
interface we can specify what a class must do but not how it does.
The syntax for defining an interface is :
access interface InterfaceName
{
static final type varname=value;
return-type method-name(parameter-list);
}
Here access can be public or default .If public then interface is available to the
others i.e., outside the package
If default then it is available to the members of the same package
Here , interface is the keyword and InterfaceName is any valid Java identifier
All variables must be declared as constants
Once an interface is defined any [Link] classes can implement that interface .
Also one class can implement any [Link] interfaces .
When a class implements an interface then it is the responsibility of the class
to provide implementation for all the methods in the interface . If the class fails to
implement at least one method in the interface then that class must be declared as
abstract
Example :
interface Shape
{
final static float pi=3.14;
Page 11 of 17
float area(float l, float b );
void display( );
}
Here is an example of an interface definition that contains two variables and one method
interface Item
{
static final int code=1001;
static final String name=”fan”;
void display( ) ;
}
Note that the code for the method is not included in the interface and the method
declaration simply end with semicolon. The class implements this interface must define
the code for the method
Extending Interfaces :
Like classes , interfaces can also be extended .That is , an interface can be subinterfaced
from other interfaces. The new subinterfaces will inherit all the members of the
superinterface in the manner similar to subclasses .This is achieved using the keyword
extends as shown below :
interface Iname2 extends Iname1
{
body of iname2 ;
}
Example:
interface ItemConstants
{
int code =1001;
String name =”fan”;
}
interface Item extends ItemConstants
{
void display( );
}
The interface Item would inherit both the constants code and name into it . Note that the
variables name and code are declared like simple variables. It is allowed because all the
variables in an interface are treated as constants although the keywords final and static are
not present.
Note:
While interfaces are allowed to extend to other interfaces , sub interfaces cannot
define the methods declared in super interfaces, because sub interfaces are still
interfaces , not classes
It is the responsibility of any class that implements the derived interface to define
all the methods (both the methods in super interface and sub interface).
Note that when an interface extends two or more interfaces, they are separated by
commas.
An interface cannot extend classes because interfaces can have only abstract
methods and constants.
Example : Demo for an interface extending another interface
import [Link].*;
interface A
{
Page 12 of 17
void method1();
void method2();
}
interface B extends A
{
void method3();
}
class C implements B
{
public void method1()
{
[Link]("From Method1");
}
public void method2()
{
[Link]("From Method2");
}
public void method3()
{
[Link]("From Method3");
}
}
class ExtendInterfaceDemo
{
public static void main(String args[])
{
C ob = new C();
ob.method1();
ob.method2();
ob.method3();
}
}
Implementing Interfaces:
Interfaces are used as “superclasses “ whose properties are inherited by classes . The
syntax for creating the class that implements the interface is given by:
class ClassName implements InterfaceName1, InterfaceName2,…
{
Body of the classname;
}
The more general form of implementation will be
Syntax :
class ClassName extends Superclass implements Interface1, Interface2, ..
{
Body of ClassName;
}
A class can extend another class while implementing interfaces .
A class can implement more than one interface , they are separated by a comma
An interface can be implemented by one or more classes
Example for an interface that can be implemented by more than one class
import [Link].*;
interface Shape
{
Page 13 of 17
public double area();
}
class Rectangle implements Shape
{
double d1,d2;
Rectangle(double d11,double d12)
{
d1=d11;
d2=d12;
}
public double area()
{
return d1*d2;
}
}
class Triangle implements Shape
{
double dim1,dim2;
Triangle(double d1,double d2)
{
dim1=d1;
dim2=d2;
}
public double area()
{
return 0.5*dim1*dim2;
}
}
class TestShape
{
public static void main(String args[])
{
Shape s;
Rectangle r = new Rectangle( 10.5, 20.5);
Triangle t =new Triangle (12.3,10.0);
s=r;
[Link]("area of Rectangle"+[Link]());
s=t;
[Link]("area of Triangle"+[Link]());
}
}
Class Implementing more than one interface:
When a class implement more than one interface it has to provide all the definitions for all
the methods in all the interfaces.
interface Iface1
{
void method1( );
void method2( );
}
interface Iface2
{
void method3( );
}
Page 14 of 17
class A implements Iface1, Iface2
{
public void method1( )
{
[Link](“ Method 1” ) ;
}
public void method2( )
{
[Link](“ Method 2” ) ;
}
public void method3( )
{
[Link](“ Method 3” ) ;
}
}
class TestA
{
public static void main(String args[ ])
{
A obj=new A( );
obj.method1( );
obj.method2( );
obj.method3( );
}
}
Accessing Interface Variables:
Interfaces can be used to declare a set of constants that can be used in different classes. This
is similar to creating header files in c++ to contain a large number of constants. Such
interfaces do not contain methods. The constant values are available to any class that
implements the interface. The values can be used in any method, as part of variable
declaration or anywhere we can use a final value
Example :
interface Inames
{
int jan =1;
int feb =2;
int march =3;
int april=4;
int may=5;
int june=6;
int july=7;
int aug=8;
int sep=9;
int oct=10 ;
int nov=11;
int dec=12;
}
class MonthDays implements Inames
{
public static void main(String args[ ])
{
int n = [Link](args[0]);
int days;
Page 15 of 17
switch(n)
{
case jan :
case march :
case may :
case july :
case aug:
case oct :
case dec :
days =31;
[Link]("days : " +days );
break;
case april:
case june :
case sep :
case nov :
days =30 ;
[Link]("days : " +days );
break;
case feb : days =28 ;
[Link]("days : " +days );
break;
}
}
}
Implementing Multiple Inheritances:
import [Link].*;
interface Publisher
{
public void getData() throws Exception;
public void display();
}
interface Sales
{
public void getSales() throws Exception;
public void displaySales();
}
class Book implements Publisher,Sales
{
String bname,pname,author;
float price;
int sale[]=new int[3];int totsal=0;
public void getData() throws Exception
{
InputStreamReader isr=new InputStreamReader([Link]);
BufferedReader br=new BufferedReader(isr);
[Link](" Enter book name\n");
bname=[Link]();
[Link]("Enter Publisher Name\n");
pname=[Link]();
[Link]("Enter Author name\n");
Page 16 of 17
author=[Link]();
[Link]("Enter Price \n");
price=[Link]([Link]());
}
public void display()
{
[Link]("BOOK NAME :\t"+bname);
[Link]("PUBLISHER :\t"+pname);
[Link]("AUTHOR NAME :\t"+author);
[Link]("PRICE :\t"+price);
}
public void getSales() throws Exception
{
InputStreamReader isr=new InputStreamReader([Link]);
BufferedReader br=new BufferedReader(isr);
for(int i=0; i<3; i++)
{
[Link]("sale in month "+(i+1));
sale[i]=[Link]([Link]());
totsal=totsal+sale[i];
}
}
public void displaySales()
{
for(int i=0; i<3; i++)
[Link]("sale in month "+(i+1)+" is "+sale[i]+"\n");
[Link]("total sales is "+totsal);
}
}
class Multiple
{
public static void main(String args[]) throws Exception
{
Book ob=new Book();
[Link]();
[Link]("Book details\n");
[Link]();
[Link]("Enter Sales of Three Months\n");
[Link]();
[Link]("Sales of the book \n");
[Link]();
}
}
*****
Page 17 of 17