广东自考JAVA语言程序设计一课后习题答案和源代码第八章.docx
《广东自考JAVA语言程序设计一课后习题答案和源代码第八章.docx》由会员分享,可在线阅读,更多相关《广东自考JAVA语言程序设计一课后习题答案和源代码第八章.docx(39页珍藏版)》请在冰豆网上搜索。
广东自考JAVA语言程序设计一课后习题答案和源代码第八章
第八章1
3.编写程序,一个画圆,一个画椭圆。
1
6.模拟排队买票,球票5元,购票者持有5,10,20,50元的,售票员手里开始没有零钱。
4
7.修改例8.5程序,使一些暂时找不到零钱等待的顾客能按照先来先买的规则,排队等待购买纪念品。
14
第八章
3.编写程序,一个画圆,一个画椭圆。
程序运行结果:
Applet的源文件:
Work8_3.java
importjava.applet.Applet;
importjava.awt.Color;
importjava.awt.Graphics;
/**
*8.3用一个红色笔画圆,同时用一个蓝色笔画椭圆
*/
publicclassWork8_3extendsAppletimplementsRunnable
{
privatestaticfinallongserialVersionUID=1L;
/**两个线程,红笔线程,和蓝笔线程*/
privateThreadred_thread,blue_thread;
/**红色画笔、蓝色画笔*/
privateGraphicsredPen,bluePen;
/**蓝、红笔画时需要旋转角度*/
privateintblue_angle=0,red_angle=0;
/**红色画的图案的中心坐标*/
privateinta_red=100,b_red=100;
/**蓝色画的图案的中心坐标*/
privateinta_blue=300,b_blue=100;
/**圆形半径*/
privateintradius_red=80;
/**椭圆的两个半径*/
privateintradius1_blue=150,radius2_blue=100;
publicvoidinit()
{
red_thread=newThread(this);
blue_thread=newThread(this);
redPen=getGraphics();
bluePen=getGraphics();
setBackground(Color.white);
setSize(470,240);
}
publicvoidstart()
{
red_thread.start();//两个线程开始
blue_thread.start();
}
publicvoidrun()
{
while(true)
{
if(Thread.currentThread()==red_thread)
{
redPen.drawOval(25,25,160,160);//绘制圆的边框。
x,y,width,height
intx=getX(radius_red,red_angle,a_red);
inty=getY(radius_red,red_angle,b_red);
redPen.setColor(Color.WHITE);
redPen.fillOval(x,y,10,10);//用白色画一次,可以擦出痕迹
red_angle+=3;
if(red_angle>=360)
red_angle=0;
x=getX(80,red_angle,100);
y=getY(80,red_angle,100);
redPen.setColor(Color.RED);
redPen.fillOval(x,y,10,10);//使用当前颜色填充外接指定矩形框的椭圆
try
{
Thread.sleep(10);
}
catch(InterruptedExceptione)
{
}
}
elseif(Thread.currentThread()==blue_thread)
{
bluePen.drawOval(a_blue/2+5,b_blue/2-45,radius1_blue*2,
radius2_blue*2);//绘制椭圆的边框。
x,y,width,height
intx=getX(radius1_blue,blue_angle,a_blue);
inty=getY(radius2_blue,blue_angle,b_blue);
bluePen.setColor(Color.WHITE);
bluePen.fillOval(x,y,10,10);//擦除痕迹
blue_angle+=3;
if(blue_angle>=360)
blue_angle=0;
x=getX(radius1_blue,blue_angle,300);
y=getY(radius2_blue,blue_angle,100);
bluePen.setColor(Color.BLUE);
bluePen.fillOval(x,y,10,10);//擦除痕迹
try
{
Thread.sleep(20);
}
catch(InterruptedExceptione)
{
}
}
}
}
/**
*用参数方程方法,计算坐标的方法
*@paramr圆的半径,椭圆的两个半轴
*@paramlocx坐标的相对偏移量
*@paramangle旋转的角度,单位为角度
*@return计算后的坐标值
*/
publicintgetX(intr,intangle,intlocX)
{
intx=locX+(int)(r*Math.cos(Math.PI/180.0*angle));
returnx;
}
/**
*用参数方程方法,计算坐标的方法
*@paramr圆的半径,椭圆的两个半轴
*@paramlocY坐标的相对偏移量
*@paramangle旋转的角度,单位为角度
*@return计算后的坐标值
*/
publicintgetY(intr,intangle,intlocY)
{
inty=locY+(int)(r*Math.sin(Math.PI/180.0*angle));
returny;
}
}
6.模拟排队买票,球票5元,购票者持有5,10,20,50元的,售票员手里开始没有零钱。
程序运行结果:
主窗体源文件:
BuyTicketFrame.java
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
/**
*8.6线程实现,购票规则,由于我想的算法太过复杂,只写到50元面值的
*@author黎明你好
*/
publicclassBuyTicketFrameextendsJFrameimplementsActionListener,Runnable
{
privatestaticfinallongserialVersionUID=1L;
privateConductorlady;
privateintarray[]={10,10,5,20,50,5,5,5,5,5,5,10,20,10,5,10,20,5};
privateintnumber=array.length;
privateThreadthread[]=newThread[number];
staticJTextAreatext;
privateJButtonbegin_button,replay_button;
privateJPanelpanel;
publicBuyTicketFrame()
{
super("第八章,第六题,线程模拟购票");
lady=newConductor(15);
text=newJTextArea();
panel=newJPanel();
begin_button=newJButton("开始买票");
replay_button=newJButton("重新开始");
for(inti=0;ithread[i]=newThread(this);
begin_button.addActionListener(this);
replay_button.addActionListener(this);
panel.setLayout(newFlowLayout());
panel.add(begin_button);
panel.add(replay_button);
this.add(panel,BorderLayout.NORTH);
this.add(newJScrollPane(text),BorderLayout.CENTER);
this.setSize(1000,700);
this.setVisible(true);
this.validate();
this.addWindowListener(newWindowAdapter()//窗口监视器
{
publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
});
}
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==begin_button)//线程开始
{
text.setText("********"+number+"个人排队买票,共"+lady.getTicketAmount()+"张票,开始卖票:
\n");
try
{
for(inti=0;i{
thread[i].start();
if(thread[i].isAlive())
text.append(""+(i+1)+"线程开始\n");
}
begin_button.setForeground(Color.RED);
}
catch(IllegalThreadStateExceptionee)
{
text.append("error:
重复启动线程"+ee.toString());
}
//检查线程是否结束
while(true)
{
intn=0;
for(inti=0;i{
if(thread[i].isAlive())
break;
else
n++;
}
if(n==array.length)
break;
}
begin_button.setForeground(Color.BLUE);//全部线程结束
text.append("\n全部线程结束,已经没有排队等待买票的了");
}
if(e.getSource()==replay_button)
{
this.removeAll();
newBuyTicketFrame();
}
}
publicvoidrun()
{
for(inti=0;i{
if(Thread.currentThread()==thread[i])
lady.rule(i+1,array[i]);
}
}
publicstaticvoidmain(Stringargs[])
{
newBuyTicketFrame();
}
}
售票员类源文件:
Conductor.java
/**
*售票员
*@author黎明你好
*/
classConductor
{
/**售票员开始持有的各种钞票的数量*/
privateintfive=0,ten=0,twenty=0,fifty=0;
/**总的票数*/
privateintticket_amount;
/**
*构造方法
*@paramnumber-票的初始数
*/
publicConductor(intnumber)
{
this.ticket_amount=number;
}
publicintgetTicketAmount()
{
returnticket_amount;
}
/**
*买票规则方法
*/
publicsynchronizedvoidrule(intindex,intmoney)
{
if(ticket_amount<=0)
{
BuyTicketFrame.text.append(index+"************售票结束**********\n");
return;
}
if(money==5)
fiveGive(index);
if(money==10)
tenGive(index);
if(money==20)
twentyGive(index);
if(money==50)
fiftyGive(index);
BuyTicketFrame.text
.append("-------------------------------------------------------------------------------");
BuyTicketFrame.text.append("剩余票数:
"+ticket_amount+"张:
¥:
5元:
"+five+"张;10元:
"+ten
+"张;20元:
"+twenty+"张;50元:
"+fifty+"张\n");
notifyAll();//唤醒所以线程
}
publicvoidfiveGive(intindex)
{
if(ticket_amount<=0)
{
BuyTicketFrame.text.append(index+"************售票结束**********\n");
return;
}
else
{
five++;
ticket_amount--;
BuyTicketFrame.text.append("第"+index+"个,5快的,钱正好,给你票\n");
}
}
publicvoidtenGive(intindex)//10元找钱规则
{
while(true)
{
if(five>=1)
break;
else
{
BuyTicketFrame.text.append("第"+index+"个,10元,找不开,发...生...等...待...☆☆☆☆☆\n");
try
{
wait();
}
catch(InterruptedExceptione)
{
}
}
}
if(ticket_amount<=0)
{
BuyTicketFrame.text.append(index+"************售票结束**********\n");
return;
}
else
{
ticket_amount--;
five--;
ten++;
BuyTicketFrame.text.append("第"+index+"个,10元的,找你5元的,给你票\n");
}
}
publicvoidtwentyGive(intindex)//20元找钱规则
{
while(true)
{
if(((five>=1&&ten>=1)||five>=3))//20元的两种找钱规则:
3*5,10+5
break;//跳出while循环,意味着有符合的规则
else
{
BuyTicketFrame.text.append("第"+index+"个,20元,找不开,发...生...等...待...☆☆☆☆☆\n");
try
{
wait();
}
catch(InterruptedExceptione)
{
}
}
}
if(ticket_amount<=0)
{
BuyTicketFrame.text.append(index+"************售票结束**********\n");
return;
}
elseif(ten>=1&&five>=1)//20元找钱规则:
10+5
{
ticket_amount--;
five--;
ten--;
twenty++;
BuyTicketFrame.text.append("第"+index+"个,20元的,找你1个5元,1个10块的,给你票\n");
return;
}
elseif(five>=3)//20元找钱规则:
5*3
{
ticket_amount--;
five-=3;
twenty++;
BuyTicketFrame.text.append("第"+index+"个,20元的,找你3个5元的,给你票\n");
}
}
publicvoidfiftyGive(intindex)//50元,找钱规则
{
while(true)
{
if((twenty>=2&&five>=1)||(twenty>=1&&ten>=2&&five>=1)
||(twenty>=1&&ten>=1&&five>=3)||(ten>=4&&five>=1)
||(ten>=3&&five>=3)||(ten>=2&&five>=5)
||(ten>=1&&five>=7)||(five>=9))
break;//50元的8种找钱方法
else
{
BuyTicketFrame.text.append("第"+index+"个,50元,找不开,发...生...等...待...☆☆☆☆☆\n");
try
{
wait();
}
catch(InterruptedExceptione)
{
}
}
}
//跳出while循环后,符合照片规则
if(ticket_amount<=0)
{
BuyTicketFrame.text.append(index+"************售票结束**********\n");
return;
}
//先可20的先找,然后在10元的
elseif(twenty>=2&&five>=1)//50元找钱规则1:
20*2+5
{
ticket_amount--;
five--;
twenty-=2;
fifty++;
BuyTicketFrame.text.append("第"+index+"个,50元的,找你2个20,1个5元的,给你票\n");
return;
}
elseif(twenty>=1&&ten>=2&&five>=1)//50元找钱规则2:
20+10*2+5
{
ticket_amount--;
twenty--;
ten-=2;
five--;
fifty++;
BuyTicketFrame.text.append("第"+index+"个,50元的,找你1个20,2个10,1个5元的,给你票\n");
return;
}
elseif(twenty>=1&&ten>=1&&five>=3)//50元找钱规则3:
20+10+5*3
{
ticket_amount--;
twenty--;
ten--;
five-=3;
fifty++;
BuyTicketFrame.text.append("第"+index+"个,50元的,找你1个20,1个10,1个5元的,给你票\n