大一下8,9,10次PTA作业总结

(1)前言:

知识点:多态,内部类,类的设计等。

题量:较少。

难度:较小,关于电信计费的题目是迭代式作业,如果前几次设计合理,后期会比较简单。

(2)设计与分析:

1.动物发声模拟器,比较简单,代码基本全部给出,只需在相关部分添加即可

 

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 public void speak(Animal animal) {
	  System.out.print(animal.getAnimalClass()+"的叫声:");
	  animal.shout();
  }

}

//定义抽象类Animal
abstract class Animal{
	
  abstract public String getAnimalClass() ;
  abstract public void shout();
}
//基于Animal类,定义猫类Cat,并重写两个抽象方法
class Cat extends Animal{

	@Override
	public String getAnimalClass() {
		return "猫";
	}

	@Override
	public void shout() {
		System.out.println("喵喵");
		
	}
  
}
//基于Animal类,定义狗类Dog,并重写两个抽象方法
class Dog extends Animal{

	@Override
	public String getAnimalClass() {
		return "狗";
	}

	@Override
	public void shout() {
		System.out.println("汪汪");
		
	}

}
//基于Animal类,定义山羊类Goat,并重写两个抽象方法
class Goat extends Animal{

	@Override
	public String getAnimalClass() {
		return "山羊";
	}

	@Override
	public void shout() {
		System.out.println("咩咩");
		
	}
  
}

2.内部类

 也是提示很详尽。

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		Shop shop = new Shop();
		int count = in.nextInt();
		shop.setMilkCount(count);
		shop.coupons50.buy();
		System.out.println("牛奶还剩"+shop.getMilkCount()+"箱");
		shop.coupons100.buy();
		System.out.println("牛奶还剩"+shop.getMilkCount()+"箱");

	}

}
class Shop{
	private int milkCount;
	public InnerCoupons coupons50 = new InnerCoupons(50);
	public InnerCoupons coupons100 = new InnerCoupons(100);
	class InnerCoupons{
		public int value;
		public InnerCoupons(int value) {
			this.value = value;
		}
		public void buy( ) {
			System.out.println("使用了面值为"+this.value+"的购物券进行支付");
			setMilkCount(getMilkCount() - this.value/50);
		}
	}
	public int getMilkCount() {
		return milkCount;
	}
	public void setMilkCount(int milkCount) {
		this.milkCount = milkCount;
	}
	
}

内部类是这一段

 内部类能调用外部内的属性从,外部类使用非静态内部类需实例化。

3.电信计费,迭代式,前期没有设计好很容易崩。

题目当时把大部分类图贴出来了。

 

 

 这是部分代码

package pta08;

import java.util.Date;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.regex.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.lang.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;


 class CallRecord extends CommunicationRecord{//通话记录类
	private Date startTime;//开始时间
	private Date endTime;//结束时间
	private String callingAddressAreaCode;//拨打电话区号
	private String answerAddressAreaCode;//接听电话区号

	public Date getStartTime() {
		return this.startTime;
	}
	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}
	public Date getEndTime() {
		return endTime;
	}
	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
	public String getCallingAddressAreaCode() {
		return callingAddressAreaCode;
	}
	public void setCallingAddressAreaCode(String callingAddressAreaCode) {
		this.callingAddressAreaCode = callingAddressAreaCode;
	}
	public String getAnswerAddressAreaCode() {
		return answerAddressAreaCode;
	}
	public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
		this.answerAddressAreaCode = answerAddressAreaCode;
	}

}

class MessageRecord extends CommunicationRecord{
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}

abstract class CommunicationRecord {//抽象的通讯记录类
	protected String callingNumber;//拨打号码
	protected String answerNummber;//接听号码

	public String getCallingNumber() {
		return callingNumber;
	}
	public void setCallingNumber(String callingNumber) {
		this.callingNumber = callingNumber;
	}
	public String getAnswerNummber() {
		return answerNummber;
	}
	public void setAnswerNummber(String answerNummber) {
		this.answerNummber = answerNummber;
	}


}
 abstract class ChargeMode{//计费方式的抽象类
	private  ArrayList<ChargeRule> chargeRules = new ArrayList<>();

	public ArrayList<ChargeRule> getChargeRules(){
		return this.chargeRules;
	}
	public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
		this.chargeRules = chargeRules;
	}
	public abstract double calCost(UserRecords userRecords);
	public abstract double getMonthlyRent();
}

class LandlinePhoneCharging extends ChargeMode{
	private double monthlyRent = 20;//月租20元

	@Override
	public double getMonthlyRent() {
		return this.monthlyRent;
	}

	@Override
	public double calCost(UserRecords userRecords) {///
		// TODO Auto-generated method stub
		return 0;
	}


}

class UserRecords{//用户记录类
	private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
	private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
	private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
	private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
	private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
	private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
	private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
	private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();

	public void addCallingInCityRecords(CallRecord callRecord) {
		this.callingInCityRecords.add(callRecord);
	}
	public void addCallingInProvinceRecords(CallRecord callRecord) {
		this.callingInProvinceRecords.add(callRecord);
	}
	public void addCallingInLandRecords(CallRecord callRecord) {
		this.callingInLandRecords.add(callRecord);
	}
	public void addAnswerInCityRecords(CallRecord callRecord) {
		this.answerInCityRecords.add(callRecord);
	}
	public void AnswerCallingInProvinceRecords(CallRecord callRecord) {
		this.answerInProvinceRecords.add(callRecord);
	}
	public void AnswerCallingInLandRecords(CallRecord callRecord) {
		this.answerInLandRecords.add(callRecord);
	}
	public ArrayList<MessageRecord> getSendMessageRecords(){
		return this.sendMessageRecords;
	}
	public ArrayList<MessageRecord> getReciveMessageRecords(){
		return this.receiveMessageRecords;
	}
	public ArrayList<CallRecord> getCallingInCityRecords(){
		return this.callingInCityRecords;
	}
	public ArrayList<CallRecord> getCallingInProvinceRecords(){
		return this.callingInProvinceRecords;
	}
	public ArrayList<CallRecord> getCallingInLandRecords(){
		return this.callingInLandRecords;
	}
	public ArrayList<CallRecord> getAnswerInCityRecords(){
		return this.answerInCityRecords;
	}
	public ArrayList<CallRecord> getAnswerInProvinceRecords(){
		return this.answerInProvinceRecords;
	}
	public ArrayList<CallRecord> getAnswerInLandRecords(){
		return this.answerInLandRecords;
	}

}

class User{
	private UserRecords userRecords = new UserRecords();//用户记录
	private double balande = 100;//余额
	private ChargeMode chargeMode ;//计费方式
	private String number;//用户号码
	public UserRecords getUserRecords() {
		return userRecords;
	}
	public void setUserRecords(UserRecords userRecords) {
		this.userRecords = userRecords;
	}
	public ChargeMode getChargeMode() {
		return chargeMode;
	}
	public void setChargeMode(ChargeMode chargeMode) {
		this.chargeMode = chargeMode;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}

	public double calBalance() {//返回余额
		this.calCost();
		this.balande -= this.calCost();
		LandlinePhoneCharging month = new LandlinePhoneCharging();
		this.balande -= month.getMonthlyRent();
		//减
		return this.balande;

	}
	public double calCost() {//返回所扣话费
		double x1,x2,x3,x4;
		//计算市内所扣话费
		LandPhoneInCityRule landphoneincityrule = new LandPhoneInCityRule();
		x1 = landphoneincityrule.calCost(this.getUserRecords().getCallingInCityRecords());
		//计算省内所扣话费
		LandPhoneInProvinceRule landphoneinprovincerule = new LandPhoneInProvinceRule();
		x2 = landphoneinprovincerule.calCost(this.getUserRecords().getCallingInProvinceRecords());
		//计算国内所扣话费
		LandPhoneInLandRule landphoneinlandrule = new LandPhoneInLandRule();
		x3 = landphoneinlandrule.calCost(this.getUserRecords().getCallingInLandRecords());

		//加起来,返回
		return (x1+x2+x3);
	}


}

abstract class ChargeRule {//计费方式所包含的各种各种计费规则的集合

}

abstract class CallChargeRule extends ChargeRule{//电话计费规则?
	public abstract double calCost(ArrayList<CallRecord> callRecords);
	//该方法针根据输入参数callRecords中的所有记录计算某用户的“某一项”费用;如市话费。
	//输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。
}

class LandPhoneInCityRule extends CallChargeRule{//座机拨打市内电话的计费规则类

	@Override
	public double calCost(ArrayList<CallRecord> callRecords) {
		double cost = 0;
		long diff = 0;
		for(CallRecord callrecord:callRecords) {
			diff =( callrecord.getEndTime().getTime()-callrecord.getStartTime().getTime() )/1000;//秒
			if(diff%60>0)
				diff = 1 +diff/60;
			else
				diff = diff/60;
			cost += diff;
		}
		cost = cost*0.1;
		return cost;
	}

}

class LandPhoneInLandRule extends CallChargeRule{//座机拨打省外电话的计费规则类

	@Override
	public double calCost(ArrayList<CallRecord> callRecords) {
		double cost = 0;
		long diff = 0;
		for(CallRecord callrecord:callRecords) {
			diff =( callrecord.getEndTime().getTime()-callrecord.getStartTime().getTime() )/1000;
			if(diff%60>0)
				diff = 1 +diff/60;
			else
				diff = diff/60;
			cost += diff;
		}
		cost = cost*0.6;
		return cost;
	}

}

class LandPhoneInProvinceRule extends CallChargeRule{//座机拨打省内电话的计费规则类

	@Override
	public double calCost(ArrayList<CallRecord> callRecords) {

		double cost = 0;
		long diff = 0;
		for(CallRecord callrecord:callRecords) {
			diff =( callrecord.getEndTime().getTime()-callrecord.getStartTime().getTime() )/1000;
			if(diff%60>0)
				diff = 1 +diff/60;
			else
				diff = diff/60;
			cost += diff;
		}
		cost = cost*0.3;
		return cost;
	}

}

class CellPhoneInCityRule_1 extends CallChargeRule{//1类,市内手机拨打,接听为市内,0.1

	@Override
	public double calCost(ArrayList<CallRecord> callRecords) {
		double cost = 0;
		long diff = 0;
		for(CallRecord callrecord:callRecords) {
			diff =( callrecord.getEndTime().getTime()-callrecord.getStartTime().getTime() )/1000;//秒
			if(diff%60>0)
				diff = 1 +diff/60;
			else
				diff = diff/60;
			cost += diff;
		}
		cost = cost*0.1;
		return cost;
	}

}
class CellPhoneInCityRule_2 extends CallChargeRule{//1类,市内手机拨打,接听为省内,0.2

	@Override
	public double calCost(ArrayList<CallRecord> callRecords) {
		double cost = 0;
		long diff = 0;
		for(CallRecord callrecord:callRecords) {
			diff =( callrecord.getEndTime().getTime()-callrecord.getStartTime().getTime() )/1000;//秒
			if(diff%60>0)
				diff = 1 +diff/60;
			else
				diff = diff/60;
			cost += diff;
		}
		cost = cost*0.1;
		return cost;
	}

}
class CellPhoneInCityRule_3 extends CallChargeRule{//1类,市内手机拨打,接听为国内,0.3

	@Override
	public double calCost(ArrayList<CallRecord> callRecords) {
		double cost = 0;
		long diff = 0;
		for(CallRecord callrecord:callRecords) {
			diff =( callrecord.getEndTime().getTime()-callrecord.getStartTime().getTime() )/1000;//秒
			if(diff%60>0)
				diff = 1 +diff/60;
			else
				diff = diff/60;
			cost += diff;
		}
		cost = cost*0.3;
		return cost;
	}

}
class CellPhoneInProvinceRule extends CallChargeRule{//2类,省内拨打

	@Override
	public double calCost(ArrayList<CallRecord> callRecords) {
		double cost = 0;
		long diff = 0;
		for(CallRecord callrecord:callRecords) {
			diff =( callrecord.getEndTime().getTime()-callrecord.getStartTime().getTime() )/1000;//秒
			if(diff%60>0)
				diff = 1 +diff/60;
			else
				diff = diff/60;
			cost += diff;
		}
		cost = cost*0.3;
		return cost;
	}

}
class CellPhoneInCountryRule extends CallChargeRule{//3.国内拨打

	@Override
	public double calCost(ArrayList<CallRecord> callRecords) {
		double cost = 0;
		long diff = 0;
		for(CallRecord callrecord:callRecords) {
			diff =( callrecord.getEndTime().getTime()-callrecord.getStartTime().getTime() )/1000;//秒
			if(diff%60>0)
				diff = 1 +diff/60;
			else
				diff = diff/60;
			cost += diff;
		}
		cost = cost*0.6;
		return cost;
	}

}

class CellReserveInCountry extends CallChargeRule{//4.手机,被拨打电话在国内

	@Override
	public double calCost(ArrayList<CallRecord> callRecords) {
		double cost = 0;
		long diff = 0;
		for(CallRecord callrecord:callRecords) {
			diff =( callrecord.getEndTime().getTime()-callrecord.getStartTime().getTime() )/1000;//秒
			if(diff%60>0)
				diff = 1 +diff/60;
			else
				diff = diff/60;
			cost += diff;
		}
		cost = cost*0.3;
		return cost;
	}

}

//下面是两个没用的类
class CellPhoneCalledInLandRule extends CallChargeRule{//5.省外漫游接听,被拨打电话在省外,拨打电话随意0.3元

	@Override
	public double calCost(ArrayList<CallRecord> callRecords) {
		double cost = 0;
		long diff = 0;
		for(CallRecord callrecord:callRecords) {
			diff =( callrecord.getEndTime().getTime()-callrecord.getStartTime().getTime() )/1000;//秒
			if(diff%60>0)
				diff = 1 +diff/60;
			else
				diff = diff/60;
			cost += diff;
		}
		cost = cost*0.3;
		return cost;
	}

}
class CellPhoneInLbyLandRule extends CallChargeRule{//6.省外蛮有拨打,拨打电话在省外,0.6元

	@Override
	public double calCost(ArrayList<CallRecord> callRecords) {
		double cost = 0;
		long diff = 0;
		for(CallRecord callrecord:callRecords) {
			diff =( callrecord.getEndTime().getTime()-callrecord.getStartTime().getTime() )/1000;//秒
			if(diff%60>0)
				diff = 1 +diff/60;
			else
				diff = diff/60;
			cost += diff;
		}
		cost = cost*0.6;
		return cost;
	}

}

class Judge {
	TreeMap<String,User> userMap = new TreeMap<String,User>();//创建一个treemap,前面的String是开户号码,便于后面输出时排序
	Scanner in = new Scanner(System.in);
	String str = in.nextLine();//用来接收所有信息
	User user;
	ArrayList<String> sn_u = new ArrayList<>();
	ArrayList<String> sn_t = new ArrayList<>();
	boolean isMatch = false;
	boolean isAccount = false;//是否已开户,假设未开户
	String pattern1 = "u-(\\d{4})(\\d{7,8})\\s(0)";
	//正则表达式1   //u-079186300001 0
	String pattern2 = "t-(\\d{4})(\\d{7,8}) (\\d{4})(\\d{7,8}) (\\d{4}.\\d{1,2}.\\d{1,2}) (\\d{2}:\\d{2}:\\d{2}) (\\d{4}.\\d{1,2}.\\d{1,2}) (\\d{2}:\\d{2}:\\d{2})";
	//正则表达式2:两个座机   //t-079186300001 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:25
	String pattern3 = "t-(\\d{4}) (\\d{7,8}) (\\d{7,8}) (\\d{3,4}) (\\d{4}.\\d{1,2}.\\d{1,2}) (\\d{2}:\\d{2}:\\d{2}) (\\d{4}.\\d{1,2}.\\d{1,2}) (\\d{2}:\\d{2}:\\d{2})";
	//正则表达式3:座机打手机		//t-079186330022 13305862264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
	String pattern4 = "t-(\\d{7,8}) (\\d{3,4}) (\\d{7,8}) (\\d{3,4}) (\\d{4}.\\d{1,2}.\\d{1,2}) (\\d{2}:\\d{2}:\\d{2}) (\\d{4}.\\d{1,2}.\\d{1,2}) (\\d{2}:\\d{2}:\\d{2})";
	//正则表达式4:手机互打	//t-18907910010 0791 13305862264 0371 2022.1.3 10:00:25 2022.1.3 10:05:11

	Pattern r1 = Pattern.compile(pattern1);//创建pattern对象
	Pattern r2 = Pattern.compile(pattern2);
	Pattern r3 = Pattern.compile(pattern3);
	Pattern r4 = Pattern.compile(pattern4);
	Matcher m1 = r1.matcher(str);//创建了一个matcher对象
	Matcher m2 = r2.matcher(str);
	Matcher m3 = r3.matcher(str);
	Matcher m4 = r4.matcher(str);

	public void judge() {



		for(int i = 0;!str.equals("end");i++) {//用来接收输入
			if( Pattern.matches(pattern1, str) ) {//u-079186300001 0
				m1.find();
				//是否已开户
				for(String s : sn_u) {
					if( s.equals(m1.group(1)+m1.group(2)) ) //该号码已经开户
						isAccount = true;
				}
				//开户
				if(!isAccount) {
					sn_u.add(str);
					user = new User();
					user.setNumber(m1.group(1)+m1.group(2));//存电话号码
					userMap.put(m1.group(1)+m1.group(2), user );//放入hashmap,key就是电话号码
				}
			}
			else if( Pattern.matches(pattern2, str) ) {//t-079186300001 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:25
				m2.find();
				//先判断拨打电话是否在map中
				//存在就存进从userMap中找,记录相应信息
				this.judge2(str);
			}
			else if(Pattern.matches(pattern3, str) ) {//t-079186330022 13305862264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
				m2.find();
				this.judge3(str);
			}
			else if(Pattern.matches(pattern4, str) ){//t-18907910010 0791 13305862264 0371 2022.1.3 10:00:25 2022.1.3 10:05:11
				m2.find();
				this.judge4(str);
			}
			str = in.nextLine();
		}
		//下面是遍历
		User user_x = null;
		Collection c = userMap.values();
		Iterator iter= c.iterator();
		while (iter.hasNext()) {
			user_x = (User)iter.next();
			System.out.println(user_x.getNumber()+" "+user_x.calCost()+" "+user_x.calBalance());

		}

	}

	public void judge2(String str0) {//座机的
		CallRecord cr = new CallRecord();
		MessageRecord mr = new MessageRecord();
		m2 = r2.matcher(str0);
		m2.find();
		if( userMap.containsKey(m2.group(1)+m2.group(2)) ) {//能找到该拨号号码
			User user1 = userMap.get(  m2.group(1)+m2.group(2)  );
			cr.setCallingAddressAreaCode( m2.group(1) );
			cr.setCallingNumber( m2.group(2) );
			cr.setAnswerAddressAreaCode( m2.group(3) );
			cr.setAnswerNummber( m2.group(4) );
			String s1 = m2.group(5)+" "+m2.group(6);
			String s2 = m2.group(7)+" "+m2.group(8);
			cr.setStartTime( this.tsd(s1) );
			cr.setEndTime( this.tsd(s2) );
			//
			if( m2.group(1).equals("0791") )
				user1.getUserRecords().addCallingInCityRecords(cr);//市内拨打
			else if((m2.group(1).compareTo("0790")>=0&&m2.group(1).compareTo("0799")<=0)||m2.group(1).equals("0701"))
				user1.getUserRecords().addCallingInProvinceRecords(cr);//省内拨打
			else
				user1.getUserRecords().addCallingInLandRecords(cr);//国内拨打
		}

	}
	public void judge3(String str0){//手机和座机的//t-079186330022 13305862264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
		CallRecord cr = new CallRecord();
		MessageRecord mr = new MessageRecord();
		m3 = r3.matcher(str0);
		m3.find();
		if( userMap.containsKey(m3.group(1)+m3.group(2)) ) {//能找到该拨号号码
			User user1 = userMap.get(  m3.group(1)+m3.group(2)  );
			cr.setCallingAddressAreaCode( m3.group(1) );
			cr.setCallingNumber( m3.group(2) );
			cr.setAnswerAddressAreaCode( m3.group(4) );
			cr.setAnswerNummber( m3.group(3) );
			String s1 = m3.group(5)+" "+m3.group(6);
			String s2 = m3.group(7)+" "+m3.group(8);
			cr.setStartTime( this.tsd(s1) );
			cr.setEndTime( this.tsd(s2) );
			//
			if( m2.group(1).equals("0791") )
				user1.getUserRecords().addCallingInCityRecords(cr);//市内拨打
			else if((m2.group(1).compareTo("0790")>=0&&m2.group(1).compareTo("0799")<=0)||m2.group(1).equals("0701"))
				user1.getUserRecords().addCallingInProvinceRecords(cr);//省内拨打
			else
				user1.getUserRecords().addCallingInLandRecords(cr);//国内拨打
		}
		if( userMap.containsKey(m3.group(4)+m3.group(3)) ) {//能找到该接听号码
			User user1 = userMap.get(  m3.group(4)+m3.group(3)  );
			cr.setCallingAddressAreaCode( m3.group(1) );
			cr.setCallingNumber( m3.group(2) );
			cr.setAnswerAddressAreaCode( m3.group(4) );
			cr.setAnswerNummber( m3.group(3) );
			String s1 = m3.group(5)+" "+m3.group(6);
			String s2 = m3.group(7)+" "+m3.group(8);
			cr.setStartTime( this.tsd(s1) );
			cr.setEndTime( this.tsd(s2) );
			//
			if( m2.group(1).equals("0791") )
				user1.getUserRecords().addCallingInCityRecords(cr);//市内拨打
			else if((m2.group(1).compareTo("0790")>=0&&m2.group(1).compareTo("0799")<=0)||m2.group(1).equals("0701"))
				user1.getUserRecords().addCallingInProvinceRecords(cr);//省内拨打
			else
				user1.getUserRecords().addCallingInLandRecords(cr);//国内拨打
		}

	}
	public void judge4(String str0){//手机的t-18907910010 0791 13305862264 0371 2022.1.3 10:00:25 2022.1.3 10:05:11

	}
	public Date tsd(String s) {
		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
		Date date = null ;
		try {
			date = simpleDateFormat1.parse(s);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}
}
class Test {
	String s1 = "2022.1.3 10:00:25";
	String s2 = "2022.1.3 10:05:26";
	public void DoIt() {
		try {
			SimpleDateFormat simple1 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
//	        String strDate1 = "2019-02-18 13:58";
			Date date1 = simple1.parse(s1);
			Date date2 = simple1.parse(s2);
			System.out.println(date1);
			long diff =( date2.getTime()-date1.getTime() );
			System.out.println(diff);
			long s =  (diff/1000);
			System.out.println("now do you feel it?");
			double x = s;
			System.out.println(s);
			System.out.println(x);


		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	public void DoIt2() throws ParseException{
		// 空参构造器,采用默认格式
		SimpleDateFormat sdf = new SimpleDateFormat();
		// 带参指定格式的构造器
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

		Date date = new Date();				// 新建一个util.Date对象
		System.out.println(date);			// Wed Jun 24 11:48:10 CST 2020
//将日期转为指定格式字符串
		String format = sdf.format(date);	// 格式化:按默认的格式
		System.out.println(format+"<1>");			// 20-6-24 上午11:48

		String format1 = sdf1.format(date);	// 格式化,按给定的格式
		System.out.println(format1);		// 2020-06-24 12:09:58

		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MMM-dd hh:mm:ss");// 3*M
		String format11 = sdf2.format(date);
		System.out.println(format11);				// 2020-六月-24 12:16:05

//		        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//		        Date fromDate3 = simpleFormat.parse("2018-03-01 12:00:05");
//		        Date toDate3 = simpleFormat.parse("2018-03-12 12:05:06");
//		        long from3 = fromDate3.getTime();
//		        long to3 = toDate3.getTime();
//		        System.out.println(to3-from3);
//		        double minutes = (double) ((to3 - from3) / (1000*60));
//		        System.out.println("两个时间之间的秒差为:" + minutes);

		try {
			SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String strDate1 = "2019-02-18 13:58";
			//           String strDate2 = "2019-02-18";
			Date date1 = simpleDateFormat1.parse(strDate1);
			System.out.println(date1);
			//           Date date2 = simpleDateFormat1.parse(strDate2);
			//          System.out.println(date2);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		//将字符串转化为date类型
		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strDate1 = "2019-02-18 13:58:34";
		Date date1 = simpleDateFormat1.parse(strDate1);
		System.out.println(date1);
//		        this.DoIt();
	}


}
public class Main {

	public static void main(String[] args) throws ParseException /* throws ParseException */ {
//		Test test = new Test();
//		test.DoIt();
		Judge judge = new Judge();
		judge.judge();
	}

}

用到了正则表达式,还有把字符串转换为日期的方法。

(3)采坑心得:关于类的设计的题目,当时以为给出的是完整的类图,没有多看看就敲代码去了,然后越来越混乱。所以说拿到题目应该把大致的方向找好。

(4)改进建议:对自己的代码比较不满意,还是有大量赘余,在记录的分配上遇到问题,也没有主动设计程序的意识。

(5)总结:学到了程序的设计,也发现自身很多不足。

教师、课程、作业、实验、课上及课下组织方式等方面的改进建议及意见:无。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值