实验目的
掌握Java类的结构、类的定义、方法和属性的定义以及对象的实现;掌握类及其成员修饰符的使用;掌握构造函数的使用;方法的参数传递和返回值的用法;掌握类变量与实例变量,以及类方法与实例方法的区别。
实验性质
验证性实验+设计性实验
实验内容
(1)分析调试教材的第3章中的实例
(2)编写程序,实现如下描述。
定义一个长方形类MyBox,成员变量有length(长)、width(宽),方法分别有计算面积、周长、修改长、修改宽等。
在另一个类中使用此类的对象,验证其正确性。
import java.util.Scanner;
import java.math.BigDecimal;
class MyBox {
double lenth;
double width;
BigDecimal Area() {
//计算面积
BigDecimal a = BigDecimal.valueOf(lenth); //高精度计算使用BigDecimal类
BigDecimal b = BigDecimal.valueOf(width);
BigDecimal c = a.multiply(b);
return c;
}
double Perimeter() {
//计算周长
double perimeter = 2*(lenth+width);
return perimeter;
}
void modifylenth(double newlenth) {
//修改长
lenth=newlenth;
}
void modifywidth(double newwidth) {
//修改宽
width=newwidth;
}
}
public class User{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("请输入矩形的长:");
double newlenth = in.nextDouble();
System.out.print("请输入矩形的宽:");
double newwidth = in.nextDouble();
MyBox A = new MyBox();
A.modifylenth(newlenth);
A.modifywidth(newwidth);
System.out.println("周长为:"+A.Perimeter());
System.out.println("面积为:"+A.Area());
in.close();
}
}
(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))
- 在另一个类中使用此类的对象,验证其正确性。
public class User3 {
public static void main(String[] args) {
Complex user3=new Complex();
int r=4,i=5;
Complex a=new Complex(r,i);
Complex b=new Complex(6,7);
a.printComplex();
System.out.print("与");
b.printComplex();
System.out.print("复数和为");
(b.addComplex(a)).printComplex();//计算a与b的结果,应且返回
System.out.println();
Complex c=new Complex(8,9);
a.printComplex();
System.out.print("与");
c.printComplex();
System.out.println("复数积为"+c.dotProduct(a));
a.printComplex();
System.out.print("与");
b.printComplex();
System.out.print("复数和为");
(Complex.addComplex(a, b)).printComplex();
}}
class Complex{
int realPart;
int imagePart;
public Complex() {
realPart=0;
imagePart=0;
}
public Complex(int r,int i) {
realPart=r;
imagePart=i;
}
Complex addComplex(Complex a){
this.realPart=a.realPart+this.realPart;
this.imagePart=a.imagePart+this.imagePart;
return this;
}
int dotProduct(Complex a) {
int x=this.realPart*a.realPart+this.imagePart*a.imagePart;
return x;
}
void printComplex() {
System.out.print(this.realPart+"+"+this.imagePart+"i");
}
static Complex addComplex(Complex a,Complex b) {
Complex temp=new Complex();
temp.realPart=a.realPart+b.realPart;
temp.imagePart=a.imagePart+b.imagePart;
return temp;
}
}
(4)编写一个完整的Application程序,包含类Student、TestStudent具体要求如下:
- Student类:
属性: number:String, 学号; name:String, 姓名;
sex:char,性别; specialty:String,专业;
address:String,家庭地址;
方法:
构造函数:无参格式(各数据设置为默认值);
有参格式(利用参数对学号、姓名、专业进行设置);
访问器/修改器:对各属性数据进行读取和设置;
public String toString():返回学生的各项信息
public boolean equals(Student s):基于学号判断当前对象与参数对象是否为同一个学生; - TestStudent作为主类完成测试功能。
public class User4 {
public static void main(String[] args) {
Student studenttest = new Student();
System.out.println("无参构造函数默认值"+studenttest.toString());
Student student = new Student("180000","轩同学","XXX工程");
student.setSex('男');
student.setAddress("XXXXX");
System.out.println("参数对象与当前对象是否为同一人?"+student.equals("180000"));
System.out.println(student.toString());
}
}
class Student{
String number;
String name;
char sex;
String specialty;
String address;
public Student(){
number="000000";
name="XXXX大学";
sex='x';
specialty="本科";
address="XX省XX市XX区XX路XX号";
}
public Student(String number,String name,String specialty){
this.number=number;
this.name=name;
this.specialty=specialty;
}
public void setNumber(String number) {
this.number=number;
}
public String getNumber() {
return number;
}
public void setName(String number) {
this.number=number;
}
public String getName() {
return name;
}
public void setSex(char sex) {
this.sex=sex;
}
public char getSex() {
return sex;
}
public void setSpecialty(String specialty) {
this.specialty=specialty;
}
public String getSpecialty() {
return specialty;
}
public void setAddress(String address) {
this.address=address;
}
public String getAddress() {
return address;
}
public String toString() {
return number+" "+name+" "+sex+" "+specialty+" "+address;
}
public boolean equals(String number) {
if(this.number==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()(计算当前时间是一年中的第几天)
- 其他函数自行完善。
- 在另一个类中使用此类的对象,验证其正确性。
public class User5 {
public static void main(String[] args) {
MyDateTime user5 = new MyDateTime(2020,3,17,10,53,36);
MyDateTime a=new MyDateTime(user5);
System.out.println("今天是"+a.year+"年"+a.month+"月"+a.day+"日");
System.out.println("2020/3/17 是今年第"+user5.dayInYear()+"天");
}
}
class MyDateTime{
int year;
int month;
int day;
int hour;
int minute;
int second;
public MyDateTime() {
//无参构造方法 成员变量值设置为0
year=0;
month=0;
day=0;
hour=0;
minute=0;
second=0;
}
public MyDateTime(int year,int month,int day,int hour,int minute,int second) {
//有参构造方法
this.year=year;
this.month=month;
this.day=day;
this.hour=hour;
this.minute=minute;
this.day=day;
}
public MyDateTime(MyDateTime user) {
//根据另一个MyDateTime对象的属性进行设置
this.year=user.year;
this.month=user.month;
this.day=user.day;
this.hour=user.hour;
this.minute=user.minute;
this.day=user.day;
}
int dayInYear() {
//计算当前时间是一年中的第几天
int monthday=0;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) //31天的月份
if(day<32&&day>0)
{
monthday=31;}
else
System.out.print("输入错误,");
else if(month==2&&year%4==0&&year%100!=0) //普通闰年
if(day<30&&day>0)
{
monthday=29;}
else
System.out.print("输入错误,");
else if(month==2&&year%400==0&&year%100==0) //特殊闰年
if(day<30&&day>0)
{
monthday=29;}
else
System.out.print("输入错误,");
else if((month==2&&year%4!=0)||(year%100==0&&year%400!=0)) //非闰年
if(day<29&&day>0)
{
monthday=28;}
else
System.out.print("输入错误,");
else
if(day<31&&day>0) //30天的月份
{
monthday=30;}
else
System.out.print("输入错误,");
int days=0;
switch(month) {
case 12:days+=31;
case 11:days+=30;
case 10:days+=31;
case 9:days+=30;
case 8:days+=31;
case 7:days+=31;
case 6:days+=30;
case 5:days+=31;
case 4:days+=30;
case 3:days+=31;
case 2:{
if(year%4==0) days+=29;
else days+=28;
}
case 1:days+=31;
}
days=days-(monthday-day);
return days;
}
}
(6)自行设计程序,练习包的使用。
//将上面的(5)进行封装
package 项目3Test;
public class User6Test {
public void jisuan(int year,int month,int day) {
int days;
int monthday;
while(true) {
if(month>0&&month<13)
break;
else
System.out.print("输入错误,");
}
while(true) {
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) //31天的月份
if(day<32&&day>0)
{
monthday=31;break;}
else
System.out.print("输入错误,");
else if(month==2&&year%4==0&&year%100!=0) //普通闰年
if(day<30&&day>0)
{
monthday=29;break;}
else
System.out.print("输入错误,");
else if(month==2&&year%400==0&&year%100==0) //特殊闰年
if(day<30&&day>0)
{
monthday=29;break;}
else
System.out.print("输入错误,");
else if((month==2&&year%4!=0)||(year%100==0&&year%400!=0)) //非闰年
if(day<29&&day>0)
{
monthday=28;break;}
else
System.out.print("输入错误,");
else
if(day<31&&day>0) //30天的月份
{
monthday=30;break;}
else
System.out.print("输入错误,");
}
days=0;
switch(month) {
case 12:days+=31;
case 11:days+=30;
case 10:days+=31;
case 9:days+=30;
case 8:days+=31;
case 7:days+=31;
case 6:days+=30;
case 5:days+=31;
case 4:days+=30;
case 3:days+=31;
case 2:{
if(year%4==0) days+=29;
else days+=28;
}
case 1:days+=31;
}
days=days-(monthday-day);
System.out.println(year+"年"+month+"月"+day+"日,是今年第"+days+"天");
}
}
//导入封装好的包,直接调用
import 项目3Test.*;
public class User6 {
public static void main(String[] args) {
User6Test in = new User6Test();
in.jisuan(2020,3,17);
}
}