前三次作业总结blog
前言:
前三次作业主要涉及到了如何创建类,运用类以及如何缩减时间复杂度空间复杂度等一些算法问题。其中第一次作业比较简单,刚接触java对于什么是类,怎么运用类还不是很清楚,所以难度不是很大,题量较多。第二次作业主要是类、数组的运用,其中涉及到了如何减少时间复杂度和空间复杂度,题目难度明显比第一次更难,题量基本不变。第三次作业和第二次作业相比有重复的题目,但课程成绩统计程序-1编程题难度陡然上涨。
设计与分析:
下面对个别具有代表性题目进行分析:
1-7-2长度质量计量单位换算
输入格式:
两个浮点数,以空格分隔,第一个是质量(以千克为单位)、第二个是长度(以米为单位)。例如:0.45359237 0.0254。
输出格式:
两个浮点数,以空格分隔,第一个是质量(以磅为单位)、第二个是长度(以英寸为单位)。例如:1.0 1.0。
实现代码:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double kilogram_kg=input.nextDouble();
double length_m=input.nextDouble();
double kilogram_pound=0,length_inch=0;
kilogram_pound=kilogram_kg/0.45359237;
length_inch=length_m/0.0254;
System.out.print((float) kilogram_pound+" "+(float) length_inch);
}
}
分析:此题比较简单,唯一要注意的就是输入和输出格式是浮点数,这个点是最常见也是最基础的。
2-7-1成绩计算-1-类、数组的基本运用
相关类图:

相关类代码:
public static class Student {
String number;
String name;
int chinese;
int math;
int physics;
int sum;
double average;
public Student()//空参构造
{
super();
}
public Student(String number, String name, int chinese, int math, int physics, int sum, double average) {
this.number = number;
this.name = name;
this.chinese = chinese;
this.math = math;
this.physics = physics;
this.sum = sum;
this.average = average;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getPhysics() {
return physics;
}
public void setPhysics(int physics) {
this.physics = physics;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
public double getAverage() {
return average;
}
public void setAverage(double average) {
this.average = average;
}
void Print(Student[] array) {
for (int i = 0; i < array.length; i++) {
//BigDecimal bg = new BigDecimal(array[i].average);
//double f = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.print(array[i].number + " " + array[i].name + " " + array[i].sum+" ");
System.out.printf("%.2f",array[i].average);
System.out.println();
}
}
分析:
通过代码和类图可以看出此题定义了一个Student类其中成员包括:
number: 学号,类型为String
name: 姓名,类型为String
chinese: 语文成绩,类型为int
math: 数学成绩,类型为int
physics: 物理成绩,类型为int
sum: 总分,类型为int
average: 平均分,类型为double
定义了空参构造方法和带参构造方法并定义了每个成员的get方法和set方法。最后定义了Print方法:接受一个Student类型的数组作为参数,遍历数组并输出每个学生的学号、姓名、总分和平均分。
在main函数中,首先使用for循环创建了一个长度为5的Student类型的数组,然后利用Scanner类从控制台输入每个学生的信息,并将这些信息存储在数组中。最后,调用Print函数可以输出所有学生的信息,其中,Print函数使用for循环遍历数组中的所有学生对象,并使用System.out.print、System.out.printf等方法按一定格式输出学生的信息。
2-7-2成绩计算-2-关联类
相关类图:

相关类代码:
public static class Student {
String number;
String name;
Score chinese;
Score math;
Score physics;
int sum;
double average;
double averageuss;
double averageeve;
public Student()//空参构造
{
super();
}
public Student(String number, String name, int chinese, int math, int physics, int sum, double average) {
this.number = number;
this.name = name;
this.sum = sum;
this.average = average;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Score getChinese() {
return chinese;
}
public void setChinese(Score chinese) {
this.chinese = chinese;
}
public Score getMath() {
return math;
}
public void setMath(Score math) {
this.math = math;
}
public Score getPhysics() {
return physics;
}
public void setPhysics(Score physics) {
this.physics = physics;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
public double getAverage() {
return average;
}
public void setAverage(double average) {
this.average =average;
}
int addsum(Score chinese, Score math, Score physics) {
sum = chinese.com + math.com + physics.com;
return sum;
}
double workaverage(int sum)
{
average=sum/3.0;
return average;
}
//成绩类
public static class Score{
String course;
int uss;
int eve;
int com;
public Score()//空参构造
{
super();
}
public Score(String course, int uss, int eve, int com) {
this.course = course;
this.uss = uss;
this.eve = eve;
this.com = com;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public int getUss() {
return uss;
}
public void setUss(int uss) {
this.uss = uss;
}
public int getEve() {
return eve;
}
public void setEve(int eve) {
this.eve = eve;
}
public int getCom() {
return (int) (uss*0.4+eve*0.6);
}
public void setCom(int com) {
this.com = com;
}
int calculate(int uss, int eve)
{
com= (int) (uss*0.4+eve*0.6);
return com;
}
}
分析:
为了解决此题目我创建了两个类,student类和score类并将student类和score类进行了关联。
学生类中(Student):
属性包括:学号(number)、姓名(name)、语文成绩(chinese)、数学成绩(math)、物理成绩(physics)、总分(sum)、平均分(average)、平均语文成绩(averageuss)、平均数学成绩(averageeve)。
构造方法:有一个空参构造方法和一个带参数的构造方法,用于初始化学生对象的属性。
方法包括:获取和设置各个属性的方法,计算总分的方法(addsum),计算平均分的方法(workaverage)。
成绩类(Score)中:
属性包括:科目名称(course)、平时考试成绩(uss)、期末考试成绩(eve)、加权综合成绩(com)。
构造方法:有一个空参构造方法和一个带参数的构造方法,用于初始化成绩对象的属性。
方法包括:获取和设置各个属性的方法,计算加权综合成绩的方法(calculate)。
通过学生类中的语文、数学、物理成绩和成绩类关联。,以此实现题目要求的功能:通过输入各科成绩计算出总分和平均分。
2-7-7 菜单计价程序-1
相关类图


相关类代码
class Dish {
private String[] names = {"西红柿炒蛋", "清炒土豆丝", "麻婆豆腐", "油淋生菜"};
private int[] prices = {15, 12, 12, 9};
public int calculatePrice(String name, int quantity) {
int index = -1;
for (int i = 0; i < names.length; i++) {
if (names[i].equals(name)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println(name + " does not exist");
} else {
return prices[index] * quantity;
}
}
}
分析:
为了解决此问题我定义了一个dish类其中属性:
names:一个字符串数组,用于存储菜品的名称。
prices:一个整型数组,用于存储菜品的价格。
方法:
calculatePrice(String name, int quantity):计算指定菜品和数量的总价格。它接收菜品名称和数量作为参数,并返回计算得到的总价格。如果指定的菜品名称不存在,则输出相应提示信息并返回0。
类的作用:
这个类提供了一个封装菜品价格计算逻辑的方法,可以根据指定的菜品和数量计算出总价格。
在主函数中,通过创建 Dish 类的对象 dish 来使用 Dish 类的功能。
主函数通过循环接受用户输入的菜品和数量,并调用 Dish 类的 calculatePrice 方法来计算每个菜品的价格,并累加得到总价格。
最后,输出总价格。
3-7-2课程成绩统计程序-1
相关类图:


相关类代码:
class 课程录入
{
课程录入()
{
}
void luru(String l,课表 c)
{
String[] in=l.split(" ");
if(in.length==3)
c.add(in[0],in[1],in[2]);
else if(in.length==2)
c.add(in[0],in[1],null);
else System.out.println("wrong format");
}
}
class 信息录入
{
ArrayList<选课> 选课= new ArrayList<选课>();
int count=0;
信息录入()
{
}
选课 luru(String l,课表 ke,ArrayList<学生> student)
{
String[] in=l.split(" ");
String hao=in[0];String name=in[1];String k=in[2];
if(ke.search(k)==null||ke.search(k).kaohe==null)//课程不存在
System.out.println(k+" does not exist");//hao+" "+name+" :"+
else if((ke.search(k).kaohe.equals("考试")&&in.length!=5)||(ke.search(k).kaohe.equals("考察")&&in.length!=4))//方式不匹配
System.out.println(hao+" "+name+" : access mode mismatch");
else if((in.length==4||in.length==5)&&searchx(l)==null)
{
if(Double.valueOf(in[3])>100||Double.valueOf(in[3])<0)
{return null;}/////////
if(in.length==4)
{
选课.add(new 选课(new 学生(hao,name),ke.search(k),0,Double.valueOf(in[3])));count++;
ke.search(k).期末+=Double.valueOf(in[3]);
}
else if(in.length==5)
{
if(Double.valueOf(in[4])<0||Double.valueOf(in[4])>100)
{return null;}
else
//选课.c=new 成绩(Double.valueOf(in[4]),Double.valueOf(in[5]));
{选课.add(new 选课(new 学生(hao,name),ke.search(k),Double.valueOf(in[3]),Double.valueOf(in[4])));count++;
ke.search(k).期末+=Double.valueOf(in[4]);
ke.search(k).平时+=Double.valueOf(in[3]);
}
}
double a=选课.get(count-1).c.计算成绩(ke.search(k));
ke.search(k).成绩+=a;ke.search(k).count++;
if(search(hao,student)!=null)
{ search(hao,student).cj+=a;search(hao,student).课程数++;}//选课.s.cj+=a;//选课.s.课程数++;
return 选课.get(count-1);
}
return null;
}
学生 search(String numb,ArrayList<学生> s)
{for(学生 student:s)
{
if(student.学号.equals(numb))
return student;
}
return null;
}
选课 searchx(String l)
{
String[] a=l.split(" ");
for(选课 x:选课)
{
if(x.s.学号.equals(a[0])&&x.s.name.equals(a[1])&&x.k.name.equals(a[2]))
return x;
}
return null;
}
}
class 课程
{
String name;
String kaohe;
String xingzhi;
double 成绩=0;
int 平均=0;
double 平时,期末;
int 平均1=0,平均2=0;
int count=0;
课程(String name,String xingzhi,String kaohe)
{
this.name=name;
this.kaohe=kaohe;
this.xingzhi=xingzhi;
}
void 计算()
{
if(count==0&&kaohe!=null)
System.out.println(name+" has no grades yet");
else if(count!=0) {
平均=(int)(成绩/count);
平均1=(int)(平时/count);
平均2=(int)(期末/count);
if(kaohe.equals("考试"))
System.out.println(name+" "+平均1+" "+平均2+" "+平均);
else if(kaohe.equals("考察"))
System.out.println(name+" "+平均2+" "+平均);
}
}
}
class 课表
{
ArrayList<课程> 课=new ArrayList<>();
boolean cz;
课程 search(String name)
{
for(课程 k:课)
{
if(k.name.equals(name))
return k;
}
return null;
}
课表()
{
}
void add(String name,String xingzhi,String kaohe)
{
if(search(name)==null)
{
if(kaohe!=null&&((xingzhi.equals("必修")||xingzhi.equals("选修"))&&(kaohe.equals("考试")||kaohe.equals("考察"))))
{
if(xingzhi.equals("必修")&&kaohe.equals("考察"))
{System.out.println(name+" : course type & access mode mismatch");
课.add(new 课程(name,null,null));
}
else 课.add(new 课程(name,xingzhi,kaohe));
}
else if(xingzhi.equals("必修")&&kaohe==null)
课.add(new 课程(name,xingzhi,"考试"));
else
{System.out.println("wrong format");课.add(new 课程(name,null,null));}
}
}
}
class 学生
{
String name;
String 学号;
//ArrayList<选课> 选课;
double cj;
int 课程数=0;
int p=0;
学生(String a,String b)
{
name=b;
学号=a;
}
public int 计算()
{
double p1;
p1=cj/课程数;
return (int)p1;
}
public int compareScore(学生 other) {
if (this.p > other.p) {
return -1;
} else if (this.p < other.p) {
return 1;
} else {
return 0;
}
}
}
class 班级
{
String 班号;
double 成绩=0;
double 平均;
int 人数;
班级(String numb)
{
班号=numb.substring(0, 6);
}
}
class 年级
{
ArrayList<班级> ban=new ArrayList<>();
年级()
{
}
班级 search(String numb)
{
String hao=numb.substring(0, 6);
for(班级 b:ban)
{
if(b.班号.equals(hao))
return b;
}
return null;
}
void add(String numb)
{
ban.add(new 班级(numb));
}
}
class 成绩
{
double 平时=0;
double 期末=0;
double 权重1=0.3,权重2=0.7;
int 总=0;
成绩(double a,double b)
{
平时=a;
期末=b;
}
int 计算成绩(课程 ke)
{
if(ke.kaohe.equals("考察"))
总=(int)期末;
else if(ke.kaohe.equals("考试"))
{
总=(int)(平时*权重1+期末*权重2);
}
return 总;
}
}
class 选课
{
学生 s;
课程 k;
成绩 c;//总成绩
int tm=0;
选课(学生 student,课程 k1,double a,double b)
{
s=student;
k=k1;
c=new 成绩(a,b);
c.计算成绩(k);
}
public 选课() {
// TODO 自动生成的构造函数存根
}
}
分析:
共创建了七个类其中包括
课程录入(CourseInput)类:
属性:
无属性
方法:
构造函数:初始化对象
luru(String l, 课表 c):将课程信息解析并添加到课表中
信息录入(InfoInput)类
属性:
ArrayList<选课> 选课:存储学生选课信息的列表
int count:记录选课的数量
方法:
构造函数:初始化对象
luru(String l, 课表 ke, ArrayList<学生> student):将学生选课信息解析并添加到选课列表中,同时计算成绩
search(String numb, ArrayList<学生> s):根据学号在学生列表中查找对应的学生对象
searchx(String l):根据选课信息字符串查找对应的选课对象
课程(Course)类:
属性:
String name:课程名称
String kaohe:考核方式
String xingzhi:性质
double 成绩:课程总成绩
int 平均:平均分
double 平时、期末:平时成绩和期末成绩
int 平均1、平均2:平时成绩和期末成绩的平均分
int count:成绩记录数
方法:
构造函数:初始化对象
计算():计算课程的平均分,并打印出来
课表(CourseSchedule)类:
属性:
ArrayList<课程> 课:存储课程的列表
boolean cz:是否操作成功的标志
方法:
构造函数:初始化对象
search(String name):根据课程名称在课程列表中查找对应的课程对象
add(String name, String xingzhi, String kaohe):将课程信息添加到课程列表中
学生(Student)类:
属性:
String name:学生姓名
String 学号:学生学号
double cj:学生总成绩
int 课程数:选修课程数量
int p:计算后的平均分
方法:
构造函数:初始化对象
计算():根据总成绩和选修课程数量计算平均分
compareScore(学生 other):比较当前学生和其他学生的成绩,返回比较结果
班级(Class)类:
属性:
String 班号:班级号码
double 成绩:班级总成绩
double 平均:班级平均分
int 人数:班级人数
方法:
构造函数:初始化对象
年级(Grade)类:
属性:
ArrayList<班级> ban:存储班级的列表
方法:
构造函数:初始化对象
search(String numb):根据班级号码在班级列表中查找对应的班级对象
add(String numb):将班级号码添加到班级列表中
通过Scanner类获取用户输入,不断循环录入课程信息或学生选课信息,直到用户输入"end"结束。每次录入后,会进行一些格式判断和数据处理,然后将录入的数据存储到相应的集合或对象中。
最后,根据需求进行排序和输出,包括学生平均成绩的排序和输出、课程平均分的排序和输出,以及班级平均成绩的计算和输出。
踩坑心得:
1、第一次作业主要注意输出格式,多次要求输出数据为float类型,如果没有按要求做测试就不会通过。


这里可以看到无论加不加float,对于测试用例来说在IDEA上输出结果都是一样的,但在pta上就是通过不了,这是因为double的精度更高,pta上有文本对照,必须要一模一样才可以通过。
2、类的定义一定要放在主类的大括号外面,比如:

一定不要像这样

把类的定义放在主类里面,这样在IDEA上可以编译但是放在PTA上输出结果为空,这个是我在第三次作业的时候才发现的,导致我第二次作业一直想不通为什么在PTA上输出结果为空。
主要困难以及改进建议:
1、区分不清静态和动态的区别,不知道什么时候用什么类型的。最后通过查找资料明白了静态的内容,基于类而存在,非静态的内容基于对象而存在。也就是说静态内容是一类事物(即基于该类的所有对象)的共有特征,而非静态内容每个对象的独有特征。静态内容一旦修改,所有对象都会被影响,修改非静态内容,只会影响当前对象。
2、如何定义构造函数方便自己可以调用和重置各个对象中各属性的值是目前学习的重点,通过这几次的PTA作业已经熟练掌握如何定义构造函数。
总结:
1、经过三次的PTA作业,我已经明白了什么是类,如何创建运用类,我觉得作业可以帮助我们巩固课堂上学到的东西。
2、通过这三次作业以及之前学过的c语言,我看到了C语言和java语言的不同点以及相似点,对于我以后学习其他语言打下来坚实的基础。
3、在作业中学到了虽然java和c语言有一些区别但大致相同比如对于数组的运用,原来创建对象也可以运用数组,只不过格式有点细小区别,同时字符串的用法也大同小异。
浙公网安备 33010602011771号