Object Oriented Programming With Java - May/June 2012

Question 3

(a) Write a program using BufferedInputStream, FileInputStream, BufferedOutputStream, FileOutputStream to copy Content of one file File1.txt into another file File2.txt.

import java.io.*;
class CopyFileInJavaH2E
{
 public static void main(String args[]) throws IOException
 {
  int ch;
  FileInputStream fin = new FileInputStream("File1.txt");
  BufferedInputStream bfin = new BufferedInputStream(fin);
  FileOutputStream fout = new FileOutputStream("File2.txt");
  BufferedOutputStream bfout = new BufferedOutputStream(fout);
  while((ch=bfin.read()) != -1)
   bfout.write(ch);
  bfin.close();
  bfout.close();   
 }
}

(b) Describe abstract class called Shape which has three subclasses say Triangle,Rectangle,Circle. Define one method area() in the abstract class and override this area() in these three subclasses to calculate for specific object i.e. area() of Triangle subclass should calculate area of triangle etc. Same for Rectangle and Circle.

abstract class Shape
{
 double dimension1,dimension2,radius;
 abstract double area();
}
class Triangle extends Shape
{
 Triangle(double d1,double d2)
 {
  dimension1=d1;
  dimension2=d2;
 }
 double area()
 {
  System.out.print("Area Of Triangle : ");
  return ((dimension1*dimension2)/2);
 }
}
class Rectangle extends Shape
{
 Rectangle(double d1,double d2)
 {
  dimension1=d1;
  dimension2=d2;
 }
 double area()
 {
  System.out.print("Area Of Rectangle : ");
  return (dimension1*dimension2);
 }
}
class Circle extends Shape
{
 Circle(double d1)
 {
  radius = d1;
 }
 double area()
 {
  System.out.print("Area Of Circle : ");
  return ((22*radius*radius)/7);
 }
}
class AbstractClassWithSubclassExampleH2E
{
 public static void main(String args[])
 {
  Triangle t = new Triangle(25,20);
  Rectangle r = new Rectangle(15,20);
  Circle c = new Circle(49);
  Shape shapeRef;
  shapeRef = t;
  System.out.println(shapeRef.area());
  System.out.println();
  shapeRef = r;
  System.out.println(shapeRef.area());
  System.out.println();  
  shapeRef = c;
  System.out.println(shapeRef.area());
 }
}


(a) Write a program to display the bytes of a file in reverse sequence. Provide the name of the file as a command line argument. (Use RandomAccessFile).

import java.io.*;
public class RandomAccessFileExampleH2E{
 public static void main(String[] args) throws IOException{
  File file = new File(args[0]);
  if(!file.exists())
  {
   System.out.println("File does not exist.");
   System.exit(0);
  }
  try{
   //Open the file for both reading and writing
   RandomAccessFile rand = new RandomAccessFile(file,"r"); 
   int i=(int)rand.length();
   System.out.println("Length: " + i);
      
   for(int ct = (i-1); ct > 0; ct--){
    rand.seek(ct);
    byte b = rand.readByte();
    System.out.print(b); //read the character
   }
   rand.close();
  }
   catch(IOException e)
  {
  System.out.println(e.getMessage());
  }
 }
}


(b) Write a program that illustrates interface inheritance. Interface P is extended by P1 and P2. Interface P12 inherits from both P1 and P2.Each interface declares one constant and one method. class Q implements P12.Instantiate Q and invoke each of its methods. Each method displays one of the constants.

interface P
{
 public static final int p = 5;
 void methodP();
}
interface P1 extends P
{
 public static final int p1 = 10;
 void methodP1();
}
interface P2 extends P
{
 public static final int p2 = 15;
 void methodP2();
}
interface P12 extends P1,P2
{
 public static final int p12 = 20;
 void methodP12();
} 
class Q implements P12
{
 public void methodP12()
 {
  System.out.println("P12 class's method and Constant : "+p12);
 }
 public void methodP1()
 {
  System.out.println("P1 class's method and Constant : "+p1); 
 }
 public void methodP2()
 {
  System.out.println("P2 class's method and Constant : "+p2); 
 }
 public void methodP()
 {
  System.out.println("P class's method and Constant : "+p); 
 }
}
class InterfaceWithInheritanceExampleH2E
{
 public static void main(String args[])
 {
  Q obj = new Q();
  obj.methodP12();
  obj.methodP1();
  obj.methodP2();
  obj.methodP();  
 }
}

Question 4

(a) Write an applet that contains three buttons OK,CANCEL and HELP and one textfield. if OK is pressed shown on the status bar-“OK is pressed” and the text field should turn red. When CANCEL is pressed -shown on the status bar-“ CANCEL is pressed “and text field should turn green. When HELP is pressed- shown on the status bar-“HELP is pressed” and the text field should turn yellow.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="AppletButtonActionEventH2E" width=300 height=300></applet>
*/
public class AppletButtonActionEventH2E extends Applet implements ActionListener
{
Button ok,cancel,help; TextField t;
String msg = "";
public void init()
{
ok = new Button("OK");
cancel = new Button("CANCEL");
help = new Button("HELP"); t = new TextField();
add(ok);
add(cancel);
add(help); add(t);
ok.addActionListener(this);
cancel.addActionListener(this);
help.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str = ae.getActionCommand();
if(str.equals("OK"))
{
msg = "OK is pressed"; t.setBackground(Color.RED);
}
else if(str.equals("CANCEL"))
{
msg = "CANCEL is pressed"; t.setBackground(Color.GREEN);
}
else
{
msg = "HELP is pressed";
}
repaint();
}
public void paint(Graphics g)
{
showStatus(msg); t.setBackground(Color.YELLOW);
}
}
(b) Explain Applet life cycle with demo program.
import java.awt.*;
import java.applet.*;
/*
<applet code="AppletLifeCycle" width=300 height=300>
</applet>
*/
public class AppletLifeCycle extends Applet
{
String msg1,msg2,msg3,msg4;
// called first
public void init()
{
// Initialization
msg1="init section begins";
}
// called second
public void start()
{
// start or resume execution
msg2="start section";
}
// called when applet stopped
public void stop()
{
// suspends execution
msg3="stop section";
}
// called when applet
public void destroy()
{
// execute shutdown activities
msg4="destroy section";
}
public void paint(Graphics g)
{
// redisplay content of window
g.drawString("Demo for basic methods of applet",20,40);
g.drawString(msg1,20,80);
g.drawString(msg2,20,90);
g.drawString(msg3,20,100);
g.drawString(msg4,20,120);
}
}

Question 5

(b) Explain Generics in java with demo program.
class Gen<G>
{
 G Obj; 
 Gen(G ObjI)
 {
  Obj = ObjI; 
 }
 G getObj()
 {
  return Obj;
 }
}
class GenericClassExampleH2E
{
 public static void main(String args[])
 {
  Gen<Integer> ObjGI = new Gen<Integer>(125);
  System.out.println("Value of Object : "+ObjGI.getObj()); 
  Gen<String> ObjGS = new Gen<String>("Help2Engg");
  System.out.println("Value of Object : "+ObjGS.getObj());
 }
}


(b) Differentiate Method Overloading and Method Overriding with example.

class H2EA
{
 int a,b;
 H2EA(int i,int j)
 {
  a=i;
  b=j; 
 }
 void inside()
 {
  System.out.println("Inside H2EA");
 }
 int Multiplication()
 {
  return a*b;
 }
}
class H2EB extends H2EA
{
 int c;
 H2EB(int i,int j,int k)
 {
  super(i,j);
  c=k; 
 }
 void inside()
 {
  System.out.println("Inside H2EB");
 }
 int Multiplication(int x)
 {
  return x*c;
 }
}
class methodOverloadingAndOverridingDifferenceH2E
{
 public static void main(String args[])
 {
  H2EB Obj = new H2EB(5,6,7);
  Obj.inside();  // Method Overriding
  System.out.println("Multiplication = "+Obj.Multiplication());  // Method Overloading
  System.out.println("Multiplication = "+Obj.Multiplication(8)); // Method Overloading 
 }
}


 

No comments:

Post a Comment