7-8 设计一个能处理异常的Loan类 (20 分)
| import java.util.*; |
| |
| class Loan{ |
| double annualInterestRate=25; |
| double numberOfYears=1; |
| double loanAmount=100; |
| Date loanDate=new Date(); |
| public Loan() { |
| |
| } |
| public Loan(double annualInterestRate, double numberOfYears, double loanAmount) throws IllegalArgumentException { |
| super(); |
| if (annualInterestRate<=0) |
| try { |
| throw new IllegalArgumentException("Annual interest rate must be positive"); |
| } |
| catch(IllegalArgumentException e) { |
| throw new IllegalArgumentException("Annual interest rate must be positive"); |
| } |
| |
| else if (numberOfYears<=0) |
| try { |
| throw new IllegalArgumentException("Number of years must be positive"); |
| } |
| catch(IllegalArgumentException e) { |
| throw new IllegalArgumentException("Number of years must be positive"); |
| } |
| else if (loanAmount<=0) |
| try{ |
| throw new IllegalArgumentException("Loan amount must be positive"); |
| } |
| catch(Exception e) { |
| throw new IllegalArgumentException("Loan amount must be positive"); |
| } |
| this.annualInterestRate = annualInterestRate; |
| this.numberOfYears = numberOfYears; |
| this.loanAmount = loanAmount; |
| } |
| public double getAnnualInterestRate() { |
| return annualInterestRate; |
| } |
| public void setAnnualInterestRate(double annualInterestRate) { |
| this.annualInterestRate = annualInterestRate; |
| } |
| public double getNumberOfYears() { |
| return numberOfYears; |
| } |
| public void setNumberOfYears(double numberOfYears) { |
| this.numberOfYears = numberOfYears; |
| } |
| public double getLoanAmount() { |
| return loanAmount; |
| } |
| public void setLoanAmount(double loanAmount) { |
| this.loanAmount = loanAmount; |
| } |
| public Date getLoanDate() { |
| return loanDate; |
| } |
| public void setLoanDate(Date loanDate) { |
| this.loanDate = loanDate; |
| } |
| |
| public double getMonthlyPayment() { |
| double mo=annualInterestRate/1200; |
| return loanAmount*mo/(1-(1/Math.pow(1+mo, numberOfYears*12))); |
| } |
| public double getTotalPayment() { |
| return getMonthlyPayment()*12*numberOfYears; |
| } |
| |
| } |
| public class Main{ |
| public static void main(String[] args) { |
| Scanner input = new Scanner(System.in); |
| while (input.hasNext()) { |
| double AIR = input.nextDouble(); |
| int NOY = input.nextInt(); |
| double LA = input.nextDouble(); |
| try { |
| Loan m = new Loan(AIR, NOY, LA); |
| |
| System.out.printf("%.3f\n",m.getTotalPayment()); |
| } catch (Exception ex) { |
| System.out.println(ex); |
| } |
| } |
| input.close(); |
| } |
| } |
| |
7-9 设计一个Tiangle异常类 (20 分)
第一个自己写的代码
| import java.util.*; |
| class Triangle{ |
| double a; |
| double b; |
| double c; |
| int flag=0; |
| public Triangle(double side1, double side2, double side3) throws IllegalTriangleException { |
| if(side1+side2<=side3 |
| ||side1+side3<=side2 |
| ||side2+side3<=side1) |
| { |
| try { |
| throw new IllegalTriangleException(side1,side2,side3); |
| } |
| catch(IllegalTriangleException e) { |
| throw new IllegalTriangleException(side1,side2,side3); |
| |
| } |
| } |
| else { |
| a=side1; |
| b=side2; |
| c=side3; |
| } |
| } |
| @Override |
| public String toString() { |
| return "Triangle [side1=" + a + ", side2=" + b + ", side3=" + c + "]"; |
| } |
| |
| } |
| class IllegalTriangleException extends Exception{ |
| public IllegalTriangleException() { |
| super(); |
| } |
| public IllegalTriangleException(double a,double b,double c) { |
| super("Invalid: " + a +","+ b+ ","+ c ); |
| } |
| } |
| |
| public class Main{ |
| public static void main(String[] args) { |
| Scanner sc = new Scanner(System.in); |
| double s1 = sc.nextDouble(); |
| double s2 = sc.nextDouble(); |
| double s3 = sc.nextDouble(); |
| try { |
| Triangle t = new Triangle(s1,s2,s3); |
| System.out.println(t); |
| } |
| catch (IllegalTriangleException ex) { |
| System.out.println(ex.getMessage()); |
| } |
| sc.close(); |
| } |
| } |
7-5 成绩录入时的及格与不及格人数统计 (10 分)
这个代码思想比较深
| import java.util.*; |
| class MyException extends Exception |
| { |
| MyException (){ |
| } |
| MyException(int s) |
| |
| { |
| super(s+"invalid!"); |
| } |
| } |
| public class Main{ |
| public static void main(String[] args) { |
| Scanner in = new Scanner(System.in); |
| int n =in.nextInt(); |
| int cont1=0,cont2=0; |
| ArrayList<Integer> c = new ArrayList <> (n); |
| |
| for(int j=0;j<n;j++) |
| { |
| int z= in.nextInt(); |
| c.add(z); |
| if(z>100||z<0) |
| n++; |
| } |
| for(int a:c) |
| { |
| try{ |
| if(a>100||a<0) |
| throw new MyException(a); |
| if(a<=100&&a>=60) |
| cont1++; |
| if(a>=0&&a<60) |
| cont2++; |
| } |
| catch(MyException e) |
| { |
| System.out.println(e.getMessage()); |
| |
| } |
| } |
| System.out.println(cont1); |
| System.out.println(cont2); |
| } |
| } |
| |
| |
| |
7-3 jmu-Java-06异常-04-自定义异常(综合) (15 分)
| import java.util.*; |
| |
| public class Main{ |
| public static void main(String[] args) { |
| Scanner sc = new Scanner(System.in); |
| String s; |
| while (true){ |
| s= sc.nextLine(); |
| if (s.equals("new")){ |
| Student stu=new Student(); |
| String s1=sc.nextLine(); |
| String[] ss=s1.split(" "); |
| try { |
| stu.setName(ss[0]); |
| }catch (Exception e){ |
| continue; |
| } |
| try{ |
| stu.addScore(Integer.parseInt(ss[1])); |
| }catch (Exception e){ |
| stu.flag=false; |
| System.out.println("java.util.NoSuchElementException"); |
| } |
| if (stu.flag) System.out.println(stu); |
| }else if (s.equals("other")){ |
| break; |
| } |
| } |
| sc.close(); |
| System.out.println("scanner closed"); |
| } |
| } |
| |
| class IllegalScoreException extends RuntimeException{ |
| |
| public IllegalScoreException() { |
| } |
| |
| public IllegalScoreException(String message) { |
| super(message); |
| } |
| |
| } |
| |
| class IllegalNameException extends RuntimeException{ |
| public IllegalNameException() { |
| } |
| |
| public IllegalNameException(String message) { |
| super(message); |
| } |
| } |
| |
| class Student{ |
| private String name; |
| private int score; |
| boolean flag=true; |
| |
| public String getName() { |
| return name; |
| } |
| |
| public void setName(String name) { |
| if (name.charAt(0)>='0'&&name.charAt(0)<='9'){ |
| try{ |
| throw new IllegalNameException("the first char of name must not be digit, name="); |
| |
| }catch (IllegalNameException e){ |
| flag=false; |
| System.out.println(e+name); |
| } |
| }else { |
| this.name = name; |
| } |
| |
| } |
| |
| public int getScore() { |
| return score; |
| } |
| |
| public void setScore(int score) { |
| this.score = score; |
| } |
| |
| @Override |
| public String toString() { |
| return "Student [" + |
| "name=" + name + |
| ", score=" + score + |
| ']'; |
| } |
| public int addScore(int score){ |
| this.score=this.score+score; |
| if (this.score>100||this.score<0){ |
| try{ |
| throw new IllegalScoreException("score out of range, score="); |
| |
| }catch (IllegalScoreException e){ |
| flag=false; |
| System.out.println(e.toString()+score); |
| } |
| } |
| return this.score; |
| } |
| } |
| |
7-4 天不假年 (5 分)
| import java.util.Scanner; |
| |
| public class Main { |
| public static void main(String[] args) { |
| Scanner sc = new Scanner(System.in); |
| int age; |
| age = sc.nextInt(); |
| Person p = new Person(age); |
| age = sc.nextInt(); |
| try { |
| p.setAge(age); |
| } catch (AgeException e) { |
| System.out.println("B"); |
| return; |
| } |
| System.out.println("A"); |
| sc.close(); |
| } |
| } |
| |
| class Person { |
| int age; |
| |
| public Person(int age) { |
| this.age = age; |
| } |
| |
| public void setAge(int age) throws AgeException { |
| if (this.age <= age) { |
| this.age = age; |
| } |
| else { |
| throw new AgeException(); |
| } |
| } |
| } |
| |
| class AgeException extends Exception { |
| |
| public AgeException() { |
| super(); |
| } |
| public AgeException(String message) { |
| super(message); |
| } |
| } |
| import java.util.*; |
| |
| public class Main{ |
| public static void main(String[] args) { |
| Scanner sc = new Scanner(System.in); |
| |
| |
| int ans=0; |
| while(sc.hasNext()) { |
| String t=sc.next(); |
| try { |
| double ddd=Double.parseDouble(t); |
| int cnt=(int) Math.round(ddd); |
| ans+=cnt; |
| } |
| catch(Exception e){ |
| continue; |
| } |
| |
| |
| } |
| System.out.println(ans); |
| sc.close(); |
| } |
| } |
| |
| |
| |
//如果直接获取int,会无限循环,原因未知
| import java.util.*; |
| |
| public class Main{ |
| public static void main(String[] args) { |
| Scanner sc = new Scanner(System.in); |
| |
| |
| |
| while(sc.hasNext()) { |
| String a=sc.next(); |
| String b=sc.next(); |
| int ans=0; |
| int flag=1; |
| try { |
| |
| int cnt=Integer.parseInt(a); |
| ans+=cnt; |
| cnt=Integer.parseInt(b); |
| ans+=cnt; |
| } |
| |
| catch(Exception e){ |
| flag=0; |
| System.out.println("Incorrect input: two integer is required"); |
| continue; |
| } |
| if(flag==1) |
| System.out.println("sum = "+ans); |
| } |
| sc.close(); |
| } |
| } |
| |
| |
| |
本文作者:kingwzun
本文链接:https://www.cnblogs.com/kingwz/p/15569400.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步