JPR-III Model Answer
JPR-III Model Answer
Class A
Class B
//Program throw a user defined exception if the given number is not positive
import java.io.*;
class MyException extends Exception
{
MyException(String msg)
{
super(msg);
}
}
class Pr8
{
public static void main(String a[])
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("Enter any no:");
int n=Integer.parseInt(br.readLine());
if(n<0)
throw new MyException("Negative Number!");
else
throw new MyException("Positive Number!");
}
catch(MyException e)
{
System.out.println(e);
}
catch(IOException e)
{
System.out.println(e);
}
}
}
c) What is package? State any four system packages along with their use? How to add class
to a user defined packages?
(package – 2 M, Four packages and their use – 1 M each, how to add class to package – 2 M)
Ans:
Package: Java provides a mechanism for partitioning the class namespace into more manageable
parts. This mechanism is the „package‟. The package is both naming and visibility controlled
mechanism. System packages with their use:(Any 4 can be considered)
1. java.lang - language support classes. These are classes that java compiler itself uses and
therefore they are automatically imported. They include classes for primitive types, strings, math
functions, threads and exceptions.
2. java.util – language utility classes such as vectors, hash tables, random numbers, date etc.
3. java.io – input/output support classes. They provide facilities for the input and output of data
4. java.awt – set of classes for implementing graphical user interface. They include classes for
windows, buttons, lists, menus and so on.
5. java.net – classes for networking. They include classes for communicating with local computers
as well as with internet servers.
6. java.applet – classes for creating and implementing applets.
package MyPack;
public class Balance
{...}
Then class Balance in included inside a user defined package „MyPack‟.
2) resume( )
syntax :
public void resume( )
This method resumes a thread which was suspended using suspend() method.
3) yield( )
syntax :
public static void yield()
The yield() method causes the currently executing thread object to temporarily pause and allow
other threads to execute.
4) wait()
syntax :
public final void wait()
This method causes the current thread to wait until another thread invokes the notify() method or
the notifyAll() method for this object.
b) Describe any four methods from graphics class.
(Each method – 1 mark)
Ans: The methods contained within the Graphics Class in the package java.awt.
Following are some of the graphics class methods
1. drawLine( ):
I. This method draws a line between (x1, y1) and (x2, y2).
II. The line is drawn below and to the left of logical coordinate.
III. Syntax:
public void drawLine(int x1, int y1, int x2, int y2);
IV. Eg:
public void drawLine(10, 10, 100, 100);
2. DrawString( );
I. This method draws the text given by the specified string, using this graphics context's
current font and color.
II. The baseline of the leftmost character is at position (x, y) in this graphics context's
coordinate system.
III. Syntax:
public void drawString(String str, int x, int y);
IV. Eg:
public void drawString(―Hello Java‖, 50, 50);
3. drawRect( );
I. This method draws the outline of the specified rectangle. The left and right edges of the
rectangle are at x and x + width.
II. The top and bottom edges are at y and y + height. The rectangle is drawn using the
graphics context's current color.
III. Syntax:
public void drawRect(int x, int y, int width, int height);
IV. Eg:
public void drawRect(10,10,120,120);
4. fillRect( ):
I. This method fills the specified rectangle. The left and right edges of the rectangle
are at x and x + width - 1. The top and bottom edges are at y and y + height - 1.
II. The resulting rectangle covers an area width pixels wide by height pixels tall.
III. Syntax:
Public void fillRect( int x, int y, int height, int width );
IV. Eg:
Public void fillRect(0,0,100,100);
c) How do we put an applet into web page? Explain. (Each point 1 marks)
Ans: An Applet can embed into the web page as below:
1. An applet developed locally and stored in a local system is known as local applet. When a
web page is trying to find a local applet, it does not need to use the Internet connection. It
simple searches the directories in the local system and locates and loads the specified
applet.
2. In the case of local applets, CODEBASE may be absent or may specify a local directory. A
remote applet is that which is developed by someone else and stored on a remote computer
connected to the Internet.
3. If our system is connected to the Internet, we can download the remote applet onto our
system via at the Internet and run it. In order to locate and load a remote applet, we must
know the applet‟s address on the Web.
4. This address is known as Uniform Resource Locator (URL) and must be specified in the
applet‟s HTML document as the value of the CODEBASE attribute. e.g. CODEBASE =
http://www, .msbte.com/applets
d) Write a program to accept a number as command line argument and print the number is
even or odd. (Any other program with correct program may also be considered) (Logic 2-M,
Syntax 2-marks)
Ans:
public class CMD
{
public static void main(String key[ ])
{
int x=Integer.parseInt(key[0]);
if (x%2 ==0)
{
System.out.println("Even Number");
}
else
{
System.out.println("Odd Number");
}
}
}
e) Explain use of following methods:
1) indexOf( )
2) charAt( )
3) substring( )
4) replace( )
(1 mark each for each method use with parameters and return type.) (Any one syntax of
each method should be considered)
Ans:
1. indexOf( ):
int indexOf(int ch):
Returns the index within this string of the first occurrence of the specified character.
int indexOf(int ch, int fromIndex):
Returns the index within this string of the first occurrence of the specified character,
starting the search at the specified index.
int indexOf(String str):
Returns the index within this string of the first occurrence of the specified substring.
int indexOf(String str, int fromIndex):
Returns the index within this string of the first occurrence of the specified substring,
starting at the specified index.
2. charAt( ):
char charAt(int index):
Returns the char value at the specified index.
3. substring( ):
String substring(int beginIndex):
Returns a new string that is a substring of this string.
String substring(int beginIndex, int endIndex):
Returns a new string that is a substring of this string. The substring begins at the specified
beginIndex and extends to the character at index endIndex - 1
4. replace( ):
String replace(char oldChar, char newChar): Returns a new string resulting from replacing all
occurrences of oldChar in this string with newChar.
2. Relational Operators:
There are following relational operators supported by Java language
Assume variable A holds 10 and variable B holds 20, then:
Show Examples
Checks if the values of two operands are equal or not, if yes then (A == B) is not
==
condition becomes true. true.
Checks if the values of two operands are equal or not, if values are
!= (A != B) is true.
not equal then condition becomes true.
Checks if the value of left operand is greater than the value of right
> (A > B) is not true.
operand, if yes then condition becomes true.
Checks if the value of left operand is less than the value of right
< (A < B) is true.
operand, if yes then condition becomes true.
Checks if the value of left operand is greater than or equal to the (A >= B) is not
>=
value of right operand, if yes then condition becomes true. true.
Checks if the value of left operand is less than or equal to the value
<= (A <= B) is true.
of right operand, if yes then condition becomes true.
c) State the use of Font class. Write syntax to create an object of Font class. Describe any 3
methods of Font class with their syntax and example of each.
[Font class 3-Marks, Syntax 2-Marks & Methods 3-Marks]
Font class: A font determines look of the text when it is painted. Font is used while painting
text on a graphics context & is a property of AWT component. The Font class defines these
variables:
Variable Meaning
String name Name of the font
float pointSize Size of the font in
points
int size Size of the font in point
int style Font style
Syntax to create an object of Font class: To select a new font, you must first construct a Font
object that describes that font. Font constructor has this general form:
Font(String fontName, int fontStyle, int pointSize)
fontName specifies the name of the desired font. The name can be specified using either the logical
or face name. All Java environments will support the following fonts: Dialog, DialogInput, Sans
Serif, Serif, Monospaced, and Symbol. Dialog is the font used by once system‟s dialog boxes.
Dialog is also the default if you don‟t explicitly set a font. You can also use any other fonts
supported by particular environment, but be careful—these other fonts may not be universally
available. The style of the font is specified by fontStyle. It may consist of one or more of these
three constants: Font.PLAIN, Font.BOLD, and Font.ITALIC. To combine styles, OR them
together. For example, Font.BOLD | Font.ITALIC specifies a bold, italics style. The size, in
points, of the font is specified by pointSize. To use a font that you have created, you must select it
using setFont( ), which is defined by Component. It has this general form: void setFont(Font
fontObj) Here, fontObj is the object that contains the desired font Methods of Font class
1. String getFamily(): Returns the family name of this Font.
2. int getStyle():Returns the style of this Font
3. int getSize() : Returns the size of this Font
4. boolean is bold(): Returns true if the font includes the bold style value. Else returns false
5. String toString(): Returns the string equivalent of the invoking font.
Example Using Methods of Font class: Program:
A program to make use of Font class methods. Which will display string ―Java Programming Is
Language‖ if font object style is BOLD else it will display string ―Java Programming Is Language‖
with another string saying ―String is not bold‖
import java.applet.*;
import java.awt.*;
/*<applet code="FontD" width=350 height=60> </applet> */
public class FontD extends Applet
{
Font f, f1; String s="";
String msg="";
public void init()
{
f=new Font("Dialog", Font.BOLD,30);
s="Java Programming";
setFont(f);
msg="Is Language";
int a=f.getSize();
String b=f.getFontName();
Int d=f.getStyle();
System.out.println("String Information: Size"+a);
System.out.println("Name:"+b);
System.out.println("Style:"+d);
}
public void paint(Graphics g)
{
if(f.isBold()==true)
g.drawString(s,50,50);
else g.drawString("String is not bold",400,400);
g.drawString(s,50,50);
g.drawString(msg,100,100);
}
}
Output:
d) Write a program to create two threads; one to print numbers in original order and other
to reverse order from 1 to 50. ( 4-marks for Logic, 4-marks for correct Syntax) (Any
other program with correct logic may also be considered)
Ans: // Program
class original extends Thread
{
public void run( )
{
try
{
for(int i=1; i<=50;i++)
{
System.out.println("\t First Thread="+i);
Thread.sleep(300);
}
}
catch(Exception e)
{}
}
}
class reverse extends Thread
{
public void run( )
{
try
{
for(int i=50; i>=1;i--)
{
System.out.println("\t Second Thread="+i);
Thread.sleep(300);
}
}
catch(Exception e) {}
}
}
class orgrev
{
public static void main(String args[])
{
new original().start();
new reverse().start();
System.out.println("Exit from Main");
}
}
6. Attempt any 4: 16 Marks
a) What is the default priority for all the threads? How to obtain and change this priority.
(Priority – 2 Marks, Change Priority-2 Marks)
Ans: Default priority of a thread is 5 (NORM_PRIORITY).
Each thread have a priority. Priorities are represented by a number between 1 and 10. In most
cases, thread schedular schedules the threads according to their priority (known as preemptive
scheduling). But it is not guaranteed because it depends on JVM specification that which
scheduling it chooses.