实验一 Java数据类型和控制结构
一、实验内容
接受键盘输入的字符串,判断其是否为合法的身份证号码,在命令行中输出验证结果。只要求验证18位身份证号。
基本过程:
使用scanner接受键盘输入
1、判断输入内容是否为空
2、判断输入字符串是否为18位
3、判断前17位是否为数字,最后1位是否为数字或X
4、判断7至14位是否为合法的日期(年符合1900至2019,月符合1至12,日符合各月的天数,可选是否考虑闰年情况,若不考虑闰年情况,2月天数一律视为28天)
二、实验代码
package experiment_2;
import java.util.Scanner;
public class solver {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String id = scan.next();
if(id==null) {
System.out.println("输入的身份证号为空");
}
if(id.length()!=18) {
System.out.println("输入的字符串不是18位");
}else {
boolean isNum = true;
for(int i=0;i<id.length()-1;i++) {
if(!Character.isDigit(id.charAt(i))) {
isNum = false;
System.out.println("前七位包含非数字字符");
break;
}
}
if(isNum) {
System.out.println("前七位均为数字");
int year = Integer.parseInt(id.substring(6, 10));
int month = Integer.parseInt(id.substring(10, 12));
int day = Integer.parseInt(id.substring(12, 14));
int[] days = {31,28,31,30,31,30,31,31,30,31,30,31};
if((year>=1900)&&(year<=2019)) {
System.out.println("年份合法");
}else {
System.out.println("年份不合法");
}
if((month>=1)&&(month<=12)) {
System.out.println("月份合法");
}else {
System.out.println("月份不合法");
}
if((day>0)&&(day<=days[month-1])) {
System.out.println("天数合法");
}else {
System.out.println("天数不合法");
}
}
}
}
}
三、实验结果
实验二 类与对象基础
一、实验内容
希腊神话中,宙斯战胜了泰坦之后成为众神之王,以此为背景,通过构造相应对象、属性和方法,并用随机的方式,模拟宙斯与泰坦的战斗过程。
构建类Titan,要求如下:
整形字段HP,以及相应的getter和setter
空参数列表构造方法Titan(),创造出的HP默认为700
带参数构造方法Titan(int HP),创造出指定HP的对象
方法attack(Zues z),参数为宙斯对象,每次调用此方法后,用随机数的方式,随机产生10-100的整数伤害,减少参数宙斯对象的HP,并且攻击结果需要在控制台输出,格式为“泰坦攻击宙斯,产生**点伤害,宙斯当前HP为**”。
构建类Zues,要求如下:
整形字段HP,以及相应的getter和setter
空参数列表构造方法Zues (),创造出的HP默认为1000
带参数构造方法Zues (int HP),创造出指定HP的对象
方法attack(Titan t),参数为泰坦对象,每次调用此方法后,用随机数的方式,随机产生0-70的整数伤害,减少参数泰坦对象的HP,并且攻击结果需要在控制台输出,格式为“宙斯攻击泰坦,产生**点伤害,泰坦当前HP为**”。
构建类ClashofTitans,要求如下:
拥有main方法,在该方法中,首先构建泰坦和宙斯对象,使用do while循环,让泰坦和宙斯相互攻击,每次攻击完毕后,判断泰坦和宙斯相应的HP,如果某一方HP小于等于0,则停止循环,并输出结果,格式为“泰坦/宙斯HP为**,已经失败,胜利者是宙斯/泰坦!”
二、实验代码
package experiment_1;
public class ClashofTitans {
public static void main(String[] args) {
Titan titan = new Titan();
Zues zues = new Zues();
do {
titan.attack(zues);
if(zues.HP<=0) {
break;
}
zues.attack(titan);
}while(titan.HP>0&&zues.HP>0);
if(titan.HP<=0) {
System.out.println("泰坦HP为"+titan.HP+",已经失败,胜利者是宙斯!");
}else {
System.out.println("宙斯HP为"+zues.HP+",已经失败,胜利者是泰坦!");
}
}
}
package experiment_1;
import java.util.Random;
public class Zues {
int HP;
Zues(){
HP = 1000;
}
Zues(int HP){
this.HP = HP;
}
int getZuesHP() {
return HP;
}
void setZuesHP(int HP) {
this.HP = HP;
}
void attack(Titan t){
Random arandom = new Random();
int damage = arandom.nextInt(70);
t.HP -=damage;
System.out.println("宙斯攻击泰坦,产生"+damage+"点伤害,泰坦当前HP为"+t.HP);
}
}
package experiment_1;
import java.util.Random;
public class Titan {
int HP;
Titan(){
HP = 700;
}
Titan(int HP){
this.HP = HP;
}
int getTitanHP() {
return HP;
}
void setTitanHP(int HP) {
this.HP = HP;
}
void attack(Zues z){
Random arandom = new Random();
int damage = arandom.nextInt(90)+10;
z.HP -=damage;
System.out.println("泰坦攻击宙斯,产生"+damage+"点伤害,宙斯当前HP为"+z.HP);
}
}
三、实验结果
实验三 类与对象高级机制
一、实验内容
- 编写一个名为TwoDimensionalShape的抽象类,拥有属性area和circumference表示面积和周长,以及抽象方法getArea()和getCircumference(),用于获取面积和周长。
- 编写Printable接口,包括一个抽象方法printShapeInfo,用于输出图形信息。
- 分别编写Rectangle、Triangle、Circular三个类,用于描述矩形、三角形和圆形,要求继承于TwoDimensionalShap类,并实现Printable接口,每个图形有各自的构造方法,矩形有length和width字段表示长和宽,三角形有base、hypotenus1和hypotenus2字段表示底和两条斜边,圆形有radius字段表示半径,按几何常识实现父类中所定义的抽象方法,printShapeInfo方法要求能够输出:(1)图形的类型(使用getClass方法)(2)图形的基本数据(使用自定义toString方法,输出图形的字段,输出示例:宽3.0, 长4.0)
- 编写一个名为ShapeDetector的类,拥有三个重载的detectShape方法,分别为detectShape(Rectangle r)、detectShape(Triangle t)、detectShape(Circular c),分别输出所接收到的图形的类型。
- 编写Test类,创建一个名为Shapes的TwoDimensionalShape型数组,并创建Rectangle、Triangle、Circular的对象存储在该数组中,用foreach循环遍历该数组,使用detectShape方法输出该对象的图形类型,用printShapeInfo方法输出该对象的图形信息,然后分别用getArea和getCircumference方法输出面积及周长。
6、(可选)给三个图形的构造方法加上验证功能,保证所输入三角形三边能够组成三角形,圆形半径、矩形的长和宽为正实数。
要求:程序文件放置在合适的包中,保证程序有良好的组织性,在编写过程中尽量保证程序的良好封装性。
附:三角形面积公式 p=(a+b+c)/2 s=√p(p-a)(p-b)(p-c)
二、实验代码
package experiment_4;
public abstract class TwoDimensionalShape {
public double area;
public double circumference;
abstract public double getArea();
abstract public double getCircumference();
}
package experiment_4;
public interface Printable {
void printShapeInfo();
}
package experiment_4;
public class Rectangle extends TwoDimensionalShape implements Printable{
private double length;
private double width;
Rectangle(){
}
Rectangle(double l,double w){
this.length = l;
this.width = w;
}
public double getArea() {
this.area = length*width;
return this.area;
}
public double getCircumference() {
this.circumference = 2*(length+width);
return this.circumference;
}
public String toString() {
String str = " 宽"+this.width+",长"+this.length;
return str;
}
public void printShapeInfo(){
Class c = this.getClass();
System.out.print(c);
System.out.println(this);
}
}
package experiment_4;
public class Triangle extends TwoDimensionalShape implements Printable{
private double base;
private double hypotenus1;
private double hypotenus2;
Triangle(){
}
Triangle(double b,double h1,double h2){
this.base = b;
this.hypotenus1 = h1;
this.hypotenus2 = h2;
}
public double getArea() {
double p = (this.base+this.hypotenus1+this.hypotenus2)/2.0;
this.area = Math.sqrt(p*(p-base)*(p-this.hypotenus1)*(p-this.hypotenus2));
return this.area;
}
public double getCircumference() {
this.circumference = this.base+this.hypotenus1+this.hypotenus2;
return this.circumference;
}
public String toString() {
String str = " 低"+this.base+",斜边"+this.hypotenus1+",斜边"+this.hypotenus2;
return str;
}
public void printShapeInfo() {
Class c = this.getClass();
System.out.print(c);
System.out.println(this);
}
}
package experiment_4;
public class Circular extends TwoDimensionalShape implements Printable{
private double radius;
Circular(){
}
Circular(double r){
this.radius = r;
}
public double getArea() {
this.area = Math.PI*Math.pow(radius,2.0);
return this.area;
}
public double getCircumference() {
this.circumference = 2*Math.PI*this.radius;
return this.circumference;
}
public String toString() {
String str = " 半径"+this.radius;
return str;
}
public void printShapeInfo() {
Class c = this.getClass();
System.out.print(c);
System.out.println(this);
}
}
package experiment_4;
public class ShapeDetector {
public void detectShape(Rectangle r) {
System.out.println(r.getClass());
}
public void detectShape(Triangle t) {
System.out.println(t.getClass());
}
public void detectShape(Circular c) {
System.out.println(c.getClass());
}
}
package experiment_4;
public class Test {
public static void main(String[] args) {
Rectangle r = new Rectangle(3.0,4.0);
Triangle t = new Triangle(3,4,5);
Circular c = new Circular(4.0);
TwoDimensionalShape Shapes[] = {r,t,c};
for(TwoDimensionalShape shape : Shapes) {
ShapeDetector sd =new ShapeDetector();
if(shape instanceof Rectangle) {
sd.detectShape((Rectangle)shape);
((Rectangle)shape).printShapeInfo();
}
if(shape instanceof Triangle) {
sd.detectShape((Triangle)shape);
((Triangle)shape).printShapeInfo();
}
if(shape instanceof Circular) {
sd.detectShape((Circular)shape);
((Circular)shape).printShapeInfo();
}
System.out.println("面积:"+shape.getArea()+" 周长:"+shape.getCircumference());
}
}
}
三、实验结果
实验四 异常处理
一、实验内容
1、编写并运行附1、2中的程序,尝试去掉注释符号再次运行程序,理解程序为何会产生的异常。
2、尝试编写附3中的程序,输入非整型值查看程序异常情况;使用异常处理机制对其进行改写,尝试在用户输入错误的情况下,给用户提示,并要求用户重新输入,使程序能够恢复到正常运行状态,而不异常终止。
3、编写一个用于表示三角形的类Triangle,并编写自定义异常类TriangleException用来在构造Triangle对象时检测所输入的三边是否和合法的三角形三边,要求检测三边是否为非负数或0,检测是否存在两边之和小于第三边的情况,异常信息要求合理的显示错误信息。另构建一个包含main方法的Test类,用于接受用户键盘输入三条边的值,并创建Triangle对象,如果合法,输出三边的值及三角形的面积和周长,如不合法,合理使用TriangleException类,输出错误信息,并让用户再次输入新的三边值。
二、实验代码
1、(1)
package cn.edu.hbut.java.exception;
import java.util.ArrayList;
public class ExceptionTest {
public static void main(String[] args) {
int sum = 0;
ArrayList al = new ArrayList();
for(int i = 0; i<10;i++) {
al.add(new Integer(i));
}
//al.add("string");
for (Object o:al) {
sum = sum+(Integer)o;
}
System.out.println(sum);
}
}
程序异常原因:String类型的值不能转型为Integer
(2)
package cn.edu.hbut.java.exception;
import java.util.ArrayList;
public class ExceptionTest1 {
public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<Integer>();
for(int i=0;i<10;i++) {
al.add(new Integer(i));
}
for (Integer i:al) {
System.out.println(i);
// if(i.equals(new Integer(5))) {
// al.remove(i);
// }
}
System.out.println(al);
}
}
程序异常原因:java的foreach循环其实就是根据list对象创建一个Iterator迭代对象,用这个迭代对象来遍历list,相当于list对象中的元素的遍历托管给了Iterator,如果要对list进行增删操作,都必须经过Iterator,否则Iterator遍历时会乱,所以直接对list进行删除时,Iterator会抛出ConcurrentModificationException异常。
2、
package cn.edu.hbut.java.exception;
import java.util.Scanner;
public class ExceptionHandle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter an integer:");
int n = sc.nextInt();
System.out.println("your number is "+n);
}
}
package cn.edu.hbut.java.exception;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ExceptionHandle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean flag = false;
do {
try {
System.out.println("Enter an integer:");
int n = sc.nextInt();
flag = true;
System.out.println("your number is "+n);
}catch(InputMismatchException e) {
System.out.println("输入的为非整型,请重新输入整型:");
sc.nextLine();
continue;
}
}while(!flag);
}
}
3、
package cn.edu.hbut.java.exception;
public class TriangleException extends Exception{
public TriangleException() {
super();
}
public TriangleException(String message) {
super(message);
}
}
package cn.edu.hbut.java.exception;
public class Triangle {
private float a,b,c;
public Triangle(float a,float b,float c) throws TriangleException{
this.a = a;
this.b = b;
this.c = c;
if(a+b<=c||a+c<=b||b+c<=a||a<=0||b<=0||c<=0) {
throw new TriangleException("不能构成三角形");
}else {
System.out.println("能构成三角形");
}
}
void getArea() {
float p = (a+b+c)/2;
float area = (float) Math.sqrt(p*(p-a)*(p-b)*(p-c));
System.out.println("该三角形的面积是:"+area);
}
void getCircumference() {
float cir = a+b+c;
System.out.println("该三角形的周长是:"+cir);
}
}
package cn.edu.hbut.java.exception;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
float[] a = new float[3];
boolean flag = false;
do {
try {
System.out.println("请输入三边的长度:");
for(int i=0;i<3;i++) {
a[i] = sc.nextFloat();
}
Triangle t = new Triangle(a[0],a[1],a[2]);
t.getArea();
t.getCircumference();
flag = true;
}catch(TriangleException e) {
System.out.println(e.getMessage());
sc.nextLine();
continue;
}
}while(!flag);
}
}