关于java的基础的题目,我是初学者。

2025-02-25 04:49:29
推荐回答(4个)
回答1:

不知道你的那个拉伸是什么意思...

package org.wubenhua;

import java.text.DecimalFormat;
import java.text.NumberFormat;

abstract class Shape {

private double xPos;
private double yPos;

public Shape() {
xPos = 0;
yPos = 0;
}

public Shape(double x, double y) {
xPos = x;
yPos = y;
}

public final double getXPos() {
return xPos;
}

public final double getYPos() {
return yPos;
}

public void moveTo(double xLoc, double yLoc) {
xPos = xLoc;
yPos = yLoc;
}

public String toString() {
String str = "(X,Y) Position: (" + xPos + "," + yPos + ")\n";
return str;
}

abstract public double area();

abstract public void stretchBy(double factor); // 拉伸因子

}

/* 三角形类*/
class Triangle extends Shape {

private double x1, y1;

private double x2, y2;

private double a , b , c;

Triangle() {}

Triangle(double x, double y, double x1, double y1, double x2, double y2) {
//点(x,y)到定点(x1,y1)(x2,y2)能构造一个三角形,三个点构造一个三角形
super(x, y);
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}

public boolean isValid() {
double x = getXPos();
double y = getYPos();
a = (double) Math.sqrt((x - x1)*(x - x1) + (y - y1)*(y - y1));
b = (double) Math.sqrt((x - x2)*(x - x2) + (y - y2)*(y - y2));
c = (double) Math.sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
/*任意两边的和大于第三边,任意两边的差小于第三边才是一个三角形*/
if((a + b > c) && (a + c > b) && (b + c > a) && (Math.abs(a - b) < c) && (Math.abs(a - c) < b) && (Math.abs(b - c) < a)) {
return true;
}
return false;
}

@Override
public double area() {
DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();
if(isValid()) {
System.out.print("三角形的面积: " + " S = ");
double result = Double.parseDouble(df.format(Math.sqrt(((a + b + c) * (a + b - c) * (a + c - b) * (b + c - a))/4)));
return result;
}
System.out.println("a = " + a + " b = " + b + " c = " + c + "不能构造一个合法的三角形!!");
return 0;
}

@Override
public void stretchBy(double factor) {
if(isValid()) {
moveTo(getXPos() + factor, getYPos() + factor);
return;
}
System.out.println("a = " + a + " b = " + b + " c = " + c + "不能构造一个合法的三角形!无法进行延伸!!");
}

}

/** 矩形类 */
class Rect extends Shape {

private double width;

private double high;

Rect() {
super();
width = 0;
high = 0;
}

Rect(double x, double y, double width, double high) {
super(x, y);
this.width = width;
this.high = high;
}

@Override
public double area() {
DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();
System.out.print("矩形的面积: " + " S= ");
double result = Double.parseDouble(df.format(width * high));
return result;

}

@Override
public void stretchBy(double factor) {

double x2 = this.getXPos() + width;
double y2 = this.getYPos() - high;

moveTo(getXPos() + factor, getYPos() + factor);
width = Math.abs(getXPos() - x2);
high = Math.abs(getYPos() - y2);
}
}

/* 圆类 */
class Circle extends Shape {

private double x1, y1;//圆心

private static final double PI = 3.14159f;

Circle() {
super();
}

Circle(double x, double y, double x1, double y1) {//点(x,y)到圆心(x1,y1)距离相等的图形就是圆了
super(x,y);
this.x1 = x1;
this.y1 = y1;
}

@Override
public double area() {
DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();
double radius = Double.parseDouble(df.format(Math.sqrt((getXPos() - x1) * (getXPos() - x1) + (getYPos() - y1) * (getYPos() - y1))));
System.out.print("圆的面积: " + " S = ");
double size = Double.parseDouble(df.format(PI * radius * radius));
return size;
}

@Override
public void stretchBy(double factor) {
moveTo(getXPos() + factor, getYPos() + factor);
}

}

public class ShapeTest {

Shape shapes[] = new Shape[3];

public void show(Shape shape) { // polymorphism method
System.out.println("\n" + shape.toString()); //动态输出点的位置
System.out.println(shape.area());//动态输出面积
}

public void stretchBeforeAndAfter() {//延伸前后
System.out.println("Before stretching ...");
for (int i = 0; i < 3; i++)
show(shapes[i]);
System.out.println("\n\nAfter stretched ...");
for (int i = 0; i < 3; i++) {
shapes[i].stretchBy(2);
show(shapes[i]); // dynamic binding
}
}

public static void main(String[] args) {
ShapeTest st = new ShapeTest();
st.shapes[0] = new Triangle(0,0,0,0,0,0);
st.shapes[1] = new Rect(1,1,4,5);
st.shapes[2] = new Circle(2,2,5,5);
st.stretchBeforeAndAfter();

}
}

回答2:

这个跟C++的差不多好不好,就是让它们子类都继承Shape 然后重写方法
如:
class Triangle extends Shape{
private double width;
private double heigth;
public double area(){
return (width*heigth)/2;
}
...
...
}
其它的自己写吧

回答3:

LZ太懒了吧,这个题可不难啊,你自己敲敲代码吧

回答4:

http://z.baidu.com/question/123806651.html
第一个回答是我的。