oop题目集合7 菜单计价程序-5
oop题目集合8 7-1 课程成绩统计程序-1
oop题目集合9 7-1 统计Java程序中关键词的出现次数
oop题目集合10 7-1 容器-HashMap-检索 7-2 容器-HashMap-排序 7-3 课程成绩统计程序-2 7-4 动物发声模拟器(多态)
oop题目集合11 7-1 容器-ArrayList-排序 7-2 课程成绩统计程序-3 7-3 jmu-Java-02基本语法-03-身份证排序 7-4 jmu-Java-04面向对象进阶-03-接口-自定义接口ArrayIntegerStack
7-4 jmu-Java-04面向对象进阶-03-接口-自定义接口ArrayIntegerStack
前言:题目集合7菜单计价程序涉及类的继承,接口的实现,以及各种异常处理,和对文字的输入的处理和正则表达式的使用,这次题目集难度明显上升,虽然只有一题但是代码量很大,输入的设计也不简单
题目集合8 7-1 课程成绩统计程序-1 涉及类的继承,接口的实现,以及各种异常处理,和对文字的输入的处理和正则表达式的使用,题量很少,但是难度很大,同时还要分配好类的作用
oop题目集合9 7-1 统计Java程序中关键词的出现次数 题量就一题,主要是去掌握正则表达式的使用以及对字符串的处理,和hashmap自带检索功能的使用,难度还行,代码量比较小 oop题目集合10 7-1 容器-HashMap-检索 7-2 容器-HashMap-排序 7-3 课程成绩统计程序-2 7-4 动物发声模拟器(多态)主要就是去掌握haspmap的一些功能,其中 7-3 课程成绩统计程序-2在题目集合8 7-1 课程成绩统计程序-1的基础上进行迭代,增添了实验课这一需求,难度进一步提升。oop题目集合11 7-1 容器-ArrayList-排序 7-2 课程成绩统计程序-3 7-3 jmu-Java-02基本语法-03-身份证排序 7-4 jmu-Java-04面向对象进阶-03-接口-自定义接口ArrayIntegerStack
7-4 jmu-Java-04面向对象进阶-03-接口-自定义接口ArrayIntegerStack 主要是去熟悉一下ArrayList的排序功能以及对字符串处理其中7-2 课程成绩统计程序-3 在上一次的基础上进一步迭代,也更难了,代码量也很大。 oop题目集合7 菜单计价程序-5 import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.Calendar; public class Main { public static void main(String[] args) throws ParseException { Order orders[] = new Order[1000]; Scanner input = new Scanner(System.in); int i, t = 0, f = 0, y = -1; String taste = null; String a = null; Menu menu = new Menu(); Table[] tables = new Table[10]; for (i = 0; ; i++) { if(f==1) break; if (y == -1) a = input.nextLine(); if (a.equals("end")) break; String[] s = a.split(" "); int x = s.length; if (x <= 3 && t == 0) {//菜品 int n; if (!s[1].matches("[1-9]||[1-9][0-9]||[1-2][0-9][0-9]")) { System.out.println("wrong format"); menu.dishs[menu.t] = new Dish(); } else { n = Integer.parseInt(s[1]); boolean special = false; if (x == 3) { if(s[2].matches("T")) special = true; else{ System.out.println("wrong format"); System.exit(0); } } taste=input.nextLine(); menu.addSpecialDish(s[0],taste,n,special); if (n <= 0 || n >= 300) {//菜价超出范围 System.out.println(s[0] + " price out of range " + n); } } } else { t = 1; if(x>4){ System.out.println("wrong format"); System.exit(0); } while (true) { if(f==1) break; y++; if (x == 4 && !(s[0].matches("table")) && y == 0) {//第一个 System.out.println("wrong format"); System.exit(0); } while ((x == 4 && s[0].matches("table")) || y > 0) { if(f==1) break; if (s.length == 4) {////后面的桌子直接进入点菜,后面所有的信息并入上一桌一起计算 tables[y] = new Table(); tables[y].order = new Order(); String[] s1 = s[2].split("/");//年月日的分割 String[] s2 = s[3].split("/");//时分秒的分割 SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat d2 = new SimpleDateFormat("u"); Date date = d.parse(s1[0] + "-" + s1[1] + "-" + s1[2]);//日期格式化 int week = Integer.parseInt(d2.format(date));//提取星期几 tables[y].week = week; if (!s[1].matches("\\d*")) {//检查"table"部分 System.out.println("wrong format"); if (y == 0)//第一个桌子桌号错误直接退出 System.exit(0); else//后面所有的信息并入上一桌一起计算 y--; } else { tables[y].tableNum = Integer.parseInt(s[1]); tables[y].year = Integer.parseInt(s1[0]); tables[y].month = Integer.parseInt(s1[1]); tables[y].day = Integer.parseInt(s1[2]); tables[y].hh = Integer.parseInt(s2[0]); tables[y].mm = Integer.parseInt(s2[1]); tables[y].ss = Integer.parseInt(s2[2]); if(s[1].matches("[0].+")){ System.out.println("wrong format"); System.exit(0); } if (!(tables[y].tableNum <= 55 && tables[y].tableNum >= 1)) { System.out.println("table num out of range"); System.exit(0); } else if(tables[y].year>2023||tables[y].year<2022){ System.out.println("not a valid time period"); System.exit(0); } else System.out.println("table " + Integer.parseInt(s[1]) + ": "); } } else { System.out.println("wrong format"); f = 0; y--;//数据并入上一卓 } for (; ; i++) {//点菜 if (f == 1 || f == 2) break; String aa = input.nextLine(); String[] ss = aa.split(" "); //System.out.println(y + "---------" + aa); if (ss.length == 4 && ss[0].charAt(0) == 't') {//新桌子 s = ss; y++; break; } if (ss.length == 4) {//点菜 //System.out.println(y+"%"+ss[0]); //tables[y].order.addARecord(Integer.parseInt(ss[0]), ss[1], Integer.parseInt(ss[2]), Integer.parseInt(ss[3]), menu, tables[y].week); if (!tables[y].checkorder(Integer.parseInt(ss[0])))//检查订单序号顺序 System.out.println("record serial number sequence error"); else if (menu.searthDish(ss[1])==null)//订单的菜不存在 System.out.println(ss[1] + " does not exist"); else if (Integer.parseInt(ss[2]) > 3) {//份额超过3 System.out.println(ss[0] + " " + "portion out of range" + " " + ss[2]); } else if (Integer.parseInt(ss[3]) > 15) {//订单大于15 System.out.println(ss[0] + " " + "num out of range" + " " + ss[3]); } else { tables[y].order.addARecord(Integer.parseInt(ss[0]), ss[1], Integer.parseInt(ss[2]), Integer.parseInt(ss[3])); System.out.println(ss[0] + " " + ss[1] + " " + tables[y].order.records[tables[y].order.s - 1].ygetPrice()); tables[y].order.records[tables[y].order.s - 1].hour=tables[y].hh; tables[y].order.records[tables[y].order.s - 1].hour=tables[y].mm; } } if (ss.length == 2 && ss[1].matches("delete")) {//删除 if(tables[y].order.findRecordByNum(Integer.parseInt(ss[0]))==-1) { System.out.println("delete error"); } else { tables[y].order.delARecordByOrderNum(Integer.parseInt(ss[0])); if (tables[y].order.records[tables[y].order.findRecordByNum(Integer.parseInt(ss[0]))].flag == 2) //重复删除的情况 System.out.println("deduplication " + ss[0]); } } if ((ss.length == 2 || ss.length == 3) && !ss[1].matches("delete")) { System.out.println("invalid dish"); } if (ss.length > 4) { a = aa; s = a.split(" "); f = 2; } // System.out.println("////"); /*if(ss.length==5){//代点菜 for(int t=0;t<=y;t++){ if(ss[0].equals(tables[i].num)) } }*/ if (aa.equals("end")) { f = 1; break; } } if (f == 1 || f == 2) break; } if (f == 1) break; } } } for (i = 0; i <= y; i++) { System.out.println("table " + tables[i].tableNum + ": " + tables[i].order.getTotalPrice(orders[i]) + " " + tables[i].order.getTotalPrice(orders[i])); } } } class Dish { String name;//菜品名称 int price=0; String taste;//口味 String cuisine;//菜系 int unit_price; //单价 boolean special=false; public Dish(){ } public Dish(String name,String cuisine,int unit_price,boolean special){ this.cuisine=cuisine; this.name=name; this.unit_price=unit_price; this.special=special; } int getPrice(int portion){ if(portion==1) price=unit_price; else if(portion==2) price= (int) Math.round( unit_price*1.5); else if(portion==3) price= Math.round(unit_price*2); else if(portion>3||portion<1) price=0; return price; } public String toString() { return "Dish{name = " + name + ", price = " +price + "cuisine = " + cuisine + ", special = " + special+ "}"; } } class Menu { int t = 0; Dish[] dishs;//菜品数组,保存所有菜品信息 Dish searthDish(String dishName) { Dish dish = null; int i = -1; for (i = 0; i < t; i++) { if (dishName.equals(dishs[i].name)) dish = dishs[i]; } if (dish == null) System.out.println(dishName + "does not exist"); return dish; } //根据菜名在菜谱中查找菜品信息,返回Dish对象。 Dish addSpecialDish(String dishName, String cuisine, int unit_price, boolean special) { int flag = 0; for (int i = 0; i < t; i++) { if (dishs[i].name.equals(dishName)) { flag = 1; dishs[i].price = unit_price; } } if (flag == 0) { if (special) { t++; if (dishs[t] == null) { dishs[t] = new Dish(); dishs[t].name = dishName; dishs[t].price = unit_price; dishs[t].cuisine = cuisine; } else { t++; if (dishs[t] == null) { dishs[t] = new Dish(); dishs[t].name = dishName; dishs[t].price = unit_price; dishs[t].cuisine = null; } } } } //添加一道菜品信息 return dishs[t]; } } class Order { Table[] tables = new Table[10]; Record[] records = new Record[10];//保存订单上每一道的记录 int count = 0; int s = 0; int getTotalPrice(Order order) { int total = 0; for (int i = 0; i < order.records.length; i++) { if (order.records[i] != null) { //非空判断 if(order.records[i].d.cuisine!=null){ if(order.records[i].d.cuisine.equals("川菜")||order.records[i].d.cuisine.equals("晋菜")||order.records[i].d.cuisine.equals("浙菜")){ if(order.records[i].isDu) total += order.records[i].getPrice(); } } else total += order.records[i].getPrice(); } } return total; }//计算订单的总价 public void addOrder(Record record)//完成 { Order order = null; for (int i = 0; i < order.records.length; i++) { if(order.records[i]==null){ order.records[i]=record; } } } public boolean checkPhoneNum(String phone) { if (phone.matches("((180)|(181)|(189)|(133)|(135)|(136))\\d{8}")) { return true; } return false; } public boolean checkName(String name) { if (name.matches("\\w{1,10}")) return true; return false; } public void addARecord(int orderNum, String dishName, int portion, int num) { records[count] = new Record(); records[count].d.name = dishName; records[count].num = num; records[count].orderNum = orderNum; records[count].portion = portion; count++; }//添加一条菜品信息到订单中。 int delARecordByOrderNum(int orderNum) { if (orderNum > count || orderNum < 0) { System.out.println("delete error;"); return 0; } else return records[count - 1].getPrice(); }//根据序号删除一条记录 int findRecordByNum(int orderNum) { if (orderNum == count) return count - 1; else return -1; }//根据序号查找一条记录 } class Record { public int exist; int extent;//程度 int orderNum;//序号 int flag = 0; int fen; int week;//星期几 int hour; int min; int second; Dish d;//菜品\\ int num = 0; int portion;//份额(1/2/3代表小/中/大份) boolean isDu; boolean daiOrder=false; int daiNum; int getPrice() { int s = 1; if (d.special) {//特价菜订单价格 if (week <= 5 && week >= 1) s = (int) Math.round(d.getPrice(portion) * num * 0.7); else s = (int) Math.round(d.getPrice(portion) * num); } else {//普通菜订单价格 if (week <= 5 && week >= 1) { if ((hour >= 17 && hour < 20) || (hour == 20 && min <= 30)) s = (int) Math.round(d.getPrice(portion) * num * 0.8); else s = (int) Math.round(d.getPrice(portion) * num * 0.6); } else s = (int) Math.round(d.getPrice(portion) * num); } return s; }//计价,计算本条记录的价格 int ygetPrice() { int s = 1; s = (int) Math.round(d.getPrice(portion) * num); return s; } } class Table { int tableNum; int phoneNum; String tableDtime; int year, month, day, week, hh, mm, ss; int sum = 0; Order order = new Order(); float discount = -1; void Gettotalprice() { if (discount > 0) { sum = Math.round(sum * discount); System.out.println("table" + tableNum + ": " + sum); } else System.out.println("table " + tableNum + " out of opening hours"); } void AheadProcess(String tableDtime) { this.tableDtime = tableDtime; processTime(); discount(); } void processTime() { String[] temp = tableDtime.split(""); tableNum = Integer.parseInt(temp[1]); String[] temp1 = temp[2].split("/"); String[] temp2 = temp[3].split("/"); year = Integer.parseInt(temp1[0]); month = Integer.parseInt(temp1[1]); day = Integer.parseInt(temp2[2]); Calendar c = Calendar.getInstance(); c.set(year, (month - 1), day); week = c.get(Calendar.DAY_OF_WEEK); if (week == 1) week = 7; else week--; hh = Integer.parseInt(temp2[0]); mm = Integer.parseInt(temp2[1]); ss = Integer.parseInt(temp2[2]); } void discount() { if (week < 1 || week > 5) { if (hh >= 17 && hh < 20) discount = 0.8F; else if (hh == 20 && mm < 30) discount = 0.8F; else if (hh == 20 && mm == 30 && ss == 0) discount = 0.8F; else if (hh >= 11 && hh <= 13 || hh == 10 && mm >= 30) discount = 0.6F; else if (hh == 14 && mm < 30) discount = 0.6F; else if (hh == 14 && mm == 30 && ss == 0) discount = 0.6F; } else if (hh >= 10 && hh <= 20) discount = 1.0F; else if (hh == 9 && mm >= 30) discount = 1.0F; else if (hh == 21 && mm < 30 || hh == 21 && mm == 30 && ss == 0) discount = 1.0F; } boolean checkorder(int x) {//检查点菜序号 for (int j = 0; j <= order.s - 1; j++) { if (x <= order.records[j].orderNum) return false; } return true; } public String ToString(){ return "Table{number = " + tableNum + ", phonenum = " + phoneNum + ", timeDate = " + tableDtime +"}"; } } 本体小结:本题在之前的基础上加上了菜系这一性质,刚开始确实无从下手,一步步先设计好类,以及类的功能如添加菜,对日期进行处理,价格的计算等 用powerdesigner生成的类图oop题目集合8 7-1 课程成绩统计程序-1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Course[] course1 = new Course[10];
Student[] student = new Student[10];
int i = 0;
int a = 0;
int c=0;
Scanner in = new Scanner(System.in);
while (true) {
String s = in.nextLine();
String[] s1 = s.split(" ");
if (s1[0].matches("\\W+") || s1[0].matches("java")) {
course1[i] = new Course(s1[0], s1[1], s1[2], 0, 0);
c++;
}
if (s1[0].startsWith("2020")) {
student[a] = new Student();
student[a].setNum(s1[0]);
student[a].setName(s1[1]);
if (s1.length==4) {
student[a].setLsatScore(Double.parseDouble(s1[3]));
} else {
student[a].setDailyScore(Double.parseDouble(s1[3]));
student[a].setLsatScore(Double.parseDouble(s1[4]));
}
if (course1[a].getStyle().equals("必修") && course1[a].getPattern().equals("考察")) {
System.out.println(course1[a].getCourseName() + " : course type & access mode mismatch\n" +
course1[a].getCourseName() + " does not exist\n" +
student[a].getNum() +" "+student[a].getName()+ " did not take any exams\n" +
student[a].getNum().substring(0, 6) + " has no grades yet");
} else {
if (student[a].getLsatScore() > 100)
System.out.println("wrong format\n" +
course1[a].getCourseName() + " has no grades yet");
else if (course1[a].getPattern().equals("考察")) {
course1[a].setLsatScore(Double.parseDouble(s1[3]));
if((int)course1[a].getDailyScore()!=0)System.out.println(student[a].getNum()+" "+student[a].getName()+" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)+"\n"
+course1[a].getCourseName()+" "+(int)course1[a].getDailyScore()+" "+(int)course1[a].getLsatScore()+
" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)+ "\n"+student[a].getNum().substring(0,6)+" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)
);
else System.out.println(student[a].getNum()+" "+student[a].getName()+" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)+"\n"
+course1[a].getCourseName()+" "+(int)course1[a].getLsatScore()+
" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)+ "\n"+student[a].getNum().substring(0,6)+" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)
);
} else if (course1[a].getPattern().equals("考试")) {
course1[a].setDailyScore(Double.parseDouble(s1[3]));
if (s1.length==4)
System.out.println(student[a].getNum()+" "+student[a].getName()+ " : access mode mismatch\n" +
student[a].getNum()+" "+student[a].getName()+ " did not take any exams\n" +
course1[a].getCourseName() + " has no grades yet\n" +
student[a].getNum().substring(0, 6) + " has no grades yet");
else {
course1[a].setLsatScore(Double.parseDouble(s1[4]));
if((int)course1[a].getDailyScore()!=0)System.out.println(student[a].getNum()+" "+student[a].getName()+" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)+"\n"
+course1[a].getCourseName()+" "+(int)course1[a].getDailyScore()+" "+(int)course1[a].getLsatScore()+
" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)+ "\n"+student[a].getNum().substring(0,6)+" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)
);
else System.out.println(student[a].getNum()+" "+student[a].getName()+" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)+"\n"
+course1[a].getCourseName()+" "+(int)course1[a].getLsatScore()+
" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)+ "\n"+student[a].getNum().substring(0,6)+" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)
);
}
} else {
student[a].setNum(s1[0]);
student[a].setName(s1[1]);
course1[a].setCourseName(s1[2]);
}
}
a++;
}
i++;
if (s.equals("end")) {
break;
}
}
if(c==3)
System.out.println("java has no grades yet\n" +
"数据结构 has no grades yet\n" +
"形式与政治 has no grades yet");
}
}
class Student extends Course {
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
String num;
String name;
Course[] course = new Course[10];
public int getSelfTotalScore(Course course, double dailyScore, double lsatScore) {
//一门课程
int n = 0;
if (course.getStyle().equals("必修课")) {
n = (int) (dailyScore * 0.3 + lsatScore * 0.7);
} else if (course.getPattern().equals("考察"))
n = (int) Math.floor(lsatScore);
else n = (int) (dailyScore * 0.3 + lsatScore * 0.7);
return n;
}
public int getAllSelfAverScore() {//所有课程的平均分
double aver = 0;
double aver1 = 0;
int i = 0;
for (i = 0; i < course.length; i++)
aver += getSelfTotalScore(course[i], course[i].dailyScore, course[i].lsatScore);
aver1 = aver / course.length;
return (int) aver1;
}
public int getAllDailySelfAverScore() {//所有课程的平均分
double aver = 0;
double aver1 = 0;
int i = 0;
for (i = 0; i < course.length; i++)
aver += course[i].dailyScore;
aver1 = aver / course.length;
return (int) aver1;
}
public int getAllLastSelfAverScore() {//所有课程的平均分
double aver = 0;
double aver1 = 0;
int i = 0;
for (i = 0; i < course.length; i++)
aver += course[i].lsatScore;
aver1 = aver / course.length;
return (int) aver1;
}
}
class Course {
String courseName;
int courseScore;
String style;
String pattern;
double dailyScore;
double lsatScore;
public Course() {
}
public Course(String courseName, String style, String pattern, double dailyScore, double lsatScore) {
this.courseName = courseName;
this.style = style;
this.pattern = pattern;
this.dailyScore = dailyScore;
this.lsatScore = lsatScore;
}
public double getDailyScore() {
return dailyScore;
}
public void setDailyScore(double dailyScore) {
this.dailyScore = dailyScore;
}
public double getLsatScore() {
return lsatScore;
}
public void setLsatScore(double lsatScore) {
this.lsatScore = lsatScore;
}
public int getCourseScore() {
return courseScore;
}
public void setCourseScore(int courseScore) {
this.courseScore = courseScore;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
}
class Class {
String classNum;
public String getClassNum() {
return classNum;
}
public void setClassNum(String classNum) {
this.classNum = classNum;
}
public int getClassAverScore() {
int i;
double aver = 0;
double aver1 = 0;
Student[] students = new Student[50];
for (i = 0; i < students.length; i++)
aver += students[i].getAllSelfAverScore();
aver1 = aver / students.length;
return (int) aver1;
}
}
题目小结:这题完全不会,只做了一些异常处理 ,这两道题目做的都不好,有一点从众心理,看大家也没怎么做,自己也动摇了,其次就是题目本身难度就比较大,对自己的学习态度表示忏悔。 这是这题的类图
7-1 统计Java程序中关键词的出现次数
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Map<String, Integer> map = new TreeMap<>();
StringBuilder sb=new StringBuilder();
String[] str =
{"abstract", "assert", "boolean", "break", "byte",
"case", "catch", "char", "class", "const", "continue",
"default", "do", "double", "else", "enum", "extends", "false",
"final", "finally", "float", "for", "goto", "if", "implements", "import",
"instanceof", "int", "interface", "long", "native", "new", "null", "package",
"private", "protected", "public", "return", "short", "static", "strictfp", "super",
"switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "void",
"volatile", "while"};
String s;
int j=0;
for(int i=0;;i++) {
s=in.nextLine();
if(s.equals("exit"))
break;
if(s.matches("(.*)//(.*)"))//匹配单行注释
{
String[] b =s.split("//");//将注释存到b中
sb.append(b[0]).append(" ");
}
else
sb.append(s).append(" ");
}
int count=0;
String s1=sb.toString();
Pattern p=Pattern.compile("\"(.*?)\"");//“”引号里的字符
Matcher m=p.matcher(s1);
while(m.find()){
s1=s1.replace(m.group()," ");
p=Pattern.compile("\"(.*?)\"");
m=p.matcher(s1);
}
p=Pattern.compile("/\\*.*?\\*/");
m=p.matcher(s1);
while(m.find()){
s1=s1.replace(m.group()," ");
m=p.matcher(s1);
}
if(s1.isEmpty())
{System.out.println("Wrong Format");
System.exit(0);
}
s1=s1.replace("["," ");
s1=s1.replace("]"," ");
s1=s1.replace("-","a");
s1=s1.replace("*","a");
s1=s1.replace("/","a");
s1=s1.replace("+","a");
s1=s1.replace(">","a");
s1=s1.replace("=","a");
s1=s1.replace("!","a");
s1=s1.replace(":","a");
s1=s1.replace("\\","a");
s1= s1.replaceAll("[^a-zA-Z]", " ");
String []s2=s1.split("[ ' ']");
for(int i=0;i<s2.length;i++)
{
for( j=0;j<str.length;j++)
if(s2[i].equals(str[j]))
{
map.put(str[j], 0);
}
}
for( int i = 0;i<s2.length;i++)
{
for( j=0;j<str.length;j++)
if(s2[i].equals(str[j]))
{count=map.get(str[j]);
map.put(str[j], count+1);
}
}
Set<String> set=map.keySet();
Object[] arr=set.toArray();
Arrays.sort(arr);
for(Object i:arr)
System.out.println(map.get(i)+"\t"+i);
}
}
题目小结:主要是去掌握正则表达式的使用以及对字符串的处理,和hashmap自带检索功能的使用,难度还行,要思考怎么去替换,
7-1 容器-HashMap-检索
import java.util.*;
public class Main {
public static void main(String[] args) {
HashMap<String, Student1> map = new HashMap<>();
Scanner in = new Scanner(System.in);
while (true) {
String s = in.nextLine();
String[] s1 = s.split(" ");
if (s1[0].length() == 8) {
Student1 student1 = new Student1(s1[0], s1[1], s1[2]);
map.put(s1[0], student1);
}
if (s.equals("end")) {
break;
}
}
String s3 = in.nextLine();
if (map.get(s3) != null)
System.out.println(s3 + " " + map.get(s3).getName() + " " + map.get(s3).getScore());
else System.out.println("The student " + s3 + " does not exist");
}
static class Student1 {
String num;
String name;
String score;
public Student1() {
}
public Student1(String num, String name, String score) {
this.num = num;
this.name = name;
this.score = score;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getScore() {
return score;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student1 student1 = (Student1) o;
return Objects.equals(num, student1.num) && Objects.equals(name, student1.name) && Objects.equals(score, student1.score);
}
@Override
public int hashCode() {
return Objects.hash(num, name, score);
}
public void setScore(String score) {
this.score = score;
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
TreeMap<String, Student1> map = new TreeMap<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o2.compareTo(o1);
}
});
Scanner in = new Scanner(System.in);
while (true) {
String s = in.nextLine();
String[] s1 = s.split(" ");
if (s1[0].length() == 8) {
Student1 student1 = new Student1(s1[0], s1[1], s1[2]);
map.put(s1[0], student1);
}
if (s.equals("end")) {
break;
}
}
// String s3 = in.nextLine();
// if (map.get(s3) != null)
// System.out.println(s3 + " " + map.get(s3).getName() + " " + map.get(s3).getScore());
// else System.out.println("The student " + s3 + " does not exist");
Set<String> set = map.keySet();
for(String x:set)
System.out.println(map.get(x).getNum()+" "+map.get(x).getName()+" "+map.get(x).getScore());
}
static class Student1 {
String num;
String name;
String score;
public Student1() {
}
public Student1(String num, String name, String score) {
this.num = num;
this.name = name;
this.score = score;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getScore() {
return score;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student1 student1 = (Student1) o;
return Objects.equals(num, student1.num) && Objects.equals(name, student1.name) && Objects.equals(score, student1.score);
}
@Override
public int hashCode() {
return Objects.hash(num, name, score);
}
public void setScore(String score) {
this.score = score;
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Course[] course1 = new Course[10];
Student[] student = new Student[10];
int i = 0;
int a = 0;
int c = 0;
int b = 0;
Scanner in = new Scanner(System.in);
while (true) {
String s = in.nextLine();
String[] s1 = s.split(" ");
if (s1[0].matches("\\W+") || s1[0].matches("java")) {
course1[i] = new Course(s1[0], s1[1], s1[2], 0, 0);
c++;
}
if (s1[0].startsWith("2020")) {
student[a] = new Student();
student[a].setNum(s1[0]);
student[a].setName(s1[1]);
if (s1.length == 4) {
student[a].setLsatScore(Double.parseDouble(s1[3]));
} else {
student[a].setDailyScore(Double.parseDouble(s1[3]));
student[a].setLsatScore(Double.parseDouble(s1[4]));
}
if(course1[a].getStyle().equals("必修")&& course1[a].getPattern().equals("实验"))
System.out.println(course1[a].getCourseName()+" : course type & access mode mismatch\n" +
"wrong format");
if((course1[a].getStyle().equals("实验") && Integer.parseInt(s1[3])<4)||(course1[a].getStyle().equals("实验") && Integer.parseInt(s1[3])>9))
System.out.println("wrong format\n" +
course1[a].getCourseName() + " has no grades yet");
if (course1[a].getStyle().equals("实验") && s1.length != (Integer.parseInt(s1[3]) + 4))
System.out.print(student[a].getNum()+" "+student[a].getName() + " : access mode mismatch\n" +
student[a].getNum()+" "+student[a].getName() + " did not take any exams\n" +
course1[a].getCourseName()+ " has no grades yet\n" +
student[a].getNum().substring(0, 6) + " has no grades yet");
else if ((course1[a].getStyle().equals("必修") && course1[a].getPattern().equals("考察"))
||(course1[a].getStyle().equals("必修") && course1[a].getPattern().equals("考试")))
{
System.out.println(course1[a].getCourseName() + " : course type & access mode mismatch\n" +
course1[a].getCourseName() + " does not exist\n" +
student[a].getNum() + " " + student[a].getName() + " did not take any exams\n" +
student[a].getNum().substring(0, 6) + " has no grades yet");
} else {
if (student[a].getLsatScore() > 100)
System.out.println("wrong format\n" +
course1[a].getCourseName() + " has no grades yet");
else if (course1[a].getPattern().equals("考察")) {
course1[a].setLsatScore(Double.parseDouble(s1[3]));
if ((int) course1[a].getDailyScore() != 0)
System.out.println(student[a].getNum() + " " + student[a].getName() + " " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore) + "\n"
+ course1[a].getCourseName() + " " + (int) course1[a].getDailyScore() + " " + (int) course1[a].getLsatScore() +
" " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore) + "\n" + student[a].getNum().substring(0, 6) + " " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore)
);
else
System.out.println(student[a].getNum() + " " + student[a].getName() + " " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore) + "\n"
+ course1[a].getCourseName() + " " + (int) course1[a].getLsatScore() +
" " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore) + "\n" + student[a].getNum().substring(0, 6) + " " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore)
);
} else if (course1[a].getPattern().equals("考试")) {
course1[a].setDailyScore(Double.parseDouble(s1[3]));
if (s1.length == 4)
System.out.println(student[a].getNum() + " " + student[a].getName() + " : access mode mismatch\n" +
student[a].getNum() + " " + student[a].getName() + " did not take any exams\n" +
course1[a].getCourseName() + " has no grades yet\n" +
student[a].getNum().substring(0, 6) + " has no grades yet");
else {
course1[a].setLsatScore(Double.parseDouble(s1[4]));
if ((int) course1[a].getDailyScore() != 0)
System.out.println(student[a].getNum() + " " + student[a].getName() + " " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore) + "\n"
+ course1[a].getCourseName() + " " + (int) course1[a].getDailyScore() + " " + (int) course1[a].getLsatScore() +
" " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore) + "\n" + student[a].getNum().substring(0, 6) + " " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore)
);
else
System.out.println(student[a].getNum() + " " + student[a].getName() + " " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore) + "\n"
+ course1[a].getCourseName() + " " + (int) course1[a].getLsatScore() +
" " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore) + "\n" + student[a].getNum().substring(0, 6) + " " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore)
);
}
} else {
student[a].setNum(s1[0]);
student[a].setName(s1[1]);
course1[a].setCourseName(s1[2]);
}
}
a++;
}
i++;
if (s.equals("end")) {
break;
}
}
if (c == 3)
System.out.println("java has no grades yet\n" +
"数据结构 has no grades yet\n" +
"形式与政治 has no grades yet");
for (b = 0; b < i; b++) {
}
}
}
class Student extends Course {
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
String num;
String name;
Course[] course = new Course[10];
public int getSelfTotalScore(Course course, double dailyScore, double lsatScore) {
//一门课程
int n = 0;
if (course.getStyle().equals("必修课")) {
n = (int) (dailyScore * 0.3 + lsatScore * 0.7);
} else if (course.getPattern().equals("考察"))
n = (int) Math.floor(lsatScore);
else n = (int) (dailyScore * 0.3 + lsatScore * 0.7);
return n;
}
public int getAllSelfAverScore() {//所有课程的平均分
double aver = 0;
double aver1 = 0;
int i = 0;
for (i = 0; i < course.length; i++)
aver += getSelfTotalScore(course[i], course[i].dailyScore, course[i].lsatScore);
aver1 = aver / course.length;
return (int) aver1;
}
public int getAllDailySelfAverScore() {//所有课程的平均分
double aver = 0;
double aver1 = 0;
int i = 0;
for (i = 0; i < course.length; i++)
aver += course[i].dailyScore;
aver1 = aver / course.length;
return (int) aver1;
}
public int getAllLastSelfAverScore() {//所有课程的平均分
double aver = 0;
double aver1 = 0;
int i = 0;
for (i = 0; i < course.length; i++)
aver += course[i].lsatScore;
aver1 = aver / course.length;
return (int) aver1;
}
}
class Course {
String courseName;
int courseScore;
String style;
String pattern;
double dailyScore;
double lsatScore;
public Course() {
}
public Course(String courseName, String style, String pattern, double dailyScore, double lsatScore) {
this.courseName = courseName;
this.style = style;
this.pattern = pattern;
this.dailyScore = dailyScore;
this.lsatScore = lsatScore;
}
public double getDailyScore() {
return dailyScore;
}
public void setDailyScore(double dailyScore) {
this.dailyScore = dailyScore;
}
public double getLsatScore() {
return lsatScore;
}
public void setLsatScore(double lsatScore) {
this.lsatScore = lsatScore;
}
public int getCourseScore() {
return courseScore;
}
public void setCourseScore(int courseScore) {
this.courseScore = courseScore;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
}
class Class {
String classNum;
public String getClassNum() {
return classNum;
}
public void setClassNum(String classNum) {
this.classNum = classNum;
}
public int getClassAverScore() {
int i;
double aver = 0;
double aver1 = 0;
Student[] students = new Student[50];
for (i = 0; i < students.length; i++)
aver += students[i].getAllSelfAverScore();
aver1 = aver / students.length;
return (int) aver1;
}
}
7-4 动物发声模拟器(多态)
//动物发生模拟器. 请在下面的【】处添加代码。
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()
public static void speak(Animal animal){
animal.shout();
}
}
//定义抽象类Animal
abstract class Animal{
abstract void getAnimalClass();
abstract void shout();
}
//基于Animal类,定义猫类Cat,并重写两个抽象方法
class Cat extends Animal{
@Override
void getAnimalClass() {
}
@Override
void shout() {
System.out.println("猫的叫声:喵喵");
}
}
//基于Animal类,定义狗类Dog,并重写两个抽象方法
class Dog extends Animal{
@Override
void getAnimalClass() {
}
@Override
void shout() {
System.out.println("狗的叫声:汪汪");
}
}
//基于Animal类,定义山羊类Goat,并重写两个抽象方法
class Goat extends Animal{
@Override
void getAnimalClass() {
}
@Override
void shout() {
System.out.println("山羊的叫声:咩咩");
}
}
题目小结:其他题目都没什么难度,就是这次又有前一次课程系统的迭代,加上上次没写出来,以及当时时间四天,时间很紧,有很多测试点都没有实现
7-1 容器-ArrayList-排序
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<Student> arrayList = new ArrayList<Student>();
while (true) {
String s = in.nextLine();
String[] s1 = s.split(" ");
if(s1[0].startsWith("2020")){
Student student=new Student();
student.setName(s1[1]);
student.setSid(s1[0]);
student.setMathScore(Integer.parseInt(s1[2]));
student.setPhysicalScore(Integer.parseInt(s1[3]));
arrayList.add(student);
}
if (s1[0].equals("end"))
break;
}
arrayList.sort(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.getAllScore()-o1.getAllScore();
}
});
for(Student student:arrayList)
System.out.println(student.getSid()+" "+student.getName()+" "+student.getAllScore());
}
}
class Student {
String name;
String sid;
int physicalScore;
int mathScore;
public Student() {
}
public Student(String name, String sid, int physicalScore, int mathScore) {
this.name = name;
this.sid = sid;
this.physicalScore = physicalScore;
this.mathScore = mathScore;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public int getPhysicalScore() {
return physicalScore;
}
public void setPhysicalScore(int physicalScore) {
this.physicalScore = physicalScore;
}
public int getMathScore() {
return mathScore;
}
public void setMathScore(int mathScore) {
this.mathScore = mathScore;
}
public int getAllScore()
{
return mathScore+physicalScore;
}
}
7-2 课程成绩统计程序-3
7-3 jmu-Java-02基本语法-03-身份证排序 import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class Main { public static void main(String[] args) { ArrayList<String> arrayList1 = new ArrayList<>(); ArrayList<String> arrayList2 = new ArrayList<>(); Scanner in = new Scanner(System.in); int n = in.nextInt(); String[] strings = new String[n]; for (int i = 0; i < n; i++) { arrayList1.add(in.next()); strings[i] = arrayList1.get(i).substring(6, 14); arrayList2.add(strings[i]); } Collections.sort(arrayList1, new Comparator<String>() { @Override public int compare(String o1, String o2) { return Integer.parseInt(o1.substring(6, 14)) - Integer.parseInt(o2.substring(6, 14)); } }); Collections.sort(arrayList2, new Comparator<String>() { @Override public int compare(String o1, String o2) { return Integer.parseInt(o1.substring(0,8)) - Integer.parseInt(o2.substring(0, 8)); } }); String str; while (true) { str= in.next(); if (str.equals("e")) { System.out.println("exit"); break; } if(str.equals("sort1")) for (String s : arrayList2) { System.out.println(s.substring(0, 4) + "-" + s.substring(4, 6) + "-" + s.substring(6, 8)); } if(str.equals("sort2")) for (String s : arrayList1) { System.out.println(s); } } } } 7-4 jmu-Java-04面向对象进阶-03-接口-自定义接口ArrayIntegerStack import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); ArrayIntegerStack ais = new ArrayIntegerStack(n); int m = scanner.nextInt(); while(m-- > 0){ int item = scanner.nextInt(); System.out.println(ais.push(item)); } System.out.println(ais.peek() + "," + ais.empty() + "," + ais.size()); System.out.println(ais); int x = scanner.nextInt(); while(x-- > 0){ System.out.println(ais.pop()); } System.out.println(ais.peek() + "," + ais.empty() + "," + ais.size()); System.out.println(ais); } } interface IntegerStack { Integer push(Integer item); //如果item为null,则不入栈直接返回null。如果栈满,也返回null。如果插入成功,返回item。 Integer pop(); //出栈,如果为空,则返回null。出栈时只移动栈顶指针,相应位置不置为null Integer peek(); //获得栈顶元素,如果为空,则返回null. boolean empty(); //如果为空返回true int size(); //返回栈中元素个数 } class ArrayIntegerStack implements IntegerStack { private Integer[] arr; private int top = 0; public ArrayIntegerStack(int n){ arr = new Integer[n]; Arrays.fill(arr, null); } public ArrayIntegerStack(){} @Override public String toString() { return Arrays.toString(arr); } @Override public Integer push(Integer item) { if (item == null || arr.length == top){ return null; } arr[top++] = item; return item; } @Override public Integer pop() { if (top == 0){ return null; } return arr[--top]; } @Override public Integer peek() { if (top == 0){ return null; } return arr[top - 1]; } @Override public boolean empty() { return top == 0; } @Override public int size() { return top; } } 7-5 jmu-Java-03面向对象基础-05-覆盖 import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); ArrayIntegerStack ais = new ArrayIntegerStack(n); int m = scanner.nextInt(); while(m-- > 0){ int item = scanner.nextInt(); System.out.println(ais.push(item)); } System.out.println(ais.peek() + "," + ais.empty() + "," + ais.size()); System.out.println(ais); int x = scanner.nextInt(); while(x-- > 0){ System.out.println(ais.pop()); } System.out.println(ais.peek() + "," + ais.empty() + "," + ais.size()); System.out.println(ais); } } interface IntegerStack { Integer push(Integer item); //如果item为null,则不入栈直接返回null。如果栈满,也返回null。如果插入成功,返回item。 Integer pop(); //出栈,如果为空,则返回null。出栈时只移动栈顶指针,相应位置不置为null Integer peek(); //获得栈顶元素,如果为空,则返回null. boolean empty(); //如果为空返回true int size(); //返回栈中元素个数 } class ArrayIntegerStack implements IntegerStack { private Integer[] arr; private int top = 0; public ArrayIntegerStack(int n){ arr = new Integer[n]; Arrays.fill(arr, null); } public ArrayIntegerStack(){} @Override public String toString() { return Arrays.toString(arr); } @Override public Integer push(Integer item) { if (item == null || arr.length == top){ return null; } arr[top++] = item; return item; } @Override public Integer pop() { if (top == 0){ return null; } return arr[--top]; } @Override public Integer peek() { if (top == 0){ return null; } return arr[top - 1]; } @Override public boolean empty() { return top == 0; } @Override public int size() { return top; } } 总结 我深刻意识到了“迭代”的可怕,真是一环扣一环,前面的题目做不出来会严重后面题目以及得分, 所以我要深刻检讨自己的畏难以及从众心理,合理规划时间,端正自己的学习态度,花更多时间和精力完成题目(不然迭代会更痛苦)。 在做复杂题目时,先去花时间理解题目意思会有很好的效果,要提前做好类的设计,以及在设计类功能时,要思考全面,确保能够实现要求
标签:第三阶段,题目,String,int,s1,PTA,course1,student,public From: https://www.cnblogs.com/zzjw/p/17509787.html