1350340122 刘光宇 实验三.docx
《1350340122 刘光宇 实验三.docx》由会员分享,可在线阅读,更多相关《1350340122 刘光宇 实验三.docx(9页珍藏版)》请在冰豆网上搜索。
1350340122刘光宇实验三
院(系)计算机学院专业计科(嵌入式)班级13计科3班
姓名刘光宇学号1350340122同组人无
实验室S4305组号日期2015.10.14
课程Java程序设计指导教师余慧成绩
实验项目编号8103200403实验项目名称面向对象编程
一、实验目的
熟悉并掌握Java中类的定义,类的成员变量和方法的定义。
二、实验环境
WindowsXP操作系统
Eclipse
三、实验原理
使用面向对象编程中设计类的基本原则,对实验二中的BuildingHouse程序进行重构
四、算法设计
【任务一】请重构实验二中的BuildingHouse程序。
五、主要代码及说明
【任务一】
Buildinghouse.java中:
importjava.util.Scanner;
publicclassBuildHouse{
publicstaticvoidmain(String[]args){
Scannersc=newScanner(System.in);
Stringtest;
test="livingroom";
System.out.println("nowroom:
"+test);
System.out.print("Pleaseimportroomname:
");
Roomdining=newRoom("dining");
dining.decoration="Acrystalchandelier";
Roomwdlivingroom=newRoomwd("livingroom");
//livingroom.name="livingroom";
livingroom.decoration="Anantiquecarpe";
livingroom.ShowDoor("Anantiquecarpe");
Roomwdkitchen=newRoomwd("kitchen");
//kitchen.name="kitchen";
kitchen.decoration="stainlesssteelapplience";
kitchen.ShowDoor("Stainlesssteelappliance");
Outwdbackyard=newOutwd("backyard");
//backyard.name="backyard";
backyard.temperature="hot";
backyard.ShowDoor("Ascreendoor");
OutwdFrontYard=newOutwd("FrontYard");
//FrontYard.name="FrontYard";
FrontYard.temperature="nothot";
FrontYard.ShowDoor("Anoakdoorwithabrassknob");
Outsidegarden=newOutside("garden");
//garden.name="garden";
garden.temperature="nothot";
garden.aisle[0]=FrontYard;
garden.aisle[1]=backyard;
FrontYard.aisle[0]=livingroom;
FrontYard.aisle[1]=garden;
backyard.aisle[0]=kitchen;
backyard.aisle[1]=garden;
kitchen.aisle[0]=dining;
kitchen.aisle[1]=backyard;
livingroom.aisle[0]=dining;
livingroom.aisle[1]=FrontYard;
dining.aisle[0]=livingroom;
dining.aisle[1]=kitchen;
while(true)
{
Stringhouse=sc.nextLine();
if(house.equals(dining.name))
{
if(test.equals(dining.aisle[0].name)||test.equals(dining.aisle[1].name))
{
System.out.println(dining.GetDecription());
test=house;
System.out.println("nowroom:
"+test);
System.out.print("pleaseimportroomname:
");
}
else
{
System.out.println("youcanoutcomeintheroom");
System.out.print("Pleaseimportroomname:
");
}
}
elseif(house.equals(livingroom.name))
{
if(test.equals(livingroom.aisle[0].name)||test.equals(livingroom.aisle[1].name))
{
System.out.println(livingroom.GetDecription());
test=house;
System.out.println("nowroom:
"+test);
System.out.print("pleaseimportroomname:
");
}
else
{
System.out.println("youcanoutcomeintheroom");
System.out.print("Pleaseimportroomname:
");
}
}
elseif(house.equals(kitchen.name))
{
if(test.equals(kitchen.aisle[0].name)||test.equals(kitchen.aisle[1].name))
{
System.out.println(kitchen.GetDecription());
test=house;
System.out.println("nowroom:
"+test);
System.out.print("pleaseimportroomname:
");
}
else
{
System.out.println("youcanoutcomeintheroom");
System.out.print("Pleaseimportroomname:
");
}
}
elseif(house.equals(backyard.name))
{
if(test.equals(backyard.aisle[0].name)||test.equals(backyard.aisle[1].name))
{
System.out.println(backyard.GetDescription());
test=house;
System.out.println("nowroom:
"+test);
System.out.print("pleaseimportroomname:
");
System.out.print("Pleaseimportroomname:
");
}
else
{
System.out.println("youcanoutcomeintheroom");
}
}
elseif(house.equals(FrontYard.name))
{
if(test.equals(FrontYard.aisle[0].name)||test.equals(FrontYard.aisle[1].name))
{
System.out.println(FrontYard.GetDescription());
test=house;
System.out.println("nowroom:
"+test);
System.out.print("pleaseimportroomname:
");
}
else
{
System.out.println("youcanoutcomeintheroom");
System.out.print("Pleaseimportroomname:
");
}
}
elseif(house.equals(garden.name))
{
if(test.equals(garden.aisle[0].name)||test.equals(garden.aisle[1].name))
{
System.out.println(garden.GetDescription());
test=house;
System.out.println("nowroom:
"+test);
System.out.print("pleaseimportroomname:
");
}
else
{
System.out.println("youcanoutcomeintheroom");
System.out.print("Pleaseimportroomname:
");
}
}
else
{
System.out.println("youimportroomnameiserror!
");
System.out.print("Pleaseimportroomname:
");
}
if(house.equals("##")){
break;
}
}
}
}
House.java中:
publicabstractclassHouse{
protectedStringname;
protectedStringdescription;
protectedHouse[]aisle=newHouse[2];
protectedStringGetDescription()
{
for(int