《JAVA程序设计》作业实验报告

实验一  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);
	}
}

三、实验结果

实验三  类与对象高级机制

一、实验内容

  1. 编写一个名为TwoDimensionalShape的抽象类,拥有属性area和circumference表示面积和周长,以及抽象方法getArea()和getCircumference(),用于获取面积和周长。
  2. 编写Printable接口,包括一个抽象方法printShapeInfo,用于输出图形信息。
  3. 分别编写Rectangle、Triangle、Circular三个类,用于描述矩形、三角形和圆形,要求继承于TwoDimensionalShap类,并实现Printable接口,每个图形有各自的构造方法,矩形有length和width字段表示长和宽,三角形有base、hypotenus1和hypotenus2字段表示底和两条斜边,圆形有radius字段表示半径,按几何常识实现父类中所定义的抽象方法,printShapeInfo方法要求能够输出:(1)图形的类型(使用getClass方法)(2)图形的基本数据(使用自定义toString方法,输出图形的字段,输出示例:3.0 4.0
  4. 编写一个名为ShapeDetector的类,拥有三个重载的detectShape方法,分别为detectShape(Rectangle r)、detectShape(Triangle t)、detectShape(Circular c),分别输出所接收到的图形的类型。
  5. 编写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);
	}
}

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java程序设计实验报告 实验一 实验题目:从键盘上读入10个字符串存入数组a中,然后输出这10个字符串中最大字符串 和最小字符串。 实验代码: public class StrPro { public static void main(String[] args) { String str[] = new String[5]; System.out.println("Please input 10 strings:"); int i; String max,min; for(i=0;i<5;i++){ System.out.print("Please input the "+(i+1)+" string:"); Scanner sc = new Scanner(System.in); str[i] = sc.nextLine(); } max = str[0]; min = str[0]; for(i=0;i<str.length;i++){ if(max.compareTo(str[i])<0){ max = str[i]; } if(min.compareTo(str[i])>0){ min = str[i]; } } System.out.println("最大的字符串为:"+max); System.out.println("最小的字符串为:"+min); } } 实验结果: 实验心得体会: 掌握了java的基本语法,数组的定义与使用,做这个实验要了解字符串数组的定义 及字符串数组的输入方法,还有比较字符串数组的大小的调用方法等。 实验二 实验题目: 自定义一个矩形类(Rectangle),包含的属性有:长(length),宽(width), 包含的方法有:关于属性的setter和getter方法,即setLength,getLength,setWidth ,getWidth,计算矩形面积的方法(getArea)。 定义矩形类的子类正方形类(Square),包含的属性和方法自行确定,要求完成的 功能是,能计算正方形的面积。 定义一个测试类(Test),测试矩形类和正方形类能否正确的计算面积。 以上类中属性和方法的访问权限自行确定,方法和构造方法若有参数,也自行确定 。 实验代码: public class Rectangle { int Length; int Width; public int getLength() { return Length; } public void setLength(int length) { Length = length; } public int getWidth() { return Width; } public void setWidth(int width) { Width = width; } int getArea(){ return Length * Width; } } public class Square extends Rectangle{ Square(int border) { super.setLength(border); super.setWidth(border); } } public class Test { public void test(){ System.out.println("请选择计算的形状的序号:1.矩形 2.正方形"); Scanner sc = new Scanner(System.in); int i = sc.nextInt(); int len,wid; if(i==1){ System.out.print("请输入矩形的长:"); Scanner s = new Scanner(System.in); len = s.nextInt(); System.out.print("请输入矩形的宽:"); wid = s.nextInt(); Rectangle re = new Rectangle(); re.setLength(len); re.setWidth(wid); System.out.println("矩形面积为:"+re.getArea()); } else if(i==2){ System.out.print("请输入正方形的边长:"); Scanner s = new Scanner(System.in); len = s.nextInt(); Square sq = new Square(len); System.out.println("正方形面积为:"+sq.getArea()); } else{ System.out.println("输入错误!"); } } public static v
Java程序设计基础是指学习和掌握Java编程语言的基本概念、语法和技巧。下面是一些Java程序设计基础的内容: 1. Java语言特点:Java是一种面向对象的编程语言,具有简单、可移植、安全、高性能等特点。 2. 基本语法:Java的基本语法包括变量、数据类型、运算符、控制流程(条件语句、循环语句)、数组等。 3. 面向对象编程:Java是一种面向对象的编程语言,具有封装、继承、多态等特性。学习面向对象编程的概念和原则是Java程序设计的基础。 4. 类和对象:Java中的类是对象的模板,对象是类的实例。学习如何定义类、创建对象、访问对象的属性和方法等是Java程序设计的基础。 5. 方法和函数:Java中的方法用于封装可重用的代码块,函数是方法的一种殊形式。学习如何定义和调用方法、传参数等是Java程序设计的基础。 6. 异常处理:Java提供了异常处理机制,用于处理程序运行过程中可能出现的异常情况。学习如何捕获和处理异常是Java程序设计的基础。 7. 输入输出:Java提供了丰富的输入输出类和方法,用于处理文件、网络等输入输出操作。学习如何读写文件、网络编程等是Java程序设计的基础。 8. 集合框架:Java提供了一套集合框架,用于存储和操作数据集合。学习如何使用集合框架进行数据操作是Java程序设计的基础。 9. 多线程编程:Java支持多线程编程,可以同时执行多个任务。学习如何创建和管理线程、线程同步等是Java程序设计的基础。 10. GUI编程:Java提供了Swing和JavaFX等图形用户界面库,用于开发图形界面应用程序。学习如何使用GUI库进行界面设计和事件处理是Java程序设计的基础。 以上是Java程序设计基础的一些内容,希望对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值