第二次java博客

一。对于近三次的pta作业分析

 先来谈谈这次的期中吧

第一题

这个Java程序包含了一个Main类和一个yuan类,主要功能是实现对用户输入的圆半径计算面积并输出结果。

首先是Main类,其中定义了一个双精度变量r,并使用Scanner类获取用户输入的半径。然后创建yuan对象y,并调用yuan方法传入获取到的半径参数,然后用户通过输出效果评价程序是否正确。

其次是yuan类,它具有一个yuan方法,该方法接收用户输入的圆的半径r,用于计算圆的面积,最后使用System.out.printf函数输出结果。如果半径小于等于0,则输出"Wrong Format"来提示用户输入信息不正确。

总结来说,该程序相对简单,缺少很多代码的优化和健壮性措施,如无法处理非数字字符、无法捕获异常、没有使用面向对象编程思想等等。为了进一步提高程序的可读性和可维护性,可以采用OOP设计模式,将计算圆面积和输出结果这两个功能分离出来,使代码更加清晰易懂。同时,可以追加无限判断,如保留位数、非法字符、格式错误等,并进行相应提示和纠错,加强程序的健壮性,从而提高程序的可靠性和可复用性。

public class Main {
public static void main(String[]args) {
double r;
Scanner in = new Scanner(System.in);
r = in.nextDouble();
yuan y=new yuan();
y.yuan(r);
}
}
class yuan{
public void yuan(double r) {
if(r>0){
double s=r*r*Math.PI;
System.out.printf("%.2f",s);
}
else
System.out.println("Wrong Format");
}
}

第二题

这个Java程序包含了一个Main类、一个Point类和一个Rectangle类,主要功能是对用户输入的点坐标信息进行处理,计算并输出矩形面积。

首先是Main类,其中定义了4个双精度变量x1、x2、y1和y2,并使用Scanner类获取用户输入的两个点的横纵坐标。接着创建Point对象dian1和dian2,分别保存两个点的坐标信息,然后根据两点创建Rectangle对象ju,并调用getAres方法计算矩形面积,最后使用System.out.printf函数输出结果。

其次是Point类,它具有一个默认构造方法、一个带有参数的构造方法和getX、getY、setX、setY等基本方法。它的作用是用于表示平面直角坐标系中的一个点(x,y)。

然后是Rectangle类,表示一个矩形形状。它包含两个Point类型的变量topLeftPoint(左上角点)和lowerRightPoint(右下角点),具有getTopLeftPoint、setTopLeftPoint、getLowerRightPoint、setLowerRightPoint、getLength、getHigh和getAres等方法。在构建Reatangle实例时,将两个方向相反的坐标转化为确定的左上角点和右下角点后,就可以利用相邻两点之间距离公式得出矩形的长与宽,从而求出矩形面积。

总结来说,该程序实现功能简单,代码清晰易懂,采用了面向对象编程的思想,将矩形的两个点作为Rectangle类的成员变量进行存储和访问,实现了高内聚、低耦合的编程风格。

 

import java.util.*;
public class Main
{

public static void main(String[] args)
{
Scanner in =new Scanner(System.in);
double x1,x2,y1,y2;
x1=in.nextDouble();
y1=in.nextDouble();
x2=in.nextDouble();
y2=in.nextDouble();
Point dian2=new Point(x2,y2);
Point dian1=new Point(x1,y1);
Reatangle ju=new Reatangle(dian1,dian2);
System.out.printf("%.2f",ju.getAres());
}

}
class Point
{
double x=0;
double y=0;
Point()
{

}
Point(double x,double y)
{
this.x = x;
this.y = y;
}
double getX() {
return x;
}
void setX(double x) {
this.x = x;
}
double getY() {
return y;
}
void setY(double y) {
this.y = y;
}
}
class Reatangle
{
Point topLeftPoint;
Point lowerRightPoint;
Reatangle()
{}
Reatangle(Point topLeftPoint,Point lowerRightPoint)
{
this.setTopLeftPoint(topLeftPoint);
this.setLowerRightPoint(lowerRightPoint);
}
Point getTopLeftPoint() {
return topLeftPoint;
}
void setTopLeftPoint(Point topLeftPoint) {
this.topLeftPoint = topLeftPoint;
}
Point getLowerRightPoint() {
return lowerRightPoint;
}
void setLowerRightPoint(Point lowerRightPoint) {
this.lowerRightPoint = lowerRightPoint;
}
double getLength()
{
return Math.abs(topLeftPoint.getX()-lowerRightPoint.getX());
}
double getHigh()
{
return Math.abs(topLeftPoint.getY()-lowerRightPoint.getY());
}
double getAres()
{
return Math.abs(getLength()*getHigh());
}

}

 

第三题

这份Java代码主要实现了从控制台获取用户输入信息,根据不同的用户选择创建相应的对象并计算其面积。程序包含四个类:Main、Shape、Point、Rectangle和yuan。

其中,Main类中使用Scanner类获取用户输入,并根据用户选择创建Shape对象进行面积计算。printArea方法用于输出图形的面积。

Shape类是一个抽象类,其中为了规范编程风格和提高代码可读性,标准化命名方式并添加无参构造函数。由于一些图形计算需要输入参数,因此定义getAres()和 chack()方法作为接口。同时,增加了chack变量的声明,但这种变量并没有被设置或使用,可能会影响类中其他方法的正常执行。

Point类保存了各个坐标点的x,y值信息,有set,get和两个构造方法,并没有承担复杂的逻辑判断及运算。

Based on the Shape类,Rectangle类表示矩形,通过左上角点和右下角点的信息来定义,包含了计算长度、高度及面积的成员方法。但该类中存在一个值得注意的问题,如两个点的坐标位置出错,有可能导致负数出现,因此可以通过在setXXX方法中添加判定值是否小于0的处理逻辑来使类更加健壮。

Lastly, yuan类表示圆形,拥有可供给Shape调用的getAres()方法和新添加的检查半径输入值是否合法的chack()方法。

总体评价来说,该程序结构清晰、逻辑合理,封装性和数据处理较为优秀。但仍可对代码进行进一步完善和优化,比如简化Main类中的选择条件语句,并在Point类中增加更多坐标点的成员变量等。同时,应该更加注重程序中数据的边界问题和异常处理能力,以提高代码结果正确性、稳定性和维护性。

import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

int choice = input.nextInt();

switch(choice) {
case 1://Circle
double radiums = input.nextDouble();
Shape y = new yuan(radiums);
printArea(y);
break;
case 2://Rectangle
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();

Point leftTopPoint = new Point(x1,y1);
Point lowerRightPoint = new Point(x2,y2);

Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);

printArea(rectangle);
break;
}

}

private static void printArea(Shape y) {
if(y.chack())
System.out.printf("%.2f",y.getAres());
}
}
abstract class Shape{

Shape()
{}
public double getAres(){
return 0;
}
boolean chack;
boolean chack() {
return true;
}
}
class Point
{
double x=0;
double y=0;
Point()
{

}
Point(double x,double y)
{
this.x = x;
this.y = y;
}
double getX() {
return x;
}
void setX(double x) {
this.x = x;
}
double getY() {
return y;
}
void setY(double y) {
this.y = y;
}
}
class Rectangle extends Shape
{
Point topLeftPoint;
Point lowerRightPoint;
Rectangle()
{}
Rectangle(Point topLeftPoint,Point lowerRightPoint)
{
this.setTopLeftPoint(topLeftPoint);
this.setLowerRightPoint(lowerRightPoint);
}
public Point getTopLeftPoint() {
return topLeftPoint;
}
public void setTopLeftPoint(Point topLeftPoint) {
this.topLeftPoint = topLeftPoint;
}
public Point getLowerRightPoint() {
return lowerRightPoint;
}
public void setLowerRightPoint(Point lowerRightPoint) {
this.lowerRightPoint = lowerRightPoint;
}
public double getLength()
{
return Math.abs(topLeftPoint.getX()-lowerRightPoint.getX());
}
public double getHigh()
{
return Math.abs(topLeftPoint.getY()-lowerRightPoint.getY());
}
@Override
public double getAres()
{
return Math.abs(getLength()*getHigh());
}

}
class yuan extends Shape{
double radiums=0;
@Override
public double getAres()
{
return Math.pow(getRadiums(),2)*Math.PI;
}
public double getRadiums() {
return radiums;

}
public void setRadiums(double radiums) {
this.radiums = radiums;
}
yuan(){}
yuan(double radiums)
{
this.radiums = radiums;
}
@Override
public boolean chack()
{
if(getRadiums()<=0)
{
System.out.println("Wrong Format");
return false;

}
else
{
return true;
}
}
}

 

第四题

这个Java程序包含了三个类,分别是Shape(抽象类)、Point和Rectangle(矩形)以及yuan(圆),并实现了Comparable接口。主要功能是对用户输入的形状信息进行排序输出面积。

首先来看Shape抽象类,它在程序中起着父类的作用,定义了一个getArea方法用于获取每个形状对象的面积,并实现了Comparable接口的compareTo方法,用于将形状对象按面积从小到大排序。该抽象类还有一个默认构造方法,以及一个chack方法,但是并没有被使用到。

其次是Point类,表示平面直角坐标系中的点(x,y),具有getX、getY、setX、setY等基本方法。该类在矩形类Rectangle中被用作左上角和右下角坐标的存储类型。

然后是Rectangle类,表示一个矩形形状,其中包含两个点(左上角和右下角),继承了Shape抽象类。它通过getLength和getHigh方法计算长和高的大小,并使用getArea方法计算矩形的面积。因此,Rectangle作为低耦合的分类,其内部一些参数可以全局访问,代码设计非常清晰。

最后是yuan类,表示一个圆型形状,继承了Shape抽象类。它通过getRadiums方法获取半径大小,并使用getArea方法计算圆的面积。此外,它还实现了chack方法,用于检查半径是否为正数,并且输出"Wrong Format"来提示用户输入信息有误。该类还包含两个构造方法一个默认构造方法和一个带有参数的构造方法。

总结来说,这个程序中所涉及到的类的耦合度相对较低,代码结构清晰且易读,完美地体现了面向对象编程的思想。同时也使用了Comparable接口和sort算法进行排序输出,通过ArrayList进行动态数组的存储和访问操作,有较高的可扩展性和灵活性,但是在具体运行过程中可能会出现其他未考虑到的错误(如用户输入非数字字符)。为了进一步提升程序的健壮性,可以加入异常处理机制来防止程序崩溃。

import java.util.*;

public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
ArrayList<Shape> list = new ArrayList<>();

int choice = input.nextInt();

while(choice != 0) {
switch(choice) {
case 1://Circle
double radiums = input.nextDouble();
Shape circle = new yuan(radiums);
list.add(circle);
break;
case 2://Rectangle
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
Point leftTopPoint = new Point(x1,y1);
Point lowerRightPoint = new Point(x2,y2);
Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);
list.add(rectangle);
break;
}
choice = input.nextInt();
}

list.sort(Comparator.naturalOrder());//正向排序

for(int i = 0; i < list.size(); i++) {
System.out.print(String.format("%.2f", list.get(i).getArea()) + " ");
}
}
}

abstract class Shape implements Comparable{

Shape()
{}
public double getArea(){
return 0;
}
boolean chack;
boolean chack() {
return true;
}
@Override
public int compareTo(Object o) {
Shape s = (Shape)o;
if(this.getArea()<s.getArea()){
return -1;
}else if(this.getArea()>s.getArea()){
return 1;
}
return 0;
}
}
class Point
{
double x=0;
double y=0;
Point()
{

}
Point(double x,double y)
{
this.x = x;
this.y = y;
}
double getX() {
return x;
}
void setX(double x) {
this.x = x;
}
double getY() {
return y;
}
void setY(double y) {
this.y = y;
}
}
class Rectangle extends Shape
{
Point topLeftPoint;
Point lowerRightPoint;
Rectangle()
{}
Rectangle(Point topLeftPoint,Point lowerRightPoint)
{
this.setTopLeftPoint(topLeftPoint);
this.setLowerRightPoint(lowerRightPoint);
}
public Point getTopLeftPoint() {
return topLeftPoint;
}
public void setTopLeftPoint(Point topLeftPoint) {
this.topLeftPoint = topLeftPoint;
}
public Point getLowerRightPoint() {
return lowerRightPoint;
}
public void setLowerRightPoint(Point lowerRightPoint) {
this.lowerRightPoint = lowerRightPoint;
}
public double getLength()
{
return Math.abs(topLeftPoint.getX()-lowerRightPoint.getX());
}
public double getHigh()
{
return Math.abs(topLeftPoint.getY()-lowerRightPoint.getY());
}
@Override
public double getArea()
{
return Math.abs(getLength()*getHigh());
}

}
class yuan extends Shape{
double radiums=0;
@Override
public double getArea()
{
return Math.pow(getRadiums(),2)*Math.PI;
}
public double getRadiums() {
return radiums;

}
public void setRadiums(double radiums) {
this.radiums = radiums;
}
yuan(){}
yuan(double radiums)
{
this.radiums = radiums;
}
@Override
public boolean chack()
{
if(getRadiums()<=0)
{
System.out.println("Wrong Format");
return false;

}

else
{
return true;
}

}
}哪些地方会有困难,有哪些优点,哪些缺点,可以如何更改。600字

 

 

 

 

 

 

public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

String a;

Menu menu=new Menu();

table[] t=new table[100];

Dish dish=new Dish();

int zuohaoshu=0;//桌号

int caishu=0;//菜数

int hangpanduan=0;

int dangqianzuohao=0;//当前处理桌号.

int dingdanshu=0;//前一桌的点菜序号

int xuhao=0,fen=0,fenshu=0,dxuhao=0,dzuohao=0,dfen=0,dfenshu=0;

//输入

boolean flagtable=false;

int putnum=0,h=0;

a=in.nextLine();

Worn worn=new Worn();

 

//开始执行

while(!a.equals("end"))

{

//flagtable=false;

hangpanduan=worn.worninput(a,putnum,hangpanduan);

boolean error=false;

if(a.matches(""))//其他错误

{

System.out.println("wrong format");

continue;

}

//分割字符

String[] xinxi = a.split(" | | | ");

 

 

 

if(xinxi.length==5&&a.charAt(0)!='t'&&xinxi[1].matches("^/d$"))//代点

{

// 1 4 麻婆豆腐 1 1

if(hangpanduan==1)

{

error=true;

if(zuohaoshu!=0&&t[dangqianzuohao].flag)

{

System.out.println("wrong format");

}

continue;

}

dxuhao=Integer.parseInt(xinxi[0]);

dzuohao=Integer.parseInt(xinxi[1]);

dfen=Integer.parseInt(xinxi[3]);

dfenshu=Integer.parseInt(xinxi[4]);

if(t[dangqianzuohao].searthtable(t,dxuhao,dangqianzuohao)==true)

{

t[dangqianzuohao].orderzi.addARecord(dzuohao, dxuhao, xinxi[2], dfen,dfenshu,true);

dish=menu.searthDish(xinxi[2]);

if(dish!=null)

{

t[dangqianzuohao].orderzi.records[t[dangqianzuohao].diancaicishu].d=dish;

int x=t[dangqianzuohao].orderzi.records[t[dangqianzuohao].diancaicishu].getPrice();

//4 table 2 pay for table 1

System.out.println(dzuohao+" table "+t[dangqianzuohao].zuoshu+" pay for table "+dxuhao+" "+x);

}

t[dangqianzuohao].diancaicishu++;

}

}

 

else if(xinxi.length==2)

{

if(xinxi[1].equals("delete"))//删菜

{

 

if(zuohaoshu!=0)

{

if(hangpanduan==1)

{

error=true;

if(zuohaoshu!=0)

{

System.out.println("wrong format");

}

continue;

}

if(t[dangqianzuohao].flag)

t[dangqianzuohao].orderzi.delARecordByOrderNum(Integer.parseInt(xinxi[0]),t[dangqianzuohao]);

}

}

else//加菜

{

if(hangpanduan==1)

{

error=true;

System.out.println("wrong format");

continue;

}

// 油淋生菜 9

if(zuohaoshu!=0&&t[dangqianzuohao].flag)//情况1

{

System.out.println("invalid dish");

}

else if(xinxi[1].matches("^[0-9]{0,}$"))

{

if(Integer.parseInt(xinxi[1])>0&&Integer.parseInt(xinxi[1])<300)

{

menu.addDish(xinxi[0], Integer.parseInt(xinxi[1]),false);

caishu++;

}

else

{

System.out.println(xinxi[0]+" price out of range "+xinxi[1]);

}

}

else

{

System.out.println("wrong format");

}

}

}

else if(xinxi.length==3)//特色菜

{

if(hangpanduan==1)

{

error=true;

System.out.println("wrong format");

continue;

}

if(zuohaoshu!=0&&t[dangqianzuohao].flag)

{

System.out.println("invalid dish");

}

else

{

if(xinxi[2].equals("T"))

{

if(Integer.parseInt(xinxi[1])>0&&Integer.parseInt(xinxi[1])<300)

{

menu.addDish(xinxi[0], Integer.parseInt(xinxi[1]),true);

caishu++;

}

else

{

System.out.println(xinxi[0]+" price out of range "+xinxi[1]);

}

}

}

}

// if(error==false&&hangpanduan[h]==1||a[h].matches("\n| | |null"))//其他错误

// {

// System.out.println("wrong format");

// }

else if(xinxi[0].charAt(4)=='e'){

if(xinxi.length==4||xinxi[0].charAt(0)=='t')

{

//System.out.println("wrong format");

if(xinxi[0].equals("table"))//加桌

{

error=true;

dingdanshu=0;

table x=new table();

x.input(a,hangpanduan);

if(zuohaoshu!=0)

dangqianzuohao=t[zuohaoshu-1].searchtime(t,x,zuohaoshu);//判断时间,若找到返回对应桌,找不到返回下一个空桌

if(dangqianzuohao==zuohaoshu)//加新桌

{

t[zuohaoshu]=new table();

t[dangqianzuohao]=x;

zuohaoshu++;

dingdanshu=0;//初始化

if(t[dangqianzuohao].flag)

System.out.println("table "+t[dangqianzuohao].zuoshu+": ");

}

else

{

if(t[dangqianzuohao].flag)

System.out.println("table "+t[dangqianzuohao].zuoshu+": ");

dingdanshu=0;

}

}

else if(xinxi[0].matches("^[1-9][0-9]{0,1}$"))//自己点菜

{

// 1 麻婆豆腐 2 2

if(hangpanduan==1)

{

error=true;

if(zuohaoshu!=0&&t[dangqianzuohao].flag)

{

System.out.println("wrong format");

}

continue;

}

xuhao=Integer.parseInt(xinxi[0]);

fen=Integer.parseInt(xinxi[2]);

fenshu=Integer.parseInt(xinxi[3]);

if(zuohaoshu!=0&&t[dangqianzuohao].flag)

{

if(xuhao>dingdanshu)

{

 

t[dangqianzuohao].orderzi.addARecord(0,xuhao,xinxi[1],fen,fenshu,true);

//System.out.println("wrong format");

dish=menu.searthDish(xinxi[1]);

 

if(dish!=null&&t[dangqianzuohao].flag)

{

if(fenshu>15)

{

System.out.println(xuhao+" num out of range "+fenshu);

}

else if(fen>3&&fen<10||fen<=0&&fen>-10)//情况9

{

//序号+" portion out of range "+份额

System.out.println(xuhao+" portion out of range "+fen);

}

 

else

{

t[dangqianzuohao].orderzi.records[t[dangqianzuohao].diancaicishu].d=dish;

int x=t[dangqianzuohao].orderzi.records[t[dangqianzuohao].diancaicishu].getPrice();

//2 油淋生菜 27

 

dingdanshu=xuhao;

System.out.println(xuhao+" "+xinxi[1]+" "+x);

}

 

}

t[dangqianzuohao].diancaicishu++;

}

else if(dangqianzuohao!=0&&xuhao>dingdanshu)

{

System.out.println("record serial number sequence error");

}

 

}

 

}

}

else

{

System.out.println("wrong format");

error=true;

}

 

}

a=in.nextLine();

 

}

for(int l=0;l<zuohaoshu;l++)

{

t[l].getTotalPrice();

}

}

 

}

 

 

                这是主类,主要是对于键盘的输入进行存储进order的records点菜记录当中,然后对总价进行计算。需要一些更改,在这里我们需要注意一下。这个题目它的输入分为三种情况,一种是删菜,一种是录入订单,还有一种是录入菜品的价格信息,那么在这里我们就可以用split函数将它分为数组,这里有一个小技巧,可以根据数组的长度判断它是录入菜品信息还是点菜或者是删菜。  这次作业相对于菜单三来说,新增加了一个特色菜的功能,特色菜的菜价的计算方法也和普通菜有些许区别,周一到周五七折,周末全价,并且在菜单四当中有很多增加的,对于一些输入的数据,如果超出范围或者是输入数据格式不对的话,都需要输出对应的格式错误的语句,就比如如果有重复的租号信息,两条信息时间不在同一时间段输出的结果,就不按记录的分别计价,或者是证书超出范围没周不能超过15份,超出的话就输出序号,加上num out of range加份数等等,所以我们可以先在这里新添加一个类名字叫worn这个累,主要就是对于每一个语句的判断,他是否是按题目要求来的?后面会详细介绍这个类,现在先在主函数当中嗯,实例化一个这个类,然后每输入一行就用这个类来判断一次,如果他输入的信息为错误的,那么此行就创立一个数组,令他数组为一,如果没有错误是正确的,那么他的初始就为零,这样就可以判断每条语句到底是格式正确还是错误,会很大的方便后面的运行。 

 

class Worn//错误判断,错误b[i]==1

{

int worninput(String a,int num,int b)

{

for(int i=0;i<num;i++)

{

String[] arr=a.split(" ");

boolean flag=false;

//麻婆豆腐 12

if(arr.length>5)

{

b=1;

}

if(a.matches("^[\u4e00-\u9fa5]{1,} ([1-9][0-9]{0,2})$")==true)//普通菜存入

{

flag=true;

}

//油淋生菜 9 T

else if(a.matches("^[\u4e00-\u9fa5]{1,4} ([1-9][0-9]{0,2}) [T]$")==true)//特色菜存入

{

flag=true;

}

//table 31 2023/2/1 14/20/00

else if(arr[0].equals("table")==true)//桌

{

int blank=0;

for(int bk=0;bk<a.length();bk++)

{

if(a.charAt(bk)==' ')

{

blank++;

}

}

if(arr[1].matches("^([1-9][0-9]{0,})$")&&blank==3)

flag=true;

}

//1 麻婆豆腐 1 16

else if(a.matches("^[1-9][0-9]{0,2} [\u4e00-\u9fa5]{1,} [1-9] ([1-9][0-9]{0,1})$")==true)//自己点菜

{

flag=true;

}

//1 1 麻婆豆腐 1 16

else if(a.matches("^([1-9][0-9]{0,1}) [1-9][0-9]{0,2} [\u4e00-\u9fa5]{1,} [1-9] ([1-9][0-9]{0,1})$")==true)//待点菜

{

flag=true;

}

//2 delete

else if(a.matches("^([1-9][0-9]{0,1}) delete$")==true)//删菜

{

flag=true;

}

else if(a.matches("^end$")==true)//结尾

{

flag=true;

}

if(flag==false)

{

b=1;

}

}

return b;

}

}

这个worn是一个检测输入的方法,该方法主要采用正则表达式来匹配输入,以确定输入的格式是否正确。该方法的优点是使用了正则表达式匹配,简单高效,方便快捷。该方法的缺点是代码过于冗长,不够简洁明了,可读性较差;同时,该方法只能判断输入是否符合格式,却没有对输入的内容进行有效性验证,存在风险。如果要改进该方法,可以结合异常处理机制,在检测输入格式的基础上,进一步完善校验规则,提高代码可读性和健壮性。

class Menu

{

Dish[] dishs =new Dish[20];

int i=0;//用于记录菜品总数

void addDish(String dishName,int unit_price,Boolean ts)

{

 

// Dish dish1=new Dish();

// System.out.println(i);

dishs[i]=new Dish();

dishs[i].name=dishName;

dishs[i].unit_price=unit_price;

dishs[i].special=ts;

i++;

//return dish1;

}

//菜品数组,保存所有菜品信息

Dish searthDish(String dishName) //根据菜名在菜谱中查找菜品信息,返回Dish对象。

{

int j=0;

int flag=0;

for(j=i-1;j>=0;j--)//从未末端开始查找比对

{

if(dishName.compareTo(dishs[j].name)==0)

{

flag=1;

break;

}

}

if(flag==1)

return dishs[j];

else

{

//麻辣鸡丝 does not exist

System.out.println(dishName+" does not exist");

return null;

}

}

}

第五次作业

 

第四次作业踩坑心得:说一下这次题的踩坑心得吧,当时做这个题算价格一直是为0,然后找了很久都没有找到原因,后来调试发现那个Gettotalprice这个方法返回的值永远是零,然后在这个方法当中添加传了实例化的order类,便能返回菜品的价格了,还有一个地方是dish类当中的计算价格,当时也是,每次这个东西返回的值都有问题,然后去查了一下,发现是math.round的这个函数,返回值不是一个整形,需要在前面强制转换为整形第四题呢当时一直不知道这个算法,想了很久都没有想出来。后来上网查资料才知道有这个算法。按照网上说的这个算法,自己打了一个,但是一直是运行超时过不了,然后去搜了一下,前面就必须要加一行用来提高运行速度的。加了这一行之后就没有运行超时了。

 

第五次作业:这个第三次作业的第一题主要还是用以前的菜单模板,但是新增加了一个ren类,第五次作业也是在第三次作业上面迭代的,他和第四次作业是两个不同的方向,第五次作业主要是需要,也是要加一个特色菜,但是特色菜里面还需要辣度值和酸度值,还有甜度值,特色菜的菜系也有川菜,晋菜和浙菜,这个酸度值和那个平均口味度就比较难,算了,我一开始是放在弟媳里面,然后打了很久,老是出现那种,如果一个人点一个菜系就不会错,但是如果点多个菜系,它的计算口味度判断它的辣度,酸度和甜度的水平就会出错,一开始是放在地形类当中来打的,后来我尝试把它放在了record当中,他的储存的维度就发生了很大的变化,相对来说就会更加简单一点,也没有出现之前的错误了,然后还增加了一个是客户的姓名,还有他的电话号码,我一开始打算是在每一桌里面去定义一个人,这个属性,但是后来想一想,无论是从程序上面去想,还是从JAVA本身是一个面向对象,从逻辑上面讲,人都不应该作为一个桌的属性,然后我就把人单独出来放在了一个类里面,然后一个人有自己的名字,也有自己的姓,他可以开很多桌,当然开着的时候也要进行比对,如果做时间之类的都是对的,他的那个输入格式是对的,那么就给开一个错,如果这个组合在之前存在过,那么价格也可以直接加到之前那一组这道题其实大体上来说还是和之前的菜单差别不大

class ren{
String renname;
String phone="";
int zuoshu=0;
static int renshu=0;
table[] t=new table[10];
int searchman(ren[] ren,String a)
{
//System.out.println(a);
for(int k=0;k<renshu;k++)
{
if(ren[k].renname.equals(a))
{
return k;
}
}
return renshu;
}
public void newren(String xinxi) {
String[]arr=xinxi.split(" ");

if(arr[3].matches("^.{1,10}$")&&arr[4].matches("^(180|181|189|133|135|136)[0-9]{8}$"))
{
this.renname=arr[3];
this.phone=arr[4];
renshu++;
}
else
{
System.out.println("wrong format");
}

}
boolean newtable(String time)
{
String[] arr=time.split(" ");
t[zuoshu]=new table();
t[zuoshu].input(time);

boolean flag=t[zuoshu].flag;
if(flag)
this.zuoshu++;
return flag;
}
void rensum() {
int sum=0;
for(int y=0;y<zuoshu;y++) {
sum=sum+t[y].sum+t[y].tesesum;
}
System.out.println(renname+" "+phone+" "+sum);
}
}
class shuru//判断输入内容
{
int shuru(String a)
{
int j=0;//返回状态1,2,3,4,5
int bk=0;
String[]arr=a.split(" | ");
//分割
int len = arr.length;
if(arr.length==7&&arr[0].equals("table")&&arr[1].matches("^[1-9][0-9]{0,}$")&&arr[2].equals(":")&&arr[3].matches("^.{1,10}$")&&arr[4].matches("^[0-9]{11}$")&&arr[5].matches("^[0-9]{4}/[1-9][0-9]{0,1}/[1-9][0-9]{0,1}$"))
{
return 7;
}
else if(len==2)
{
if(arr[1].equals("delete")&&arr[0].matches("^[0-9]{1,}$"))
return 2;
else if(arr[1].matches("^[0-9]{1,}$"))
return 2;
}
else if(len==4)
{
if(arr[3].equals("T")&&arr[1].matches("^(川菜)|(晋菜)|(浙菜)$")&&arr[2].matches("^[0-9]{1,}$"))
return 4;
else if(arr[0].matches("^[0-9]{1,}$")&&arr[2].matches("^[0-9]{1,}$")&&arr[3].matches("^[0-9]{1,}$"))
return 4;
}
else if(len==5&&arr[0].matches("^[1-9][0-9]{0,}$"))
{
if(arr[1].matches("^[0-9]{1,}$"))
return 5;
else
return 5;
}
else if(len==6)
{
return 6;
}
System.out.println("wrong format");
return 0;
}
}

这个程序包含两个类ren和shuru。ren类中包含人员信息和预定餐桌信息。其中searchman、newren和rensum实现了对人员信息的查找、添加和统计预定餐桌消费金额等功能,newtable则用于添加新的餐桌预定。shuru类则用来判断输入内容是否符合格式要求。

该程序的优点在于每个方法分工明确,并且操作简单易懂。但其缺点也显而易见,代码结构混乱、缩进不规范,命名不规范等问题都会降低代码的可读性和可维护性。而且输入格式判断的实现方法不够健壮,只考虑了少数情况,并不能涵盖所有可能存在的输入错误。

针对以上缺点,可以尝试重构代码结构,采用面向对象编程的思想进行编写。同时可以完善输入格式判断,考虑更多可能出现的异常情况,从而提高程序的稳定性和健壮性。此外,可以加入更多的注释以提高代码的可读性。

 

第五次作业踩坑心得:总结一下第五次作业所踩的坑吧,第一个就是第一个菜单类的题开了两个order,一个是table里面有个order,还有一个是主类当中有一个order,就导致后面两个东西写的分开了,他计算价格的时候就全部为空,因为定义了两次,然后还有一个问题就是最后他一直是运行超时,运行超时这个地方呢有一个地方是get price方法,在一个if里面用了三次,那么就可以使用一个变量接收它一次,然后每一个后面的都用这个变量就可以了,就不用反复的调用方法,减少运行时间。还有一个就是它的格式问题,格式问题当时空格那些都是对准了的,但是他还是格式错误,然后就是去测试用力那边直接复制过来,然后再进行更改,它格式就没问题了。还有最后里面那个判断时间一开始用的是lolcaldate,但是不知道为什么算出来就是有问题,后来就改用成了calendar,现在也还没搞出为什么会有这个问题。还有一个就是那个字符串字母的排序的那个题,那个题必须是先进行字母的排序在进行字符长短的排序,如果反过来的话,它排出来就是有问题

这第五次作业做完了,现在总结一下吧,对于这三次题目,其实最主要的还是对于一些算法的应用,还有对于一些类的应用,在面向对象这一方面尤为出众,和上学期的面向过程区别还是有一些的,一开始写着挺不适应的主要是需要把一个东西分为好几个单个的部分来写。后来写着写着感觉其实这种面相推向好处还是挺多的。感触最深的就是这种写法能够减少很多代码量,以后还要多多深入学习,对于类的用法,尽量的用少一些的代码打出程序,还要学一些算法,一些好的算法往往能够减少很多代码量,实验方面吧感觉难度还行,就是可能有些时候实验加pta时间上可能有一点紧张,有两次交作业,刚好超过了一分钟。。。。。。

posted @ 2023-05-16 21:42  风-笛  阅读(37)  评论(0)    收藏  举报