The first step of Java[10]

来源:互联网 发布:撞库攻击软件 编辑:程序博客网 时间:2024/05/16 19:04
/*
   Create a class named Square that contains data fields for height, weight, and surfaceArea, 
   and a method named computeSurfaceArea().  

   ·Create a child class named Cube. 
   ·Cube contains an additional data field named depth, and a computeSurfaceArea() 
     method that overrides the parent method.  
   ·Write a program called DemoSquare that instantiates a Square object and a Cube object 
     and displays the surface areas of the objects.  
   ·Put all the programs in a package called edu.ynu.java.hw51.
*/

package edu.ynu.java.hw51;

public class Square 
{
    
protected double height;
    
protected double weight;
    
protected double surfaceArea;
    
    
public Square()
    {
        height 
= 0.0;
        weight 
= 0.0;
        surfaceArea 
= 0.0;
    }
    
    
    
public Square(double height)
    {
        
this.height = height;
        
this.weight = 0.0;
    }
    
    
public double computeSurfaceArea()
    {
        
return height * height;
    }
}
package edu.ynu.java.hw51;


public class Cube extends Square
{
    
private double depth;
    
    
public Cube()
    {
        depth 
= 0.0;
    }
    
    
public Cube(double height, double weight, double depth)
    {
        
this.height = height;
        
this.weight = weight;
        
this.depth = depth;
    }
    
    
public double computeSurfaceArea()
    {
        
return height * height * 2 + height * depth * 4;
    }
}
package edu.ynu.java.hw51;


public class DemoSquare 
{
    
public static void main(String[] args)
    {
        Square square 
= new Square(1.5);
        Cube cube 
= new Cube(2.05.53.0);
        
        System.out.println(
"The surface area of this Square is " + square.computeSurfaceArea());
        System.out.println(
"The surface area of this Cube is " + cube.computeSurfaceArea());
    }
}

 

 


/*
   Create an abstract base class, Shape, to store instances of 2-dimensional geometric shapes. 
   At a minimum, a Shape should have: 
   ·an (X,Y) position, 
   ·accessor methods to return its X and Y coordinates, 
   ·a translate(new_x,new_y) method to move the shape to a new location, and 
   ·a toString() method to return its description. 
   Define subclasses of Shape for rectangles, circles, ellipses, and lines. Each class should 
   have one or more constructors which take a suitable set of parameters. For example, a circle 
   is defined by its center (X & Y) and radius, while a rectangle can be specified by the coordinates
   of one corner along with a width and a height. 
   Each class should override the toString() method of the Shape class. 
   
   Write a testbed class, Homework02, to test your classes. Create a Shape array which can store at 
   least a dozen shapes of various types. Populate the array with a mixed set of concrete objects 
   using individual assignment statements. (Or, if you're feeling adventurous, you can write a loop 
   and use the Random class to choose the type and location of Shape to insert into the array.) 
   Using only a simple for loop and a println() statement, print the description of each element of the array. 
   Other requirements: 
   ·All classes should belong to a package named edu.ynu.java.hw52. 
   ·Use doubles to store all fields of shapes. 
   ·The description generated by each toString() method should include the type of the shape and its parameters. 
   ·Use the simple, non-mathematical definition of ellipse: a circle stretched to fit a rectangular bounding box. 
*/

package edu.ynu.java.hw52;

public abstract class Shape
{    
    
protected Point2D center;
    
protected String description;
    
    
// Return a shape's description
    public abstract String toString();
    
// Translate a shape to a new position, using geometric center to locate 
    public void translate(Point2D newPoint)
    {
        
this.center = newPoint;
    }
    
    
public Point2D getCenter()
    {
        
return this.center;
    }
}

 

package edu.ynu.java.hw52;


public class Circle extends Shape
{
    
private double radius;
    
    
public double getRadius()
    {
        
return radius;
    }

    
public void setRadius(double newRadius)
    {
        
this.radius = newRadius;
    }
    
    
public String toString()
    {
        String para 
= ": center = (" + Double.toString(this.getCenter().getXpos()) + "" +
            Double.toString((
this.getCenter().getYpos())) + ") radius = " + Double.toString(radius);
        
return this.description + para;
    }
    
    
public Circle()
    {
        
this.center = new Point2D(0.00.0);
        
this.radius = 1.0;
        
this.description = "Circle";
    }
    
    
public Circle(Point2D center, double radius)
    {
        
this.center = new Point2D(center);
        
this.radius = radius;
        
this.description = "Circle";
    }
}
package edu.ynu.java.hw52;

public class Ellipse extends Rectangle
{
    
private double majorAxis;
    
private double minorAxis;
    
    
public Ellipse(Line width, Line height)
    {
        
super(width, height);
        majorAxis 
= width.Length();
        minorAxis 
= height.Length();
        
this.description = "Ellipse";
    }
    
    
public String toString()
    {
        String para 
= "  maxjor axis = " + Double.toString(majorAxis) 
            
+ "  minor axis = " + Double.toString(minorAxis);
        
return super.toString() + para;
    }
    
}
package edu.ynu.java.hw52;

public class Line extends Shape
{
    
private Point2D a, b; // tow end point
    
    
public Line()
    {
        
this.a = new Point2D(-1.00);
        
this.b = new Point2D(1.00);
        
this.center = new Point2D((this.a.getXpos() + this.b.getXpos()) / 2,
                (
this.a.getYpos() + this.b.getYpos()) / 2);
        
this.description = "Line";
    }
    
    
public Line(Point2D a, Point2D b)
    {
        
if (0 == (a.getXpos() - b.getXpos()) && (0 == a.getYpos() - b.getYpos()))
        {
// if a(x, y) == b(x, y) then, must not be a line
            throw new IllegalArgumentException("point a equals to point b!");
        }
        
this.a = new Point2D(a);
        
this.b = new Point2D(b);
        
this.center = new Point2D((this.a.getXpos() + this.b.getXpos()) / 2
                                   (
this.a.getYpos() + this.b.getYpos()) / 2);
        
this.description = "Line";
    }
    
    
public double Length()
    {
        
return Math.pow(((a.getXpos() - b.getXpos()) * (a.getXpos() - b.getXpos())
            
+ (a.getYpos() - b.getYpos()) * (a.getYpos() - a.getYpos())), 0.5);
    }
    
    
public boolean equals(Object obj)
    {
        Line tmpLine 
= (Line)obj;
        
if ((tmpLine.getA().equals(this.a) && tmpLine.getB().equals(this.b)) 
             
|| (tmpLine.getA().equals(this.b) && tmpLine.getB().equals(this.a)))
        {
            
return true;
        }
        
else
        {
            
return false;
        }
    }
    
    
public String toString()
    {
        String para 
= ": center = (" + Double.toString(this.getCenter().getXpos()) + "" +
        Double.toString((
this.getCenter().getYpos())) + ")  a = (" + getA().getXpos() + "" 
        
+ getA().getYpos() + ")  b = (" + getB().getXpos() + "" + getB().getYpos() + ")";
        
return this.description + para;
    }
    
    
public void translate(Point2D newPoint)
    {
        
double oldXposOfA = this.a.getXpos();
        
double oldYposOfA = this.a.getYpos();
        
double oldXposOfCenter = this.center.getXpos();
        
double oldYposOfCenter = this.center.getYpos();
        
double currentXpos = newPoint.getXpos();
        
double currentYpos = newPoint.getYpos();
        
        
this.a.setXpos(oldXposOfCenter + currentXpos - b.getXpos());
        
this.a.setYpos(oldYposOfCenter + currentYpos - b.getYpos());
        
this.b.setXpos(oldXposOfCenter + currentXpos - oldXposOfA);
        
this.b.setYpos(oldYposOfCenter + currentYpos - oldYposOfA);
        
super.translate(newPoint);
    }
    
    
public void setA(Point2D newA)
    {
        
if (0 == (newA.getXpos() - b.getXpos()) && (0 == newA.getYpos() - b.getYpos()))
        {
// if a(x, y) == b(x, y) then, must not be a line
            throw new IllegalArgumentException("point newA equals to point b!");
        }
        
this.a = newA;
    }
    
    
public void setB(Point2D newB)
    {
        
if (0 == (a.getXpos() - newB.getXpos()) && (0 == a.getYpos() - newB.getYpos()))
        {
// if a(x, y) == b(x, y) then, must not be a line
            throw new IllegalArgumentException("point a equals to point newB!");
        }
        
this.b = newB;
    }
    
    
public Point2D getA()
    {
        
return this.a;
    }
    
    
public Point2D getB()
    {
        
return this.b;
    }
}
package edu.ynu.java.hw52;

public class Point2D
    {
        
private double xPos;
        
private double yPos;
        
        
public boolean equals(Object obj)
        {
            Point2D tmpPoint 
= (Point2D)obj;
            
if (tmpPoint.getXpos() == this.xPos && tmpPoint.getYpos() == this.yPos)
            {
                
return true;
            }
            
else
            {
                
return false;
            }
        }
        
        
public Point2D(Point2D point2D)
        {
            
this.xPos = point2D.getXpos();
            
this.yPos = point2D.getYpos();
        }
        
        
public Point2D(double xPos, double yPos)
        {
            
this.xPos = xPos;
            
this.yPos = yPos;
        }
        
        
public void setYpos(double yPos)
        {
            
this.yPos = yPos;
        }
        
        
public void setXpos(double xPos)
        {
            
this.xPos = xPos;
        }
        
        
public double getYpos()
        {
            
return yPos;
        }
        
        
public double getXpos()
        {
            
return xPos;
        }
        
        
public Point2D()
        {
            xPos 
= yPos = 0.0;
        }
    }
package edu.ynu.java.hw52;

public class Rectangle extends Shape
{
    
// 4 corners
    protected Point2D a, b, c, d;

    
public Rectangle(Line width, Line height)
    {
        
if (width.equals(height))
        {
            
throw new IllegalArgumentException("width covered with height!");
        }
        
// Notice: uprightness validation is unredeemed!
        
// suppose width.A == height.A
        a = new Point2D(width.getA());
        b 
= new Point2D(width.getB());
        c 
= new Point2D(height.getB());
        
this.center = new Point2D((this.b.getXpos() + this.c.getXpos()) / 2,
                                   (
this.b.getYpos() + this.c.getYpos()) / 2);
        d 
= new Point2D(2 * this.center.getXpos() - this.a.getXpos(), 2 * this.center.getYpos() - this.a.getYpos());
        
this.description = "Rectangle";
    }
    
    
public String toString()
    {
        String para 
= ": center = (" + Double.toString(this.getCenter().getXpos()) + "" +
        Double.toString((
this.getCenter().getYpos())) + ")  a = (" + getA().getXpos() + "" 
        
+ getA().getYpos() + ")  b = (" + getB().getXpos() + "" + getB().getYpos() + ")  " +
        
"c = (" + getC().getXpos() + "" 
        
+ getC().getYpos() + ")  d = (" + getD().getXpos() + "" + getD().getYpos() + ")";
        
return this.description + para;
    }
    
    
public Point2D getA()
    {
        
return this.a;
    }
    
    
public Point2D getB()
    {
        
return this.b;
    }
    
    
public Point2D getC()
    {
        
return this.c;
    }
    
    
public Point2D getD()
    {
        
return this.d;
    }
}
package edu.ynu.java.hw52;
import java.util.ArrayList;;

public class TestShape 
{
    
public static void main(String[] args)
    {
        Point2D a 
= new Point2D(0.00.0);
        
        Circle circle1 
= new Circle(a, 2.5);
        Circle circle2 
= new Circle(new Point2D(1.05.3), 3.5);
        Circle circle3 
= new Circle(a, 5.0);
        
        Point2D b 
= new Point2D(1.00.0);
        
        Line line1 
= new Line(a, b);
        Line line2 
= new Line(a, new Point2D(5.5-1.0));
        Line line3 
= new Line(new Point2D(b), a);
        Point2D c 
= new Point2D(0.0-1.0);
        Point2D d 
= new Point2D(1.0-1.0);
        Rectangle rectangle1 
= new Rectangle(new Line(a, b), new Line(a, c));
        Rectangle rectangle2 
= new Rectangle(new Line(a, c), new Line(c, d));
        Rectangle rectangle3 
= new Rectangle(new Line(a, c), new Line(c, d));
        
        Ellipse ellipse1 
= new Ellipse(new Line(a, b), new Line(c, d));
        Ellipse ellipse2 
= new Ellipse(line2, line1);
        Ellipse ellipse3 
= new Ellipse(new Line(b, c), line1);
        
        ArrayList
<Shape> shapeArrayList = new ArrayList();
        shapeArrayList.add(circle1);
        shapeArrayList.add(circle2);
        shapeArrayList.add(circle3);
        shapeArrayList.add(line1);
        shapeArrayList.add(line2);
        shapeArrayList.add(line3);
        shapeArrayList.add(rectangle1);
        shapeArrayList.add(rectangle2);
        shapeArrayList.add(rectangle3);
        shapeArrayList.add(ellipse1);
        shapeArrayList.add(ellipse2);
        shapeArrayList.add(ellipse3);
        
        
for (int i = 0; i < shapeArrayList.size(); i++)
        {
            System.out.println(shapeArrayList.get(i).toString());
        }
    }

}
原创粉丝点击