Java教程

博客3

本文主要是介绍博客3,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

前三次作业总结

前言

1. 第一次题目集

第七次题目集的题目数目为两个题目,题目数目比较少。这两个题目分别为题目7-1图形卡片排序排序游戏,7-2图形卡片分组游戏。这两到题目的难度较大,涉及的知识点较多。这两到题目所涉及的具体知识点为以下,继承和多态中的,调用父类的构造方法,构造方法链,调用父类的普通方法,方法重写与重载,object类,多态,对象转化和instanceof,arraylist类,抽象类和接口中的comparable接口和cloneable接口,类的设计原则的内聚型,一致性,封装性等等知识点。

2. 第二次题目集

第二次题目集的题目数目为一个题目,题目为8-1ATM机类结构设计一,题目数目虽然少,但难度较大。这题是对所学的综合运用,所涉及的知识点很多,对象和类中的使用构造方法构造对象,通过引用变量访问对象,静态变量,方法,数据域封装,不可变对象和类,类的关系的关联,聚集和组合,继承和多态中的调用父类构造方法,构造方法链,调用父类的普通方法,方法重写与重载,对象转化,抽象类在的接口和类的设计原则等等知识点。

3. 第三次题目集

第三次题目集的题目数目也为一个题目,题目为9-1ATM机列结构设计二,题目数虽然为一个,但难度依然很大。与上一题不同的是这题多了借记卡和信用卡,和跨行办理业务的功能,所涉知识点和上题相似,对象和类中的使用构造方法构造对象,通过引用变量访问对象,静态变量,方法,数据域封装,不可变对象和类,类的关系的关联,聚集和组合,对象转化和instanceof操作符,protected数据据和方法,防止继承和重写,object类的equals方法,arraylist类,继承和多态中的调用父类构造方法,构造方法链,调用父类的普通方法,方法重写与重载,对象转化,抽象类在的接口和类的设计原则等等知识点。

具体分析建议及心得总结的

题目集七的7-1

7-1 图形卡片排序游戏 (40 分)

掌握类的继承、多态性使用方法以及接口的应用。详见作业指导书 

输入格式:

  • 首先,在一行上输入一串数字(1~4,整数),其中,1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各数字之间以一个或多个空格分隔,以“0”结束。例如: 1 3 4 2 1 3 4 2 1 3 0
  • 然后根据第一行数字所代表的卡片图形类型,依次输入各图形的相关参数,例如:圆形卡片需要输入圆的半径,矩形卡片需要输入矩形的宽和长,三角形卡片需要输入三角形的三条边长,梯形需要输入梯形的上底、下底以及高。各数据之间用一个或多个空格分隔。

输出格式:

  • 如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边不能组成三角形),则输出Wrong Format
  • 如果输入合法,则正常输出,所有数值计算后均保留小数点后两位即可。输出内容如下:
  1. 排序前的各图形类型及面积,格式为图形名称1:面积值1图形名称2:面积值2 …图形名称n:面积值,注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格;
  2. 排序后的各图形类型及面积,格式同排序前的输出;
  3. 所有图形的面积总和,格式为Sum of area:总面积值

输入样例1:

在这里给出一组输入。例如:

1 5 3 2 0

输出样例1:

在这里给出相应的输出。例如:

Wrong Format

输入样例2:

在这里给出一组输入。例如:

4 2 1 3 0
3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 3.5

输出样例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

其代码如下

import java.util.ArrayList;

import java.util.Collections;

import java.util.Scanner;

import java.util.*;

import java.lang.Math;

import java.util.Arrays;

public class Main {

              public static Scanner input = new Scanner(System.in);

              public static void main(String[] args){

              ArrayList<Integer> list = new ArrayList<Integer>();

              int num = input.nextInt();

              while(num != 0){

              if(num < 0 || num > 4){

              System.out.println("Wrong Format");

              System.exit(0);

              }

               list.add(num);

               num = input.nextInt();

              }

              DealCardList dealCardList = new DealCardList(list);

              if(!dealCardList.validate()){

              System.out.println("Wrong Format");

              System.exit(0);

              }

              dealCardList.showResult();

              input.close();

              }

       }

class Card{

        Shape shape;

       Card(){

             

       }

       Card(Shape shape){

              this.shape=shape;

       }

       public Shape getShape() {

              return shape;

       }

       public void setShape(Shape Shape) {

              this.shape=shape;

       }

      

}

class DealCardList{

       ArrayList<Card> cardList=new ArrayList<Card>();

       DealCardList(){

             

       }

       DealCardList(ArrayList<Integer> list){

              for(int i=0;i<list.size();i++)

              {

                     if(list.get(i)==1)

                     {

                            double r=Main.input.nextDouble();

                            Circle circle=new Circle(r);

                            Card card=new Card(circle);

                            card.getShape().setShapeName("Circle");

                            cardList.add(card);

                     }

                     if(list.get(i)==2) {

                            double a=Main.input.nextDouble();

                            double b=Main.input.nextDouble();

                            Rectangle rectangle=new Rectangle(a,b);

                            Card card=new Card(rectangle);

                            card.getShape().setShapeName("Rectangle");

                            cardList.add(card);

                     }

                     if(list.get(i)==3) {

                            double a=Main.input.nextDouble();

                            double b=Main.input.nextDouble();

                            double c=Main.input.nextDouble();

                            Triangle triangle=new Triangle(a,b,c);

                            Card card=new Card(triangle);

                            card.getShape().setShapeName("Triangle");

                            cardList.add(card);

                     }

                     if(list.get(i)==4) {

                            double a=Main.input.nextDouble();

                            double b=Main.input.nextDouble();

                            double c=Main.input.nextDouble();

                            Traperoid traperoid=new Traperoid(a,b,c);

                            Card card=new Card(traperoid);

                            card.getShape().setShapeName("Trapezoid");

                            cardList.add(card);

                     }

                     }

       }

       public boolean validate() {

              for(int i=0;i<cardList.size();i++)

                     {if(!cardList.get(i).getShape().vaildate())

                                   return false;}

              return true;

       }

       public void cardSort() {

              for(int k=0;k<cardList.size();k++)

                     for(int i=k+1;i<cardList.size();i++)

                     {

                            if(cardList.get(k).getShape().getArea()<cardList.get(i).getShape().getArea())

                                   Collections.swap(cardList, k, i);

                     }

             

             

       }

       public double getAllArea() {

              double s=0;

              for(int i=0;i<cardList.size();i++)

              s=s+cardList.get(i).getShape().getArea();

              return s;

       }

       public void showResult() {

              System.out.println("The original list:");

              for(int i=0;i<cardList.size();i++)

              System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

              System.out.println();

              System.out.println("The sorted list:");

              cardSort();

              for(int i=0;i<cardList.size();i++)

       System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

              System.out.println();

              System.out.println("Sum of area:"+String.format("%.2f",getAllArea()));

       }

}

 class Shape {

       private String shapeName;

       Shape(){

             

       }

       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 vaildate() {

              return true;

       }

      

}

class Circle extends Shape{

       private double radius;

       Circle(){

             

       }

       Circle(double radius){

              this.radius=radius;

       }

       public double getArea() {

              return Math.PI*radius*radius;

       }

       public boolean vaildate() {

              if(radius>0)

              return true;

              else return false;

       }

}

class Rectangle extends Shape{

       private double width,length;

       Rectangle (double width,double length){

              this.width=width;

              this.length=length;

       }

       public double getArea() {

              return width*length;

       }

       public boolean vaildate() {

              if(width>0&&length>0)

              return true;

              else return false;

       }

      

}

class Triangle extends Shape{

        double side1,side2,side3;

        Triangle(double side1,double side2,double side3){

               this.side1=side1;

               this.side2=side2;

               this.side3=side3;

        }

        public double getArea() {

               double c=(side1+side2+side3)/2;

               double s=Math.sqrt(c*(c-side1)*(c-side2)*(c-side3));

                     return s;

              }

        public boolean vaildate() {

                     if(side1+side2>side3&&side1+side3>side2&&side2+side3>side1)

                     return true;

                     else

                            return false;

              }

 

      

}

class Traperoid extends Shape{

       private double topSide,bottomSide,height;

       Traperoid(){

             

       }

       Traperoid(double topSide,double bottomSide,double height){

              this.bottomSide=bottomSide;

              this.height=height;

              this.topSide=topSide;

       }

       public double getArea() {

              return (topSide+bottomSide)*height/2;

       }

       public boolean validate() {

              if(topSide>0&&bottomSide>0&&height>0)

                     return true;

              else return false;

       }

}

class Circle1 {

       double radius;

       public double area() {

              return Math.PI*radius*radius;

       }

}

class Rectangle1 {

       double width;

       double length;

       public double area() {

              return width*length;

       }

}

class Triangle1 {

       double length1;

       double length2;

       double length3;

       public double area() {

              double s=(length1+length2+length3)/2;

             double area=Math.sqrt(s*(s-length1)*(s-length2)*(s-length3));

              return area;

       }

}

这题要求我们掌握类的继承,多态性使用以及接口的引用,要我们对图像进行分类,分为四种形状,分别为:圆形(Circle)、矩形(Rectangle)、三角形(Triangle)及梯形(Trapezoid),并按图片的大小从小到大进行排序和求出面积之和,这题排序时要用comparable接口,Card 类需要实现 Comparable 接口中的 CompareTo()方法,comparable接口定义了compareto方法,用于比较对象,是找出两个相同类型对象中较大者的通用方法,这两个对象必须是可比较的,compareto方法判断这个对象相当于给定对象的顺序,并且但这个对象小于,等于或大于时,分别负整数,零或正整数。

 

 

题目集七的7-2

7-2 图形卡片分组游戏 (60 分)

掌握类的继承、多态性使用方法以及接口的应用。 具体需求参考作业指导书。

2021-OO第07次作业-2指导书V1.0.pdf

输入格式:

  • 在一行上输入一串数字(1~4,整数),其中,1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各数字之间以一个或多个空格分隔,以“0”结束。例如:1 3 4 2 1 3 4 2 1 3 0
  • 根据第一行数字所代表的卡片图形类型,依次输入各图形的相关参数,例如:圆形卡片需要输入圆的半径,矩形卡片需要输入矩形的宽和长,三角形卡片需要输入三角形的三条边长,梯形需要输入梯形的上底、下底以及高。各数据之间用一个或多个空格分隔。

输出格式:

  • 如果图形数量非法(<=0)或图形属性值非法(数值<0以及三角形三边不能组成三角形),则输出Wrong Format
  • 如果输入合法,则正常输出,所有数值计算后均保留小数点后两位即可。输出内容如下:
  1. 排序前的各图形类型及面积,格式为[图形名称1:面积值1图形名称2:面积值2 …图形名称n:面积值n ],注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格,在结束符“]”之前;
  2. 输出分组后的图形类型及面积,格式为[圆形分组各图形类型及面积][矩形分组各图形类型及面积][三角形分组各图形类型及面积][梯形分组各图形类型及面积],各组内格式为图形名称:面积值。按照“Circle、Rectangle、Triangle、Trapezoid”的顺序依次输出;
  3. 各组内图形排序后的各图形类型及面积,格式同排序前各组图形的输出;
  4. 各组中面积之和的最大值输出,格式为The max area:面积值

输入样例1:

在这里给出一组输入。例如:

1 5 3 2 0

输出样例1:

在这里给出相应的输出。例如:

Wrong Format

输入样例2:

在这里给出一组输入。例如:

4 2 1 3 0
3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 3.5

输出样例2:

在这里给出相应的输出。例如:

The original list:
[Trapezoid:1.14 Rectangle:3.22 Circle:98.52 Triangle:4.02 ]
The Separated List:
[Circle:98.52 ][Rectangle:3.22 ][Triangle:4.02 ][Trapezoid:1.14 ]
The Separated sorted List:
[Circle:98.52 ][Rectangle:3.22 ][Triangle:4.02 ][Trapezoid:1.14 ]
The max area:98.52

输入样例3:

在这里给出一组输入。例如:

2 1 2 1 1 3 3 4 4 1 1 1 2 1 0
2.3 3.5 2.5 4.5 2.1 2.6 8.5 3.2 3.1 3.6 8.5 7.5 9.1245 6.5 3.4 10.2 11.2 11.6 15.4 5.8 2.13 6.2011 2.5 6.4 18.65

输出样例3:

在这里给出相应的输出。例如:

The original list:
[Rectangle:8.05 Circle:19.63 Rectangle:9.45 Circle:21.24 Circle:226.98 Triangle:4.65 Triangle:29.80 Trapezoid:50.49 Trapezoid:175.56 Circle:105.68 Circle:14.25 Circle:120.81 Rectangle:16.00 Circle:1092.72 ]
The Separated List:
[Circle:19.63 Circle:21.24 Circle:226.98 Circle:105.68 Circle:14.25 Circle:120.81 Circle:1092.72 ][Rectangle:8.05 Rectangle:9.45 Rectangle:16.00 ][Triangle:4.65 Triangle:29.80 ][Trapezoid:50.49 Trapezoid:175.56 ]
The Separated sorted List:
[Circle:1092.72 Circle:226.98 Circle:120.81 Circle:105.68 Circle:21.24 Circle:19.63 Circle:14.25 ][Rectangle:16.00 Rectangle:9.45 Rectangle:8.05 ][Triangle:29.80 Triangle:4.65 ][Trapezoid:175.56 Trapezoid:50.49 ]
The max area:1601.31

输入样例4:

在这里给出一组输入。例如:

1 1 3 0
6.5 12.54 3.6 5.3 6.4

输出样例4:

在这里给出相应的输出。例如:

The original list:
[Circle:132.73 Circle:494.02 Triangle:9.54 ]
The Separated List:
[Circle:132.73 Circle:494.02 ][][Triangle:9.54 ][]
The Separated sorted List:
[Circle:494.02 Circle:132.73 ][][Triangle:9.54 ][]
The max area:626.75

代码如下

import java.util.ArrayList;

import java.util.Collections;

import java.util.Scanner;

import java.util.*;

import java.lang.Math;

import java.util.Arrays;

public class Main {

              public static Scanner input = new Scanner(System.in);

              public static void main(String[] args){

              ArrayList<Integer> list = new ArrayList<Integer>();

              int num = input.nextInt();

              while(num != 0){

              if(num < 0 || num > 4){

              System.out.println("Wrong Format");

              System.exit(0);

              }

               list.add(num);

               num = input.nextInt();

              }

              DealCardList dealCardList = new DealCardList(list);

              if(!dealCardList.validate()){

              System.out.println("Wrong Format");

              System.exit(0);

              }

              dealCardList.showResult();

              input.close();

              }

       }

class Card{

        Shape shape;

       Card(){

             

       }

       Card(Shape shape){

              this.shape=shape;

       }

       public Shape getShape() {

              return shape;

       }

       public void setShape(Shape Shape) {

              this.shape=shape;

       }

      

}

class DealCardList{

       ArrayList<Card> cardList=new ArrayList<Card>();

       DealCardList(){

             

       }

       DealCardList(ArrayList<Integer> list){

              for(int i=0;i<list.size();i++)

              {

                     if(list.get(i)==1)

                     {

                            double r=Main.input.nextDouble();

                            Circle circle=new Circle(r);

                            Card card=new Card(circle);

                            card.getShape().setShapeName("Circle");

                            cardList.add(card);

                     }

                     if(list.get(i)==2) {

                            double a=Main.input.nextDouble();

                            double b=Main.input.nextDouble();

                            Rectangle rectangle=new Rectangle(a,b);

                            Card card=new Card(rectangle);

                            card.getShape().setShapeName("Rectangle");

                            cardList.add(card);

                     }

                     if(list.get(i)==3) {

                            double a=Main.input.nextDouble();

                            double b=Main.input.nextDouble();

                            double c=Main.input.nextDouble();

                            Triangle triangle=new Triangle(a,b,c);

                            Card card=new Card(triangle);

                            card.getShape().setShapeName("Triangle");

                            cardList.add(card);

                     }

                     if(list.get(i)==4) {

                            double a=Main.input.nextDouble();

                            double b=Main.input.nextDouble();

                            double c=Main.input.nextDouble();

                            Traperoid traperoid=new Traperoid(a,b,c);

                            Card card=new Card(traperoid);

                            card.getShape().setShapeName("Trapezoid");

                            cardList.add(card);

                     }

                     }

       }

       public boolean validate() {

              for(int i=0;i<cardList.size();i++)

                     {if(!cardList.get(i).getShape().vaildate())

                                   return false;}

              return true;

       }

       public void cardSort() {

              for(int k=0;k<cardList.size();k++)

                     for(int i=k+1;i<cardList.size();i++)

                     {

                            if(cardList.get(k).getShape().getArea()<cardList.get(i).getShape().getArea())

                                   Collections.swap(cardList, k, i);

                     }

             

             

       }

       public double getAllArea() {

              double s=0;

              for(int i=0;i<cardList.size();i++)

              s=s+cardList.get(i).getShape().getArea();

              return s;

       }

       public void showResult() {

              System.out.println("The original list:");

              for(int i=0;i<cardList.size();i++)

              System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

              System.out.println();

              System.out.println("The sorted list:");

              cardSort();

              for(int i=0;i<cardList.size();i++)

       System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

              System.out.println();

              System.out.println("Sum of area:"+String.format("%.2f",getAllArea()));

       }

}

 class Shape {

       private String shapeName;

       Shape(){

             

       }

       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 vaildate() {

              return true;

       }

      

}

class Circle extends Shape{

       private double radius;

       Circle(){

             

       }

       Circle(double radius){

              this.radius=radius;

       }

       public double getArea() {

              return Math.PI*radius*radius;

       }

       public boolean vaildate() {

              if(radius>0)

              return true;

              else return false;

       }

}

class Rectangle extends Shape{

       private double width,length;

       Rectangle (double width,double length){

              this.width=width;

              this.length=length;

       }

       public double getArea() {

              return width*length;

       }

       public boolean vaildate() {

              if(width>0&&length>0)

              return true;

              else return false;

       }

      

}

class Triangle extends Shape{

        double side1,side2,side3;

        Triangle(double side1,double side2,double side3){

               this.side1=side1;

               this.side2=side2;

               this.side3=side3;

        }

        public double getArea() {

               double c=(side1+side2+side3)/2;

               double s=Math.sqrt(c*(c-side1)*(c-side2)*(c-side3));

                     return s;

              }

        public boolean vaildate() {

                     if(side1+side2>side3&&side1+side3>side2&&side2+side3>side1)

                     return true;

                     else

                            return false;

              }

 

      

}

class Traperoid extends Shape{

       private double topSide,bottomSide,height;

       Traperoid(){

             

       }

       Traperoid(double topSide,double bottomSide,double height){

              this.bottomSide=bottomSide;

              this.height=height;

              this.topSide=topSide;

       }

       public double getArea() {

              return (topSide+bottomSide)*height/2;

       }

       public boolean validate() {

              if(topSide>0&&bottomSide>0&&height>0)

                     return true;

              else return false;

       }

}

class Circle1 {

       double radius;

       public double area() {

              return Math.PI*radius*radius;

       }

}

class Rectangle1 {

       double width;

       double length;

       public double area() {

              return width*length;

       }

}

class Triangle1 {

       double length1;

       double length2;

       double length3;

       public double area() {

              double s=(length1+length2+length3)/2;

             double area=Math.sqrt(s*(s-length1)*(s-length2)*(s-length3));

              return area;

       }

}

这题是上一题的递进,是进一步完善类结构设计首先根据卡片类型将所有卡片进行分组(一个类型分为一组, 所以最多四组),然后能够对每组内的卡片根据面积值从大到小进行排序,同时求出该组内所有卡片 的面积之和,最后求出每组卡片面积之和中的最大值。输出要“[圆形分组各图形类型及面积][矩形分组各图形类型 及面积][三角形分组各图形类型及面积][梯形分组各图形类型及面积],设计为上一题的递进,在前一题的基础上才能做出此题。这题用了ArrayList 工具类的使用,要符合开闭原则

 

 

这篇关于博客3的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!