面向对象技术实验指导Word文件下载.docx
- 文档编号:21659860
- 上传时间:2023-01-31
- 格式:DOCX
- 页数:14
- 大小:278.74KB
面向对象技术实验指导Word文件下载.docx
《面向对象技术实验指导Word文件下载.docx》由会员分享,可在线阅读,更多相关《面向对象技术实验指导Word文件下载.docx(14页珍藏版)》请在冰豆网上搜索。
xxx”.
界面2是控制视图,上面的控件有:
1)有一个菜单条,上面有一个菜单,菜单里有“start”、“stop”和“quit”命令。
2)有一个输入框,旁边的文本为"
EnterBPM:
"
,可以在输入框内容输入特定的节拍。
3)有一个“set”按钮,可以改变每分钟的节拍数。
4)还有两个微调箭头按钮“<
<
”和“>
>
”,使用这两个按钮可以微调节拍的值。
注意,选择“start”开始产生节拍,选择“stop”停止产生节拍。
在选择开始产生节拍时,stop都应该是无效的,但是节拍产生后,start应该是无效的。
2.该设备配有一个控制器,控制mp3播放,存储播放歌曲的数据和节拍状态,并包含播放mp3的逻辑。
3.该设备还可以监测脉动。
4.在播放MP3时,在界面2上调整节拍后,会调整mp3播放器歌曲的节拍,同时界面1上显示出当前歌曲的节拍状态。
5.在做监测脉动时,屏幕界面2上所有的设置按钮都是无效的,因为我们不能去设置脉动。
总之,希望界面可以复用,希望模型数据的变化能够反映到界面上
看看能不能使用观察者模式、策略模式、适配模式,另外设计时将界面显示view、控制controler、模型model分开
实验一设计参考:
packagesolitaire;
importjava.awt.Graphics;
importjava.util.LinkedList;
importjava.util.List;
publicabstractclassCardPile{
protectedListpile;
protectedintx;
protectedinty;
/∗∗∗MakeanEmptyPile∗/
publicCardPile(intx,inty){
pile=newLinkedList();
this.x=x;
this.y=y;
}
publicbooleanempty(){
returnpile.isEmpty();
publicCardtopCard(){
if(!
empty())
return(Card)pile.get(pile.size()−1);
else
returnnull;
publicCardpop(){
return(Card)pile.remove(pile.size()−1);
publicbooleanincludes(inttx,intty){
returnx<
=tx&
&
tx<
=x+Card.width
y<
=ty&
ty<
=y+Card.height;
publicvoidaddCard(CardaCard){
pile.add(aCard);
publicvoiddraw(Graphicsg){
if(empty()){
g.drawRect(x,y,Card.width,Card.height);
topCard().draw(g,x,y);
publicabstractbooleancanTake(CardaCard);
publicabstractvoidselect();
importjavax.swing.∗;
importjava.awt.∗;
publicclassSolitaireextendsJPanelimplementsMouseListener{
staticDeckPiledeckPile;
staticDiscardPilediscardPile;
staticTablePiletableau[];
staticSuitPilesuitPile[];
staticCardPileallPiles[];
publicSolitaire(){
setBackground(Color.green);
addMouseListener(this);
allPiles=newCardPile[13];
suitPile=newSuitPile[4];
tableau=newTablePile[7];
intdeckPos=600;
intsuitPos=15;
allPiles[0]=deckPile=newDeckPile(deckPos,5);
allPiles[1]=discardPile=
newDiscardPile(deckPos−Card.width−10,5);
for(inti=0;
i<
4;
i++)
allPiles[2+i]=suitPile[i]=
newSuitPile(suitPos+(Card.width+10)∗i,5);
7;
allPiles[6+i]=tableau[i]=
newTablePile(suitPos+(Card.width+10)∗i,
Card.height+20,i+1);
repaint();
publicvoidpaintComponent(Graphicsg){
super.paintComponent(g);
13;
allPiles[i].draw(g);
publicstaticvoidmain(String[]args){
JFrameframe=newJFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(800,600);
frame.setTitle("
Solitaire"
);
Solitaires=newSolitaire();
frame.add(s);
frame.validate();
s.repaint();
publicvoidmouseClicked(MouseEvente){
intx=e.getX();
inty=e.getY();
12;
if(allPiles[i].includes(x,y)){
allPiles[i].select();
publicvoidmousePressed(MouseEvente){}
publicvoidmouseReleased(MouseEvente){}
publicvoidmouseEntered(MouseEvente){}
publicvoidmouseExited(MouseEvente){}
*theDeckPileClassThisclassextendstheCardPileclass.Itmustcreate
afulldeckofcards(storedinitssuperclass’spileattribute.)Thecards
shouldbeshuffledaftercreation(useCollections.shuffle(...)).You
neveraddcardstotheDeckPilesoitscanTakemethodalwaysreturnsfalse.
TheselectmethodremovesacardfromthedeckPileandaddsittothe
discardPile(IntheSolitaireclass).
*TheDiscardPileClassThismaintainsapileofcardsthatdonotgointoany
oftheotherpiles.OverridetheaddCardmethodtocheckfirstifthecardis
faceUpandflipitifitsnot.Thenaddthecardtothepile.Youneveraddcards
totheDiscardPilesoitscanTakemethodalwaysreturnsfalse.Theselect
methodrequirescarefulthought.Rememberthatthismethodrunswhenthe
userselectsthispile.Now,whathappenswhentheuserclicksonthetopCard
inthediscardPile?
WemustcheckifanySuitPile(4ofthem)oranyTablePile
(7ofthem)(allintheSolitaireclass)cantakethecard.Ifanyofthesepilescan
takethecardweaddtheCardtothatpile.Ifnot,weleaveitonthediscardPile.
*TheSuitPileClassTheselectmethodisempty(Cardsareneverremoved
fromthispile).ThecanTakemethodshouldreturntrueiftheCardisthesame
suitasallothersinthepileandifitsrankisonemorethatitstopCard.
*TheTablePileClassWritetheconstructortoinitializethetablepile.The
constructoracceptsthreeparameters,thexandycoordinatesofthepile,and
anintegerthattellithowmanycardsitcontains.(rememberthatthefirst
tablePilecontains1card,thesecond2Cardsetc.).IttakesCardsfromthedeck-
Pile.Thetablepileisdisplayeddifferentlyfromtheotherpiles(thecardsover-
lap).Wethusneedtooverridetheincludesthemethodandthedrawmethod.
ThecanTakemethodisalsodifferent.Thetablepileswillacceptacardifits
suitisoppositeincoloranditsrankisonelessthanthepile’stopCard.The
selectmethodissimilartotheoneinDiscardPile.Wemustcheckifany
SuitPile(4ofthem)oranyTablePile(7ofthem)(allintheSolitaireclass)can
takethecard.IfanyofthesepilescantakethecardweaddtheCardtothat
pileotherwiseweleaveitinthistabePile.
实验二设计参考:
packagepattern.strategy.sort;
/**
*策略模式:
StrategyPattern
*
*排序算法策略
*
*/
publicclassSortStrategyTest{
publicstaticvoidmain(String[]args){
Sorter<
?
sorter;
Integer[]data=newInteger[]{548,85,984,3,2,44,99};
sorter=newSorter<
Integer>
(data,"
QuickSort"
);
sorter.printArray();
sorter.sort();
String[]strs=newString[]{"
B"
"
C"
D"
A"
X"
F"
E"
};
String>
(strs,"
InsertionSort"
}
classSorter<
TextendsComparable<
T>
{
privateT[]data;
privateSortStrategy<
strategy;
@SuppressWarnings("
unchecked"
)
publicSorter(T[]data,StringsortStrategy){
this.data=data;
//利用反射动态加载策略类
Stringpack=this.getClass().getPackage().getName();
pack+="
."
;
try{
Class<
cl=Class.forName(pack+sortStrategy);
strategy=(SortStrategy<
)cl.newInstance();
//unchecked
}catch(InstantiationExceptione){
e.printStackTrace();
}catch(IllegalAccessExceptione){
}catch(ClassNotFoundExceptione){
}
publicvoidsort(){
strategy.sort(data);
publicvoidprintArray(){
for(inti=0;
i<
data.length;
i++){
System.out.print("
+data[i]+"
"
System.out.println("
*抽象排序算法类
*@authorWinty
*@param<
实现了Comparable<
接口的类
abstractclassSortStrategy<
publicabstractvoidsort(T[]data);
*插入排序
classInsertionSort<
extendsSortStrategy<
@Override
publicvoidsort(T[]data){
intlen;
Ttemp;
len=data.length;
for(inti=1;
len;
intk;
k=0;
temp=data[i];
for(intj=i-1;
j>
=0;
j--){
k=i;
if(data[j].compareTo(temp)>
0){
data[j+1]=data[j];
k=j;
}
elseif(data[j].compareTo(temp)<
k=j+1;
break;
}
data[k]=temp;
}
}
*快速排序
classQuickSort<
quick(data,0,data.length-1);
privatevoidquick(T[]data,intp,intr){
intq;
if(p<
r){
q=partition(data,p,r);
quick(data,p,q-1);
quick(data,q+1,r);
privateintpartition(T[]data,intp,intr){
inti;
Tpivot,temp;
i=p-1;
pivot=data[r];
for(intj=p;
j<
=r-1;
j++){
if(data[j].compareTo(pivot)<
i++;
temp=data[i];
data[i]=data[j];
data[j]=temp;
temp=data[i+1];
data[i+1]=data[r];
data[r]=temp;
returni+1;
上述方案还没有考虑多种顺序
方案2
方案3
实验四设计参考:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向 对象 技术 实验 指导
![提示](https://static.bdocx.com/images/bang_tan.gif)