Java语言程序设计与数据结构(基础篇)课后练习题 第十三章(一)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java语言程序设计与数据结构(基础篇)课后练习题 第十三章(一),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含9552字,纯文字阅读大概需要14分钟。
内容图文
此文转载自:https://blog.csdn.net/IT_Holmes/article/details/11181065813.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 + "\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 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+"\nColor: "+color+"\nFilled: "+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("\tEnter width: ");
double width = input.nextDouble();
System.out.print("\tEnter height: ");
double height = input.nextDouble();
System.out.print("\tEnter Color: ");
String color = input.next();
System.out.print("\tIs 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();
}
}
}
内容总结
以上是互联网集市为您收集整理的Java语言程序设计与数据结构(基础篇)课后练习题 第十三章(一)全部内容,希望文章能够帮你解决Java语言程序设计与数据结构(基础篇)课后练习题 第十三章(一)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。