第一次题目集1~3总结
一,前言
知识
这三次题目集,学习到正则表达式,类型转换,字符串处理,链表,以及例如Arraylist等包的知识,和熟悉对类与对象的设计d思想.
题量与难度
题目集的题量都差不多,每次题目集的最后一题难度相对大,并有迭代升级的性质.有一些题目具有针对性,例如对数据的处理,时间类的运用,有针对性的训练到某个地方的运用.
二,设计与分析
第一次题目集
7-1设计一个风扇Fan类
源码如下
点击查看代码
import java.util.Scanner;
class Fan {
public static final int SLOW = 1;
public static final int MEDIUM = 2;
public static final int FAST = 3;
private int speed;
private boolean on;
private double radius;
private String color;
public Fan() {
speed = SLOW;
on = false;
radius = 5.0;
color = "white";
}
public Fan(int fanSpeed, boolean fanOn, double fanRadius, String fanColor) {
speed = fanSpeed;
on = fanOn;
radius = fanRadius;
color = fanColor;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String toString() {
if (on) {
return "fan is on";
} else {
return "fan is off";
}
}
}
public class Main{
public static void main(String[] args) {
Fan fan1 = new Fan();
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
scanner.nextLine();
boolean b= scanner.nextBoolean();
int c = scanner.nextInt();
scanner.nextLine();
String d = scanner.nextLine();
Fan fan2 = new Fan(a, b, c, d);
System.out.println("-------\nDefault\n-------");
System.out.println("speed " + fan1.getSpeed());
System.out.println("color " + fan1.getColor());
System.out.println("radius " + fan1.getRadius());
System.out.println(fan1.toString());
System.out.println("-------\nMy Fan\n-------");
System.out.println("speed " + fan2.getSpeed());
System.out.println("color " + fan2.getColor());
System.out.println("radius " + fan2.getRadius());
System.out.println(fan2.toString());
}
}
点击查看代码
import java.util.Scanner;
class Student{
private String name;
private String sex;
private String studentID;
private int age;
private String major;
public Student(){
}
public Student(String sname,String ssex,int sage,String smajor,String sstudentID){
name = sname;
sex = ssex;
studentID = sstudentID;
age = sage;
major = smajor;
}
public String getname(){
return name;
}
public void setname(String name){
this.name=name;
}
public String getsex(){
return sex;
}
public void setsex(String sex){
this.sex=sex;
}
public String getstudentID(){
return studentID;
}
public void setstudentID(String studentID){
this.studentID=studentID;
}
public int getage(){
return age;
}
public void setage(int age){
this.age=age;
}
public String getmajor(){
return major;
}
public void setmajor(String major){
this.major=major;
}
public String toString(){
return name+sex+studentID+age+major;
}
public void printInfo(){
System.out.println("姓名:"+name+",性别:"+sex+",学号:"+studentID+",年龄:"+age+",专业:"+major);
}
}
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String a=scanner.next();
String b=scanner.next();
int c=scanner.nextInt();
String d=scanner.next();
String e =scanner.next();
Student student =new Student(a,b,c,d,e);
student.printInfo();
}
}
点击查看代码
import java.util.Scanner;
import java.text.DecimalFormat;
class Student {
private String studentID;
private String name;
private int chineseScore;
private int mathScore;
private int physicsScore;
public Student(String studentID, String name, int chineseScore, int mathScore, int physicsScore) {
this.studentID = studentID;
this.name = name;
this.chineseScore = chineseScore;
this.mathScore = mathScore;
this.physicsScore = physicsScore;
}
public int calculateTotalScore() {
return chineseScore + mathScore + physicsScore;
}
public String calculateAverageScore() {
DecimalFormat df = new DecimalFormat("#.00");
return df.format((double) calculateTotalScore()/3);
}
public String getStudentID() {
return studentID;
}
public String getName() {
return name;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Student[] students = new Student[5];
for (int i = 0; i < 5; i++) {
String input = scanner.nextLine();
String[] info = input.split(" ");
String studentID = info[0];
String name = info[1];
int chineseScore = Integer.parseInt(info[2]);
int mathScore = Integer.parseInt(info[3]);
int physicsScore = Integer.parseInt(info[4]);
students[i] = new Student(studentID, name, chineseScore, mathScore, physicsScore);
}
for (Student student : students) {
if (student != null) {
int totalScore = student.calculateTotalScore();
String averageScore = student.calculateAverageScore();
System.out.println(student.getStudentID() + " " + student.getName() + " " + totalScore + " " + averageScore);
}
}
}
}
点击查看代码
import java.util.Scanner;
class Score {
public int dailyScore;
public int finalScore;
public Score(int dailyScore, int finalScore) {
this.dailyScore = dailyScore;
this.finalScore = finalScore;
}
public int calculateTotalScore() {
return (int) (dailyScore * 0.4 + finalScore * 0.6);
}
}
class Student {
private String studentID;
private String name;
private Score chineseScore;
private Score mathScore;
private Score physicsScore;
public Student(String studentID, String name, Score chineseScore, Score mathScore, Score physicsScore) {
this.studentID = studentID;
this.name = name;
this.chineseScore = chineseScore;
this.mathScore = mathScore;
this.physicsScore = physicsScore;
}
public int calculateTotalScore() {
return chineseScore.calculateTotalScore() + mathScore.calculateTotalScore() + physicsScore.calculateTotalScore();
}
public double calculateAverageScore() {
return Math.round((calculateTotalScore() / 3.0) * 100.0) / 100.0;
}
public double calculateAverageScore1() {
return Math.round(((chineseScore.dailyScore+mathScore.dailyScore+physicsScore.dailyScore )/ 3.0) * 100.0) / 100.0;
}
public double calculateAverageScore2() {
return Math.round(((chineseScore.finalScore+mathScore.finalScore+physicsScore.finalScore)/ 3.0) * 100.0) / 100.0;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
Score []a = new Score[3];
String studentID= new String();
String name= new String();
for(int j = 0;j < 3;j++){
String studentInfo = scanner.nextLine();
String[] infoArray = studentInfo.split(" ");
studentID = infoArray[0];
name = infoArray[1];
a[j] = new Score(Integer.parseInt(infoArray[3]), Integer.parseInt(infoArray[4]));
}
Student student = new Student(studentID, name, a[0], a[1], a[2]);
System.out.printf("%s %s %d %.2f %.2f %.2f\n",studentID,name, student.calculateTotalScore(),student.calculateAverageScore1(),student.calculateAverageScore2(), student.calculateAverageScore());
}
}
}
点击查看代码
import java.util.*;
class MobilePhone implements Comparable<MobilePhone> {
public String type;
private int price;
public MobilePhone(String type, int price) {
this.type = type;
this.price = price;
}
@Override
public int compareTo(MobilePhone otherPhone) {
return this.price - otherPhone.price;
}
@Override
public String toString() {
return "型号:" + type + ",价格:" + price;
}
public int getPrice() {
return price;
}
}
public class Main {
public static void main(String[] args) {
List<MobilePhone> phones = new LinkedList<>();
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
String type = scanner.next();
int price = scanner.nextInt();
phones.add(new MobilePhone(type, price));
}
System.out.println("排序前,链表中的数据:");
for (MobilePhone phone : phones) {
System.out.println(phone);
}
Collections.sort(phones);
System.out.println("排序后,链表中的数据:");
for (MobilePhone phone : phones) {
System.out.println(phone);
}
String type = scanner.next();
int price = scanner.nextInt();
MobilePhone fourthPhone = new MobilePhone(type, price);
boolean found = false;
MobilePhone foundPhone = null;
for (MobilePhone phone : phones) {
if (phone.getPrice() == fourthPhone.getPrice()) {
found = true;
foundPhone = phone;
break;
}
}
if (found) {
System.out.println(fourthPhone.type + "与链表中的" + foundPhone.type + "价格相同");
} else {
System.out.println("链表中的对象,没有一个与"+fourthPhone.type+"价格相同的");
}
}
}
点击查看代码
import java.util.Scanner;
class Circle {
private int radius;
public Circle() {
this.radius = 2;
System.out.println("This is a constructor with no para.");
}
public Circle(int radius) {
if (radius <= 0) {
this.radius = 2;
} else {
this.radius = radius;
}
System.out.println("This is a constructor with para.");
}
public void setRadius(int radius) {
if (radius <= 0) {
this.radius = 2;
} else {
this.radius = radius;
}
}
public int getRadius() {
return this.radius;
}
public double getArea() {
return Math.PI * this.radius * this.radius;
}
@Override
public String toString() {
return "Circle [radius=" + radius + "]";
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Circle c1 = new Circle();
System.out.println(c1);
System.out.printf("%.2f\n", c1.getArea());
Circle c2 = new Circle();
System.out.println(c2);
System.out.printf("%.2f\n", c2.getArea());
int radiusInput = scanner.nextInt();
c2.setRadius(radiusInput);
System.out.println(c2);
System.out.printf("%.2f\n", c2.getArea());
int radiusInput2 = scanner.nextInt();
Circle c3 = new Circle(radiusInput2);
System.out.println(c3);
System.out.printf("%.2f\n", c3.getArea());
}
}
点击查看代码
/**
* @author Teacher
* 主类,用来在pta中运行。
* 其中有一处代码需要完善
*/
public class Main {
public static void main(String[] args) {
Car car = new Car("bob", 0.0f, 0.0f);
System.out.printf("车主:%s,速度:%.1f,角度:%.1f\n", car.getOwnerName(), car.getCurSpeed(), car.getCurDirInDegree());
// 设置新速度
car.changeSpeed(10);
System.out.printf("车主:%s,速度:%.1f,角度:%.1f\n", car.getOwnerName(), car.getCurSpeed(), car.getCurDirInDegree());
// 停车
car.stop();
System.out.printf("车主:%s,速度:%.1f,角度:%.1f\n", car.getOwnerName(), car.getCurSpeed(), car.getCurDirInDegree());
}
}
/**
* @author Teacher
* 汽车类,其中有两个内容需要完善。
*/
class Car {
// 车主姓名
private String ownerName;
// 当前车速
private float curSpeed;
// 当前方向盘转向角度
private float curDirInDegree;
public Car(String ownerName) {
this.ownerName = ownerName;
}
public Car(String ownerName, float speed, float dirInDegree) {
this(ownerName);
this.curSpeed = speed;
this.curDirInDegree = dirInDegree;
}
// 提供对车主姓名的访问
public String getOwnerName() {
return ownerName;
}
// 提供对当前方向盘转向角度的访问
public float getCurDirInDegree() {
return curDirInDegree;
}
// 提供对当前车速的访问
public float getCurSpeed() {
return curSpeed;
}
// 提供改变当前的车速
public void changeSpeed(float newSpeed) {
this.curSpeed = newSpeed;
}
// 提供停车
public void stop() {
this.curSpeed = 0.0f;
}
}
点击查看代码
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
//调用无参构造方法,并通过setter方法进行设值
String sid1 = sc.next();
String name1 = sc.next();
int age1 = sc.nextInt();
String major1 = sc.next();
Student student1 = new Student();
student1.setSid(sid1);
student1.setName(name1);
student1.setAge(age1);
student1.setMajor(major1);
//调用有参构造方法
String sid2 = sc.next();
String name2 = sc.next();
int age2 = sc.nextInt();
String major2 = sc.next();
Student student2 = new Student(sid2, name2, age2, major2);
//对学生student1和学生student2进行输出
student1.print();
student2.print();
}
}
class Student{
private String sid;
private String name;
private int age;
private String major;
public Student() {
}
public Student(String sid, String name, int age, String major) {
super();
this.sid = sid;
this.name = name;
if(age>0)
this.age = age;
this.major = major;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age>0)
this.age = age;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public void print(){
System.out.println("学号:"+sid+",姓名:"+name+",年龄:"+age+",专业:"+major);
}
}
点击查看代码
import java.util.Scanner;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class Main {
public static int[] month_day = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String date = input.next();
String start = input.next();
String end = input.next();
int date2 = Integer.parseInt(number(date));
int start2 = Integer.parseInt(number(start));
int end2 = Integer.parseInt(number(end));
int dateDay = date2 % 100;
int dateMonth = date2 / 100 % 100;
int dateYear = date2 / 10000;
int startDay = start2 % 100;
int startMonth = start2 / 100 % 100;
int startYear = start2 / 10000;
int endDay = end2 % 100;
int endMonth = end2 / 100 % 100;
int endYear = end2 / 10000;
int judgeRun = runYear(dateYear);
if (judgeRun == 1) {
month_day[1] = 29;
}
int judgeLeg = judgeDate(dateMonth, dateDay, date2);
if (judgeLeg == 0) {
System.out.println(date + "无效!");
} else {
if (judgeRun == 1) {
System.out.println(date + "是闰年.");
}
int sum_day = daysOfYear(dateMonth, dateDay);
LocalDate startDate = LocalDate.of(2000, 1, 2);
LocalDate endDate = LocalDate.of(dateYear, dateMonth, dateDay);
long days = Math.abs(ChronoUnit.DAYS.between(startDate, endDate)) % 7;
if (days == 0) {
days = 7;
}
System.out.println(date + "是当年第" + sum_day + "天,当月第" + dateDay + "天,当周第" + days + "天.");
}
judgeRun = runYear(startYear);
if (judgeRun == 0) {
month_day[1] = 28;
}
int judgeLegS = judgeDate(startMonth, startDay, start2);
int judgeLegE = judgeDate(endMonth, endDay, end2);
if (judgeLegS == 0 || judgeLegE == 0) {
System.out.println(start + "或" + end + "中有不合法的日期.");
} else if (start2 > end2) {
System.out.println(end + "早于" + start + ",不合法!");
} else {
int yearSub = endYear - startYear;
int monthSub = endMonth - startMonth;
LocalDate startDate = LocalDate.of(startYear, startMonth, startDay);
LocalDate endDate = LocalDate.of(endYear, endMonth, endDay);
long days = ChronoUnit.DAYS.between(startDate, endDate);
System.out.print(end + "与" + start + "之间相差" + days + "天,所在月份相差" + monthSub + ",所在年份相差" + yearSub + ".");
}
}
public static String number(String str) {
str = str.trim();
StringBuilder str2 = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
if (Character.isDigit(str.charAt(i))) {
str2.append(str.charAt(i));
}
}
return str2.toString();
}
public static int runYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) ? 1 : 0;
}
public static int daysOfYear(int month, int day) {
int sum = 0;
for (int i = 0; i < month - 1; i++) {
sum += month_day[i];
}
sum += day;
return sum;
}
public static int judgeDate(int month, int day, int whole) {
return whole < 100000000 && whole >= 10000000 && day >= 1 && day <= month_day[month - 1] ? 1 : 0;
}
}
点击查看代码
import java.util.*;
class Question{
int number;
String question;
String answer1;
public Question(int number,String question,String answer1){
this.number = number;
this.question = question;
this.answer1 = answer1;
}
}
class Student{
List<String> studentNumber;
List<String> name;
public Student(List <String> studentNumber,List <String> name){
this.studentNumber=studentNumber;
this.name=name;
}
}
class Test{
int id;
List<Integer> num;
List<Integer> score;
public Test(int id, List<Integer> num, List<Integer> score) {
this.id = id;
this.num = num;
this.score = score;
}
}
class Sheet{
int sheet_id;
String number1;
List<String> answer;
List<String> annum;
public Sheet(int sheet_id,String number1,List<String> answer, List<String> annum){
this.sheet_id=sheet_id;
this.number1=number1;
this.answer=answer;
this.annum=annum;
}
}
class Del{
int sum;
public Del(int sum){
this.sum=sum;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String pi=scanner.nextLine();
List <Question> question=new ArrayList<>();
List <Test> test =new ArrayList<>();
List <Student> student =new ArrayList<>();
List <Del> del=new ArrayList<>();
List <Sheet> sheet =new ArrayList<>();
while(!pi.equals("end")){
if(pi.startsWith("#N")){
String[] pis=pi.split(":");
pis[3]=pis[3].trim();
pis[1]=pis[1].substring(0,pis[1].length()-2);
pis[1]=pis[1].trim();
pis[2]=pis[2].substring(0,pis[2].length()-2);
pis[2]=pis[2].trim();
int number1=Integer.parseInt(pis[1]);
Question quesrion1=new Question(number1,pis[2],pis[3]);
question.add(quesrion1);
}
if(pi.startsWith("#T")){
String[] pis=pi.split(" ");
int number1=Integer.parseInt(pis[0].substring(3));
List<Integer> num1=new ArrayList<>();
List<Integer> score1=new ArrayList<>();
for(int i=1;i<pis.length;i++){
String[] piss=pis[i].split("-");
num1.add(Integer.parseInt(piss[0]));
score1.add(Integer.parseInt(piss[1]));
}
Test test1=new Test(number1,num1,score1);
test.add(test1);
}
if(pi.startsWith("#S")){
String[] pis=pi.split(" ");
int number2=Integer.parseInt(pis[0].substring(3));
String s=pis[1];
List<String> answer1 =new ArrayList<>();
List<String> annum1 =new ArrayList<>();
for(int i=2;i<pis.length;i++){
answer1.add(pis[i].substring(3,4));
annum1.add(pis[i].substring(5));
}
Sheet sheet1 =new Sheet(number2,s,answer1,annum1);
sheet.add(sheet1);
}
if(pi.startsWith("#X")){
String[] pis=pi.split(":");
String[] piss =pis[1].split("-");
List<String> stunumber =new ArrayList<>();
List<String> name1 =new ArrayList<>();
for(int i=0;i<piss.length;i++){
String[] ppiss=piss[i].split(" ");
stunumber.add(ppiss[0]);
name1.add(ppiss[1]);
}
Student student1 =new Student(stunumber,name1);
student.add(student1);
}
if(pi.startsWith("#D")){
String[] pis =pi.split("-");
del.add(new Del (Integer.parseInt(pis[1])));
}
pi=scanner.nextLine();
}
//System.out.println(question.get(0).question);
//System.out.println(test.get(0).id);
//System.out.println(sheet.get(0).number1);
// System.out.println(student.get(0).name.get(0));
// System.out.println(del.get(0).sum);
for(int i=0;i<test.size();i++){
int conut=0;
for(int j=0;j<test.get(i).score.size();j++){
conut=test.get(i).score.get(j)+conut;
}
if(conut!=100){
System.out.println("alert: full score of test paper"+test.get(i).id+" is not 100 points");
}
}
for(int i=0;i<sheet.size();i++){//答卷
int ca=0;
int scores=0;
List<Integer> sco=new ArrayList<>();
for(int j=0;j<test.size();j++){//试卷
if(sheet.get(i).sheet_id==test.get(j).id){//找试卷
ca=1;
for(int k=0;k<test.get(j).num.size();k++){//题号
int cb=0;
for(int io=0;io<question.size();io++){//题目
if(test.get(j).num.get(k)==question.get(io).number){//找题目
cb=1;
int cc=0;
for(int ip=0;ip<sheet.get(i).answer.size();ip++){
if(Integer.parseInt(sheet.get(i).answer.get(ip))==k+1){//找答案
cc=1;
if(sheet.get(i).annum.get(ip).equals(question.get(io).answer1)){
System.out.println(question.get(io).question+"~"+sheet.get(i).annum.get(ip)+"~true");
sco.add(test.get(j).score.get(io));
}
else{
System.out.println(question.get(io).question+"~"+sheet.get(i).annum.get(ip)+"~false");
sco.add(0);
}
}
}
if(cc==0){
System.out.println("answer is null");
}
}
}
if(cb==0){
System.out.println("non-existent question~0");
}
}
System.out.println(sheet.get(i).number1+" "+student.get(0).name.get(0)+": 0~0");
}
}
if(ca==0){
System.out.println("The test paper number does not exist");
}
}
}}
public Test(int id, List<Integer> num, List<Integer> score) {
this.id = id;
this.num = num;
this.score = score;
}
}
在main中在List一个test,对于数据使用时,要分清楚哪个数据,就像一个二维数组一样.
在进行判题时,要理好不同判断的主次,按照找试卷,找题目,找答案的顺序去循环,一层套一层,以答卷开始,通过答卷找试卷,再通过试卷去找题目,将类的数据以此连接起来,处理数据.
三,踩坑心得
题目集判题前几个的时,都是直接去写代码,但之后就会错误重重,不好修改,所以在判题三中我每写好一个数据的输入就测试,虽然这个在c语言中就应该做到,但这次我对这个有更深的体会,特别是之后java的代码更长的时候.
五,总结
学习到了正则表达式,类型转换,字符串处理,链表,以及例如Arraylist等包的知识,和熟悉对类与对象的设计的思想.
这次题目集我最大的问题就是因为在判题一中,由于不会对于数据输入的处理,就放弃了这个题目,可之后的题目集中还是这个题目,所以要好好对待每个题目,在题目有迭代的时候,好好处理好每个阶段.
要加强的正则表达式的使用,还有就是在遇到问题的自学处理,和与同学交流学习的能力.