博客作业

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了博客作业相关的知识,希望对你有一定的参考价值。

第三次博客作业

图形卡片排序游戏

考点:类的继承,多态的使用,接口的应用
解题思路:输入采用在线做法,一边读入一边判断数据,通过case语句处理,如果超出1-4则输出Wrong Format,1-4则进行数据处理,矩阵属于梯形计算方法,因此可以直接用继承关系,梯形作为父类,case3时需要特判不是三角形的情况,先判断再跳转至计算,答案利用数组存储,先输出原数组,再进行排序,最后输出排序之后的数组
改进:可以增加图形加大复杂度

  • 测试数据1
    1 5 3 2 0
  • 输出样例1
    Wrong Format
  • 测试数据2
    4 2 1 3 0 3.2 2.5 0.4 2
  • 输出样例2
    The original list: Trapezoid:1.14 Rectangle:3.22 Circle:98.52 Triangle:4.02 The sorted list: Circle:98.52 Triangle:4.02 Rectangle:3.22 Trapezoid:1.14 Sum of area:106.91
  • 测试数据3
    4 2 1 3 0 3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 8.4
  • 输出样例3
    Wrong Format

根据本题设计出类图如下所示:
image
以下为完整代码
`import java.util.Arrays;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeSet;
abstract class Shape{
private String ShapeName;
public Shape(){

}

public Shape(String ShapeName)
{
    this.ShapeName=ShapeName;
}

public String getShapeName()
{
    return ShapeName;
}

public void setShapeName(String ShapeName)
{
    this.ShapeName=ShapeName;
}
/*public  double getArea()
{
    return 0.0;
}
public boolean validate()
{
    return true;
}*/

public abstract double getArea();
public abstract boolean validate();


public String toString()
{
    return getShapeName()+":"+String.format("%.2f ",getArea());
}

}

class Card implements Comparable
{
private Shape shape;

public Card()
{

}

public Card(Shape shape)
{
    this.shape=shape;
}

public Shape getShape()
{
    return shape;
}

public void setShape(Shape shape)
{
    this.shape=shape;
}


@Override
public int compareTo(Card card)
{
    return -(int)(shape.getArea()-card.getShape().getArea());
}

}

class Circle extends Shape
{
private double Radius;
public Circle()
{

}
public Circle(double Radius)
{
    this.Radius=Radius;
}

public double getRadius()
{
    return Radius;
}

public void setRadius(double Radius)
{
    this.Radius=Radius;
}

@Override


public double getArea()
{
    return Math.PI*Radius*Radius;
}

@Override


public boolean validate()
{
    return Radius>0;
}

}

class Rectangle extends Shape
{

private double Width;
private double Length;

public Rectangle()
{

}


public Rectangle(double Width,double Length)
{
    this.Length=Length;
    this.Width=Width;


}

public double getWidth()
{
    return Width;
}

public void setWidth(double Width)
{
    this.Width=Width;
}

public double getLength()
{
    return Length;
}

public void setLength(double Length)
{
    this.Length=Length;
}


@Override



public double getArea()
{
    return Width*Length;
}


@Override



public boolean  validate()
{
    return Width>0&&Length>0;
}

}

class Triangle extends Shape
{
private double a;
private double b;
private double c;

public Triangle()
{

}

public Triangle(double a,double b,double c)
{
    this.a=a;
    this.b=b;
    this.c=c;
}

public double getA()
{
    return a;
}

public void setA(double a)
{
    this.a=a;
}

public double getB()
{
    return b;
}

public void setB(double b)
{
    this.b=b;
}


public double getC()
{
    return c;
}

public void setC(double c)
{
    this.c=c;
}

@Override



public double getArea()
{
    double d=(a+b+c)/2;
    return Math.sqrt(d*(d-a)*(d-b)*(d-c));
}

@Override



public boolean validate()
{
    boolean t=true;
    if(!(a>0&&b>0&&c>0))
    {
        t= false;
    }
    else
    {
        if(!(a+b>c&&a+c>b&&b+c>a))
        {
            t=false;
        }
    }

    return t;
    //double[] arr=new double[3];
    //arr[0]=a;
    //arr[1]=b;
    //arr[2]=c;
    //Arrays.sort(arr);
    //return arr[0]+arr[1]>arr[2];
}

}

class Traperzoid extends Shape{
private double topSide;
private double bottomSide;
private double height;

Traperzoid(){

}


Traperzoid(double topSide,double bottomSide,double height){
    this.bottomSide=bottomSide;
    this.height=height;
    this.topSide=topSide;
}


public double getTopside()
{
    return topSide;
}


public void setTopSide(double topSide)
{
    this.topSide=topSide;
}


public double getBottomSide()
{
    return bottomSide;
}

public void setBottomSide()
{
    this.bottomSide=bottomSide;
}


public double getHeight()
{
    return height;
}

public void setHeight()
{
    this.height=height;
}


@Override


public double getArea()
{
    return (topSide+bottomSide)*height/2;
}


@Override


public boolean validate()
{
    return topSide>0&&bottomSide>0&&height>0;

}

}

public class Main{
public static Scanner input=new Scanner(System.in);
public static void main(String[] args){
ArrayList list=new ArrayList();
int n=input.nextInt();
while(n!=0)
{
if(n<0||n>4)
{
System.out.println("Wrong Format");
System.exit(0);
}
list.add(n);
n=input.nextInt();
}

    CardList cardList=new CardList(list);
    if(!cardList.validate())
    {
        System.out.println("Wrong Format");
        System.exit(0);
    }

    cardList.show();




}

}

class CardList{
ArrayList cardList=new ArrayList<>();

public CardList()
{

}

public CardList(ArrayList<Integer> card)
{
    for(Integer integer: card)
    {
        switch (integer)
        {
            case 1:
                double radius=Main.input.nextDouble();
                Circle circle=new Circle(radius);
                Card card1=new Card(circle);
                card1.getShape().setShapeName("Circle");
                cardList.add(card1);
                break;


            case 2:
                double w=Main.input.nextDouble();
                double l=Main.input.nextDouble();
                Rectangle rectangle=new Rectangle(w,l);
                Card card2=new Card(rectangle);
                card2.getShape().setShapeName("Rectangle");
                cardList.add(card2);
                break;



            case 3:
                double a=Main.input.nextDouble();
                double b=Main.input.nextDouble();
                double c=Main.input.nextDouble();
                Triangle triangle=new Triangle(a,b,c);
                Card card3=new Card(triangle);
                card3.getShape().setShapeName("Triangle");
                cardList.add(card3);
                break;


            case 4:
                double k=Main.input.nextDouble();
                double y=Main.input.nextDouble();
                double z=Main.input.nextDouble();
                Traperzoid traperzoid=new Traperzoid(k,y,z);
                Card card4=new Card(traperzoid);
                card4.getShape().setShapeName("Trapezoid");
                cardList.add(card4);
                break;


        }
    }


}

public boolean validate()//判断数据是否合法
{
    boolean t=true;
    for(Card card : cardList)
    {
        if(!card.getShape().validate())
        {
            t=false;
            break;
        }
    }

    return t;
}


public double getSum()//得到面积的总和
{
    double sum=0;
    for(Card card : cardList)
    {
        sum+=card.getShape().getArea();
    }

    return sum;
}


public void cardSort()//输出图形名字
{
    TreeSet<Card> cardSort =new TreeSet<>(cardList);
    for(Card card : cardSort)
    {
        System.out.print(card.getShape());
    }
}


public void show()//整体输出效果
{
    System.out.println("The original list:");
    for(Card card : cardList)
    {
        System.out.print(card.getShape());
    }

    System.out.println();
    System.out.println("The sorted list:");
    cardSort();
    System.out.println();
    System.out.printf("Sum of area:%.2f\\n",getSum());
}

}

图形卡片分组游戏

考点:类的继承,多态的使用,接口的应用
解题思路:与第一题相似输入采用在线做法,一边读入一边判断数据,通过case语句处理,如果超出1-4则输出Wrong Format,1-4则进行数据处理,矩阵属于梯形计算方法,因此可以直接用继承关系,梯形作为父类,case3时需要特判不是三角形的情况,先判断再跳转至计算,答案利用数组存储,先输出原数组,再进行排序,最后输出排序之后的数组,再循环求出面积最大值
改进:可以增加图形加大复杂度

  • 测试数据1
    1 5 3 2 0
  • 输出样例1
    Wrong Format
  • 测试数据2
    4 2 1 3 0 3.2 2.5 0.4 2
  • 输出样例2
    The original list: Trapezoid:1.14 Rectangle:3.22 Circle:98.52 Triangle:4.02 The sorted list: Circle:98.52 Triangle:4.02 Rectangle:3.22 Trapezoid:1.14 Sum of area:106.91
  • 测试数据3
    4 2 1 3 0 3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 8.4
  • 输出样例3
    Wrong Format

根据本题设计出类图如下所示

`

`

以上是关于博客作业的主要内容,如果未能解决你的问题,请参考以下文章

HTML5期末大作业:餐饮美食网站设计——咖啡(10页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 咖啡网页设计 美食餐饮网页设计...(代码片段

第5次博客作业

博客作业04--树

第八次团队作业:汇总博客

Wordpress阻止访问wp admin€“wpsnipp.com网站你博客的Wordpress代码片段

博客作业四——树