ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

BLOG-3

2022-06-18 23:06:34  阅读:154  来源: 互联网

标签:false point hasCross System BLOG public out


前言   在BLOG-3中,我将总结pta中的PTA大作业中的五、六、七、八以及最后的一次考试和超星上布置提交的四次实验作业。首先,在PTA大作业五中,我做的比较差,我认为这次作业都是即复杂又难的题。第一题在算法上还要想很久,第二题基本就没什么头绪了。在PTA大作业六中,第一题是电信计费系列1-座机计费,虽然复杂,但不至于太难,而第二题就很简单了,构建几个类个关系而已。在PTA大作业七中,第一题是电信计费系列2-手机+座机计费,在前一次作业的基础上问题不大。第二题很简单,一个主类就解决了。第三题照着给出的模板补充就行。在PTA大作业八中,第一题是电信计费系列3-短信计费,同样,在之前作业的基础上来做也不算难。第二题也简单,就两个类,主要考内部类用法。而第三题则是考抽象类,“动物发声”老师在课上就讲过,所以没什么难度。而最后一次考试,难度不算难,但每题类也不算少,而且还要根据所给类图来写代码,所以考试大概就是考察学生的照类图编写程序的基本功吧,在两个小时内写完对于我来说压力山大。对于超星上提交的作业,也就是四次实验,这四次实验都是写的农夫过河游戏,只不过越往后代码就要按所学多态、继承、抽象类、接口、JavaFX来改进。相对PTA大作业,超星作业还是相对友善的。 设计与分析   PTA大作业五第一题源码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String a = new String();
a = in.nextLine();
int num = 0;
int x = 0;
float[] point = new float[14];
if (a.substring(1, 2).equals(":") == false
|| (a.substring(0, 1).equals("1") == false && a.substring(0, 1).equals("2") == false
&& a.substring(0, 1).equals("3") == false)) {
System.out.print("Wrong Format");
return;
}
for (int i = 0; i + 2 < a.length(); i++) {
if (a.substring(i, i + 2).equals("++") == true || a.substring(i, i + 2).equals("+-") == true
|| a.substring(i, i + 2).equals("-+") == true || a.substring(i, i + 2).equals("--") == true) {
System.out.print("Wrong Format");
return;
}
}
for (int i = 0; i + 1 < a.length(); i++) {
if (a.substring(i, i + 1).equals(",")) {
num++;
}
}
if (num != 5 && a.substring(0, 1).equals("3") == false || num != 7 && a.substring(0, 1).equals("3") == true) {
System.out.print("wrong number of points");
return;
}
String b[] = a.substring(2, a.length()).split(" ");
for (int i = 0; i < b.length; i++, x = x + 2) {
String c[] = b[i].split(",");
point[x] = Integer.parseInt(c[0]);
point[x + 1] = Integer.parseInt(c[1]);
}
if ((point[0] == point[2] && point[1] == point[3]) && a.substring(0, 1).equals("3") == true) {
System.out.print("points coincide");
return;
} else {
if (a.substring(0, 1).equals("1")) {
section1(point);
return;
}
if (a.substring(0, 1).equals("2")) {
section2(point);
return;
}
if (a.substring(0, 1).equals("3")) {
section3(point);
return;
}
}
}

public static void section1(float[] point) {
float[] zhong = new float[2];
float k1 = (point[1] - point[3]) / (point[0] - point[2]);
float k2 = (point[1] - point[5]) / (point[0] - point[4]);
float k3 = (point[1] - point[7]) / (point[0] - point[6]);
float k4 = (point[1] - point[9]) / (point[0] - point[8]);
if(k1==k2||k1==k3||k1==k4||k2==k3||k2==k4||k3==k4) {
System.out.println("not a pentagon");
}
else {
System.out.println("false");
}
}

public static void section2(float[] point) {
System.out.println("not a pentagon");
}

public static void section3(float[] point) {
if(point[0]==point[2]&&point[1]==point[3]) {
System.out.println("points coincide");
}
else {
System.out.println("The line is coincide with one of the lines");
}
}
}

这一题的做法:首先写关于格式正确与否的判断语句,用String之间的比较即可。至于判断是否构成五边形,按所给点顺序依次构成线段,通过计算查看它们是否有交点就行(端点除外)。至于判断凹凸五边形,计算相邻两条线的斜率乘积,若其中有正数则为凹,否则为凸。至于选项三,同样,通过线段算出交点,然后计算所需面积即可。

踩坑心得:无。

改进建议:寻找更简便的算法。

  PTA大作业五第二题源码:(由于当时没做完,不展示源码)

这一题做法:选项4:分别计算两个五边形的线段,若它们之间有交点(除端点),则是交错。若交点只是1端点(或者算出只有1线段重合),则是连接。无交点则是分离。若输入端点两组都有对应相等的,则是完全重合。至于包含和被包含,首先要判断没有交点,其次算出它们的中心点距离是否小于它们的边长,小于则是包含关系。然后算出两五边形面积,前者大则是包含,反之则是被包含。选项五、六难到我了,所以当时没做完,这里就略过了。

踩坑心得:五。

改进建议:寻找更简便的算法。

  PTA大作业6-7-8:

  注:因为这三次作业除了第一题外都很简单,所以就不对那些题目做分析了。而第一题源码都很多,所以也不展示,只讲设计和分析的思路。

  六-1:

这一题的做法:整体上:按照题目所给类图先做个大体的框架,然后补充类图中提及的数据成员与方法,最后按照题目给出的相关方法说明写出方法体。

       细节上:”本题非法输入只做格式非法的判断,不做内容是否合理的判断(时间除外,否则无法计算)”,据此判断输入是否合法。“2、记录中如果同一电话号码的多条通话记录时间出现重                                              合,这种情况也不做判断,直接 计算每条记录的费用并累加。3、用户区号不为南昌市的区号也作为正常用户处理”据此排除可能要踩的坑,以节约时间。(字体没设好,别                   介意)。此外,可以看看所给测试用例来调整代码。

  七-1,八-1:

这一题的做法:同六-1。

  考试题目1源码:

踩坑心得:无。

改进建议:无。

 

import java.util.Scanner;

 

public class Main {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int choice = input.nextInt();
switch (choice) {
case 1:
Circle circle = new Circle(input.nextDouble());
showArea(circle);
break;
case 2:
Rectangle rectangle = new Rectangle(input.nextDouble(),input.nextDouble());
showArea(rectangle);
break;
case 3:
Ball ball = new Ball(input.nextDouble());
showArea(ball);
break;
case 4:
Box box = new Box(input.nextDouble(),input.nextDouble(),input.nextDouble());
showArea(box);
break;
default:
System.out.println("Wrong Format");
break;
}
input.close();
}
private static void showArea(AbstractShape a) {
a.show();
}
}

public abstract class AbstractShape implements Comparable{
public abstract double getArea();
public abstract void show();
}

public class Ball extends Circle{
public Ball() {

}
public Ball(double radius) {
setRadius(radius);
}
@Override
public double getArea() {
return 4*this.getRadius()*this.getRadius()*3.14;
}
@Override
public void show() {
if(this.getArea() == 0) {
System.out.print("Worng Format");
return;
}
System.out.print("Type:"+this.getClass().getName()+",Area:");
System.out.printf("%.2f",this.getArea());
}
}

public class Box extends Rectangle{
private double height = 0;
public Box() {

}
public Box(double width,double length,double height) {
setWidth(width);
setLength(length);
setHeight(height);
}
public void setHeight(double height) {
this.height=height;
}
public double getHeight() {
return height;
}
@Override
public double getArea() {
return getWidth()*getLength()*2+this.getWidth()*this.getHeight()*2+this.getHeight()*this.getLength()*2;
}
public void show() {
if(this.getHeight() == 0||this.getLength()==0||this.getWidth()==0) {
System.out.print("Worng Format");
return;
}
System.out.print("Type:"+this.getClass().getName()+",Area:");
System.out.printf("%.2f",this.getArea());
}
}

public class Circle extends AbstractShape{
private double radius = 0;

public Circle() {

}
public Circle(double radius) {
this.setRadius(radius);
}
public void setRadius(double radius) {
this.radius=radius;
}
public double getRadius() {
return radius;
}
@Override
public double getArea() {
return this.getRadius()*this.getRadius()*3.14;
}
@Override
public void show() {
if(this.getArea() == 0) {
System.out.print("Worng Format");
return;
}
System.out.print("Type:"+this.getClass().getName()+",Area:");
System.out.printf("%.2f",this.getArea());
}
}

public class Rectangle extends AbstractShape{
private double width = 0;
private double length = 0;

public Rectangle() {

}
public Rectangle(double width,double length) {
this.setLength(length);
this.setWidth(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 this.getLength()*this.getWidth();
}
@Override
public void show() {
if(this.getArea() == 0) {
System.out.print("Worng Format");
return;
}
System.out.print("Type:"+this.getClass().getName()+",Area:");
System.out.printf("%.2f",this.getArea());
}

}

这一题的做法:按照所给类图编写程序,然后根据面积公式补充完getArea()函数,最后在Main类中按照所给switch部分代码写完要执行的程序。

踩坑心得:无。

改进建议:无。

超星作业源码:

注:因为都是农夫过河的程序,这里只给出第二次实验的代码。

 

public class Cabbage {

 

public boolean crossRiver=false;
public boolean isAlive=true;
public boolean hasCross=false;

 

 

 

}

public class Farmer {

public boolean crossRiver=false;
public boolean hasCross=false;

}

import java.util.Scanner;

public class Game {
Wolf wolf;
Sheep sheep;
Cabbage cabbage;
Farmer farmer;
// GameGui gui;

Game() {
wolf = new Wolf();
sheep = new Sheep();
cabbage = new Cabbage();
farmer = new Farmer();
}

void play() {
Scanner input = new Scanner(System.in);
int choice = 0; // 用户输入选择
boolean gameOver = false, // 游戏结束标志,默认为false,代表游戏进行中,未结束
win = false; // 游戏输赢标志,默认为false,代表未赢得游戏。
while (!gameOver) {
GameGui.menu();
choice = input.nextInt();
switch (choice) {
case 0:
gameOver = true;
break;
case 1:/* 农夫独自过河的处理 */
farmer.crossRiver = !(farmer.crossRiver);
farmer.hasCross=!(farmer.hasCross);
break;
case 2:/* 农夫带狼的处理 */
farmer.crossRiver = !(farmer.crossRiver);
wolf.crossRiver = !(wolf.crossRiver);
wolf.hasCross=!(wolf.hasCross);
farmer.hasCross=!(farmer.hasCross);
break;
case 3:/* 农夫带羊的处理 */
farmer.crossRiver = !(farmer.crossRiver);
sheep.crossRiver = !(sheep.crossRiver);
sheep.hasCross=!(sheep.hasCross);
farmer.hasCross=!(farmer.hasCross);
break;
case 4:/* 农夫带白菜的处理 */
farmer.crossRiver = !(farmer.crossRiver);
cabbage.crossRiver = !(cabbage.crossRiver);
cabbage.hasCross=!(cabbage.hasCross);
farmer.hasCross=!(farmer.hasCross);
break;
}
wolf.eatSheep(sheep,wolf,farmer);// 狼吃羊,如果羊不在同一边,则吃不到,如果在同一边,羊被吃
sheep.eatCabbage(cabbage,sheep,farmer);// 同上
wolf.crossRiver=false;
sheep.crossRiver=false;
cabbage.crossRiver=false;
farmer.crossRiver=false;
GameGui.showStatus(farmer, wolf, sheep, cabbage);
gameOver = isGameOver();
}
win = this.hasWin();
if (win) {
System.out.println("game over: you win !");
} else {
System.out.println("game over: you lose !");
}
input.close();
}

/*
* 判断游戏是否结束 输入:无 运算:羊、白菜任一实体被吃,游戏结束,或者狼、羊、白菜均未被吃且全部渡过河,游戏结束 输出:游戏结束--返回true
* ,未结束--返回false
*/
public boolean isGameOver() {
if (sheep.isAlive == false || cabbage.isAlive == false) {
return true;
}
if (wolf.hasCross && sheep.hasCross && cabbage.hasCross) {
return true;
}
return false;
}

/*
* 判断游戏是否胜利 前置条件:游戏结束 输入:无 运算:狼、羊、白菜均未被吃且全部渡过河,游戏胜利,否则失败 输出:游戏胜利--返回true
* ,失败--返回false
*/
public boolean hasWin() {
if (sheep.isAlive == false || cabbage.isAlive == false) {
return false;
}
if (wolf.hasCross && sheep.hasCross && cabbage.hasCross) {
return true;
}
else {
return false;
}
}

}

public class GameGui {
public static void showStatus(Farmer farmer, Wolf wolf, Sheep sheep, Cabbage cabbage) {
/* 输出农夫、各种动物、物品的状态(生存、位置) */
System.out.println("Farmer has Cross :"+farmer.hasCross);
System.out.println("Wolf is alive :true Wolf has Cross :"+wolf.hasCross);
System.out.println("Sheep is alive :"+sheep.isAlive+" Sheep has Cross :"+sheep.hasCross);
System.out.println("Cabbage is alive :"+cabbage.isAlive+" Cabbage has Cross :"+cabbage.hasCross);
}
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 class Main {

public static void main(String[] args) {
Game game = new Game();
game.play();
}
}

public class Sheep {

public boolean crossRiver=false;
public boolean isAlive=true;
public boolean hasCross=false;

public void eatCabbage(Cabbage cabbage, Sheep sheep, Farmer farmer) {
// TODO Auto-generated method stub
if(cabbage.hasCross==sheep.hasCross&&(farmer.crossRiver==cabbage.crossRiver||farmer.crossRiver==sheep.crossRiver)||cabbage.hasCross!=sheep.hasCross) {
cabbage.isAlive=true;
}
else {
cabbage.isAlive=false;
}
}

 

}

public class Wolf {

public boolean crossRiver=false;
public boolean hasCross=false;

public void eatSheep(Sheep sheep, Wolf wolf, Farmer farmer) {
// TODO Auto-generated method stub
if(sheep.hasCross==wolf.hasCross&&(sheep.crossRiver==farmer.crossRiver||wolf.crossRiver==farmer.crossRiver)||sheep.hasCross!=wolf.hasCross) {
sheep.isAlive=true;
}
else {
sheep.isAlive=false;
}
}

 

}

这一题的做法:按照所给实验书给出的代码来编写程序。

踩坑心得:注意细节部分,如crossRiver要重置成false。

改进建议:实验三做法。

实验三:

这一题的做法:对属性进行封装,也就是设置Farmer等类的属性私有,通过方法进行调用,目的是为了日后的代码扩展和维护。

踩坑心得:无。

改进建议:实验四做法。

实验四:

这一题的做法:根据所给类图来编写农夫过河代码,通过抽象类,接口来使代码更简洁高效。

踩坑心得:无。

改进建议:无。

实验五:这里是用JavaFX来设计农夫过河的界面,做法,踩坑心得·和·改进建议就不做描述了。

总结

  通过写这些周的作业,我对java编程更加熟悉了,但随之而来的就是我还有一些不足之处,如程序写的不怎么规范,算法太过复杂等等。虽然每次作业基本都又难又耗费时间,但总归学到了些东西,如封装、多态、继承、抽象类、接口、javaFX等等。

 

标签:false,point,hasCross,System,BLOG,public,out
来源: https://www.cnblogs.com/liudaoping/p/16389502.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有