13.1
package dishisanzhang;
import java.util.Date;
public class GeometricObject {
private String color = "white";
private boolean filled;
private Date dateCreated;
public GeometricObject() {
dateCreated = new Date();
}
public GeometricObject(String color, boolean filled) {
dateCreated = new Date();
this.color = color;
this.filled = filled;
}
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;
}
public String toString() {
return "Created on: " + dateCreated + "
Color: " + color + "
Filled: " + 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+"
Color: "+color+"
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 "
Rectangle 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 "
Circle Radius : " + getRadius();
}
}
package dishisanzhang;
public class dishisanzhang {
public static void main(String[] args) {
// TODO Auto-generated method 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));
}
}
13.6
package dishisanzhang;
public class Circle {
private double radius = 1.0;
public Circle() {
radius = 1.0;
}
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return radius * radius * Math.PI;
}
}
package dishisanzhang;
public class ComparableCircle extends Circle implements Comparable< ComparableCircle > {
public ComparableCircle() {
super();
}
public ComparableCircle(double radius) {
super(radius);
}
@Override
public int compareTo(ComparableCircle o) {
if (this.getArea() > o.getArea()) {
return 1;
} else if (this.getArea() < o.getArea()) {
return -1;
} else
return 0;
}
}
package dishisanzhang;
public class dishisanzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
ComparableCircle c1 = new ComparableCircle(2.0);
ComparableCircle c2 = new ComparableCircle(3.0);
if (c1.compareTo(c2) > 0)
System.out.println("c1 > c2");
else if (c1.compareTo(c2) == 0)
System.out.println("c1 = c2");
else
System.out.println("c1 < c2");
}
}
13.7
package dishisanzhang;
import java.util.Date;
public abstract class 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;
}
@Override
public String toString(){
return "Created on "+dateCreated+"
Color: "+color+"
Filled: "+filled;
}
public abstract double getArea();
public abstract double getPerimeter();
}
package dishisanzhang;
interface Colorable {
public abstract void howToColor();
}
package dishisanzhang;
public class Square extends GeometricObject implements Colorable{
private double width;
private double height;
public Square(){
}
public Square(double width,double height){
this(width,height,"white",false);
}
public Square(double width, double height, String color, boolean filled) {
super(color, filled);
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
@Override
public double getPerimeter() {
return 2 * (width + height);
}
@Override
public void howToColor(){
System.out.println("Color all four sides");
}
@Override
public String toString(){
if(super.isFilled())
howToColor();
return super.toString();
}
}
package dishisanzhang;
import java.util.Scanner;
public class dishisanzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
GeometricObject[] squares = new Square[5];
for (int i = 0; i < 5; i++) {
System.out.println(i + ":Square ");
System.out.print(" Enter ");
double width = input.nextDouble();
System.out.print(" Enter height: ");
double height = input.nextDouble();
System.out.print(" Enter Color: ");
String color = input.next();
System.out.print(" Is Filled: ");
boolean filled = input.nextBoolean();
squares[i] = new Square(width, height, color, filled);
}
for (int i = 0; i < 5; i ++) {
System.out.println(squares[i].toString());
System.out.println();
}
}
}