Professional Documents
Culture Documents
R-
1 CR CO4
P2
Output will be: Four < Five < Six < Seven
Example program:
class DisplayOverloading
{
public void display(char ch)
{
System.out.println("Displaying a character ch="+ch);
4 U CO3
}
public void display(int num, char ch)
{
System.out.println("Displaying int and char => "+num+","+ch );
}
public void display(char ch, int num)
{
System.out.println("Displaying char and int => "+ch+","+num );
}
}
class OverloadingDemo
{
public static void main(String args[])
{
DisplayOverloading obj = new DisplayOverloading();
obj.display('K');
obj.display(2, 'R');
obj.display('S',1);
}
}
Output:
Displaying a character ch=K
Displaying int and char => 2,R
Displaying char and int => S,1
b. Write a java program to demonstrate an interface with suitable example.
Answer:
interface Shape
{
void area();
}
class Rectangle implements Shape
{
int l,b;
Rectangle(int l,int b)
{
this.l=l;
this.b=b;
}
public void display()
{
System.out.println("length="+l+
", Breadth="+b); 4 Ap CO3
}
public void area()
{
System.out.println("Rectangle Area="+(l*b));
}
}
class InterfaceDemonsration {
public static void main(String args[])
{
Rectangle robj=new Rectangle(32,11);
robj.display();
robj.area();
}
}
Output:
length=32, Breadth=11
Rectangle Area= 352
(OR)
c. Describe the process of creating and accessing of classes in a package
Answer:
Important points while creating a package
Package statement must be first statement in the program even
before the import statement.
A package is always defined as a separate folder having the same
name as the package name.
Store all the classes in that package folder.
All classes of the package which we wish to access outside the
package must be declared public.
All classes within the package must have the package statement as
its first line.
All classes of the package must be compiled before use.
A class which uses the class/interface of a package should import
the class in either of the following ways.
import packagename.ClassName;
or
import packagename.*;
A class can even use the class/interface of a package without
import statement i.e using fully qualified name of the class using
package name.
3. Execute MainClass.java
java MainClass
--
Method-2: Execution of packages
1. Create a folder with package name
Syntax: md packageName
Ex: md mypack
2. Copy all packageFile.java to the packageFolder
Move to the package folder and compile the java program
cd mypack
javac packageFile.java
4. Moving/changing to the parent directory
cd ..
Note: Here " .."( double dot) indicates parent directory
5. Compile the MainClass.java which is using the classs of a package
javac MainClass.java
6. Execute MainClass.java
java MainClass
Note: If the package itself contains MainClass then we can follow the following
steps
javac -d . packageFile.java
java packagename.ClassName
d. Write a java program to demonstrate dynamic method dispatch.
Answer:
Definition of Dynamic method dispatch: Dynamic method dispatch is
the mechanism by which a call to an overridden method will be resolved
at run-time rather than at compile time.
class Figure
{
double dim1;
double dim2;
Figure(double a, double b)
{
dim1 = a;
dim2 = b;
}
double area()
{
System.out.println("Area for Figure is undefined.");
return 0;
}
}
figref = f;
System.out.println("Area is " + figref.area());
System.out.println("-------------------------");
figref = r;
System.out.println("Area is " + figref.area());
System.out.println("-------------------------");
figref = t;
System.out.println("Area is " + figref.area());
}
}
Output:
Area for Figure is undefined.
Area is 0.0
-------------------------
Inside Area for Rectangle.
Area is 45.0
-------------------------
Inside Area for Triangle.
Area is 40.0
try
{
// block of code to monitor for errors
}
catch (ExceptionType1 exOb1)
{
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb2)
{
// exception handler for ExceptionType2
}
// ...
.
.
.
finally
{
// block of code to be executed before try block ends
}
System.out.println("Given file:"+fileName+
" is created successfully.");
}
catch(FileNotFoundException e)
{
System.out.println("Exception caught- File Unable to create");
System.out.println("Exception details:"+e);
}
catch(Exception e)
{
System.out.println("Exception caught:"+e);
}
finally
{
if(fout!=null)
fout.close();
if(sobj!=null)
sobj.close();
}
}
}
(OR)
c. Explain reading and writing using character stream classes.
Answer:
Character Streams − These handle data in 16-bit Unicode. Using these you
can read and write text data only.
The Reader and Writer classes (abstract classes) are the super classes of all
the character stream classes: classes that are used to read/write character
streams. Following are the character array stream classes provided by Java
–
Reader Writer
BufferedReader BufferedWriter
CharacterArrayReader CharacterArrayWriter
StringReader StringWriter
InputStreamReader OutputStreamWriter
FileReader FileWriter
FileReader : This class is used to read the data from the file.
BufferedReader: This class is a buffered version of FileReader, using the
method readLine() in BufferedReader we can read a line of text from the 4 U CO4
file. This is also used to read the characters from a specific stream.
CharacterArrayReader: This class is used to read the characters from the
character array.
StringReader: This class is used to read the data from the String object
InputSreamReader: An InputStreamReader is a bridge from byte streams
to character streams: It reads bytes and decodes them into characters using
a specified charset .
2. getChars()
If we need to extract more than one character at a time, you can
use the getChars( ) method
public void getChars(int sourceStart, int sourceEnd, char target[ ], int targetStart)
3. getBytes()
This method converts the characters in string to byte array
byte[] getBytes();
4. toCharArray()
This method converts this string to a new character array.
char[] toCharArray()
Example on String Comparsion:
public class StringCompareDemo{
public static void main(String args[]){
String s1 = "Hello";
String s2 = "java";
String s3 = new String("java");
String s4="Hello";
String s5="JAVA";
System.out.println("s1="+s1);
System.out.println("s2="+s2);
System.out.println("s3="+s3);
System.out.println("s4="+s4);
System.out.println("s5="+s5);
System.out.println();
if(s1==s4)
System.out.println("s1==s4");
else
System.out.println("s1!=s4");
if(s2==s3)
System.out.println("s2==s3");
else
System.out.println("s2!=s3");
if(s2.equals(s3))
System.out.println("s2 & s3 contains same content");
else
System.out.println("s2 & s3 contains different content");
if(s2.equalsIgnoreCase(s5))
System.out.println("s2 &s5 contains same content-Ignoring the case");
else
System.out.println("s2 &s3 contains different content");
System.out.println("s1.compareTo(s2)="+
s1.compareTo(s2));
System.out.println("s2.compareTo(s3)="+
s2.compareTo(s3));
System.out.println("s2.compareTo(s5)="+
s2.compareTo(s5));
System.out.println("s2.compareToIgnorecase(s5)="+
s2.compareToIgnoreCase(s5));
}
}
Output:
s1=Hello
s2=java
s3=java
s4=Hello
s5=JAVA
s1==s4
s2!=s3
s2 & s3 contains same content
s2 &s5 contains same content-Ignoring the case
s1.compareTo(s2)=-34
s2.compareTo(s3)=0
s2.compareTo(s5)=32
s2.compareToIgnorecase(s5)=0
char ch=s1.charAt(4);
System.out.println("\ns1.charAt(4)="+ch);
Output:
s1=ABCDToJAVA
s1.charAt(4)=T
Chars from 6 to 9
J
A
V
A
This problem can be solved with the help of interfaces as interface will gen
erally have function declarations
Example:
interface Address
{
void printAddress();
}
interface MyMath
{
void add(int a,int b);
}
}
public class MultipleInterfacesInSingleClass
{
public static void main(String s[])
{
SingleClass obj=new SingleClass();
obj.printAddress();
obj.add(3,2);
obj.nonInterfaceMethod();
System.out.println("---------------------------");
4 An CO4
Example program:
class ThrowsDemo
{
static void throwOne() throws IllegalAccessException
{
System.out.println("Inside throwOne");
throw new IllegalAccessException("demo");
}
public static void main(String args[])
{
try
{
throwOne();
}
catch(IllegalAccessException e)
{
System.out.println("caught " +e);
}
}
}
(OR)
c. Justify the uses of interfaces over abstract classes.
Answer:
Interface solves multiple inheritance problem compared to abstract class
and the other differences on interfaces over abstract class are as follow.
9)Example: Example:
public abstract class Shape public interface Drawable
{ {
public abstract void draw(); void draw();
} }
d. Discriminate string searching methods.
Answer:
Following are the different methods used in string searching methods.
int indexOf(int ch)
int lastIndexOf(int ch)
class indexOfDemo {
public static void main(String args[]) {
String s = "Now is the time for all good men " +
"to come to the aid of their country.";
System.out.println(s); 4 An CO4
System.out.println("indexOf(t) = " + s.indexOf('t'));
System.out.println("lastIndexOf(t) = " + s.lastIndexOf('t'));
System.out.println("indexOf(the) = " + s.indexOf("the"));
System.out.println("lastIndexOf(the) = " + s.lastIndexOf("the"));
System.out.println("indexOf(t, 10) = " + s.indexOf('t', 10));
System.out.println("lastIndexOf(t, 60) = " + s.lastIndexOf('t', 60));
System.out.println("indexOf(the, 10) = " + s.indexOf("the", 10));
System.out.println("lastIndexOf(the, 60) = " + s.lastIndexOf("the", 60));
}
}