Java 语言程序设计与数据结构(基础篇)课后练习题 第十三章
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public Date getDatecreated() {
return dateCreated;
}
public String toString() {
return "Created on: " + dateCreated + "\nColor: " + color + "\nFilled: " + filled;
}
}
package dishisanzhang;
import java.util.Scanner;
public class Triangle extends GeometricObject {
private double side1 = 1.0;
private double side2 = 1.0;
private double side3 = 1.0;
public Triangle() {
}
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public void setSide1(double side1) {
this.side1 = side1;
}
public double getSide1() {
return side1;
}
public void setSide2(double side2) {
this.side2 = side2;
}
public double getSide2() {
return side2;
}
public void setSide3(double side3) {
this.side3 = side3;
}
public double getSide3() {
return side3;
}
public double getArea() {
double result;
double s = (side1 + side2 + side3) / 2;
result = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
return result;
}
public double getPerimeter() {
return side1 + side2 + side3;
}
public String toString() {
return "Triangle:side1=" + side1 + "side2=" + side2 + "side3=" + side3;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
Triangle triangle = new Triangle();
System.out.print("Enter three sides: ");
double side1 = input.nextDouble();
double side2 = input.nextDouble();
double side3 = input.nextDouble();
triangle = new Triangle(side1, side2, side3);
System.out.print("Enter the color of the triangle: ");
input.nextLine();
String color = input.nextLine();
System.out.print("Enter whether to fill: ");
boolean filled = input.nextBoolean();
triangle.setColor(color);
triangle.setFilled(filled);
System.out.println("Area: " + triangle.getArea());
System.out.println("Perimeter: " + triangle.getPerimeter());
System.out.println("Color: " + triangle.getColor());
System.out.println("Filled: " + filled);
}
}
[](()13.2
=================================================================
package dishisanzhang;
import java.util.ArrayList;
import java.util.Arrays;
public class dishisanzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer[] n = {1,2,3,4,5,6};
ArrayList<Number> list = new ArrayList<>(Arrays.asList(n));
shuffle(list);
System.out.println(list);
}
public static void shuffle(ArrayList<Number> list){
Number tmp;
for(int i=0;i<list.size();i++){
int tmp1 = (int)(Math.random()*(list.size()));
tmp = list.get(i);
list.set(i, list.get(tmp1));
list.set(tmp1, tmp);
}
}
}
[](()13.3
=================================================================
package dishisanzhang;
import java.util.ArrayList;
import java.util.Arrays;
public class dishisanzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer[] n = {1, 5, 6, 4, 3, 2};
ArrayList<Number> list = new ArrayList<>(Arrays.asList(n));
sort(list);
System.out.println(list);
}
public static void sort(ArrayList<Number> list){
Number tmp; //下面是一个冒泡排序
for(int i=0;i<list.size()-1;i++)
for(int j=0;j<list.size()-i-1;j++)
if((int)(list.get(j+1))<(int)(list.get(j))){
tmp = list.get(j);
list.set(j, j+1);
list.set(j+1, tmp);
}
}
}
[](()13.4
=================================================================
看清单!
[](()13.5
=================================================================
package dishisanzhang;
import java.util.Date;
public abstract class GeometricObject implements Comparable< GeometricObject >{
private String color;
private boolean filled;
private Date dateCreated;
protected GeometricObject(){
dateCreated = new Date();
}
protected GeometricObject(String color,boolean filled){
this.color = color;
this.filled = filled;
dateCreated = new Date();
}
public String getColor(){
return color;
}
public void setColor(String color){
this.color = color;
}
public boolean isFilled(){
return filled;
}
public void setFilled(boolean filled){
this.filled = filled;
}
public Date getDateCreated(){
return dateCreated;
}
@Override
public String toString(){
return "Create on "+dateCreated+"\nColor: "+color+"\n and Filled"+filled;
}
@Override
public int compareTo(GeometricObject o){
if (getArea() < o.getArea())
{
return -1;
}
else if (getArea() > o.getArea())
{
return 1;
}
else
return 0;
}
public static GeometricObject max(GeometricObject o1,GeometricObject o2){
if (o1.compareTo(o2) > 0)
{
return o1;
}
else
return o2;
}
public abstract double getArea();
public abstract double getPerimeter();
}
package dishisanzhang;
public class Rectangle extends GeometricObject {
private double width;
private double height;
public Rectangle() {
}
public Rectangle(double width, double height) {
this(width, height, "white", false);
}
public Rectangle(double width, double height, String color, boolean filled) {
super(color, filled);
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
@Override
public double getPerimeter() {
return 2 * (width + height);
}
@Override
public String toString() {
return "\nRectangle Width: " + getWidth() + " and Height: " + getHeight();
}
}
package dishisanzhang;
public class Circle extends GeometricObject {
private double radius;
public Circle() {
}
public Circle(double radius) {
this(radius, "white", false);
}
public Circle(double radius, String color, boolean filled) {
super(color, filled);
this.radius = radius;
}
public double getRadius() {
return radius;
}
@Override
public double getArea() {
return radius * radius * Math.PI;
}
@Override
public double getPerimeter() {
return 2 * radius * Math.PI;
}
@Override
public String toString() {
return "\nCircle Radius : " + getRadius();
}
}
package dishisanzhang;
public class dishisanzhang {
public static void main(String[] args) {
// TODO Auto-generated meth 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 od stub
Rectangle r1 = new Rectangle(1,2);
Rectangle r2 = new Rectangle(2,3);
System.out.println("The Max: "+GeometricObject.max(r1, r2));
Circle c1 = new Circle(2);
Circle c2 = new Circle(3);
System.out.println("The Max: "+GeometricObject.max(c1, c2));
}
评论