java实验.docx

上传人:b****5 文档编号:29171406 上传时间:2023-07-21 格式:DOCX 页数:13 大小:142.39KB
下载 相关 举报
java实验.docx_第1页
第1页 / 共13页
java实验.docx_第2页
第2页 / 共13页
java实验.docx_第3页
第3页 / 共13页
java实验.docx_第4页
第4页 / 共13页
java实验.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

java实验.docx

《java实验.docx》由会员分享,可在线阅读,更多相关《java实验.docx(13页珍藏版)》请在冰豆网上搜索。

java实验.docx

java实验

 

实验三:

Java语言面向对象基础编程

实验目的:

1.能够正确的定义类,生成对象和使用对象。

2.能够使用对象组合来实现重用

3.能使用继承和多态来实现面向对象编程

4.初步学会使用面向对象编程

实验内容

1.简单类的定义。

2.对象创建和使用。

3.使用关联类进行属性的定义。

4.访问控制修饰符的作用。

5.掌握静态属性、方法和初始化器的特点。

6.包的应用

实验数据记录及分析(或程序及运行结果)

1、写一个名为Rectangle的类表示矩形。

其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。

要求该类提供计算面积的方法getArea()方法,以及修改width和height的值及获得width和height当前值的方法。

要求:

(1)使用构造函数完成各属性的初始赋值

(2)使用getter和setter的形式完成属性的访问及修改

publicclassRecTangle{

privatedoubleheight;

privatedoublewidth;

privateStringcolor;

publicdoublegetHeight(){

returnheight;

}

publicvoidsetHeight(doubleheight){

this.height=height;

}

publicdoublegetWidth(){

returnwidth;

}

publicvoidsetWidth(doublewidth){

this.width=width;

}

publicStringgetColor(){

returncolor;

}

publicvoidsetColor(Stringcolor){

this.color=color;

}

publicRecTangle(doublewidth,doubleheight,Stringcolor){

this.setColor(color);

this.setHeight(height);

this.setWidth(width);

}

publicvoidgetArea(){

doublearea=0;

area=this.height*this.width;

System.out.println("矩形的面积为"+area);

}

publicStringtoString(){

StringrecStr="矩形的高度:

"+this.getHeight()+"宽度:

"+this.getWidth()+"颜色:

"+this.getColor();

returnrecStr;

}/***测试函数*@paramargs*/

publicstaticvoidmain(String[]args){

RecTanglerec=newRecTangle(3,4,"红色");

rec.getArea();

System.out.println(rec.toString());

}

}

2·一个Fan有自己的品牌和型号,其功能有start,stop,speed,start和stop可以改变Fan的状态status(on/off),speed可以调整Fan的速度(档级在0、1、2、3),请分析并提

importjava.util.Scanner;

publicclassFan{

privateStringbrand;

privateStringtype;

privateintlevel;

@SuppressWarnings("unused")

privateStringstatus;

publicFan(intlevel,Stringstatus){

this.brand="midea";

this.type="hero-007";

this.level=level;

this.status=status;

}

publicvoidstart(){

this.level=1;

this.status="on";

System.out.printf("风扇已启动转速是%d\n",this.level);

}

publicvoidstop(intlevel,Stringstatus){

this.level=level;

this.status=status;

System.out.printf("风扇已关闭进入待机状态\n");

}

publicvoidsetspeed(intlevel){

this.level=level;

System.out.printf("你已经设置的转速等级是%d\n",this.level);

}

publicvoidspeed(){

System.out.printf("请设置你要的转速*1,2,3*\n");

Scannerconn=newScanner(System.in);

intle=conn.nextInt();

//System.out.printf("1123");

switch(le){

//conn.close();

case1:

this.setspeed

(1);break;

case2:

this.setspeed

(2);break;

case3:

this.setspeed(3);break;

case0:

this.stop(0,"off");break;

//default:

System.out.printf("你设置的转速为"this.level);

}

}

publicstaticvoidmain(String[]args){

Fanf=newFan(1,"on");

Scannercon=newScanner(System.in);

System.out.printf("^^欢迎使用%s公司的产品,你用的型号是%s^^\n",f.brand,f.type);

while(1>0){

System.out.printf("请输入你要设置有start,stop,speed分别有数字1,2,3替代");{

//if(con.hasNextInt())

intac=con.nextInt();

switch(ac){

case1:

{f.start();break;}

case2:

{f.stop(0,"off");break;}

case3:

{f.speed();break;}

}

}

}

}

3.一个计算机商店销售很多品牌的计算机,每台计算机都应该记录其配置信息,包括处理器、主板、显示器、内存、硬盘等基本设备,每个设备都有自己的品牌、价格、型号信息,请你尝试构造合适的类并利用组合的方法来表示计算机,并为该计算机类添加计算价格(各设备价格之和)、打印配置信息等方法。

要求:

将定义的类都放在一个包product内。

packageproduct;

publicabstractclassProduct{

privateStringbrand;//品牌

privatedoubleprice;//价格

privateStringmodel;//型号

publicStringgetBrand(){

returnbrand;

}

publicvoidsetBrand(Stringbrand){

this.brand=brand;

}

publicStringgetModel(){

returnmodel;

}

publicvoidsetModel(Stringmodel){

this.model=model;

}

publicdoublegetPrice(){

returnprice;

}

publicvoidsetPrice(doubleprice){

this.price=price;

}

publicStringtoString(){

returnbrand+""+model+""+price;

}

}

packageproduct;

publicclassCPUextendsProduct{

}

packageproduct;

publicclassMemoryextendsProduct{

}

packageproduct;

publicclassHardDiskextendsProduct{

}

packageproduct;

publicclassMotherboardextendsProduct{

}

packageproduct;

publicclassMonitorextendsProduct{

}

packageproduct;

publicclassComputer{

privateCPUcpu;

privateMemorymemory;

privateHardDiskhardDisk;

privateMotherboardmotherboard;

privateMonitormonitor;

publicCPUgetCpu(){

returncpu;

}

publicvoidsetCpu(CPUcpu){

this.cpu=cpu;

}

publicHardDiskgetHardDisk(){

returnhardDisk;

}

publicvoidsetHardDisk(HardDiskhardDisk){

this.hardDisk=hardDisk;

}

publicMemorygetMemory(){

returnmemory;

}

publicvoidsetMemory(Memorymemory){

this.memory=memory;

}

publicMonitorgetMonitor(){

returnmonitor;

}

publicvoidsetMonitor(Monitormonitor){

this.monitor=monitor;

}

publicMotherboardgetMotherboard(){

returnmotherboard;

}

publicvoidsetMotherboard(Motherboardmotherboard){

this.motherboard=motherboard;

}

publicStringtoString(){

return"主板:

"+motherboard.toString()+"\n"

+"CPU:

"+cpu.toString()+"\n"

+"内存:

"+memory.toString()+"\n"

+"硬盘:

"+hardDisk.toString()+"\n"

+"显示器:

"+monitor.toString();

}

publicvoidprintDetail(){

System.out.println(this.toString());

System.out.println("总价:

"+totalPrice());

}

publicdoubletotalPrice(){

returncpu.getPrice()+memory.getPrice()

+motherboard.getPrice()+hardDisk.getPrice()

+monitor.getPrice();

}

}

 

4.实现一个如下功能的程序:

程序包含了3个类Main,Circle,Point。

Point表示一个点,含有属性x、y和方法movex、movey、move、distanceFrom,属性x、y表示圆的中心点坐标,3个move方法用来实现圆的移动,方法distanceFrom用来计算当前圆和另外一个圆的距离。

类Circle的属性r表示圆的半径,默认为5;属性center表示圆的圆心;静态公有字段pi定义为值3.14;方法len返回圆的周长,方法area返回圆的面积;3个move方法用来移动圆;print方法用来输出圆的信息,格式如”圆心为(2,12),半径为4”;方法isInset用来获取当前圆是否和另外一个圆相交。

Main类保护主方法main,在主方法main中,你需要完成以下工作:

(1)新建一个圆对象c1,圆心为(20,10),半径为10。

(2)输出该圆的信息,然后输出该圆的周长和面积。

(3)创建另外一个圆c2,圆心为(20,-10),半径为5。

判断两个圆c1,c2是否相交。

(4)将圆c1沿着x方向移动-6,然后输出圆c1的信息,再判断圆c1和c2是否相交。

(5)将圆c2沿着x方向移动1,y方向移动1,然后输出圆c2的信息,在判断圆c1和c2是否相交。

 

packageCircle;

publicclassPoint{

doublex,y;

Point(){

x=0;

y=0;

}

Point(doublex,doubley){

this.x=x;

this.y=y;

}

doublemovex(doublex1){

returnx=x+x1;

}

doublemovey(doubley1){

returny=y+y1;

}

voidmove(doublex1,doubley1){

movex(x1);

movey(y1);

}

doubledistanceFrom(doublex1,doubley1,doublex2,doubley2){

returnMath.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

}

}

///////////////////////////////////////

packageCircle;

publicclassCircleextendsPoint{

doubler=5;

publicPointcenter;

publicstaticdoublepi=3.14;

doublelen(){

return2*pi*r;

}

doublearea(){

returnpi*r*r;

}

voidprint(){

System.out.println("该圆的圆心为:

"+"("+center.x+","+center.y+")"+"半径为:

"+r);

}

booleanisInset(doubledistanceFrom,doubleR){//R为两个圆的半径之和

if(distanceFrom<=R)returntrue;

returnfalse;

}

 

}

//////////////////////////////////////////

packageCircle;

publicclassMain{

publicstaticvoidmain(String[]args){

Circlec1=newCircle();

Pointp1=newPoint(20,10);

c1.center=p1;

c1.r=10;

c1.print();

System.out.println("圆的周长为:

"+c1.len()+"圆的面积为:

"+c1.area());

Circlec2=newCircle();

Pointp2=newPoint(20,-10);

c2.center=p2;

c2.r=5;

doubledistanceFrom=c1.distanceFrom(p1.x,p1.y,p2.x,p2.y);

System.out.println("此时两圆心之间的距离为:

"+distanceFrom);

if(c2.isInset(distanceFrom,c1.r+c2.r))

System.out.println("两圆相交");

elseSystem.out.println("两圆不相交");

p1.movex(-6);

System.out.println("将圆c1沿着x方向移动-6后:

");

c1.print();

distanceFrom=c1.distanceFrom(p1.x,p1.y,p2.x,p2.y);

System.out.println("此时两圆心之间的距离为:

"+distanceFrom);

if(c2.isInset(distanceFrom,c1.r+c2.r))

System.out.println("两圆相交");

elseSystem.out.println("两圆不相交");

p2.move(1,1);

System.out.println("将圆c2沿着x方向移动1,y方向移动1后:

");

c2.print();

distanceFrom=c1.distanceFrom(p1.x,p1.y,p2.x,p2.y);

System.out.println("此时两圆心之间的距离为:

"+distanceFrom);

if(c2.isInset(distanceFrom,c1.r+c2.r))

System.out.println("两圆相交");

elseSystem.out.println("两圆不相交");

}

}

 

学号:

姓名:

日期:

年月日

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1