CloudBerryExplorerSetup_v4.7.2.13
提供访问任意支持S3 API接口的应用
采用PAC模式实现一个简单的交互式系统,模拟Monty Hall 猜谜游戏,该游戏来源于电视节目“Let’s Make a Deal”, 游戏规则如下:游戏中设有三扇门,其中一扇门后是一辆汽车,另外两扇门后各有一头山羊。游戏参与者首先选择其中一扇门,然后,节目主持人将另外两扇门中藏有山羊的那扇打开(由于有两头山羊,不管游戏参与者如何选择,节目主持人总是可以打开一扇藏有山羊的门),并给游戏参与者一次改选的机会,这样,游戏参与者实际上有三种不同的选择方式: 维持原先的选择不变。 随机决定是维持原先的选择还是选择另一扇门(例如抛硬币作决定)。 直接选择另一扇门。 模拟程序运行时,提示用户输入其所作的决定以及用户希望玩多少次游戏。程序使用随机数产生器来决定汽车在哪扇门后面以及用户最开始选择的是哪扇门。若用户采用上述的第二种选择方式,程序随机选择是维持原决定还是换一扇门。程序将用户玩的次数和胜出率打印出来(若用户选择了藏有汽车的那扇门就算胜利)。在运行程序之前,请猜测一下你认为上述三种方式中哪种方式最好,程序运行结果可能会让你大吃一惊(有关Monty Hall 猜谜游戏的技术讨论参见L. Gillman所撰论文《The car and the goats》,Amer. Math. Mo. 99(1992): 3-7期)。
Java实现一个模型、两个视图和两个控制器的功能软件,即采用MVC模式或者说是观察者模式,本程序通过输入球体半径,显示球体形状,面积体积等 Sphere.java package Model; import java.util.Observable; public class Sphere extends Observable { private double radius;//球体半径 private double area;//球体面积 private double volume;//球体体积 public Sphere() { radius=100d; area=4*Math.PI*Math.pow(radius, 2); volume=4*Math.PI*Math.pow(radius, 3)/3; } public double getRadius() { return radius; } public double getArea() { return area; } public double getVolume() { return volume; } public void setRadius(double radius) { this.radius = radius; this.area = 4*Math.PI*Math.pow(radius, 2); this.volume=4*Math.PI*Math.pow(radius, 3)/3; this.setChanged(); this.notifyObservers(); } } textView.java package View; import java.util.Observer; import java.util.Observable; import java.text.NumberFormat; import javax.swing.*; import Controller.TextController; import Model.Sphere; import java.awt.*; import java.awt.event.*; public class TextView extends JPanel implements Observer { private JLabel radiusLab;//提示用户输入球体半径 private JTextField radiusTextField;//接受用户输入球体半径 //private JLabel radiusRang; private JLabel areaLab;//显示球体面积 private JTextField areaTextField;//显示输入球体半径对应的面积 private JLabel volumeLab;//显示球体体积 private JTextField volumeTextField;//显示输入球体半径对应的体积 public TextView() { try { Init(); } catch(Exception e) { e.printStackTrace(); } } private void Init() throws Exception { radiusLab=new JLabel("球体半径"); radiusLab.setForeground(new Color(0,165,168)); //radiusRang=new JLabel("[0-200]"); radiusTextField = new JTextField(12); radiusTextField.setForeground(new Color(223,100,158)); radiusTextField.setBackground(new Color(210,204,230)); areaLab=new JLabel("球体面积"); areaLab.setForeground(new Color(0,165,168)); areaTextField = new JTextField(12); areaTextField.setBackground(new Color(193,219,219)); areaTextField.setEditable(false);//该控件不能被编辑 volumeLab=new JLabel("球体体积"); volumeLab.setForeground(new Color(186,141,190)); volumeTextField = new JTextField(12); volumeTextField.setBackground(new Color(253,236,234)); volumeTextField.setEditable(false);//该控件不能被编辑 //TextArea text = new TextArea(10,10); setLayout(new GridLayout(3, 1));//设置3行1列 JPanel radiusJpanel = new JPanel();//球体半径显示面板 radiusJpanel.setLayout(new FlowLayout());//设置组件的布局方式为流型,板上增加的组件默认是按照从左到右顺序排列 radiusJpanel.add(radiusLab); radiusJpanel.add(radiusTextField); //radiusJpanel.add(radiusRang); radiusJpanel.setBackground(new Color(178,170,205)); JPanel areaJpanel = new JPanel();//球体面积显示面板 areaJpanel.setLayout(new FlowLayout());//设置组件的布局方式为流型,板上增加的组件默认是按照从左到右顺序排列 areaJpanel.add(areaLab); areaJpanel.add(areaTextField); areaJpanel.setBackground(new Color(151,222,219)); JPanel volumeJpanel = new JPanel();//球体体积显示面板 volumeJpanel.setLayout(new FlowLayout());//设置组件的布局方式为流型,板上增加的组件默认是按照从左到右顺序排列 volumeJpanel.add(volumeLab); volumeJpanel.add(volumeTextField); volumeJpanel.setBackground(new Color(251,222,219)); add(radiusJpanel); add(areaJpanel); add(volumeJpanel); } @Override public void update(Observable o, Object arg) { Sphere sphere=(Sphere) o; radiusTextField.setText(String.valueOf(sphere.getRadius())); areaTextField.setText(String.valueOf(sphere.getArea())); areaTextField.setEditable(false);//该控件不能被编辑 volumeTextField.setText(String.valueOf(sphere.getVolume())); volumeTextField.setEditable(false);//该控件不能被编辑 // TODO Auto-generated method stub } public void addActionListener(TextController controler) { radiusTextField.addActionListener(controler); } } GraphicsView.java GraphicController.java TextController.java SphereWindow.java等等