Question 1
(b) Explain the followings:(i) Dynamic Method Dispatch with example
class First
{
void display()
{
System.out.println("First Class's metohd Display");
}
}
class Second extends First
{
void display()
{
System.out.println("Second Class's metohd Display");
}
}
class Third extends First
{
void display()
{
System.out.println("Third Class's metohd Display");
}
}
public class dynamicMethodDispatchExampleH2E
{
public static void main(String args[])
{
First f = new First();
Second s = new Second();
Third t = new Third();
First fRef;
fRef = f;
fRef.display();
fRef = s;
fRef.display();
fRef = t;
fRef.display();
}
}
Question 2
(b) Design a class named Fan to represent a fan. The class contains:
- Three constants named SLOW, MEDIUM and FAST with values 1,2 and 3 to denote the fan speed.
- An int data field named speed that specifies the speed of the fan (default SLOW).
- A boolean data field named f_on that specifies whether the fan is on(default false).
- A double data field named radius that specifies the radius of the fan (default 4).
- A data field named color that specifies the color of the fan (default blue).
- A no-arg constructor that creates a default fan.
- A parameterized constructor initializes the fan objects to given values.
- A method named display() will display description for the fan. If the fan is on, the display() method displays speed, color and radius. If the fan is not on, the method returns fan color and radius along with the message “fan is off”.
Write a test program that creates two Fan objects. One with default values and the other with medium speed, radius 6, color brown, and turned on status true. Display the descriptions for two created Fan objects.
(b) Define the Rectangle class that contains:
Two double fields x and y that specify the center of the rectangle, the data field width and height ,
A no-arg constructor that creates the default rectangle with (0,0) for (x,y) and 1 for both width and height.
A parameterized constructor creates a rectangle with the specified x,y,height and width.
A method getArea() that returns the area of the rectangle.
A method getPerimeter() that returns the perimeter of the rectangle.
A method contains(double x, double y) that returns true if the specified point (x,y) is inside this rectangle.
Write a test program that creates two rectangle objects. One with default values and other with user specified values. Test all the methods of the class for both the objects.
class Test
{
public static void main(String args[])
{
A a = new A("Jaydip");
a.print();
}
}
class A
{
String s;
A(String s)
{
this.s=s;
}
public void print()
{
System.out.println(s);
}
}
//count is 101
//time is 0
public class Test
{
public static void main(String args[])
{
Count myCount = new Count();
int times=0;
for(int i=0;i<100;i++)
increment(myCount,times);
System.out.println("count is "+myCount.count);
System.out.println("times is "+times);
}
public static void increment(Count c,int times)
{
c.count++;
times++;
}
}
class Count
{
public int count;
Count(int c)
{
count=c;
}
Count()
{
count=1;
}
}
- Three constants named SLOW, MEDIUM and FAST with values 1,2 and 3 to denote the fan speed.
- An int data field named speed that specifies the speed of the fan (default SLOW).
- A boolean data field named f_on that specifies whether the fan is on(default false).
- A double data field named radius that specifies the radius of the fan (default 4).
- A data field named color that specifies the color of the fan (default blue).
- A no-arg constructor that creates a default fan.
- A parameterized constructor initializes the fan objects to given values.
- A method named display() will display description for the fan. If the fan is on, the display() method displays speed, color and radius. If the fan is not on, the method returns fan color and radius along with the message “fan is off”.
Write a test program that creates two Fan objects. One with default values and the other with medium speed, radius 6, color brown, and turned on status true. Display the descriptions for two created Fan objects.
class Fan
{
public static final int SLOW = 1;
public static final int MEDIUM = 2;
public static final int FAST = 3;
int speed;
boolean f_on;
double radius;
String color;
Fan()
{
speed = SLOW;
f_on = false;
radius = 4;
color = "blue";
}
Fan(int s,boolean f, double r, String c)
{
speed = s;
f_on = f;
radius = r;
color = c;
}
public void display()
{
if(f_on)
{
System.out.println("Speed of Fan : "+speed);
System.out.println("Radius of Fan : "+radius);
System.out.println("Color of Fan : "+color);
}
else
{
System.out.println("Fan is Off.");
System.out.println("Radius of Fan : "+radius);
System.out.println("Color of Fan : "+color);
}
}
}
class Q2bFanClassH2E
{
public static void main(String args[])
{
Fan f1 = new Fan();
f1.display();
Fan f2 = new Fan(2,true,6,"brown");
f2.display();
}
}
(b) Define the Rectangle class that contains:
Two double fields x and y that specify the center of the rectangle, the data field width and height ,
A no-arg constructor that creates the default rectangle with (0,0) for (x,y) and 1 for both width and height.
A parameterized constructor creates a rectangle with the specified x,y,height and width.
A method getArea() that returns the area of the rectangle.
A method getPerimeter() that returns the perimeter of the rectangle.
A method contains(double x, double y) that returns true if the specified point (x,y) is inside this rectangle.
Write a test program that creates two rectangle objects. One with default values and other with user specified values. Test all the methods of the class for both the objects.
class Rectangle
{
double centerX,centerY,width,height;
double x1,x2,x3,x4,y1,y2,y3,y4;
Rectangle()
{
centerX=centerY=0;
width=height=1;
}
Rectangle(double x,double y,double w,double h)
{
centerX=x;
centerY=y;
width=w;
height=h;
}
double getArea()
{
return (width*height);
}
double getPerimeter()
{
return ( 2*(width+height) );
}
boolean contains(double x,double y)
{
x1=x3= ( centerX - (width/2) );
x2=x4= ( centerX + (width/2) );
y1=y2= (centerY + (height/2));
y3=y4= (centerY - (height/2));
if( (x > x1 && x < x4) && (y < y1 && y > y3))
return true;
else
return false;
}
}
class RectangleH2E
{
public static void main(String args[])
{
Rectangle r1Obj = new Rectangle();
Rectangle r2Obj = new Rectangle(200,200,100,50);
System.out.println("Rectangle 1 : Area "+r1Obj.getArea());
System.out.println("Rectangle 1 : Perimeter "+r1Obj.getPerimeter());
System.out.println("Rectangle 1 : Contains (3,2) "+r1Obj.contains(3,2));
System.out.println();
System.out.println("Rectangle 2 : Area "+r2Obj.getArea());
System.out.println("Rectangle 2 : Perimeter "+r2Obj.getPerimeter());
System.out.println("Rectangle 2 : Contains (160,190) "+r2Obj.contains(160,190));
}
}
Question 3
(c) State whether any error exists in the following code. If so, correct the error and
give output.
class Test {
public static void main(String args[]) {
A a = new A();
a.print();
}
}
class A {
String s;
A(String s) {
this.s=s;
}
public void print() {
System.out.println(s);
}
}
class Test
{
public static void main(String args[])
{
A a = new A("Jaydip");
a.print();
}
}
class A
{
String s;
A(String s)
{
this.s=s;
}
public void print()
{
System.out.println(s);
}
}
(c) Give output of the following program:
public class Test {
public static void main(String args[]) {
Count myCount = new Count();
int times=0;
for(int i=0;i<100;i++)
increment(myCount,times);
System.out.println("count is "+myCount.count);
System.out.println("times is "+times);
}
public static void increment(Count c,int times) {
c.count++;
times++;
}
}
class Count {
public int count;
Count(int c){ count=c; }
Count(){ count=1; }
}
//count is 101
//time is 0
public class Test
{
public static void main(String args[])
{
Count myCount = new Count();
int times=0;
for(int i=0;i<100;i++)
increment(myCount,times);
System.out.println("count is "+myCount.count);
System.out.println("times is "+times);
}
public static void increment(Count c,int times)
{
c.count++;
times++;
}
}
class Count
{
public int count;
Count(int c)
{
count=c;
}
Count()
{
count=1;
}
}
Question 4
(b) The abstract Vegetable class has three subclasses named Potato, Brinjal and Tomato. Write an application that demonstrates how to establish this class hierarchy. Declare one instance variable of type String that indicates the color of a vegetable. Create and display instances of these objects. Override the toString() method of Object to return a string with the name of the vegetable and its color.import java.lang.*;
abstract class Vegetable
{
String colorVeg;
abstract public String toString();
}
class Potato extends Vegetable
{
public String toString()
{
colorVeg = "Yellow";
return colorVeg;
}
}
class Brinjal extends Vegetable
{
public String toString()
{
colorVeg = "Purpul";
return colorVeg;
}
}
class Tomato extends Vegetable
{
public String toString()
{
colorVeg = "Red";
return colorVeg;
}
}
class abstractClassH2E
{
public static void main(String args[])
{
Potato p = new Potato();
Brinjal b = new Brinjal();
Tomato t = new Tomato();
Vegetable vegref;
vegref = p;
System.out.println("Color of Potato : "+vegref.toString());
vegref = b;
System.out.println("Color of Brinjal : "+vegref.toString());
vegref = t;
System.out.println("Color of Tomato : "+vegref.toString());
}
}
(a) (ii) Give output of the following program for value of y=0 and y=2:
public class Test {
public static void main(String args[]) {
try {
System.out.println("calling method a");
a();
System.out.println("return from method a");
} catch(ArithmeticException e) {
System.out.println("main: catch");
} finally {
System.out.println("main: finally");
}
}
public static void a() {
try {
int x=8,y=0;
int z=x/y;
System.out.println("value of z="+z);
} catch(NumberFormatException e) {
System.out.println("method a:catch");
} finally {
System.out.println("method a:finally");
} }}
// y = 0
// calling metod a
// method a : finally
// main : catch
// main : finally
// y = 2
// calling metod a
// value of z=4
// method a : finally
// return from method a
// main : finally
public class Test
{
public static void main(String args[])
{
try
{
System.out.println("calling method a");
a();
System.out.println("return from method a");
}
catch(ArithmeticException e)
{
System.out.println("main: catch");
}
finally
{
System.out.println("main: finally");
}
}
public static void a()
{
try
{
int x=8,y=0;
int z=x/y;
System.out.println("value of z="+z);
}
catch(NumberFormatException e)
{
System.out.println("method a:catch");
}
finally
{
System.out.println("method a:finally");
}
}
}
(b) Write a program that takes input for filename and search word from commandline arguments and checks whether that file exists or not. If exists, the program will display those lines from a file that contains given search word.
import java.io.*;
class FileExistsWordSearchH2E
{
public static void main(String args[]) throws IOException
{
int fileExistFlag;
boolean wordExists=false;
File f1h2e = new File(args[0]);
fileExistFlag = f1h2e.exists()?1:0;
if(fileExistFlag == 1)
{
BufferedReader fileReader = new BufferedReader(new FileReader(f1h2e));
String wholeLine;
String word = args[1];
while ((wholeLine = fileReader.readLine()) != null)
{
String[] wordArr = wholeLine.split(" ");
for(String wordElem:wordArr)
{
if(wordElem.equalsIgnoreCase(word))
{
System.out.println("'"+word+"'" + " exists in line '" + wholeLine+"'");
wordExists = true;
}
}
}
if(!wordExists)
System.out.println("Word Not Exists in File");
}
else
{
System.out.println("File Not Exists");
}
}
}
Question 5
(a) Write an applet that draws four horizontal bars of equal size & of different colors such that they cover up the whole applet area. The applet should operate correctly even if it is resized.import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="appletComponentResizedH2E" width=400 height=400>
</applet>
*/
public class appletComponentResizedH2E extends Applet implements ComponentListener
{
int fullAppletHeight = 400, fullAppletWidth = 400;
int oneRectHeight = (fullAppletHeight/4);
public void init()
{
addComponentListener(this);
}
public void componentHidden(ComponentEvent e)
{
repaint();
}
public void componentMoved(ComponentEvent e)
{
repaint();
}
public void componentResized(ComponentEvent e)
{
fullAppletWidth = e.getComponent().getWidth();
fullAppletHeight = e.getComponent().getHeight();
oneRectHeight = (fullAppletHeight/4);
repaint();
}
public void componentShown(ComponentEvent e)
{
repaint();
}
public void paint(Graphics g)
{
g.setColor(Color.orange);
g.fillRect(0,0,fullAppletWidth,oneRectHeight);
g.setColor(Color.blue);
g.fillRect(0,oneRectHeight,fullAppletWidth,oneRectHeight);
g.setColor(Color.yellow);
g.fillRect(0,(oneRectHeight*2),fullAppletWidth,oneRectHeight);
g.setColor(Color.black);
g.fillRect(0,(oneRectHeight*3),fullAppletWidth,oneRectHeight);
}
} (a) Write an applet that tracks the position of the mouse when it is dragged or moved. At the current mouse position, it displays message (x, y) showing current position of the mouse. The message should disappear as soon as the user releases the mouse.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="mouseMoveDraggedH2E" width=300 height=300>
</applet>
*/
public class mouseMoveDraggedH2E extends Applet implements MouseListener, MouseMotionListener {
String msg="";
int mouseX=0,mouseY=0;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
repaint();
}
public void mouseEntered(MouseEvent me)
{
repaint();
}
public void mouseExited(MouseEvent me)
{
repaint();
}
public void mousePressed(MouseEvent me)
{
repaint();
}
public void mouseReleased(MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
msg = "";
repaint();
}
public void mouseDragged(MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
msg = "Help2Engg at ("+mouseX+","+mouseY+")";
repaint();
}
public void mouseMoved(MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
msg = "Help2Engg at ("+mouseX+","+mouseY+")";
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,mouseX,mouseY);
}
}
(b) Write a program that counts the no. of words in a text file. The file name is passed as a command line argument. The program should check whether the file exists or not. The words in the file are separated by white space characters.
import java.io.*;
class FileWordCountH2E
{
public static void main(String args[]) throws IOException
{
int ch;
boolean prev=false;
boolean last=false;
int word_count = 0;
int line_count = 0;
FileInputStream h2ein = new FileInputStream(args[0]);
while((ch=h2ein.read())!=-1)
{
last = false;
if(!prev && ch == ' ')
{
++word_count;
last=true;
}
if(ch==' ')
{
prev=true;
last=true;
}
else
prev=false;
if(ch=='\n')
{
++line_count;
last=true;
}
}
word_count += line_count;
if(!last)
++word_count;
System.out.println("No. of Words = "+word_count);
h2ein.close();
}
}
No comments:
Post a Comment