Вы находитесь на странице: 1из 6

Three level point/circle/cylinder hierarchy 1. Point o x-y coordinate pair 2. Circle o x-y coordinate pair o Radius 3.

Cylinder o x-y coordinate pair o Radius o Height Cylinder , use Point3 + Circle 4 , ( 4 classes )

public class Cylinder extends Circle4 { private double height; // Cylinder's height // no-argument constructor public Cylinder() { // implicit call to Circle4 constructor occurs here } // constructor public Cylinder( int xValue, int yValue, double radiusValue, double heightValue ) { super( xValue, yValue, radiusValue ); // call Circle4 constructor setHeight( heightValue ); } // set Cylinder's height public void setHeight( double heightValue ) { height = ( heightValue < 0.0 ? 0.0 : heightValue ); }

// get Cylinder's height public double getHeight() { return height; } // override Circle4 method getArea to calculate Cylinder area public double getArea() { return 2 * super.getArea() + getCircumference() * getHeight(); } // calculate Cylinder volume public double getVolume() { return super.getArea() * getHeight(); } // return String representation of Cylinder object public String toString() { return super.toString() + "; Height = " + getHeight(); } } // end class Cylinder import java.text.DecimalFormat; import javax.swing.JOptionPane; public class CylinderTest { public static void main( String[] args ) { // create Cylinder object Cylinder cylinder = new Cylinder( 12, 23, 2.5, 5.7 ); // get Cylinder's initial x-y coordinates, radius and height String output = "X coordinate is " + cylinder.getX() + "\nY coordinate is " + cylinder.getY() + "\nRadius is " + cylinder.getRadius() + "\nHeight is " + cylinder.getHeight(); cylinder.setX( 35 ); // set new x-coordinate cylinder.setY( 20 ); // set new y-coordinate cylinder.setRadius( 4.25 ); // set new radius cylinder.setHeight( 10.75 ); // set new height // get String representation of new cylinder value output += "\n\nThe new location, radius and height of cylinder are\n" + cylinder.toString(); // format floating-point values with 2 digits of precision DecimalFormat twoDigits = new DecimalFormat( "0.00" ); // get Cylinder's diameter output += "\n\nDiameter is " + twoDigits.format( cylinder.getDiameter() ); // get Cylinder's circumference

output += "\nCircumference is " + twoDigits.format( cylinder.getCircumference() ); // get Cylinder's area output += "\nArea is " + twoDigits.format( cylinder.getArea() ); // get Cylinder's volume output += "\nVolume is " + twoDigits.format( cylinder.getVolume() ); JOptionPane.showMessageDialog( null, output ); // display output System.exit( 0 ); } // end main } // end class CylinderTest C1-IO3 public class RoundTwoDecimalPlaces{ public static void main(String[] args) { float num = 2.954165f; float round = Round(num,2); System.out.println("Rounded data: " + round); } public static float Round(float Rval, int Rpl) { float p = (float)Math.pow(10,Rpl); Rval = Rval * p; float tmp = Math.round(Rval); return (float)tmp/p; }

C3-IO3

Simple Program On Java for the implementation of Multiple inheritance using interfaces to calculate the area of a rectangle and triangle /* Area Of Rectangle and Triangle using Interface * / interface Area { float compute(float x, float y); } class Rectangle implements Area { public float compute(float x, float y) { return(x * y); } } class Triangle implements Area

{ public float compute(float x,float y) { return(x * y/2); } } class InterfaceArea { public static void main(String args[]) { Rectangle rect = new Rectangle(); Triangle tri = new Triangle(); Area area; area = rect; System.out.println("Area Of Rectangle = "+ area.compute(1,2)); area = tri; System.out.println("Area Of Triangle = "+ area.compute(10,2)); } } /** OUTPUT ** Area Of Rectangle = 2.0 Area Of Triangle = 10.0 */ C3-IO4 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. class A implements IntfA {public int a; A() {this(10); } A(int x) {a=x; } public String MethodofIntfA() {String s="mehul"; return s; } public void methodofA() {System.out.println(" Method Of A " +a); } } interface IntfA extends IntfB { //constant can be declare;;;;;;;;;;;;;;;;;;;; how use it's public String MethodofIntfA(); //public String MethodofIntfA1(); }

28. 29. public interface IntfB 30. {//constant can be declare;;;;;;;;;;;;;;;;;;;; how use it's 31. public String MethodofIntfB(); 32. //public String MethodofIntfA1(); 33. } C3-IO2 Write a java program for creating and accesing iner class. Public class outs { Private int outerInt=100; Public class ins { Private int innerInt=25; Public void specialmethod() { System.out.println(outerInt); System.out.println(innerInt); } } }

C3-IO1 Write a program to create thread using anonymous class/local class. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 O/P:Class testAno { public static void main(String args[]) { Thread t =new Thread(new runnable(){ public void run() { System.out.println("I am in Thread"); } }); t.start(); } } I am in Thread OR Runnable Interface:

Create Thread Using Interface 1 2 3 4

class Method { public static void main(String args[]) {

5 6 7 8 9 10 11 12 13 14 15 16 17 }

TestThread runnable_obj=new TestThread(); Thread t=new Thread(runnable_obj); t.start(); } } class TestThread implements Runnable { public void run() { System.out.println("In Thread Main"); }

Вам также может понравиться