第二次java总结

前言:这段时间主要涉及的知识点为继承和多态,正则表达式。题量适中,难度较难。

设计与分析:

 

期中考试第一题

 

据类图设计类,一共有两个类,Point类和Line类是组合关系,Point类的对象作为Line类的属性,两个类均有display()方法用于显示自身状态。main类中来引用Line类和Point类。

 

其中考试第二题

 

 据类图设计类,Line类和Point类仍然是组合关系,Point类的对象为Line类的属性,Plane原本应该也和Line类一样是组合关系,但题目降低了难度,只给Plane类一个color属性,这三个类都有一个共同的父类Element,都继承了Element的display()方法,从而实现了多态功能,让Plane,Point,Line类面对同一消息,会采用不同操作来展示不同的状态信息。

 

期中考试第三题

 

 

第三题则增加了一个GeometryObject类来存放信息

Main类控制结构如下

public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner in=new Scanner(System.in);
GeometryObject geometryObject=new GeometryObject();
int operation=in.nextInt();
while(operation!=0) {
switch(operation) {
case 1:
double x=in.nextDouble();
double y=in.nextDouble();
if(x<=0||x>200||y<=0||y>200){
System.out.println("Wrong Format");
}
else{
Point point=new Point(x,y);
geometryObject.add(point);
}

break;
case 2:
double a=in.nextDouble();
double b=in.nextDouble();
double c=in.nextDouble();
double d=in.nextDouble();
if(a<=0||a>200||b<=0||b>200||c<=0||c>200||d<=0||d>200){
System.out.println("Wrong Format");
}
else{
String color=in.next();
Point point1=new Point(a,b);
Point point2=new Point(c,d);
Line line=new Line(point1,point2,color);
geometryObject.add(line);
}

break;
case 3:
String color1=in.next();
Plane plane=new Plane(color1);
geometryObject.add(plane);
break;
case 4:
int e=in.nextInt();
geometryObject.remove(e-1);
break;
}
operation=in.nextInt();

}
for(int i=0;i<geometryObject.getObject().size();i++) {
geometryObject.getObject().get(i).display();
}

}

 

PTA题目集07

 第一道题和第三道题考察较为简单,主要为正则表达式和static关键词的使用,而第二道题则更多的是与数学有关的问题

PTA题目集06

在最后一题中

设计一个银行仿真系统。

设计了9个类

类图如下

 

 类之间的耦合度非常的低,而题目中要求的数据表格

 

 则被一层一层的包裹在Control类的方法中

 

public UnionPay dataBase() {
Card card1=new Card("6217000010041315709");
Card card2=new Card("6217000010041315715");
Card card3=new Card("6217000010041315718");
Card card4=new Card("6217000010051320007");
Card card5=new Card("6222081502001312389");
Card card6=new Card("6222081502001312390");
Card card7=new Card("6222081502001312399");
Card card8=new Card("6222081502001312400");
Card card9=new Card("6222081502051320785");
Card card10=new Card("6222081502051320786");

ArrayList<Card> cards1=new ArrayList<Card>();
ArrayList<Card> cards2=new ArrayList<Card>();
ArrayList<Card> cards3=new ArrayList<Card>();
ArrayList<Card> cards4=new ArrayList<Card>();
ArrayList<Card> cards5=new ArrayList<Card>();
ArrayList<Card> cards6=new ArrayList<Card>();
ArrayList<Card> cards7=new ArrayList<Card>();
ArrayList<Card> cards8=new ArrayList<Card>();
cards1.add(card1);
cards1.add(card2);
cards2.add(card3);
cards3.add(card4);
cards4.add(card5);
cards5.add(card6);
cards6.add(card7);
cards6.add(card8);
cards7.add(card9);
cards8.add(card10);
Account account1=new Account("3217000010041315709",10000.00,cards1);
Account account2=new Account("3217000010041315715",10000.00,cards2);
Account account3=new Account("3217000010051320007",10000.00,cards3);
Account account4=new Account("3222081502001312389",10000.00,cards4);
Account account5=new Account("3222081502001312390",10000.00,cards5);
Account account6=new Account("3222081502001312399",10000.00,cards6);
Account account7=new Account("3222081502051320785",10000.00,cards7);
Account account8=new Account("3222081502051320786",10000.00,cards8);

ArrayList<Account> accounts1=new ArrayList<Account>();
ArrayList<Account> accounts2=new ArrayList<Account>();
ArrayList<Account> accounts3=new ArrayList<Account>();
ArrayList<Account> accounts4=new ArrayList<Account>();
accounts1.add(account1);
accounts1.add(account2);
accounts2.add(account3);
accounts3.add(account4);
accounts3.add(account5);
accounts3.add(account6);
accounts4.add(account7);
accounts4.add(account8);
User user1=new User("杨过",accounts1);
User user2=new User("郭靖",accounts2);
User user3=new User("张无忌",accounts3);
User user4=new User("韦小宝",accounts4);

ArrayList<User> users1=new ArrayList<User>();
ArrayList<User> users2=new ArrayList<User>();
users1.add(user1);
users1.add(user2);
users2.add(user3);
users2.add(user4);



ATM atm1=new ATM("01");
ATM atm2=new ATM("02");
ATM atm3=new ATM("03");
ATM atm4=new ATM("04");
ATM atm5=new ATM("05");
ATM atm6=new ATM("06");
ArrayList<ATM> atms1=new ArrayList<ATM>();
ArrayList<ATM> atms2=new ArrayList<ATM>();
atms1.add(atm1);
atms1.add(atm2);
atms1.add(atm3);
atms1.add(atm4);
atms2.add(atm5);
atms2.add(atm6);
Bank bank1=new Bank(atms1,users1,"中国建设银行");
Bank bank2=new Bank(atms2,users2,"中国工商银行");
ArrayList<Bank> banks=new ArrayList<Bank>();

banks.add(bank1);
banks.add(bank2);
UnionPay unionPay=new UnionPay(banks);
return unionPay;
}

 

第四次实验中的农夫过河重构则设计了15个类

参考类图如下

 

 Abstract类为抽象类,有4个子类,

MaterialObject类为抽象类,有3个子类,

Abstract类为抽象类,本题中只有Boat一个子类。

各个类之间通过GameData类来打交道。

代码如下

public abstract class AbstracTransport {
protected String place;//所在地
protected int capacity;//容量
protected ArrayList goodes;//货物列表
protected String moveTo;//目的地

public AbstracTransport(String place, int capacity, ArrayList goodes, String moveTo) {
super();
this.place = place;
this.capacity = capacity;
this.goodes = goodes;
this.moveTo = moveTo;
}

public String getPlace() {
return place;
}

public void setPlace(String place) {
this.place = place;
}

public int getCapacity() {
return capacity;
}

public void setCapacity(int capacity) {
this.capacity = capacity;
}

public ArrayList getGoodes() {
return goodes;
}

public void setGoodes(ArrayList goodes) {
this.goodes = goodes;
}

public String getMoveTo() {
return moveTo;
}

public void setMoveTo(String moveTo) {
this.moveTo = moveTo;
}

public AbstracTransport() {
// TODO 自动生成的构造函数存根
}
public void moveTo() {

}

}

public class AbstractRule {

public AbstractRule() {
// TODO 自动生成的构造函数存根
}

}

public class Animal extends MaterialObject {

private HashSet recipe=new HashSet();





public void setRecipe(HashSet<Animal> recipe) {
this.recipe = recipe;
}

public Animal() {
// TODO 自动生成的构造函数存根
}
public Animal(String type) {
this.setType(type);
// TODO 自动生成的构造函数存根
}
public void eat(Animal animal) {
if(this.recipe.contains(animal)==true&&this.getPlace().equals(animal.getPlace())==true) {
animal.setExist(false);
}
}
public void eat(Plante plante) {
if(this.recipe.contains(plante)==true&&this.getPlace().equals(plante.getPlace())==true) {
plante.setExist(false);
}
}
public void addedToRecipe(Animal animal) {
if(animal.getType().equals("羊")==true) {
this.recipe.add(animal);
}

}
public void addedToRecipe(Plante plante) {
if(plante.getType().equals("菜")==true) {
this.recipe.add(plante);
}

}
public boolean isFood(Animal animal) {
if(this.getRecipe().contains(animal)==true) {
return true;
}
else {
return false;
}
}
public boolean canBeEat(Animal animal) {
if(animal.getRecipe().contains(this)==true) {
return true;
}
else {
return false;
}
}
public HashSet<Animal> getRecipe() {
return recipe;
}

}

public class Boat extends AbstracTransport {

public Boat() {
// TODO 自动生成的构造函数存根
}
public void crossRiver() {
if(this.getPlace().equals("原岸")==true) {
this.setPlace("对岸");
}
else {
this.setPlace("原岸");
}
}
public void board(MaterialObject m) {

}
public void disembark() {

}
public void crossRiver(Person person) {
if(person.getPlace().equals("原岸")==true){
person.setPlace("对岸");
}
else {
person.setPlace("原岸");
}
}
public void crossRiver(Person person,MaterialObject m) {
if(person.getPlace().equals("原岸")==true){
person.setPlace("对岸");
}
else {
person.setPlace("原岸");
}
if(m.getPlace().equals("原岸")==true){
m.setPlace("对岸");
}
else {
m.setPlace("原岸");
}
}

}

public class CrossRiverRule {

public CrossRiverRule() {
// TODO 自动生成的构造函数存根
}
public boolean hasCross(MaterialObject m) {
if(m.getPlace().equals("对岸")==true) {
return true;
}
else {
return false;
}
}
}

public class Game {

public Game() {
// TODO 自动生成的构造函数存根
}


public void run() {
Animal wolf=new Animal("狼");
Animal sheep=new Animal("羊");
Plante cabbage=new Plante("菜");
Person farmer=new Person("农民");
GameSuccessRule gameSuccessRule=new GameSuccessRule();
Scanner in=new Scanner(System.in);
int choice=0;
while(gameSuccessRule.hasWin(sheep, wolf, cabbage, farmer)==false) {
GameGui.menu();
choice=in.nextInt();
switch(choice) {
case 0: System.exit(0);
break;
case 1:/* 农夫独自过河的处理 */

farmer.crossRiver();

break;
case 2:/* 农夫带狼的处理 */
farmer.crossRiver();
wolf.crossRiver();
break;
case 3:/* 农夫带羊的处理 */
farmer.crossRiver();
sheep.crossRiver();
break;
case 4:/* 农夫带白菜的处理 */
farmer.crossRiver();
cabbage.crossRiver();
break;
}
wolf.addedToRecipe(sheep);
sheep.addedToRecipe(cabbage);
wolf.eat(sheep);
sheep.eat(cabbage);
wolf.showStatus();
sheep.showStatus();
cabbage.showStatus();
farmer.showStatus();
}
gameSuccessRule.win();
}

}

public class GameGui {
public static void menu()
{
/* 显示菜单 */
System.out.println("==================Please choose operation============");
System.out.println("\t==========1:Cross the river alone===========");
System.out.println("\t==========2:Cross the river with wolf=========");
System.out.println("\t==========3:Cross the river with sheep============");
System.out.println("\t==========4:Cross the river with cabbage==========");
System.out.println("\t==========0:Quit===============");
System.out.println("===================================================");
System.out.println("Input the number(0~4):");
}
public static void menu2()
{
/* 显示菜单 */
System.out.println("==================Please choose operation============");
System.out.println("\t==========1:Cross the river alone===========");
System.out.println("\t==========2:Cross the river with wolf and rabbit=========");
System.out.println("\t==========3:Cross the river with sheep and wolf============");
System.out.println("\t==========4:Cross the river with cabbage and carrot==========");
System.out.println("\t==========5:Cross the river with cabbage and wolf==========");
System.out.println("\t==========6:Cross the river with wolf and carrot==========");
System.out.println("\t==========7:Cross the river with cabbage and sheep==========");
System.out.println("\t==========8:Cross the river with cabbage and rabbit==========");
System.out.println("\t==========9:Cross the river with sheep and carrot==========");
System.out.println("\t==========10:Cross the river with sheep and rabbit==========");
System.out.println("\t==========11:Cross the river with rabbit and carrot==========");
System.out.println("\t==========0:Quit===============");
System.out.println("===================================================");
System.out.println("Input the number(0~11):");
}

public static void showStatus(Person person, Animal wolf, Animal sheep, Plante cabbage) {
/* 输出农夫、各种动物、物品的状态(生存、位置) */
person.showStatus();
wolf.showStatus();
sheep.showStatus();
cabbage.showStatus();
}

}

public class GameSuccessRule {

public GameSuccessRule() {
// TODO 自动生成的构造函数存根
}
public boolean hasWin(Animal sheep,Animal wolf,Plante cabbage,Person farmer) {
if(sheep.isExist()==false||wolf.isExist()==false||cabbage.isExist()==false||sheep.getPlace().equals("原岸")==true||wolf.getPlace().equals("原岸")==true||farmer.getPlace().equals("原岸")==true||cabbage.getPlace().equals("原岸")==true) {
return false;
}
else {
return true;
}

}
public static void win() {
System.out.println("you win the game");
}
}

public class Main {

public Main() {
// TODO 自动生成的构造函数存根
}

public static void main(String[] args) {
// TODO 自动生成的方法存根
Game game=new Game();
game.run();
}

}

public class MaterialObject {
private String type;
private String place="原岸";
private boolean isExist=true;
public MaterialObject(String type) {
super();
this.type = type;

}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public boolean isExist() {
return isExist;
}
public void setExist(boolean isExist) {
this.isExist = isExist;
}
public MaterialObject() {
// TODO 自动生成的构造函数存根
}
public void diedOut() {

}

public void showStatus() {
System.out.println(this.type+"在"+this.place+"是否存活"+this.isExist);
}
public void crossRiver() {
if(this.place.equals("原岸")==true) {
this.setPlace("对岸");
}
else {
this.setPlace("原岸");
}
}

}

public class ObjectExistRule {

public ObjectExistRule() {
// TODO 自动生成的构造函数存根
}

}

public class Person extends MaterialObject {

public Person() {
// TODO 自动生成的构造函数存根
}

public Person(String string) {
// TODO 自动生成的构造函数存根
this.setType(string);
}

}

public class Plante extends MaterialObject{

public Plante() {
// TODO 自动生成的构造函数存根
}

public Plante(String string) {
this.setType(string);
}

}

3踩坑心得

在PTA06题目集最后一题时不应该采用组合的方式来设计类,而应该用继承和多态的方式来存储题目中的表格信息。

4、改进建议:建议多出一些设计类题目,而不是纯数学问题的题目。

5、总结

  本阶段学会了类和对象、类与类之间的关系、java七大设计原则、一些基本的关键字、泛型容器以及尝试运用过配置文件来修改参数、java三大特性封装、继承、多态。

  对泛型容器的相关知识还需要进一步的学习和研究,希望老师能够多讲一些教材和慕课上之外的东西,希望课程能够更加紧凑一点,作业可以适当的增加一点,实验可以稍微频繁一点,上课时可以增加一点互动,使课堂更加的有趣一点,并且可以分配小组,一起完成任务,增加同学们的积极性。

posted @ 2022-05-01 15:58  一条丶学渣  阅读(37)  评论(0编辑  收藏  举报