java实验三

1.设计题

代码
class Student {
String ID, Name;
char Sex;
double English, Math, Computer, Sum, Averrage;

public Student(String str1, String str2, char ch, double a, double b, double c){
ID
= str1;
Name
= str2;
Sex
= ch;
English
= a;
Math
= b;
Computer
= c;
}

public double getSum(){
Sum
= (English + Math + Computer);
return Sum;
}

public double getAve(){
Averrage
= getSum() / 3.0;
return Averrage;
}

}


class Graduate extends Student {
String Major, Field;
double Analysis;
public Graduate(String str1, String str2, char ch, double a, double b,
double c, String str3, String str4, double d){
super(str1, str2, ch, a, b, c);
Major
= str3;
Field
= str4;
Analysis
= d;
}

public double getSum(){
Sum
= English + Analysis + Computer;
return Sum;
}

public double getAve() {
Averrage
= getSum()/3.0;
return Averrage;
}
}

public class one {
public static void main(String[] args) {
Student s
= new Student("0800308128", "xiaohe", 'M', 90, 90, 90);
Graduate gg
= new Graduate("0800308128", "crazyac", 'M', 100, 100, 100, "计算机科学技术", "算法分析", 100);
System.
out.print("学生的总成绩是: " + s.getSum());
System.
out.println(" 平均成绩是: " + s.getAve());
System.
out.print("研究生的总成绩是: " + gg.getSum());
System.
out.println(" 平均成绩是: " + gg.getAve());
}
}

 2.设计题

代码
class Ellipse {
double a, b, x, y, area;
final double PI = 3.14159265;

public Ellipse(double d1, double d2, double d3, double d4) {
a
= d1; b = d2;
x
= d3; y = d4;
}

public double getArea() {
area
= PI * a * b;
return area;
}
}

class Circle extends Ellipse{
double r;
public Circle(double d1, double d2, double d3, double d4, double d5) {
super(d1, d2, d3, d4);
r
= d5;
}

public double getArea() {
area
= PI * r * r;
return area;
}

public double getLength() {
return 2 * 3.1415926 * r;
}
}


public class two {
public static void main(String[] args) {
Ellipse e
= new Ellipse(1, 2, 0, 0);
Circle c
= new Circle(1, 2, 0, 0, 3);
System.out.println(
"椭圆的面积:" + e.getArea());
System.out.println(
"圆的面积是: " + c.getArea());
System.out.println(
"圆的周长是: " + c.getLength());
}
}

 3.设计题

(1)

代码
import java.awt.Point;

abstract class Shap {
abstract Point center();
abstract double diameter();
abstract double area();
}

class Circle extends Shap {
private Point center;
private double radius;

Circle(Point center,
double radius) {
this.center = center;
this.radius = radius;
}

Circle() {
this.center = new Point(0, 0);
this.radius = 0;
}

Point center() {
return center;
}

double diameter() {
return radius * 2;
}

double area() {
return 3.14159265 * radius * radius;
}

public String toString() {
return new String ("{center = " + center + ", radius = " + radius + "}");
}
}

class myCircle {
public static void main(String[] args) {
Circle circle
= new Circle(new Point(3, 1), 2.0);
System.out.println(
"The Circle is " + circle);
System.out.println(
"center is " + circle.center());
System.out.println(
"diameter = " + circle.diameter());
System.out.println(
"area = " + circle.area());
}
}

(2)

代码
import java.awt.Point;
import java.math.*;

abstract class Shap {
abstract Point center();
abstract double diameter();
abstract double area();
}

class Square extends Shap {
private Point coner;
private double side;

Square(Point coner,
double side) {
this.coner = coner;
this.side = side;
}

Square() {
this.coner = new Point(0, 0);
this.side = 2;
}

Point center() {
Point c
= new Point(coner);
c.translate((
int)(side/2), (int)(side/2));
return c;
}

double diameter() {
return side * Math.sqrt(2);
}

double area() {
return side * side;
}

public String toString() {
return new String("{ coner = " + coner + ", side = " + side + "}");
}
}


class mySquare {
public static void main(String[] args) {
// Square square = new Square(new Point(1, 5), 4.0);
Square square = new Square();
System.out.println(
"The Square is " + square);
System.out.println(
"The center is " + square.center());
System.out.println(
"Its diameter = " + square.diameter());
System.out.println(
"Its area = " + square.area());
}
}

4.设计题

代码
import java.math.*;
import java.util.Comparator;
import java.util.Arrays;

class mypoint {
double x, y;
mypoint(
double a, double b) { x = a; y = b; }
mypoint() { x
= y = 0; }
}

class globle {
static mypoint p; //设置全局变量
globle(double a, double b) { p = new mypoint(a, b); }
}

//四边形///////////////////////////////////////////////////////////////////////
class Quadrilateral {
mypoint p1, p2, p3, p4;
double Lenth, Area;
Quadrilateral(mypoint a, mypoint b, mypoint c, mypoint d) {
p1
= a; p2 = b;
p3
= c; p4 = d;
}

void setOrder() {
mypoint temp;
mypoint ps[]
= new mypoint[3];
if(p2.x < p1.x || (p2.x == p1.x && p2.y < p1.y)) { temp = p2; p2 = p1; p1 = temp; }
if(p3.x < p1.x || (p3.x == p1.x && p3.y < p1.y)) { temp = p3; p3 = p1; p1 = temp; }
if(p4.x < p1.x || (p4.x == p1.x && p4.y < p1.y)) { temp = p4; p4 = p1; p1 = temp; }
ps[
0] = p2; ps[1] = p3; ps[2] = p4;
globle g
= new globle(p1.x, p1.y);
Arrays.sort(ps,
new myComparator());
p2
= ps[0];
p3
= ps[1];
p4
= ps[2];
}

double getDis(mypoint a, mypoint b) {
double temp;
temp
= (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
return Math.sqrt(temp);
}

double getLenth() {
double temp;
temp
= getDis(p1, p2) + getDis(p2, p3) + getDis(p3, p4) + getDis(p4, p1);
return temp;
}

//海伦公式
double getArea() {
double a, b, c, s;
a
= getDis(p1, p2);
b
= getDis(p1, p4);
c
= getDis(p2, p4);
s
= (a + b + c) / 2;
Area
= Math.sqrt(s * (s - a) * (s - b) * (s - c));
a
= getDis(p2, p3);
b
= getDis(p3, p4);
s
= (a + b + c) / 2;
Area
+= Math.sqrt(s * (s - a) * (s - b) * (s - c));
return Area;
}

}

class myComparator implements Comparator { //定义一个比较器
public int compare(Object obj1, Object obj2) {
mypoint p1
= (mypoint)(obj1);
mypoint p2
= (mypoint)(obj2);
double d1 = (double)(p1.y - globle.p.y)/(double)(p1.x - globle.p.x);
double d2 = (double)(p2.y - globle.p.y)/(double)(p2.x - globle.p.x);
if(d1 > d2) return 1; //按极坐标的大小排序
else return 0;
}
}

//平行四边形////////////////////////////////////////////////////////////////////
class Parallelogram extends Quadrilateral {
Parallelogram (mypoint a, mypoint b, mypoint c, mypoint d) {
super(a, b, c, d);
}
}

//矩形//////////////////////////////////////////////////////////////////////////
class Rectangle extends Parallelogram{
Rectangle(mypoint a, mypoint b, mypoint c, mypoint d) {
super(a, b, c, d);
}

@Override
double getArea() {
double d1, d2;
d1
= getDis(p1, p2);
d2
= getDis(p2, p3);
return d1 * d2;
}
}

//正方形///////////////////////////////////////////////////////
class Square extends Rectangle {
Square(mypoint a, mypoint b, mypoint c, mypoint d) {
super(a, b, c, d);
}
double getArea() {
double d1;
d1
= getDis(p1, p2);
return d1 * d1;
}
}

///////////////////////////////////////////////////////////////////////////////
public class four {
public static void main(String[] args) {
//四边形
mypoint p1 = new mypoint(0, 0);
mypoint p2
= new mypoint(1, 2);
mypoint p3
= new mypoint(1, 0);
mypoint p4
= new mypoint(0, 1);
Quadrilateral q
= new Quadrilateral(p1, p3, p2, p4);
q.setOrder();
System.out.println(
"四边形的边长是: " + q.getLenth());
System.out.println(
"四边形的面积是: " + q.getArea());

//平行四边形
p1 = new mypoint(0, 0);
p2
= new mypoint(2, 1);
p3
= new mypoint(1, 0);
p4
= new mypoint(1, 1);
Parallelogram para
= new Parallelogram(p1, p2, p3, p4);
para.setOrder();
System.out.println(
"平行四边形的边长是: " + para.getLenth());
System.out.println(
"平行四边形的面积是: " + para.getArea());

//矩形
p1 = new mypoint(0, 0);
p2
= new mypoint(2, 1);
p3
= new mypoint(2, 0);
p4
= new mypoint(0, 1);
Rectangle rect
= new Rectangle(p1, p2, p3, p4);
rect.setOrder();
System.out.println(
"矩形的边长是: " + rect.getLenth());
System.out.println(
"矩形的面积是: " + rect.getArea());

//正方形
p1 = new mypoint(0, 0);
p2
= new mypoint(1, 1);
p3
= new mypoint(1, 0);
p4
= new mypoint(0, 1);
Square squa
= new Square(p1, p2, p3, p4);
squa.setOrder();
System.out.println(
"正方形的边长是: " + squa.getLenth());
System.out.println(
"正方形的面积是: " + squa.getArea());
}
}

 

 

 

posted on 2010-11-17 17:34  CrazyAC  阅读(263)  评论(0编辑  收藏  举报