Blog作业2
一前言:
1. 第四次作业的难度还是比较大的,较为简单的题目只有第三题,运用的知识比较的简单。其他两道题就比较复杂了。尤其是第一题,运用了正则表达式,我对正则表达式不太熟悉,所以这道题对于我来说难度还是挺大的。
2. 从第五次作业开始难度就开始下了,题目就稍微增加了一点。其实我感觉这样也挺好的,题目难度适中,方便我们更好的掌握这类题目,我们会更容易去吸收里面的知识。有助于我们掌握这门高级语言,其实还会增加我们学好这门语言的信心。
二设计与分析:
import java.util.Scanner;
class Shape{
Shape(){
System.out.println("Constructing Shape");
}
public double getArea() {
return 0.0;
}
}
class Circle extends Shape{
private double radius;
Circle() {
System.out.println("Constructing Circle");
}
@Override
public double getArea() {
return radius*radius*Math.PI ;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
}
class Rectangle extends Shape{
private double width,length;
Rectangle() {
System.out.println("Constructing Rectangle");
}
@Override
public double getArea() {
return width*length;
}
public void setWidth(double width) {
this.width = width;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public double getLength() {
return length;
}
}
class Ball extends Circle{
Ball(){
System.out.println("Constructing Ball");
}
@Override
public double getArea() {
return super.getArea()*4;
}
public double getVolume() {
return getArea()/3*getRadius();
}
}
class Box extends Rectangle{
private double height;
Box(){
System.out.println("Constructing Box");
}
@Override
public double getArea() {
return 2*(getWidth()*getLength()+getWidth()*getHeight()+getLength()*getHeight());
}
public double getVolume() {
return super.getArea()*height;
}
public void setHeight(double height) {
this.height = height;
}
public double getHeight() {
return height;
}
}
public class Main {
private static Scanner type;
public static void main(String[] args) {
type = new Scanner(System.in);
int a=type.nextInt();
if(a>4||a<1) {
System.out.println("Wrong Format");
return;
}
if(a==1) {
double b=type.nextDouble();
if(b<0) {
System.out.println("Wrong Format");
return;
}
Circle c=new Circle();
c.setRadius(b);
System.out.println("Circle's area:"+String.format("%.2f",c.getArea()));
}
if(a==2) {
double b=type.nextDouble();
double c=type.nextDouble();
if(b<0||c<0) {
System.out.println("Wrong Format");
return;
}
Rectangle d=new Rectangle();
d.setWidth(b);
d.setLength(c);
System.out.println("Rectangle's area:"+String.format("%.2f",d.getArea()));
}
if(a==3) {
double b=type.nextDouble();
if(b<0) {
System.out.println("Wrong Format");
return;
}
Ball c=new Ball();
c.setRadius(b);
System.out.println("Ball's surface area:"+String.format("%.2f",c.getArea()));
System.out.println("Ball's volume:"+String.format("%.2f",c.getVolume()));
}
if(a==4) {
double b=type.nextDouble();
double c=type.nextDouble();
double d=type.nextDouble();
if(b<0||c<0||d<0) {
System.out.println("Wrong Format");
return;
}
Box e=new Box();
e.setHeight(b);
e.setLength(c);
e.setWidth(d);
System.out.println("Box's surface area:"+String.format("%.2f",e.getArea()));
System.out.println("Box's volume:"+String.format("%.2f",e.getVolume()));
}
}
}
这是第四次作业的第三题,本题目的难度一般,运用类的继承的知识来解决这到题目,算是对类的继承的一个比较好的运用,难度一般,可以让我们更好的巩固类的继承这个知识。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year = 0;
int month = 0;
int day = 0;
int choice = input.nextInt();
if (choice == 1) { // test getNextNDays method
int m = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());
DateUtil date = new DateUtil(year, month, day);
if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}
m = input.nextInt();
if (m < 0) {
System.out.println("Wrong Format");
System.exit(0);
}
}
}
}
class DateUtil {
private int year;
private int month;
private int day;
public DateUtil(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public DateUtil(){}
public void setYear(int year) {
this.year = year;
}
public void setMonth(int month) {
this.month = month;
}
public void setDay(int day) {
this.day = day;
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
private final int[] DAY_OF_MONTH = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
private int getDayOfMonth(int year, int month) {
int days = DAY_OF_MONTH[month - 1];
if (month == 2 && isLeapYear(year)) {
days = 29;
}
return days;
}
public boolean checkInputValidity()//检测输入的年、月、日是否合法
{
if (year < 1820 || year > 2020) return false;
if (month < 1 || month > 12) return false;
// int _day = this.getDayOfMonth(year, month);
// return day <= _day;
return day >= 1 && day <= 31;
}
public boolean isLeapYear(int year)//判断year是否为闰年
{
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
public DateUtil getNextNDays(int n)//取得year-month-day的下n天日期
{
int year = this.year;
int month = this.month;
int day = this.day;
// day = Math.min(day, this.getDayOfMonth(year, month));
for (int i = 0; i < n; i++) {
day++;
if (day > getDayOfMonth(year, month)) {
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
}
}
}
return new DateUtil(year, month, day);
}
public DateUtil getPreviousNDays(int n)//取得year-month-day的前n天日期
{
int year = this.year;
int month = this.month;
int day = this.day;
for (int i = 0; i < n; i++) {
day--;
while (day < 1) {
month--;
if (month < 1) {
month = 12;
year--;
}
day += getDayOfMonth(year, month);
}
}
return new DateUtil(year, month, day);
}
public boolean compareDates(DateUtil date)//比较当前日期与date的大小(先后)
{
if (this.year > date.year) return true;
if (this.year == date.year) {
if (this.month > date.month) return true;
if (this.month == date.month) {
if (this.day >= date.day) return true;
}
}
return false;
}
public boolean equalTwoDates(DateUtil date)//判断两个日期是否相等
{
if (date != null) {
if (year == date.year && month == date.month && day == date.day) {
return true;
}
}
return false;
}
private static final int[] mon = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
public int getDaysofDates(DateUtil date)//求当前日期与date之间相差的天数
{
DateUtil dateUtil1 = this; // 小
DateUtil dateUtil2 = date; // 大
if (this.compareDates(date)) {
dateUtil1 = date;
dateUtil2 = this;
}
int days;
int leapYearNum = 0;
for (int i = dateUtil1.getYear(); i < dateUtil2.getYear(); i++) {
if (isLeapYear(i)) {
leapYearNum++;
}
}
days = 365 * (dateUtil2.getYear() - dateUtil1.getYear()) + leapYearNum;
int d1 = mon[dateUtil1.getMonth() - 1] + dateUtil1.getDay() + (dateUtil1.getMonth() > 2 && isLeapYear(dateUtil1.getYear()) ? 1 : 0);
int d2 = mon[dateUtil2.getMonth() - 1] + dateUtil2.getDay() + (dateUtil2.getMonth() > 2 && isLeapYear(dateUtil2.getYear()) ? 1 : 0);
return days - d1 + d2;
}
public String showDate()//以“year-month-day”格式返回日期值
{
return year + "-" + month + "-" + day;
}
}
这是第四次作业的第二题,有以下三种输入格式
- 1 year month day n //测试输入日期的下n天
- 2 year month day n //测试输入日期的前n天
- 3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数
- 要写出这道题的难度还是比较大的,思维要非常的缜密,写出这道题要花费大量的时间。所以要学好这门高级语言,我们要投入大量的时间去深度学习这门课程。
-
import java.util.Scanner; public class Main { //记录是否全为合法数据 static int mark=0; public static void main(String[] args) { Scanner in=new Scanner(System.in); String s=null; int cnt=0; //记录总流量 double sum=0; //记录最大实际水位 double mmax=0; while(!(s=in.nextLine()).equals("exit")){ //记录行数 cnt++; if(s.length()==0) continue; //预处理字符串,返回分割后字符串数组 String [] arrayString=Preprocessing(s); //检验整体合法 if(!checkArray(arrayString)){ System.out.println("Wrong Format"); System.out.println("Data:"+s); continue; } //如果分隔合法,则对各数据进行分别进行合法检验,如果返回值为真进行,加入总流量中 if(checkArrayAll(arrayString,cnt,s)) { sum += Double.valueOf(arrayString[4]) * 60 * 60 * 2; mmax= Math.max(mmax,Double.valueOf(arrayString[2])); } } if(mark==0){ System.out.printf("Max Actual Water Level:%.2f\n",mmax); System.out.printf("Total Water Flow:%.2f\n",sum); } } //去除两端空格,同时根据|来切开字符串,检验该数据是否是由|分开的5部分 public static String []Preprocessing(String s){ return s.trim().split("\\|"); } //对返回字符串数组进行检验 public static boolean checkArray(String[] s){ if(s.length!=5) return false; return true; } public static boolean checkArrayAll(String[] s,int rows,String data){ boolean flag=true; for(int i=0;i<=4;i++) s[i]=s[i].trim(); //去除多余空格 String test=s[0]; //检查日期时间合法 /* 格式为“年/月/日 时:分”,其中年份取值范围为[1,9999], “月”与“日”为一位数时之前不加“0”,日期与时间之间有一个空格,“时”与“分”之间采用冒号分隔(英文半角), “时”为一位数时之前不加“0”, “分”始终保持两位,“秒”始终为“00”。注意:“时”数必须是24小时进制中的偶数值。 */ //时间正则 String time="(?:(?:( [1-9]| 1[0-9]| 2[0-3]| 00):00))"; //日期正则,空格也会有影响,注意空格 // /所有年份 /1-12都有1-28 /1,3,5,7,8,10,12都有1-31天 /除2月外 都有29-30天 String regex1="(?:((?:([1-9]([0-9]{0,3}))/((?:(([1-9]|(1[0-2]))/(?:([1-9]|([1-2][0-8])|19))))|(?:(([13578])|([1][02])(/31)))|(?:(([13-9]|1[02])/(29|30)))))(?:(?:( [02468]| 1[02468]| 2[02]|):00))))"; //1或2位闰年 /3,4位闰年 //400年一润,百年不润 String regex2="(?:((?:([48]|[2468][048]|[13579][26]))|((?:(([1-9]([0-9]?))?:(0[48]|[2468][048]|[13579][26])))|(?:([48]|[2468][048]|[13579][26])00))/2/29)(?:(?:( [02468]| 1[02468]| 2[02]|):00)))"; String regexend=regex1+"|"+regex2; if(!test.matches(regexend)) {System.out.printf("Row:%d,Column:%dWrong Format\n",rows,1);flag=false;} //检查水位数据合法 test=s[1]; //水位数据正则 目标水位、实际水位、流量:均为实型数,取值范围为[1,1000), 小数点后保留1-3位小数或无小数(也无小数点) String water ="(?:(?:(([1-9]([0-9]{0,2})))(?:((.[0-9]{1,3})?))))"; if(!test.matches(water)){System.out.printf("Row:%d,Column:%dWrong Format\n",rows,2);flag=false;} test=s[2]; if(!test.matches(water)){System.out.printf("Row:%d,Column:%dWrong Format\n",rows,3);flag=false;} //检查开度 test=s[3]; int id=test.indexOf("/"); String test1=test.substring(0,id); //目标开度 String test2=test.substring(id+1,test.length());//实际开度 String hot="(?:(([1-9])(?:(.[0-9]{2}))))"; if(!test1.matches(hot)){System.out.printf("Row:%d,Column:%dWrong Format\n",rows,4);flag=false;} if(!test2.matches(hot)){System.out.printf("Row:%d,Column:%dWrong Format\n",rows,5);flag=false;} //检查流量合法 test=s[4]; if(!test.matches(water)){System.out.printf("Row:%d,Column:%dWrong Format\n",rows,6);flag=false;} //如果开度输入合法但大小不合法 double target=Double.valueOf(test1); double actual=Double.valueOf(test2); if(flag) if (actual > target) System.out.printf("Row:%d GateOpening Warning\n",rows); //如果存在不合法的数据,打印一次该数据未处理前的原数据 if(flag==false) System.out.println("Data:"+data); if(flag==false) mark=1; return flag; } -
这是第四次作业的第一题,由于水平有限,所以这道题我不怎么会做,但是我希望通过这道题意识到正则表达式在Java中的重要作用,让我以后对这个好好反思,认真学习好正则表达式
-
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {/*
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
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);String a;
StringBuilder ss=new StringBuilder();
Map<String, Integer> map=new HashMap<String, Integer>();
String []key= { "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"
};
int j=0;
for(int i=0;;i++) {
a=input.nextLine();
if(a.equals("exit"))
break;
if(a.matches("(.*)//(.*)"))
{String b[]=a.split("//");
ss.append(b[0]+" ");
//ss.append('\n');
}
else
{ss.append(a+" ");
//ss.append('\n');
}
}
int count=0;
String s=ss.toString();
// System.out.println(s);
Pattern p=Pattern.compile("\"(.*?)\"");
Matcher m=p.matcher(s);
while(m.find()){
s=s.replace(m.group()," ");
p=Pattern.compile("\"(.*?)\"");
m=p.matcher(s);
}
p=Pattern.compile("/\\**(.*?)/");
m=p.matcher(s);
while(m.find()){
s=s.replace(m.group()," ");
// p=Pattern.compile("/\\*(.*?)\\*/");
m=p.matcher(s);
}
// System.out.println(s);
if(s.isEmpty())
{System.out.println("Wrong Format");
System.exit(0);
}
s=s.replace("["," ");
s=s.replace("]"," ");
s=s.replace("-","a");
s=s.replace("*","a");
s=s.replace("/","a");
s=s.replace("+","a");
s=s.replace(">","a");
s=s.replace("=","a");
s=s.replace("!","a");
s=s.replace(":","a");
s=s.replace("\\","a");
s= s.replaceAll("[^a-zA-Z]", " ");
String []s1=s.split("[ ' ']");
for(int i=0;i<s1.length;i++)
{//System.out.println(s1[i]);
for( j=0;j<key.length;j++)
if(s1[i].equals(key[j]))
{
map.put(key[j], 0);
}
}
for( int i = 0;i<s1.length;i++)
{
for( j=0;j<key.length;j++)
if(s1[i].equals(key[j]))
{ count=map.get(key[j]);
map.put(key[j], count+1);
}
}
Set set=map.keySet();
Object[] arr=set.toArray();
Arrays.sort(arr);
for(Object k:arr){
System.out.println(map.get(k)+"\t"+k);
}
}
}这是第五次作业的第四题,本题主要是让我们统计Java程序中关键词的出现次数,这道题的难度还是有一点的,但是也不能说是太难,同样的本题也需要运用正则表达式,但相对与前面那题来说,本题的难度就不算那么变态,所以我觉得这个难度可以帮助我们理解好正则表达式的作业
-
三踩坑心得:我感觉我没有完全投入到java的学习中,这让我写第二次作业和第三次作业显得非常吃力。这几次作业也是出现了非常多的错误
测试永远是检查一个代码是否有bug的最简洁的方法。
四改进建议:希望以后自己会跟加的小心,话一些时间来测试bug
五总结:这三次作业让我清楚的看到了自己的实力,也让我下定决心要学好java这门语言,不能浪费自己的时间。要把以前自己不会的都要补上来,努力学习好这门课程。
------------恢复内容结束------------
------------恢复内容结束------------
------------恢复内容结束------------
浙公网安备 33010602011771号