import java.io.*;(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.
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();
}
}
abstract class Shape(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).
{
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());
}
}
import java.io.*;(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.
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());
}
}
}
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();
}
}
Admin
Mukund Dohal