【JAVA课程作业】实验四 类和对象

实验四 类和对象;

类的继承和派生;多态性; 接口;构造器应用
一、实验目的
1 、 掌握类与对象的关系;
2 、 掌握类的定义;
3 、 掌握对象的声明及使用;
4 、 掌握构造方法的概念及调用时机;
5 、 掌握构造方法的重载;
6 、 掌握匿名对象的使用。

二、实验学时
2 学时
三、实验类型 验证性实验
四、实验需求
1、硬件
每位学生配备计算机一台
2、软件
Windows XP 操作系统, JDK,eclipse ,MySQL
3、网络

4、工具

五、实验理论与预备知识
1 、 类与对象的关系;
2 、 类的定义;
3 、 对象的声明及使用;
4 、 构造方法的概念及调用时机;
5 、 构造方法的重载;
6 、 匿名对象的使用。

六、实验内容与结果
1 、 编写一个应用程序, 该程序中有 3 个类: Trangle 、Leder 和 Circle,分别用来刻画“三角形”、“梯 形”和“圆形”。

package book;

import java.util.Scanner;

class Trangle
{
	private double a,b,c;
	private double C,S;
	//构造函数
	public Trangle(double a,double b,double c) 
	{
		System.out.println("------------------正在计算三角形---------------------");
		this.setA(a);
		this.setB(b);
		this.setC(c);
	}
	//设置a,b,c的set,get函数
	public void setA(double a) {this.a=a;}
	public void setB(double b) {this.b=b;}
	public void setC(double c) {this.c=c;}
	public double getA() {return this.a;}
	public double getB() {return this.b;}
	public double getC() {return this.c;}
	//周长
	public double returnC() {return this.a+this.b+this.c;}
	//面积
	public double returnS() 
	{
		double s=(this.a+this.b+this.c)/2;
		this.S=Math.sqrt(s*(s-this.a)*(s-this.b)*(s-this.c));
		return this.S;
	}
	//是否三角形
	public boolean yes_no_trangle() 
	{
		if(this.a+this.b>this.c&&this.a+this.c>this.b&&this.b+this.c>this.a)
			return true;
		else if(this.a<0||this.b<0||this.c<0)
			return false;
		else 
		    return false;
	}	
}

class Leder
{
	private double top,bottom,h;
	private double S;
	public Leder(double top,double bottom,double h) 
	{
		System.out.println("------------------正在计算梯形:---------------");
		this.settop(top);
		this.setbottom(bottom);
		this.seth(h);
	}
	//设置属性值
	public void settop(double top) {this.top=top;}
	public void setbottom(double bottom){this.bottom=bottom;}
	public void seth(double h){this.h=h;}
	public double gettop() {return this.top;}
	public double getbottom() {return this.bottom;}
	public double geth() {return this.h;}
	//面积
	public double returnS()
	{
		this.S=(this.top+this.bottom)*this.h/2.0;
		return this.S;
	}
}
class Circle
{
	private double r;
	private double C,S;
	public Circle(double r) 
	{
		System.out.println("-----------------正在计算圆形-----------------");
		this.setr(r);
	}
	//设置属性值
	public void setr(double r) {this.r=r;}
	public double getr() {return this.r;}
	//周长
	public double returnC() 
	{
		this.C=2*Math.PI*this.r;
		return this.C;
	}
	//面积
	public double returnS() 
	{
		this.S=Math.PI*Math.pow(r, 2);
		return this.S;
	}
}

public class Work4_1 
{
	
	public static void main(String[] args) 
	{
		System.out.println("选择计算类型:1.三角形 2.梯形 3.圆形");
		boolean bo=true;
		while(bo)
		{
		Scanner type=new Scanner(System.in);
		int t=type.nextInt();		
		if(t==1)type1();
		else if(t==2)type2();
		else if(t==3)type3();
		else System.out.println("选择错误|重选");
		}
	}
	
	public static void type1() 
	{
		System.out.println("输入a,b,c:");
		Scanner input=new Scanner(System.in);
		double a=input.nextInt();
		double b=input.nextInt();
		double c=input.nextInt();
		Trangle x1=new Trangle(a,b,c);
		if(x1.yes_no_trangle())
		{
			System.out.println("是三角形(可以计算)");
			System.out.print("三角形周长:"+x1.returnC()+"\n");
			System.out.print("三角形面积:"+x1.returnS()+"\n");
		}
		else 
		{
			System.out.println("不是三角形(无法计算)");
		}

	}
	public static void type2() 
	{
		System.out.println("输入top,bottom,h:");
		Scanner input=new Scanner(System.in);
		double top=input.nextDouble();
		double bottom=input.nextDouble();
		double h=input.nextDouble();
		Leder x2=new Leder(top, bottom, h);
		System.out.println("梯形面积:"+x2.returnS());	
	}
	public static void type3() 
	{
		System.out.println("输入r:");
		Scanner input=new Scanner(System.in);
		double r=input.nextDouble();
		Circle x3=new Circle(r);
		System.out.println("圆形周长:"+x3.returnC());
		System.out.println("圆形面积:"+x3.returnS());
	}

}

2 、 编写一个公司员工类。
(1) 数据成员:员工号、姓名、薪水、部门。
(2) 方法: ①设置数据成员信息; ②显示信息。

要求:利用构造方法完成设置信息,其中有四个重载的构造方法
单参:只传递员工号,则员工姓名:无名氏。薪水: 0,部门:未定。
双参:传递员工号,姓名,则员工薪水为 1000,部门:后勤
4 参:传递员工号,姓名,部门,薪水
无参:则均为空值

package book;

import java.util.Scanner;

class Staff
{
	private String sno,name,partment;
	private int money;
	public Staff() 
	{
		
	}
	public Staff(String sno) 
	{
		this.setsno(sno);
	}
	public Staff(String sno,String name) 
	{
		this.setsno(sno);
		this.setname(name);
	}
	public Staff(String sno,String name,int money) 
	{
		this.setsno(sno);
		this.setname(name);
		this.setmoney(money);
	}
	public Staff(String sno,String name,int money,String partment) 
	{
		this.setsno(sno);
		this.setname(name);
		this.setmoney(money);
		this.setpartment(partment);
	}
	public void setsno(String sno) {this.sno=sno;}
	public void setname(String name) {this.name=name;}
	public void setmoney(int money) {this.money=money;}
	public void setpartment(String partment) {this.partment=partment;}
	public String getsno() {return this.sno;}
	public String getname() {return this.name;}
	public int getmoney() {return this.money;}
	public String getpartment() {return this.partment;}
	
}

public class Work4_2 
{

	public static void main(String[] args) 
	{
		System.out.println("输入参数个数:(0,1,2,3,4)");
		Scanner input=new Scanner(System.in);
		int t=input.nextInt();
		if(t==0)type0();
		else if(t==1)type1();
		else if(t==2)type2();
		else if(t==3)type3();
		else if(t==4)type4();
		else System.out.println("参数错误!!");
	}
	
	public static void type0() 
	{
		System.out.println("禁止输入!");
	}
	public static void type1() 
	{
		System.out.println("输入一个参数sno:");
		Scanner input=new Scanner(System.in);
		String sno=input.next();
		Staff x1=new Staff(sno);
		System.out.println("sno   name   money   partment");
		System.out.println("输出信息:"+x1.getsno());
	}
	public static void type2() 
	{
		System.out.println("输入两个参数sno,name:");
		Scanner input=new Scanner(System.in);
		String sno=input.next();
		String name=input.next();
		Staff x2=new Staff(sno,name);
		System.out.println("sno   name   money   partment");
		System.out.println("输出信息:"+x2.getsno()+"\t"+x2.getname());
	}
	public static void type3() 
	{
		System.out.println("输入三个参数sno,name,money:");
		Scanner input=new Scanner(System.in);
		String sno=input.next();
		String name=input.next();
		int money=input.nextInt();
		Staff x3=new Staff(sno,name,money);
		System.out.println("sno   name   money   partment");
		System.out.println("输出信息:"+x3.getsno()+"\t"+x3.getname()+"\t"+x3.getmoney());
	}
	public static void type4() 
	{
		System.out.println("输入四个参数sno,name,money,partment:");
		Scanner input=new Scanner(System.in);
		String sno=input.next();
		int money=input.nextInt();
		String name=input.next();
		String partment=input.next();
		Staff x4=new Staff(sno,name,money,partment);
		System.out.println("sno   name   money   partment");
		System.out.println("输出信息:"+x4.getsno()+"\t"+x4.getname()+"\t"+x4.getmoney()+"\t"+x4.getpartment());
	}
	

}

3 、 完成图书馆管理系统中相关的类定义。
(1) 定义与编写学生 student 类, 学生属性包括“学号”、“姓名”、“性别”、“专业”、“班级”。
(2) 定义与编写图书 book 类, 图书属性包括“书名”、“ISBN 号”、“册数”。
(3) 登陆角色 role 类,角色类包括“用户名”、“密码”、“角色类型”。

要求: 1、完成以上各类中的 setter 和 getter 接口
2、测试以上各类,并写出每个测试用例

package book;
class Student
{
	private int sno;
	private String name,sex,major,stu_class;
	public Student(int sno,String name,String sex,String major,String stu_class)
	{
		this.setmajor(major);
		this.setname(name);
		this.setsex(sex);
		this.setsno(sno);
		this.setstu_class(stu_class);
	}
	public void setsno(int sno) {this.sno=sno;}
	public void setname(String name) {this.name=name;}
	public void setsex(String sex) {this.sex=sex;}
	public void setmajor(String major) {this.major=major;}
	public void setstu_class(String stu_class) {this.stu_class=stu_class;}

	public int getsno() {return this.sno;}
	public String getname() {return this.name;}
	public String getsex() {return this.sex;}
	public String getmajor(String major) {return this.major;}
	public String getstu_class(String stu_class) {return this.stu_class;}

	public void info() 
	{
		System.out.println("学号:"+this.sno+"\t"+"姓名:"+this.name+"\t"+"性别:"+this.sex+"\t"+"专业:"+this.major+"\t"+"班级:"+this.stu_class);
	}
}
class B_ook
{
	private String name,bno;
	private int tatol;
	public B_ook(String name,String bno,int tatol)
	{
		this.setname(name);
		this.setbno(bno);
		this.settatol(tatol);
	}
	public void setname(String name) {this.name=name;}
	public void setbno(String  bno) {this.bno=bno;}
	public void settatol(int tatol) {this.tatol=tatol;}
	
	public String getname() {return this.name;}
	public String getbno() {return this.bno;}
	public int gettatol() {return this.tatol;}
	
	public void info() 
	{
		System.out.println("书名:"+this.name+"\t"+"ISBN号:"+this.bno+"\t"+"册数:"+this.tatol);
	}
	
}
class Role
{
	private String username,password,type;
	public Role(String username,String password,String type) 
	{
		this.setusername(username);
		this.setpassword(password);
		this.settype(type);
	}
	public void setusername(String username) {this.username=username;}
	public void setpassword(String  password) {this.password=password;}
	public void settype(String type) {this.type=type;}
	
	public String getusername() {return this.username;}
	public String getpassword() {return this.password;}
	public String gettype() {return this.type;}
	
	public void info() 
	{
		System.out.println("用户名:"+this.username+"\t"+"密码:"+this.password+"\t"+"角色类型:"+this.type);
	}
}
public class Work4_3 
{
//	(1) 定义与编写学生 student 类, 学生属性包括“学号”、“姓名”、“性别”、“专业”、“班级”。
//	(2) 定义与编写图书 book 类, 图书属性包括“书名”、“ISBN 号”、“册数”。
//	(3) 登陆角色 role 类,角色类包括“用户名”、“密码”、“角色类型”。
	public static void main(String[] args) 
	{
		
		Role role=new Role("dyh","d10001","student");
		Student st=new Student(2020188032, "杜艳贺","男","软件工程", "2020级软件工程本科班");
		B_ook book=new B_ook("数据库概述","ISBM001",10);
		st.info();
		book.info();
		role.info();
	}

}

4 、 字符串操作。
(1) 从字符串“Java 技术学习班 20121205”中提取开班日期。
(2) 将字符串“Java 技术学习班 20121205”中的“Java”替换为“JavaEE”。
(3) 取出“Java 技术学习班 20121205”第八个字符。
(4) 清除“Java 技术学习班 20121205”中所有的“0”。
(5) 从任意给定的身份证号中提取此人的出生日期。

package book;

public class Work4_4 
{

	public static void main(String[] args) 
	{
		System.out.println("--------1--------------------");
		String str1="Java 技术学习班 20121205";
		String time="999666200010015418";//7-14
		String str[]=str1.split(" ");
		System.out.println(str[2]);
		System.out.println("--------2--------------------");
		System.out.println(str1.replaceAll("Java", "JavaEE"));
		System.out.println("--------3--------------------");
		char s[]=str1.toCharArray();
		System.out.println(s[7]);
		System.out.println("--------4--------------------");
		System.out.println(str1.replaceAll("0",""));
		System.out.println("--------5--------------------");
		System.out.println(time.substring(6,14));
	}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿斯卡码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值