Vous êtes sur la page 1sur 11

30608205014 PROGRAM: SHAPE.

JAVA Public abstract class shape { private static int counter=0; private int idnumber; public shape() { idnumber=++counter; } Public int getidnumber() { Return idnumber; } Public double area() { Return 0.0; } Public double volume() { Return 0.0; Public abstract String getname(); } POINT.JAVA public class point extends shape { protected int x,y; public point(int a,int b) { x=a; y=b; } public int getx() { return x; } public int gety() { return y;

} public String toString() { return "["+x+","+y+"]"; } public String getname() { return "point"; } } CIRCLE.JAVA public class circle extends point { protected double radius; public circle(double r,int a,int b) { super(a,b); radius=(r>=0 ? r:0); } public double area() { return Math.PI*radius*radius; } public String toString() { return "center="+super.toString()+"Radius="+radius; } public String getname() { return "circle"; } } CYLINDER.JAVA public class cylinder extends circle { protected double height; public cylinder(double h,double r,int a,int b) { super(r,a,b); height=(h>=0 ? h:0); } public double area()

{ return 2*super.area()+2*Math.PI*radius*height; } public double volume() { return super.area()*height; } public String toString() { return super.toString()+"Height="+height; } public String getname() { return "Cylinder"; } } RECTANGLE.JAVA public class rectangle extends point { protected double length; protected double width; public rectangle(double len,double wid,int a,int b) { super(a,b); length=(len>=0 ? len:0); width=(wid>=0 ? wid:0); } public double area() { return length*width; } public String toString() { return Center=+super.toString()+Length=+length+,width=+width; } public String getname() { if(length==width) return square; else return Rectangle; } }

RECTANGULARSOLID.JAVA public class rectangularsolid extends rectangle { protected double height; public rectangularsolid(double h,double len,double wid,int a,int b) { super(len,wid,a,b); height=(h>=0 ? h:0); } public double area() { return 2*super.area()+2*length*height+2*width*height; } public double volume1() { return super.area()*height; } public double volume() { return super.area()*height; } public String toString() { return super.toString()+";height="+height; } public String getname() { if(length==width && length==height) return("cube"); else return "Rectangular solid"; } } SHAPETEST.JAVA public class shapetest { private shape shapes[]; public void createshapes() { shapes=new shape[15];

shapes[0]=new rectangle(3.0,4.0,6,8); shapes[1]=new point(7,11); shapes[2]=new circle(3.5,22,8); shapes[3]=new cylinder(10,3.3,10,10); shapes[4]=new rectangularsolid(2.0,3.0,4.0,6,8); shapes[5]=new point(8,12); shapes[6]=new rectangle(2.0,2.0,7,9); shapes[7]=new circle(3.6,23,9); shapes[8]=new cylinder(12,3.4,20,20); shapes[9]=new rectangle(6.0,6.0,5,7); shapes[10]=new rectangularsolid(3.0,3.0,3.0,10,4); shapes[11]=new point(9,13); shapes[12]=new circle(3.7,24,10); shapes[13]=new cylinder(14,3.5,30,30); shapes[14]=new rectangularsolid(4.0,4.0,4.0,8,9); } public void printshapes() { System.out.println("PRINT THE SHAPES AS AN ARRAY OF SHAPE"); for(int i=0;i<shapes.length;i++) { System.out.println(shapes[i].getname()+":"+shapes[i].toString() +",ID:"+shapes[i].getidnumber()); System.out.println("Area="+shapes[i].area()); System.out.println("Volume="+shapes[i].volume()); System.out.println(); } } public void insertionsort() { for(int index=1;index<shapes.length;index++) { shape key=shapes[index]; int position=index; while(position>0 && compareshapes(shapes[position-1],key)>0) { shapes[position]=shapes[position-1]; position--; } shapes[position]=key; } } private int compareshapes(shape s1,shape s2) { if((s1.getname()).equals(s2.getname())) return s1.getidnumber()-s2.getidnumber();

else return (s1.getname()).compareTo(s1.getname()); } public static void main(String args[]) { shapetest Shapetest=new shapetest(); Shapetest.createshapes(); Shapetest.insertionsort(); Shapetest.printshapes(); } }

OUTPUT PRINT THE SHAPES AS AN ARRAY OF SHAPE rectangle:Center=[6,8],length=3.0,width=4.0,ID:1 Area=12.0 Volume=0.0 point:[7,11],ID:2 Area=0.0 Volume=0.0 circle:center=[22,8]Radius=3.5,ID:3 Area=38.48451000647496 Volume=0.0 Cylinder:center=[10,10]Radius=3.3Height=10.0,ID:4 Area=275.76900313211206 Volume=342.11943997592846 Rectangular solid:Center=[6,8],length=3.0,width=4.0;height Area=52.0 Volume=24.0 point:[8,12],ID:6 Area=0.0 Volume=0.0 square:Center=[7,9],length=2.0,width=2.0,ID:7 Area=4.0 Volume=0.0 circle:center=[23,9]Radius=3.6,ID:8 Area=40.71504079052372 Volume=0.0 Cylinder:center=[20,20]Radius=3.4Height=12.0,ID:9 Area=328.9875826839231 Volume=435.8017329059761 square:Center=[5,7],length=6.0,width=6.0,ID:10 Area=36.0 Volume=0.0

cube:Center=[10,4],length=3.0,width=3.0;height=3.0,ID:11 Area=54.0 Volume=27.0 point:[9,13],ID:12 Area=0.0 Volume=0.0 circle:center=[24,10]Radius=3.7,ID:13 Area=43.00840342764427 Volume=0.0 Cylinder:center=[30,30]Radius=3.5Height=14.0,ID:14 Area=384.84510006474966 Volume=538.7831400906495 cube:Center=[8,9],length=4.0,width=4.0;height=4.0,ID:15 Area=96.0 Volume=64.0

30608205014 PROGRAM: import java.io.*; import java.util.*; class Stack { static void showpush(Stack st,int a) { st.push(new Integer(a)); System.out.println("Push("+a+")"); System.out.println("Stack:"+st); } static void showpop(Stack st) { System.out.println("Pop----->"); Integer a=(Integer)st.pop(); System.out.println(a); System.out.println("Stack:"+st); } public static void main(String arg[])throws IOException { DataInputStream d=new DataInputStream(System.in); Stack st=new Stack(); int n; System.out.println("Stack:"+st); do { System.out.println("Enter the value of n:"); n=Integer.parseInt(d.readLine()); switch(n) { case 1: { System.out.println("Enter the number to be inserted "); int x=Integer.parseInt(d.readLine()); showpush(st,x); break; } case 2: { showpop(st);

break; } case 0: { System.out.println("Wrong case"); } } } while(n!=0); } }

OUTPUT Stack[] Enter the value of n 1 Enter the number to be inserted 3 Push(3) Stack(3) Enter the value of n 1 Enter the number to be inserted 5 Push(5) Stack(3,5) Enter the value of n 2 Pop---> Stack[3] Enter the value of n 2 Pop----> Stack[] Enter the value of n 0 Wrong case

Vous aimerez peut-être aussi