• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Ass2 1 (2)
 

Ass2 1 (2)

on

  • 197 views

 

Statistics

Views

Total Views
197
Views on SlideShare
197
Embed Views
0

Actions

Likes
0
Downloads
6
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Ass2 1 (2) Ass2 1 (2) Document Transcript

    • //https://code.google.com/p/fresher01dn/source/browse/trunk/?r=13#trunk %2FJavaEE_Day03%2Fsrc%2Fpk%2Fassignment01 public class Main { private Shape shapeArray[]; private TwoDimensionalShape twoDArray[]; private ThreeDimensionalShape threeDimensionalShape[]; public Main() { shapeArray = new Shape[6]; twoDArray = new TwoDimensionalShape[3]; threeDimensionalShape = new ThreeDimensionalShape[3]; Circle circle = new Circle(5); Square square = new Square(5); Triangle triangle = new Triangle(3, 4, 5); Sphere sphere = new Sphere(5); Cube cube = new Cube(3, 4); Tetrahedron tetrahedron = new Tetrahedron(3, 5, 6); shapeArray[0] = circle; twoDArray[0] = circle; shapeArray[1] = square; twoDArray[1] = square; shapeArray[2] = triangle; twoDArray[2] = triangle; shapeArray[3] = sphere; threeDimensionalShape[0] = sphere; shapeArray[4] = cube; threeDimensionalShape[1] = sphere; shapeArray[5] = sphere; threeDimensionalShape[2] = tetrahedron; } public void displayShapeInfo() { for (int i = 0; i < shapeArray.length; i++) { System.out.print(shapeArray[i].getName() + " "); shapeArray[i].print(); } System.out.println("============================================"); for (int i = 0; i < twoDArray.length; i++) { System.out.println(twoDArray[i].getName() + " co dien tich : " + twoDArray[i].getArea()); } for (int i = 0; i < threeDimensionalShape.length; i++) { System.out.println(threeDimensionalShape[i].getName() +" co the tich "+ threeDimensionalShape[i].getVolume()); } } public static void main(String[] args) { Main m = new Main(); m.displayShapeInfo(); } } ============================================ public abstract class Shape { // abstract methods public abstract String getName(); public abstract void print(); } ================================================= public abstract class TwoDimensionalShape extends Shape {
    • private int s1, s2,s3; public TwoDimensionalShape( int s1, int s2,int s3) { this.s1 = s1; this.s2 = s2; this.s3 = s3; } public int getS1() { return s1; } public void setS1(int s1) { this.s1 = s1; } public int getS3() { return s3; } public void setS3(int s3) { this.s3 = s3; } public int getS2() { return s2; } public void setS2(int s2) { this.s2 = s2; } public abstract int getArea(); } class Circle extends TwoDimensionalShape{ public Circle( int radius) { super( radius, radius,radius); } @Override public int getArea() { return (int)Math.PI *super.getS1()*super.getS1(); } @Override public String getName() { // TODO Auto-generated method stub return "Hinh tron"; } @Override public void print() { } System.out.println("co ban kinh = " + super.getS1()+" "); } class Square extends TwoDimensionalShape{ public Square(int canh) { super(canh, canh,canh);
    • } @Override public int getArea() { return super.getS1()*super.getS1(); } @Override public String getName() { // TODO Auto-generated method stub return "Hinh vuong"; } @Override public void print() { // TODO Auto-generated method stub System.out.println("Co canh = "+ super.getS1()); } } class Triangle extends TwoDimensionalShape{ public Triangle(int canh1, int canh2,int canh3) { super(canh1, canh2, canh3); // TODO Auto-generated constructor stub } @Override public int getArea() { // TODO Auto-generated method stub int sTamGiac = 0; // s = căn[p.(p - a)(p - b)(p - c)] // trong ăó p = (a + b + c)2 float p = (super.getS1()+super.getS2()+super.getS3())/2; sTamGiac =(int) Math.sqrt(p*( p -super.getS1())*(psuper.getS2())*(p-super.getS3())); return sTamGiac; } @Override public String getName() { // TODO Auto-generated method stub return "Hinh tam giac"; } @Override public void print() { // TODO Auto-generated method stub System.out.println("Co canh 1 = " +super.getS1() +" Canh 2 = "+ super.getS2() + " Canh 3 = "+ super.getS3()); } } ==================================================== public abstract class ThreeDimensionalShape extends Shape { public int getS1() { return s1; } public void setS1(int s1) { this.s1 = s1; }
    • public int getS2() { return s2; } public void setS2(int s2) { this.s2 = s2; } public int getS3() { return s3; } public void setS3(int s3) { this.s3 = s3; } int s1, s2, s3; public ThreeDimensionalShape(int s1, int s2, int s3) { this.s1 = s1; this.s2 = s2; this.s3 = s3; } public abstract int getVolume(); } class Sphere extends ThreeDimensionalShape { public Sphere(int randius) { super(randius, randius, randius); } @Override public int getVolume() { return (int) ((int) Math.PI * super.s1 * super.s1 * super.s1 * 0.75); } @Override public String getName() { return "Hinh cau "; } @Override public void print() { // TODO Auto-generated method stub System.out.println("Co ban kinh = " + super.getS1()); } } class Cube extends ThreeDimensionalShape { public Cube(int s1, int s2) { super(s1, s2, 0); } @Override public int getVolume() { return (int) (Math.PI * super.s1 * super.s1 * super.s2); }
    • @Override public String getName() { return "Hinh tru"; } @Override public void print() { System.out.println("Co ban kinh " = " + super.getS1() + " chieu cao = + super.getS2()); } } class Tetrahedron extends ThreeDimensionalShape { public Tetrahedron(int s1, int s2, int s3) { super(s1, s2, s3); // TODO Auto-generated constructor stub } @Override public int getVolume() { return super.s1 * super.s2 * super.s3; } @Override public String getName() { return "Hinh hop"; } @Override public void print() { System.out.println("Chieu rong = " + super.s1 + " chiue dai = " + super.s2 + " chieu cao " + super.s3); } } ================================================================================ ======== Cau 2 package pk.assignment02; import java.util.ArrayList; public class MyCharSequence implements CharSequence { private ArrayList<Character> data = null; public MyCharSequence() { this.setData(new String()); } @Override public char charAt(int index) { char c = (char) this.getData().get(index); return c; } @Override public int length() { return getData().size(); }
    • @Override public CharSequence subSequence(int start, int end) { if (start < 0) { throw new StringIndexOutOfBoundsException(start); } if (end > getData().size()) { throw new StringIndexOutOfBoundsException(end); } if (start > end) { throw new StringIndexOutOfBoundsException(start - end); } } MyCharSequence ch = new MyCharSequence(); ch.setData(ch.getData().subList(start, end).toString()); return ch; @Override public String toString() { String str = this.getData().toString(); return str; } public void setData(String data) { ArrayList<Character> ar = new ArrayList<Character>(); char[] cArray = data.toCharArray(); for (int i = 0; i < cArray.length; i++) { ar.add(cArray[i]); } this.data = ar; } public ArrayList<Character> getData() { return data; } } ** * Write a class that implements the CharSequence interface found in the * java.lang package. (Javadoc API: * http://download.oracle.com/javase/6/docs/api/java/lang/CharSequence.html) * Your implementation should return the string backwards (in reverse order). * Select one of the sentences from this home work to use as the data. * Write a small main method to test your class; make sure to call all four * methods. */ package pk.assignment02; public class Program { /** * @param args */ public static void main(String[] args) { MyCharSequence mcs = new MyCharSequence(); mcs.setData("Fresher01DN"); char c = mcs.charAt(2); MyCharSequence mcs2 = (MyCharSequence) mcs.subSequence(0, 4); String str = mcs.toString(); int mcsLength = mcs.length(); }
    • }