Java-学校项目3---类与对象

项目3 类与对象

实验目的:掌握Java类的结构、类的定义、方法和属性的定义以及对象的实现;掌握类及其成员修饰符的使用;掌握构造函数的使用;方法的参数传递和返回值的用法;掌握类变量与实例变量,以及类方法与实例方法的区别。
实验性质:验证性实验+设计性实验
实验内容:
(1)分析调试教材的第3章中的实例
(2)编写程序,实现如下描述。
定义一个长方形类MyBox,成员变量有length(长)、width(宽),方法分别有计算面积、周长、修改长、修改宽等。
在另一个类中使用此类的对象,验证其正确性。

package 实验三;
/**
 * 定义一个长方形类MyBox,成员变量有length(长)、width(宽),方法分别有计算面积、周长、修改长、修改宽等。
 * 在另一个类中使用此类的对象,验证其正确性。
 */
public class Box {
	public static void main(String[] args) {
		MyBox m=new MyBox(9,1);
		System.out.println("初始长方形长为9、宽为1");
		m.setLength(4);
		System.out.println("修改长方形长为4");
		m.setWidth(3);
		System.out.println("修改长方形宽为3");
		m.area();
		m.Circumference();
	}
}
class MyBox//长方形类
{
	double length;//长
	double width;//宽
	public void setLength(double length)//设置长
	{
		this.length = length;
	}
	public void setWidth(double width)//设置宽
	{
		this.width = width;
	}
	MyBox(int a,int b)
	{
		length=a;
		width=b;
	}
	void area()//面积
	{
		System.out.println("面积"+length*width);
	}
	void Circumference()//周长
	{
		System.out.println("周长"+(length+width)*2);
	}
}

(3)定义满足以下要求的复数类Complex
属性: realPart(int型,代表复数的实数部分)
imagePart(int型,代表复数的虚数部分)
方法:
 构造方法:Complex():将实部和虚部设置为0;
Complex(int r,int i):将实部和虚部分别设置为r和i;
 Complex addComplex(Complex a)(计算当前复数对象与参数对象a的相加之和,结果存储于当前对象,运算规则:(x1,y1)+(x2,y2)=>(x1+x2,y1+y2)
 int dotProduct(Complex a)(计算当前复数对象与参数对象a的相乘,运算规则:(x1,y1)(x2,y2)=>x1x2+y1+y2 )
 void printComplex()(以3+2i的格式显示当前复数信息)
 static Complex addComplex(Complex a,Complex b)(计算参数对象a与b的相加之和,运算规则:(x1,y1)+(x2,y2)=>(x1+x2,y1+y2))
在另一个类中使用此类的对象,验证其正确性。

package 实验三;
/**
 * 复数加法和乘法实现
 */
public class Thrid {
	public static void main(String[] args) {
		Complex a=new Complex(1,2);
		Complex b=new Complex(3,4);
		System.out.print("复数a:");
		a.printComplex();
		System.out.print("复数b:");
		b.printComplex();
		System.out.print("复数a+b:");
		a.ComplexaddComplex(b);
		a.printComplex();
		a.setRealPart(1);a.setImagePart(2);//复原复数a
		System.out.print("复数a*b:");
		a.dotProduct(b);
		a.printComplex();
		a.setRealPart(1);a.setImagePart(2);//复原复数a
		Complex.ComplexaddComplex(a, b);
	}
}
class Complex//复数类
{
	int realPart;//int型,代表复数的实数部分
	int imagePart;//int型,代表复数的虚数部分
	public void setRealPart(int realPart) {
		this.realPart = realPart;
	}
	public void setImagePart(int imagePart) {
		this.imagePart = imagePart;
	}
	Complex()
	{
		realPart=0;
		imagePart=0;
	}
	Complex(int r,int i)
	{
		this.realPart=r;
		this.imagePart=i;
	}
	void ComplexaddComplex(Complex a)//计算当前复数对象与参数对象a的相加之和,结果存储于当前对象
	{
		this.realPart=this.realPart+a.realPart;
		this.imagePart=this.imagePart+a.imagePart;
	}
	int dotProduct(Complex a)//计算当前复数对象与参数对象a的相乘z1 × z2=(ac-bd,bc+ad)
	{
		int r=this.realPart*a.realPart-this.imagePart*a.imagePart;
		int i=this.imagePart*a.realPart+this.realPart*a.imagePart;
		this.realPart=r;
		this.imagePart=i;
		return 0;
	}
	public void printComplex()//以3+2i的格式显示当前复数信息
	{
		System.out.println(this.realPart+"+"+this.imagePart+"i");
	}
	static void ComplexaddComplex(Complex a,Complex b)//计算参数对象a与b的相加之和
	{
		int r=a.realPart+b.realPart;
		int i=a.imagePart+b.imagePart;
		System.out.println("复数相加和:"+r+"+"+i+"i");
	}
}

(4)编写一个完整的Application程序,包含类Student、TestStudent具体要求如下:
①Student类:
属性: number:String, 学号; name:String, 姓名;
sex:char,性别; specialty:String,专业;
address:String,家庭地址;
方法:
 构造函数:无参格式(各数据设置为默认值);
有参格式(利用参数对学号、姓名、专业进行设置);
 访问器/修改器:对各属性数据进行读取和设置;
 public String toString():返回学生的各项信息
 public boolean equals(Student s):基于学号判断当前对象与参数对象是否为同一个学生;
②TestStudent作为主类完成测试功能。

package 实验三;
import java.util.Scanner;
/**
 * 学生信息程序
 * 功能: 1、录入学生信息
 * 		2、设置学生信息
 * 		3、浏览学生信息
 * 		4、通过学号核对学生信息
 */
public class TestStudent {
	
	static Student s=new Student();
	
	public static void main(String[] args) {
		int button;//按钮
		Scanner sc=new Scanner(System.in);
		System.out.println("------------------------------");
		System.out.println("-----------欢迎使用-------------");
		while(true)
		{
			System.out.println("------------------------------");
			System.out.println("1、录入学生信息");
			System.out.println("2、设置学生信息");
			System.out.println("3、浏览学生信息");
			System.out.println("4、通过学号核对学生信息");
			System.out.println("5、退出");
			System.out.println("------------------------------");
			System.out.print("请输入:");
			button=sc.nextInt();
			switch(button)
			{case 1:luru();break;
			case 2:set();break;
			case 3:liulan();break;
			case 4:hedui();break;
			case 5:break;
			default:System.out.println("请重新输入");break;
			}
			if(button==5)
				break;
		}
	}
	public static void luru()//1、录入学生信息
	{
		Scanner sc=new Scanner(System.in);
		System.out.print("请输入学号:");
		s.number=sc.nextLine();
		System.out.print("请输入姓名:");
		s.name=sc.nextLine();
		while(true) {
		System.out.print("请输入性别(0-男、1-女):");
		int sex=sc.nextInt();
		if(sex==0)
			{s.sex='男';break;}
		else if(sex==1)
			{s.sex='女';break;}
		else
			System.out.print("请重新输入:");
		}
		System.out.print("请输入专业:");
		sc=new Scanner(System.in);
		s.specialty=sc.nextLine();
		System.out.print("请输入家庭地址:");
		s.address=sc.nextLine();
	}
	public static void set()//2、设置学生信息
	{
		Scanner sc=new Scanner(System.in);
		while(true)
		{
			System.out.println("------------------------------");
			System.out.println("1、设置学号");
			System.out.println("2、设置姓名");
			System.out.println("3、设置性别");
			System.out.println("4、设置专业");
			System.out.println("5、设置家庭住址");
			System.out.println("6、退出");
			System.out.println("------------------------------");
			System.out.print("请输入:");
			int button=sc.nextInt();
			sc=new Scanner(System.in);
			switch(button)
			{case 1:{System.out.print("请输入学号:");s.number=sc.nextLine();}break;
			case 2:{System.out.print("请输入姓名:");s.name=sc.nextLine();}break;
			case 3:{while(true) {
					System.out.print("请输入性别(0-男、1-女):");
					int sex=sc.nextInt();
					if(sex==0)
						{s.sex='男';break;}
					else if(sex==1)
						{s.sex='女';break;}
					else
						System.out.print("请重新输入:");
						}
					}break;
			case 4:{System.out.print("请输入专业:");sc=new Scanner(System.in);
			s.specialty=sc.nextLine();}break;
			case 5:{System.out.print("请输入家庭地址:");s.address=sc.nextLine();}break;
			case 6:break;
			default:System.out.println("请重新输入");break;
			}
			if(button==6)
				break;
		}
	}
	public static void liulan()//3、浏览学生信息
	{
		System.out.println(s.toSring());
	}
	public static void hedui()//4、通过学号核对学生信息
	{
		Scanner sc=new Scanner(System.in);
		Student b=new Student();
		System.out.print("请输入学号:");b.number=sc.nextLine();
		if(s.equals(b))
			System.out.println("同一个学生");
		else
			System.out.println("不是同一个学生");;
	}
}
class Student
{
	String number;//学号
	String name;//名字
	char sex;//性别
	String specialty;//专业
	String address;//家庭地址
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public char getSex() {
		return sex;
	}
	public void setSex(char sex) {
		this.sex = sex;
	}
	public String getSpecialty() {
		return specialty;
	}
	public void setSpecialty(String specialty) {
		this.specialty = specialty;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	Student()
	{
		number=null;//学号
		name=null;//名字
		sex='空';//性别
		specialty=null;//专业
		address=null;//家庭地址
	}
	Student(String number,String name,String specialty)
	{
		this.number=number;
		this.name=name;
		this.specialty=specialty;
	}
	public String toSring()//返回学生的各项信息
	{
		String s=new String("学号:"+number+"、姓名:"+name+"、性别:"+sex+"、专业:"+specialty+"、家庭地址:"+address);
		return s;
	}
	public boolean equals(Student s)
	{//基于学号判断当前对象与参数对象是否为同一个学生
		if(this.number.equalsIgnoreCase(s.number))
			return true;
		else 
			return false;
	}
}

(5)定义一个描述时间信息的类MyDateTime:
属性:year,month,day,hour,minute,second
方法:
 构造方法:3个
 分别将6个属性设置为0;
 根据6个整型形参设置属性;
 根据另一个MyDateTime对象的属性进行设置;
 MyDateTime passTime(int length,int type)(在当前时间的基础上加上length时间段,时间段单位由type决定:1-6分别代表年/月/日/时/分/秒)
 (选做)int diffDateTime(MyDateTime dayx ,int measure )(计算当前时间与参数对象dayx之间相差的时间段,时间段单位由measure决定:1-3分别代表天/时/秒)
 int dayInYear()(计算当前时间是一年中的第几天)
其他函数自行完善。
在另一个类中使用此类的对象,验证其正确性。

package 实验三;
import java.util.*;
import java.text.SimpleDateFormat;
public class MyDate {
	public static void main(String[] args){
		//当前系统时间
		Date day=new Date();
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("当前系统时间:"+df.format(day));
		Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
		//获取当前系统时间
		int yyyy = c.get(Calendar.YEAR); 
		int MM= c.get(Calendar.MONTH)+1; 
		int dd = c.get(Calendar.DATE); 
		int HH = c.get(Calendar.HOUR_OF_DAY); 
		int mm= c.get(Calendar.MINUTE); 
		int ss= c.get(Calendar.SECOND);
        MyDateTime m=new MyDateTime(yyyy,MM,dd,HH,mm,ss);
        MyDateTime dayx=new MyDateTime(m);
        System.out.println("当前对象dayx时间:"+dayx.getYear()
        +"-"+dayx.getMonth()+"-"+dayx.getDay()+"\t"+dayx.getHour()
        +":"+dayx.getHour()+":"+dayx.getMinute()+":"+dayx.getSecond());
        dayx.passTime(1, 3);//加一天
        System.out.println("加一天后:"+dayx.getYear()
        +"-"+dayx.getMonth()+"-"+dayx.getDay()+"\t"+dayx.getHour()
        +":"+dayx.getHour()+":"+dayx.getMinute()+":"+dayx.getSecond());
        System.out.println("当前系统时间与dayx差:"+m.diffDateTime(dayx,1)+"天");
        System.out.println("当前是一年中的第:"+m.dayinYear()+"天");
	}
}
class MyDateTime
{
	private int year;
	private int month;
	private int day;
	private int hour;
	private int minute;
	private int second;
	public int getYear() {
		return year;
	}
	public int getMonth() {
		return month;
	}
	public int getDay() {
		return day;
	}
	public int getHour() {
		return hour;
	}
	public int getMinute() {
		return minute;
	}
	public int getSecond() {
		return second;
	}
	MyDateTime()//分别将6个属性设置为0
	{
		this.year=0;
		this.month=0;
		this.day=0;
		this.hour=0;
		this.minute=0;
		this.second=0;
	}
	MyDateTime(int year,int month,int day,int hour,int minute,int second)//根据6个参数设置属性
	{
		this.year=year;
		this.month=month;
		this.day=day;
		this.hour=hour;
		this.minute=minute;
		this.second=second;
	}
	MyDateTime(MyDateTime m)//根据另一个MyDateTime对象进行属性设置
	{
		this.year=m.year;
		this.month=m.month;
		this.day=m.day;
		this.hour=m.hour;
		this.minute=m.minute;
		this.second=m.second;
	}
	MyDateTime passTime(int length,int type)//在当前时间的基础上加length
	{
		switch(type)
		{case 1:this.year+=length;break;
		case 2:this.month+=length;break;
		case 3:this.day+=length;break;
		case 4:this.hour+=length;break;
		case 5:this.minute+=length;break;
		case 6:this.second+=length;break;
		}
		return this;
	}
	int diffDateTime(MyDateTime dayx,int measure)//计算当前时间与参数对象dayx之间相差的时间段
	{
		switch(measure)
		{case 1:return dayx.day-this.day;
		case 2:return dayx.hour-this.hour;
		case 3:return dayx.minute-this.minute;
		}
		return 0;
	}
	int dayinYear()//计算当前时间是一年中的第几天
	{
		int sum=this.day;
		for(int i=1;i<this.month;i++)
		{
			switch(i)
			{case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:sum+=31;break;
			case 4:
			case 6:
			case 9:
			case 11:sum+=30;break;
			case 2:{if(this.year%4==0)
						sum+=28;
					else
						sum+=29;}break;
			}
		}
		return sum;
	}
}

(6)自行设计程序,练习包的使用。
在这里插入图片描述

package World;
import Hello.Hello;;
public class World {
   public static void main(String[] args) {
   	Hello h=new Hello();
   	System.out.println(h.getS()+" world!");
   }
}
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值