第二次博客作业

  经过数周的学习,我终于迎来了第二次博客作业。

  这次是关于第四次作业,第五次作业和期中考试的,相比于上次作业的题目,这次的难度有了比较明显提升,我的成绩有了明显的下滑。

 个人心得:java是需要我们去全面了解的一门科目,我们也需要有一个良好的大局观来帮助我们去写代码,这是不可或缺的。学习不局限于一时,但一定需要我们长久努力。

第四次作业:菜单计价程序-4。

第五次作业: 菜单计价程序-5。

期中考试:

1,测验1-圆类设计

2,测验2-类结构设计

3,测验3-继承与多态

 4,测验4-抽象类与接口
以下为代码分析:
package 菜单计价程序;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
//import java.time.LocalDate;
import java.util.Date;
//import java.time.LocalTime;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Main {
public static void main(String[] args) {
    Scanner in=new Scanner(System.in);
    String a;
    Menu p=new Menu();
    Order o=new Order();
    Record r=new Record();
    Table m=new Table();
    int flag=0;
    int count=0;
    int[] table=new int[55];
    while(true) {
        a=in.nextLine();
        if(a.equals("end")) break;
        String[] b=a.split(" ");
        if(b[0].equals("table")&&count==0) {
         m.addTable(Integer.parseInt(b[1]), b[2], b[3]);
         table[count]=Integer.parseInt(b[1]);
         count++;
        }
        if(!b[0].equals("table")&&count==0)
        {
             p.addDish(b[0],Integer.parseInt(b[1]));
        
        }
        if(!b[0].equals("table")&&count!=0) {        
          r.addrecord(b[1],Integer.parseInt(b[2]),Integer.parseInt(b[3]));
          flag++;
        }
    }//数据录入完成,开始输出
    //System.out.println(table);
    for(int i=0;i<=count;i++) {
        int tablename=table[count];
        int discount=(int)m.getdiscount(tablename);
        if(discount>1) System.out.println("not a valid time period");
        else {
            System.out.println("table"+" "+tablename);
            for(int j=0;;j++) {   
            int x=(int)p.searchDish(r.a[j]);
            if(x==0) System.out.println(r.a[j]+"does not exist");
            else System.out.println(j+" tablename "+o.getprice(j));
            }
        }
}
}
}
class Dish {
    String name;
    int unit_price;
    int getPrice(int portion) {
        if (portion == 1) {
            return ((int) (Math.round(unit_price * 1.0)));
        }
        if (portion == 2) {
            return ((int) (Math.round(unit_price * 1.5)));
        }
        if (portion == 3) {
            return ((int) (Math.round(unit_price * 2.0)));
        }
        return 0;
    }
}
class Record {//////////////////////////
    String[] a=new String[1000];//菜品名
    float[] b=new float[1000];//份额
    float[] c=new float[1000];//数量
    int i3=0;
    public void addrecord(String cai,int fen,int shu) {
        a[i3]=cai;
        b[i3]=fen;
        c[i3]=shu;
        i3++;
    }
}
class Order {//////////////////////////////////
    Record r=new Record();
    Dish d=new Dish();
    Menu p=new Menu();
    int i1=0;
    public void addorder() {
       
    }
    public int getprice(int a) {
        int jg=p.searchDish(r.a[a]);
        int price=(int)(Math.round(jg*r.c[a]*(0.5+r.b[a]*0.5)));
        
        return price;
    }
    
}
class Table{
  float[] table=new float[55];
  String Date1="2022/1/1";
  String Date2= "2023/12/31";
  String Time1="10/30/00";
  String Time2="14/30/00";
  String Time3="17/00/00";
  String Time4="20/30/00";
  String Time5="9/30/00";
  String Time6="21/30/00";
  //Integer i=beginTime.compareTo(endTime);
  public void addTable(int a,String b,String c) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd");
        Date d=null;
        try {
            d=f.parse(b);
        } catch (ParseException e) {

            e.printStackTrace();
        }
        cal.setTime(d);
        int w=cal.get(Calendar.DAY_OF_WEEK)-1;
        if(w==0) w=7;
        if(w!=6&&w!=7&&Date1.compareTo(b)<=0&&Date2.compareTo(b)>=0&&Time2.compareTo(c)>=0&&Time1.compareTo(c)<=0&&Time4.compareTo(c)>=0&&Time3.compareTo(c)<=0) table[a]=(float)0.7; 
        else if(w!=4&&w!=5&&w!=3&&w!=2&&w!=1&&Date1.compareTo(b)<=0&&Date2.compareTo(b)>=0&&Time6.compareTo(c)>=0&&Time5.compareTo(c)<=0) table[a]=(float)1; 
        else table[a]=(float)1.2;
  }
public float getdiscount(int a) {
       return table[a];
  }
  
    
}
class Menu {
    String[] a=new String[55];
    float[] b=new float[55];
    String[] c=new String[55];
    int i2=0;
    public void addDish(String dishName,int price) {
        a[i2]=dishName;
        b[i2]=price;
        //c[i2]=t;
        i2++;
    }
    public int searchDish(String dishname) {
        int i=0;
        int flag=0;
        for( i=0;i<=55;i++) {
            if(a[i].equals(dishname)) {
                flag=(int)b[i2];
                break;
            }
        }
        if(flag!=0) return flag;
        else return 0;
    }
}

第四次和第五次是同一题的不同类型,对此,我只对其中一道题作出分析。第四次作业是要我在原来的作业上拓展,增加了许多信息,比如这道题要我为“table","dish","time"."order"等类设计,需要我全面分析,再去书写代码。如此才能写出正确答案,但是

由于自身的原因,没有写出来。

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
          Scanner in = new Scanner(System.in);
          float r;
        r=in.nextFloat();
        Cricle ma=new Cricle();
        ma.Cricle(r);
        ma.print();
    }
}
class Cricle{
private float r;

    public void Cricle(float r1){
     this.r=r1;
    }
    public float getr(){
        
        return r;
  }
    public void print(){
        if(r<=0) System.out.print("Wrong Format");
        else System.out.print(String.format("%.2f",(r*r*Math.PI)));
    }
}

这道题比较简单,作为期中考试的第一道题,它的难度还算可以。这道题考查我们能否熟练运用类。

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
  
            double x1 = input.nextDouble();
               double y1 = input.nextDouble();
               double x2 = input.nextDouble();
               double y2 = input.nextDouble();
               
               Point a1 = new Point(x1,y1);
               Point a2= new Point(x2,y2);
               
               Rectangle rectangle = new Rectangle(a1,a2);
               rectangle.getarea();
              
        }
    }
class Point{
        double x;
         double y;
         public Point( double sid2,double name2){
             this.x=sid2;
             this.y=name2;
            
         }
         Point(){
             
         }
    }
class Rectangle{
        Point a1;
        Point a2;
        double l;
        double h;
        public Rectangle( Point sid2,Point name2){
            this.a1=sid2;
            this.a2=name2;  
        }
        Rectangle(){
            
        }
           public void getarea(){
                if(a1.x>a2.x) l=a1.x-a2.x;
                else l=a2.x-a1.x;
                if(a1.y>a2.y) h=a1.y-a2.y;
                else h=a2.y-a1.y;
           System.out.println(String.format("%.2f",(l*h)));
         }
    }

这道题是第二道题,在第一题的基础上增加了类与类联系,要求我们必须为这俩个类设立一种联系,使之能够相互传递参数,便于计算。

 
import java.util.Scanner;
public class Main{
    abstract class Shape {
    abstract double printarea();
}
class Cricle extends Shape{
    private float r;
        public void Cricle(float r1){
         this.r=r1;
        }
        public float getr(){     
            return r;
      }
      @Override
        double printArea() {
            return Pi * r * r;
        }
    }
    class Point{
        double x;
         double y;
         public Point( double sid2,double name2){
             this.x=sid2;
             this.y=name2;
            
         }
         Point(){
             
         }
    }
    class Rectangle extends Shape{
            Point a1;
            Point a2;
            double l;
            double h;
            public Rectangle( Point sid2,Point name2){
                this.a1=sid2;
                this.a2=name2;  
            }
           @Override
               double printtarea(){
              
               return h*l;
             }
    }
        public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        
        int choice = input.nextInt();
        
        switch(choice) {
        case 1://Circle
            double radiums = input.nextDouble();
            Shape circle = new Circle(radiums);
            printArea(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);
            
            printArea(rectangle);
            break;
        }
        
    }
       

这是第三题,这道题需要我去使用继承与多态,这是java中的一个知识点,而且这也是java重要的一部分,需要我去努力学习。这道题没什么好说的,单纯考验我能否熟练运用继承与多态,而这也是考试中的拉分点,需要我仔细去写代码。

 

 期中考试剩下的一道题,我不会做,这道题是在第1题和第2题的基础上直接考察我们抽象类与接口,而这恰好是我的薄弱点,我并没有跟上老师的进度,没有对抽象类和接口进行仔细的研究,导致了我在面对这一题时茫然无措,根本不知道如何去做,只能够放弃。
总结:
1,首先整个代码结构要清楚,不然东写一次西行一次,必须联系全局,类与类之间的联系不能够太分散
2,其次是如果我们要参考别人的源代码,浏览他人的说明,可以自己先参考这个代码并写出一份自己的代码来试验一下,确保没有问题之后再去找别的和自己的比对,看看有哪些地方不合适
3,然后明确主流程,就是总共有哪几步来完成整个任务,在主函数中把它写出来,后面再根据每个流程来建具体的实现方法,写具体的代码。子流程的代码的流程同理.
4,代码注释一定要明确,报错的信息也要写出来,这样才好定位错误.
 
posted @ 2023-05-17 21:42  科z  阅读(48)  评论(0)    收藏  举报