文章目录

  • 11.01
    • 实现思路
    • Code
  • 12.06
    • 实现思路
    • Code
  • 12.08
    • 实现思路
  • 12.11
    • 实现思路
    • Code
  • 11.01
    • 实现思路
    • Code
  • 12.05
    • 实现思路
    • Code
  • 12.06
    • 实现思路
    • Code
  • 12.08
    • 实现思路
  • 12.11
    • 实现思路
    • Code
    • 运行结果

11.01

实现思路

子类可以继承父类的方法,如果覆盖了则需要创建父类对象来调用(如父类的toString());在之类中可以用super调用父类的方法.

Code

import java.util.Scanner;class GeometricObject {private String color ="white";private boolean filled;private java.util.Date dateCreated;public GeometricObject(){dateCreated = new java.util.Date();}public GeometricObject(String color,boolean filled){dateCreated = new java.util.Date();this.color = color;this.filled = filled;}public String getColor(){return color;}public void setColor(String color){this.color = color;}public boolean isfilled(){return filled;}public void setFilled(boolean filled){this.filled = filled;}public java.util.Date getDateCreated(){return dateCreated;}public String toString(){return "created on " + dateCreated +"\ncolor: "+color+" and filled: "+filled;}
}
class Triangle extends GeometricObject{private double side1,side2,side3;public Triangle(){this("white", false ,1.0,1.0,1.0);}public Triangle(String color,boolean filled,double side1, double side2, double side3){super(color,filled);this.side1 = side1;this.side2 = side2;this.side3 = side3;}public double getSide1() {return side1;}public double getSide2() {return side2;}public double getSide3() {return side3;}public double getArea(){double p=this.getPerimeter()/2.0;return  Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));}public double getPerimeter(){return side1+side2+side3;}public String toString(){return getClass().getName()+": side1="+side1+" side2="+side2+" side3="+side3;}
}
public class exercise_11_01{public static void main(String[] args){Scanner input = new Scanner(System.in);System.out.println("Please enter three sides respectively:");double side1 = input.nextDouble();double side2 = input.nextDouble();double side3 = input.nextDouble();System.out.println("Please enter the color of the triangle and if it is filled:");String color = input.next();boolean filled = input.nextBoolean();GeometricObject g = new GeometricObject(color, filled);Triangle triangle = new Triangle(color, filled,side1,side2,side3);System.out.println(g.toString());System.out.println(""+triangle);System.out.println("The area is "+triangle.getArea());System.out.println("The perimeter is "+triangle.getPerimeter());input.close();}
}

###运行结果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QyGNqmrU-1580201837990)(./1572317817360.png)]

##12.05
###实现思路
定义IllegalTriangleException然后声明异常并接住异常,需要注意的是声明是在方法签名"()"之后,"{}"之前: throws IllegalTriangleException
###Code

import java.util.Scanner;
class GeometricObject {private String color ="white";private boolean filled;private java.util.Date dateCreated;public GeometricObject(){dateCreated = new java.util.Date();}public GeometricObject(String color,boolean filled){dateCreated = new java.util.Date();this.color = color;this.filled = filled;}public String toString(){return "created on " + dateCreated +"\ncolor: "+color+" and filled: "+filled;}
}
class IllegalTriangleException extends Exception{/****/private static final long serialVersionUID = 1L;//private double side1, side2, side3;public IllegalTriangleException(double side1,double side2,double side3){super("Invalid side"+" "+ side1+" "+side2+" "+ side3);// this.side1=side1;// this.side2=side2;// this.side3=side3;}
}
class Triangle extends GeometricObject{private double side1,side2,side3;public Triangle() throws IllegalTriangleException {this("white", false ,1.0,1.0,1.0);}public Triangle(String color,boolean filled,double side1, double side2, double side3) throws IllegalTriangleException {super(color,filled);if(side1+side2>side3 && side1+side3>side2 && side2+side3>side1){this.side1 = side1;this.side2 = side2;this.side3 = side3;}elsethrow new IllegalTriangleException(side1,side2,side3);}public double getArea(){double p=this.getPerimeter()/2.0;return  Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));}public double getPerimeter(){return side1+side2+side3;}public String toString(){return getClass().getName()+": side1="+side1+" side2="+side2+" side3="+side3;}
}
public class exercise_12_05{public static void main(String[] args){Scanner input = new Scanner(System.in);System.out.println("Please enter three sides respectively:");double side1 = input.nextDouble();double side2 = input.nextDouble();double side3 = input.nextDouble();System.out.println("Please enter the color of the triangle and if it is filled:");String color = input.next();boolean filled = input.nextBoolean();try{GeometricObject g = new GeometricObject(color, filled);Triangle triangle = new Triangle(color, filled,side1,side2,side3);System.out.println(g.toString());System.out.println(""+triangle);System.out.println("The area is "+triangle.getArea());System.out.println("The perimeter is "+triangle.getPerimeter());input.close();}catch (IllegalTriangleException ex){System.out.println(ex.getMessage());}}
}

12.06

实现思路

将字符串先转化为一个个字符,然后对字符通过减去ASCII起始码的操作转化为数字.最后将这些数字按照十六进制数的计数法则(每逢是16进一位)得到其十进制的数值.
NumberFormatException是已经有的异常,判断不是十六进制字符串的条件应当是其自身的每个字符是否符合了在0~1A~F的输入表示法.

Code

import java.util.Scanner;
public class exercise_12_06{public static void main(String[] args){Scanner input = new Scanner(System.in);System.out.print("Please enter a hex number: ");try {String hex = input.nextLine();System.out.println("The decimal value for hex number " + hex+"is "+hexToDec(hex.toUpperCase()));input.close();} catch (NumberFormatException ex) {System.out.println(ex);}}public static int hexToDec(String hex) throws NumberFormatException {//十六进制字符串除了在开头用0x标志之外,本身它的构成也只可能是0~1与A~Fint hexcharToDecimal=0,decimalValue=0;for(int i=0;i<hex.length();i++){char ch = hex.charAt(i);if(ch>='A' && ch<='F'){hexcharToDecimal = 10+ ch - 'A';}else if(ch>='0' && ch<='9')hexcharToDecimal = ch - '0';else//异常本身也是一种方法,故而需要()throw new NumberFormatException();decimalValue = decimalValue * 16 + hexcharToDecimal;}return decimalValue;}}

12.08

实现思路

如果是NumberFormatException ex,ex即java.lang.NumberFormatException,ex.getMessage()无内容;对于自定义的异常类,ex为HexFormatException: HexFormatException,ex.getMessage()有内容即HexFormatException

12.11

实现思路

命令行读入文件名与需要的替换字符串,创建file对象,判断file.exists(),在构建器中读入文本用replaceAll()处理后,再写进原文档.
值得一提的是.replaceAll()方法不支持中文,如果是文章开头是中文,将会没有输出;如果文章中间是中文,会输出乱码. 此外,命令行似乎不接受"",想通过读入空字符串然后替代删除原文中字符串不可以,于是在源代码中replaceAll()写入"".

Code

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class exercise_12_11 {public static void main(String[] args) throws FileNotFoundException {File sourceFile  = new File(args[1]);//判断文件是否存在if(!sourceFile.exists()){System.out.println("File don't exist.");}//创建一个空的字符串构建器,sb的对象为StringBuilder类Scanner input = new Scanner(sourceFile);StringBuilder sb = new StringBuilder();//在还有内容可以读的时候,不停地读入下一行while(input.hasNext()){String line = input.nextLine();//对windows而言,回车/r加换行/n才能真正换行sb.append(line.replaceAll(args[0],"")+"\r\n");}input.close();//再把内容从构建器中输出出来PrintWriter output = new PrintWriter(sourceFile);//必须使用toString()方法才可以将构建器中的字符序列输出给新的String类对象output.println(sb.toString());output.close();}
}

11.01

实现思路

子类可以继承父类的方法,如果覆盖了则需要创建父类对象来调用(如父类的toString());在之类中可以用super调用父类的方法.

Code

import java.util.Scanner;class GeometricObject {private String color ="white";private boolean filled;private java.util.Date dateCreated;public GeometricObject(){dateCreated = new java.util.Date();}public GeometricObject(String color,boolean filled){dateCreated = new java.util.Date();this.color = color;this.filled = filled;}public String getColor(){return color;}public void setColor(String color){this.color = color;}public boolean isfilled(){return filled;}public void setFilled(boolean filled){this.filled = filled;}public java.util.Date getDateCreated(){return dateCreated;}public String toString(){return "created on " + dateCreated +"\ncolor: "+color+" and filled: "+filled;}
}
class Triangle extends GeometricObject{private double side1,side2,side3;public Triangle(){this("white", false ,1.0,1.0,1.0);}public Triangle(String color,boolean filled,double side1, double side2, double side3){super(color,filled);this.side1 = side1;this.side2 = side2;this.side3 = side3;}public double getSide1() {return side1;}public double getSide2() {return side2;}public double getSide3() {return side3;}public double getArea(){double p=this.getPerimeter()/2.0;return  Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));}public double getPerimeter(){return side1+side2+side3;}public String toString(){return getClass().getName()+": side1="+side1+" side2="+side2+" side3="+side3;}
}
public class exercise_11_01{public static void main(String[] args){Scanner input = new Scanner(System.in);System.out.println("Please enter three sides respectively:");double side1 = input.nextDouble();double side2 = input.nextDouble();double side3 = input.nextDouble();System.out.println("Please enter the color of the triangle and if it is filled:");String color = input.next();boolean filled = input.nextBoolean();GeometricObject g = new GeometricObject(color, filled);Triangle triangle = new Triangle(color, filled,side1,side2,side3);System.out.println(g.toString());System.out.println(""+triangle);System.out.println("The area is "+triangle.getArea());System.out.println("The perimeter is "+triangle.getPerimeter());input.close();}
}

12.05

实现思路

定义IllegalTriangleException然后声明异常并接住异常,需要注意的是声明是在方法签名"()"之后,"{}"之前: throws IllegalTriangleException

Code

import java.util.Scanner;
class GeometricObject {private String color ="white";private boolean filled;private java.util.Date dateCreated;public GeometricObject(){dateCreated = new java.util.Date();}public GeometricObject(String color,boolean filled){dateCreated = new java.util.Date();this.color = color;this.filled = filled;}public String toString(){return "created on " + dateCreated +"\ncolor: "+color+" and filled: "+filled;}
}
class IllegalTriangleException extends Exception{/****/private static final long serialVersionUID = 1L;//private double side1, side2, side3;public IllegalTriangleException(double side1,double side2,double side3){super("Invalid side"+" "+ side1+" "+side2+" "+ side3);// this.side1=side1;// this.side2=side2;// this.side3=side3;}
}
class Triangle extends GeometricObject{private double side1,side2,side3;public Triangle() throws IllegalTriangleException {this("white", false ,1.0,1.0,1.0);}public Triangle(String color,boolean filled,double side1, double side2, double side3) throws IllegalTriangleException {super(color,filled);if(side1+side2>side3 && side1+side3>side2 && side2+side3>side1){this.side1 = side1;this.side2 = side2;this.side3 = side3;}elsethrow new IllegalTriangleException(side1,side2,side3);}public double getArea(){double p=this.getPerimeter()/2.0;return  Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));}public double getPerimeter(){return side1+side2+side3;}public String toString(){return getClass().getName()+": side1="+side1+" side2="+side2+" side3="+side3;}
}
public class exercise_12_05{public static void main(String[] args){Scanner input = new Scanner(System.in);System.out.println("Please enter three sides respectively:");double side1 = input.nextDouble();double side2 = input.nextDouble();double side3 = input.nextDouble();System.out.println("Please enter the color of the triangle and if it is filled:");String color = input.next();boolean filled = input.nextBoolean();try{GeometricObject g = new GeometricObject(color, filled);Triangle triangle = new Triangle(color, filled,side1,side2,side3);System.out.println(g.toString());System.out.println(""+triangle);System.out.println("The area is "+triangle.getArea());System.out.println("The perimeter is "+triangle.getPerimeter());input.close();}catch (IllegalTriangleException ex){System.out.println(ex.getMessage());}}
}

12.06

实现思路

将字符串先转化为一个个字符,然后对字符通过减去ASCII起始码的操作转化为数字.最后将这些数字按照十六进制数的计数法则(每逢是16进一位)得到其十进制的数值.
NumberFormatException是已经有的异常,判断不是十六进制字符串的条件应当是其自身的每个字符是否符合了在0~1A~F的输入表示法.

Code

import java.util.Scanner;
public class exercise_12_06{public static void main(String[] args){Scanner input = new Scanner(System.in);System.out.print("Please enter a hex number: ");try {String hex = input.nextLine();System.out.println("The decimal value for hex number " + hex+"is "+hexToDec(hex.toUpperCase()));input.close();} catch (NumberFormatException ex) {System.out.println(ex);}}public static int hexToDec(String hex) throws NumberFormatException {//十六进制字符串除了在开头用0x标志之外,本身它的构成也只可能是0~1与A~Fint hexcharToDecimal=0,decimalValue=0;for(int i=0;i<hex.length();i++){char ch = hex.charAt(i);if(ch>='A' && ch<='F'){hexcharToDecimal = 10+ ch - 'A';}else if(ch>='0' && ch<='9')hexcharToDecimal = ch - '0';else//异常本身也是一种方法,故而需要()throw new NumberFormatException();decimalValue = decimalValue * 16 + hexcharToDecimal;}return decimalValue;}}

12.08

实现思路

如果是NumberFormatException ex,ex即java.lang.NumberFormatException,ex.getMessage()无内容;对于自定义的异常类,ex为HexFormatException: HexFormatException,ex.getMessage()有内容即HexFormatException

12.11

实现思路

命令行读入文件名与需要的替换字符串,创建file对象,判断file.exists(),在构建器中读入文本用replaceAll()处理后,再写进原文档.
值得一提的是.replaceAll()方法不支持中文,如果是文章开头是中文,将会没有输出;如果文章中间是中文,会输出乱码. 此外,命令行似乎不接受"",想通过读入空字符串然后替代删除原文中字符串不可以,于是在源代码中replaceAll()写入"".

Code

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class exercise_12_11 {public static void main(String[] args) throws FileNotFoundException {File sourceFile  = new File(args[1]);//判断文件是否存在if(!sourceFile.exists()){System.out.println("File don't exist.");}//创建一个空的字符串构建器,sb的对象为StringBuilder类Scanner input = new Scanner(sourceFile);StringBuilder sb = new StringBuilder();//在还有内容可以读的时候,不停地读入下一行while(input.hasNext()){String line = input.nextLine();//对windows而言,回车/r加换行/n才能真正换行sb.append(line.replaceAll(args[0],"")+"\r\n");}input.close();//再把内容从构建器中输出出来PrintWriter output = new PrintWriter(sourceFile);//必须使用toString()方法才可以将构建器中的字符序列输出给新的String类对象output.println(sb.toString());output.close();}
}

运行结果

  • 从命令行传入参数
  • 运行之前的文本:
  • 运行之后的文本:

Java程序与设计11_一些题目相关推荐

  1. java程序语言设计第三章答案_java语言程序设计课后习题解答张思民第三章

    java语言程序设计课后习题解答张思民第三章 1 第3章 面向对象程序设计基础 [1]什么是 Java 程序使用的类?什么是类库? [解答]:Java 程序的基本单位是类.对象是对事物的抽象,而类是对 ...

  2. 扫雷java程序算法设计_基于Java的Windows扫雷游戏的设计与实现毕业论文+任务书+翻译及原文+源码+辅导视频...

    基于Java的Windows扫雷游戏的设计与实现 摘 要 扫雷这款游戏有着很长的历史,从扫雷被开发出来到现在进行了无数次的优化,这款游戏变得越来越让人爱不释手了,简单的玩法在加上一个好看的游戏界面,每 ...

  3. 计算机二级java程序语言设计,全国计算机等级考试二级教程:Java语言程序设计(2016年版)...

    由教育部考试中心推出的计算机等级考试是一种客观.公正.科学的专门测试计算机应用人员的计算机知识与技能的全国性考试,它面向社会,服务于社会. <全国计算机等级考试二级教程:Java语言程序设计(2 ...

  4. java程序语言设计课后答案向金海,看看这篇文章吧!

    前言 在网络技术中基于浏览器的B/S结构无论在PC端还是手机端都充当着至关重要的角色. PC端自不必说,手机中很多应用虽然是以APP的形式存在,但它采用的还是B/S结构.如今日头条.微信的朋友圈等,这 ...

  5. Java程序界面设计

    用JavaFX实现图形用户界面,计算应交的税款. 代码详情: Mytax.java import javafx.application.Application; import javafx.colle ...

  6. Java程序员非技术性面试题目

    为何辞去原来的工作? 搬家,换城市,或者工作地点离家较远,路上花费时间多,发生交通问题时,影响工作. 公司非常大的转型,自己不适合新的工作内容. 切记不要回答: 工资低,待遇不好,福利不好 这类问题没 ...

  7. java面向对象的程序编程设计

    单核CPU在执行程序的时候,一次只能执行一个命令.这就是说,程序命令在单核CPU上是线性的.操作系统对程序命令进行管理,在不同的情况下把不同的程序命令推给单核CPU处理.操作系统简化了计算机的使用,让 ...

  8. java双语试卷_Java程序设计基础(双语)答案试题题目及答案,期末考试题库,章节测验答案...

    Java程序设计基础(双语)答案试题题目及答案,期末考试题库,章节测验答案 更多相关问题 写出下列反应的化学方程式并括号中注明反应类型("化合"."分解".&q ...

  9. java游戏课程设计报告_java课程设计报告游戏_相关文章专题_写写帮文库

    时间:2019-05-14 00:00:44 作者:admin 课 程 设 计 课程名称 Java语言课程设计 题目名称 人事管理系统的设计与实现 学生学院 应用数学学院 专业班级 学 号 学生姓名 ...

  10. java入门-java程序

    前言 java入门系列,自我学习总结,用来记录一些入门简单的知识点和自己的思考总结,不会很详细的进行记录. 参考文档地址:菜鸟教程 参考文档地址:廖雪峰 Java 教程 参考视频笔记:b站尚硅谷 什么 ...

最新文章

  1. 三分钟了解“Java重写”
  2. 开放一些3D视觉相关职位!
  3. 深入理解特征值与特征向量
  4. JavaScript数组方法大全解
  5. 生活的色彩——摄影作品欣赏
  6. 菜鸟,下一代分布式体系架构的设计理念
  7. 转载 | Systemd的使用简介
  8. android webview 多次加载,android – 重复webview,我想在每个加载相同
  9. electronjs设置宽度_Js操作DOM元素及获取浏览器高宽的简单方法
  10. 工具---genymotion
  11. 常见Sqlite管理工具
  12. GreenSock (TweenMax) 极简入门指南
  13. 互联网日报 | 2月8日 星期一 | 乐视回应App图标“欠122亿”;中国联通成立联通数科;高德地图上线13万个旅游厕所信息...
  14. UserWarning: Usage of dash-separated ‘script-dir‘ will not be supported in future versions. 笔记
  15. 易语言游戏选服务器,网吧游戏菜单服务器含服务器端和客户端
  16. IT十年人生过客-七-眉毛与恶名
  17. 对于因果模型的常见评估函数:SHD 和 FDR
  18. (windows注册表大全)别人写的,我记录下
  19. 人工智能和中国国家人工智能发展战略
  20. 【UWB 定位】高精度定位

热门文章

  1. 主机与虚拟机ping通
  2. Python 下载百度文库
  3. 爬虫抓取百度文库中的文献
  4. 【ruby】ruby图像处理模块“mini_magick”
  5. 使用FFmpegFrameGrabber获取视频缩略图
  6. aria2 txt导入_共一章 · mac下使用Aria2教程-迅雷和百度盘终极解决方案 · 看云
  7. 前端分页加载功能实现?
  8. 海康摄像头车牌识别和顶拍同步抓拍图片
  9. 朋友们,想去一线大厂?卷起来...
  10. icon 的css,【iview】icon样式