(1)前言

这三次大作业都是电信计费系列,分别是讨论了座机,手机,手机短信之间的创建用户,打电话,接电话,收发短信。运动到了容器,Date类,SimpleDateFormat类,Collections运用类。相较于之间的多边形系列来说,减少了对算法的要求,但增加了对类的设计的要求。

(2)设计与分析

第六次PTA作业:

第二题是简单的题目,是对这作业需要用到的知识的最基础的运用。
以下是源码:
import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        DecimalFormat df = new DecimalFormat("0.##");
        Scanner in = new Scanner (System.in);
        int n = in.nextInt();
        Container[] c = new Container[n];
        for(int i=0;i<n;i++) {
            String s = in.next();
            if(s.equals("cube")) {
                c[i] = new Cube(in.nextDouble());
            }
            if(s.equals("cylinder")) {
                c[i] = new Cylinder(in.nextDouble(),in.nextDouble());
            }
        }
        System.out.println(df.format(Container.sumofArea(c)));
        System.out.println(df.format(Container.sumofVolume(c)));
    }

}
interface Container{
    
    public static final double pi=3.1415926;
    public abstract double area();
    public abstract double volume();
    static double sumofArea(Container c[]) {
        double s = 0;
        for(int i = 0;i < c.length ;i++)
        {
            s += c[i].area();
        }
        return s;
    }
    
    static double sumofVolume(Container c[]){
        double s = 0;
        for(int i = 0;i < c.length ;i++)
        {
            s += c[i].volume();
        }
        return s;
    }
    
}
 class Cube implements Container{
     double a;
    public Cube(double a) {
        super();
        this.a = a;
    }

    @Override
    public double area() {
        // TODO Auto-generated method stub
        return 6*a*a;
    }

    @Override
    public double volume() {
        // TODO Auto-generated method stub
        return a*a*a;
    }
    
 }
 class Cylinder implements Container{
     double a,b;
    public Cylinder(double a, double b) {
        super();
        this.a = a;
        this.b = b;
    }

    @Override
    public double area() {
        // TODO Auto-generated method stub
        return (2*pi*a*a+2*pi*a*b);
    }

    @Override
    public double volume() {
        // TODO Auto-generated method stub
        return pi*a*a*b;
    }
    
 }

 

第一题则就是电信计费系列的第一题:座机之间的拨打电话。

 

 

这次作业老师给出了基础的类的参考,主要是我们要怎么去这些类关联起来,计算出正确的答案。

第五次PTA作业:

第二题的源码:


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int n;
        
        HashMap<String,Student> students = new HashMap<>();
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        for(int i=1; i<=n ; i++) {
            Student s = new Student(in.next(),in.next(),in.nextInt(),in.next());
            students.put(s.num, s);
            
        }
        Set<String> students_num = students.keySet();
        ArrayList<String> stu_num = new ArrayList<String>(students_num);
        
        Collections.sort(stu_num);
        System.out.println(students.size());
        for(String i:stu_num)
            System.out.println(students.get(i));
    }
    
}
class Student{
    String num;
    String name;
    int age;
    String sex;
    public Student(String num, String name, int age, String sex) {
        super();
        this.num = num;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    @Override
    public String toString() {
        return num + " " + name + " " + age + " " + sex;
    }
}
第三题的源码:


import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.Set;



//1、导入相关包

//定义员工类
class Employee {

    private String name;
    private int age;

    public Employee() {
        super();
    }

    public Employee(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return  name + "---" + age;
    }
}

//主函数
public class Main {
    static Scanner in = new Scanner(System.in);
    public static void main(String[] args) {
                // 1、创建有序集合对象
                Collection<Employee> c = new ArrayList<Employee>() ;

   // 创建3个员工元素对象
        for (int i = 0; i < 3; i++) {
            
            String employeeName = in.next();
            int employeeAge = in.nextInt();
            
            Employee employee = new Employee(employeeName, employeeAge);
            c.add(employee);
        }            
                
                
                
                // 2、创建迭代器遍历集合
                Iterator<Employee> it = c.iterator();
                
                //3、遍历
                while (it.hasNext()) {
                    
                    //4、集合中对象未知,向下转型
                    System.out.println(it.next());
                }
    }

}


第一题也就是电信系列的衍生。

 

 

需要考虑的情况也就更加的多样。多了手机实时计费的情况

第八次PTA作业:

第二题:


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        Shop myshop = new Shop();
        myshop.setMilkCount(in.nextInt());
        myshop.coupons50.buy();
        myshop.coupons100.buy();
    }

}
class Shop{
    int milkCount;
    InnerCoupons coupons50 = new InnerCoupons(50);
    InnerCoupons coupons100 = new InnerCoupons(100);
    public int getMilkCount() {
        return milkCount;
    }

    public void setMilkCount(int milkCount) {
        this.milkCount = milkCount;
    }
    class InnerCoupons{
        int value;

        public InnerCoupons(int value) {
            super();
            this.value = value;
        }
        void buy() {
            milkCount = milkCount-(value/50);
            System.out.println("使用了面值为" + value +"的购物券进行支付");
            System.out.println("牛奶还剩" + milkCount + "箱");
        }
    }
}
第三题:


import java.util.Scanner;

//动物发生模拟器.  请在下面的【】处添加代码。
public class Main {
  public static void main(String[] args) {        
       Cat cat = new Cat();
       Dog dog = new Dog();        
      Goat goat = new Goat();
       speak(cat);
       speak(dog);
       speak(goat);
  }
  //定义静态方法speak()
  static void speak(Animal animal) {
      animal.getAnimal();
      animal.shout();
  }

}

//定义抽象类Animal
abstract class Animal{
  void getAnimal() {
      
  }
  void shout() {
      
  }
}
//基于Animal类,定义猫类Cat,并重写两个抽象方法
class Cat extends Animal{
    void getAnimal() {
          System.out.print("猫的叫声:");
      }
      void shout() {
          System.out.println("喵喵");
      }
}
//基于Animal类,定义狗类Dog,并重写两个抽象方法
class Dog extends Animal{
    void getAnimal() {
          System.out.print("狗的叫声:");
      }
      void shout() {
          System.out.println("汪汪");
      }
}
//基于Animal类,定义山羊类Goat,并重写两个抽象方法
class Goat extends Animal{
    void getAnimal() {
          System.out.print("山羊的叫声:");
      }
      void shout() {
          System.out.println("咩咩");
      }
}
第一题也就是电信系列的第三题:

 

这次作业对于上两次作业之间关联性非常小,所以我没有使用上两次的代码,而是自己重写了一遍。

(3)踩坑心得

在Main里面写方法要加上static,因为Main类无法实例化。注意next()和nextLine()的区别。把职责细化,堆在一起写改起来很复杂。

(4)改进建议

类的设计要更加的完善,先画好类图,每个部分要实现的功能,再去写,而不是想道哪就写到那,这样虽然可能会很快解决一些基础的问题,但是当这些问题稍微深入一点,解决起来就会非常的困难。
基础知识要稳固,写题要细心,做事不要急躁。好好学习,天天向上。

(5)总结

对类的设计还是不够到位,像现在这样的连续性的作业到后面全是推翻重来,折磨自己,而且最重要的时完全没有办法解决问题。
写代码时一定要细心,不要急躁。
简单的题目也要去做到面向对象程序设计,而不是面向过程程序设计。
posted on 2022-06-10 14:34  青我刚门会上影  阅读(29)  评论(0)    收藏  举报