33 Pages

220-Day13_200820

Course: CSSE 200820, Fall 2009
School: Rose-Hulman
Rating:
 
 
 
 
 

Word Count: 2014

Document Preview

More Threads algorithm efficiency analysis, Big-Oh Work on Minesweeper See the nine announcements in the email message that I sent you yesterday afternoon. By now, everyone should know how to submit files in AFS and to SVN repositories. I have been rather lenient in the past if you didn't get it submitted correctly. By now you should be able to submit it to the right place on time. It is important that we not...

Register Now

Unformatted Document Excerpt

Coursehero >> Indiana >> Rose-Hulman >> CSSE 200820

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
More Threads algorithm efficiency analysis, Big-Oh Work on Minesweeper See the nine announcements in the email message that I sent you yesterday afternoon. By now, everyone should know how to submit files in AFS and to SVN repositories. I have been rather lenient in the past if you didn't get it submitted correctly. By now you should be able to submit it to the right place on time. It is important that we not only be able to write object-oriented programs, but that we build a vocabulary that enables us to communicate with each other about them. That is why I asked you to spend four weeks learning the "lingo" of OOP in Java. Tomorrow is the check-up on that. This ANGEL-based quiz is closed book and notes. It consists of matching questions, and you will only have about 30 seconds per term to complete it. So know your terms well! Each class day this week. A progress report is due at the end of each class. Discuss a Java feature (threads, function objects) A little bit on algorithm analysis Some time to work on Minesweeper (typically 30+ minutes). It is basically an updated version of your IEP, showing your progress on the phases that you outlined. Name today's report Day 13 progress Report.xlsx (You should be able to use "Save as" in Excel to do this.) Commit it to your Minesweeper repository. Additional requirement for your project: You should add a "cheat" feature Details are on the Assignments discussion forum to help you debug your code to help me test your code more easily Multithreaded Programs More on Algorithm analysis Big Oh Work on Minesweeper Often we want our program to do multiple (semi) independent tasks at the same time Each thread of execution can be assigned to a different processor, or one processor can simulate simultaneous execution through "time slices" (each probably a large fraction of a millisecond) Time Slices running thread 1 running thread 2 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 There is always one default thread; you can create others. Uses for additional threads A thread may sleep for a specified amount of time by calling Thread.sleep(numberOfMilliseconds); Animation that runs while still allowing user interaction. A server (such as a web server) communicates with multiple clients. Animate multiple objects (such as the timers in the CounterThreads example in a few minutes). How to create and run a new thread Define a new class that implements the Runnable interface. (it has one method: public void run(); ) Place the code for the threaded task in the run() method: class MyRunnable implements Runnable { public void run () { // task statements go here } } Runnable r = new MyRunnable(); Thread t = new Thread(r); t.start(); Create an object of this class: Construct a Thread object from this Runnable object Call the start method to start the thread Greetings simple threads, different wait times AnimatedBall move balls, stop with click CounterThreads multiple independent counters CounterThreadsRadioButtons same as above, but with radio buttons. The remaining two are more advanced than we will use in this course, dealing with race conditions and synchronization. Detailed descriptions are in Big Java. BankAccount SelectionSorter One thread prints the Hello messages; the other Thread prints the Goodbye messages. Each thread sleeps for a random amount of time after printing each line. Thu Thu Thu Thu Thu Thu Thu Thu Thu Thu Thu Thu Thu Thu Thu Thu Thu Thu Thu Thu . . Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan . 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 16:09:36 16:09:36 16:09:36 16:09:36 16:09:36 16:09:36 16:09:37 16:09:37 16:09:38 16:09:38 16:09:38 16:09:38 16:09:39 16:09:39 16:09:39 16:09:39 16:09:39 16:09:39 16:09:40 16:09:40 EST EST EST EST EST EST EST EST EST EST EST EST EST EST EST EST EST EST EST EST 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 Hello, World! Goodbye, World! Hello, World! Goodbye, World! Goodbye, World! Hello, World! Goodbye, World! Hello, World! Hello, World! Goodbye, World! Goodbye, World! Hello, World! Goodbye, World! Goodbye, World! Goodbye, World! Hello, World! Hello, World! Goodbye, World! Hello, World! Goodbye, World! This example was adapted from Cay Horstmann's Big Java, Chapter 23 public class GreetingThreadTester{ public static void main(String[] args){ // Create the two Runnable objects GreetingRunnable r1 = new GreetingRunnable("Hello, World!"); GreetingRunnable r2 = new GreetingRunnable("Goodbye, World!"); // Create the threads from the Runnable objects Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); We do not // Start the threads running. t1.start(); t2.start(); } } directly. Instead we call start(), which sets up the thread environment, and calls run() for us. call run() import java.util.Date; public class GreetingRunnable implements Runnable { private String greeting; private static final int REPETITIONS = 15; private static final int DELAY = 1000; public GreetingRunnable(String aGreeting) { greeting = aGreeting; } public void run() { try { for (int i = 1; i <= REPETITIONS; i++){ Date now = new Date(); System.out.println(now + " " + greeting); Thread.sleep((int)(DELAY*Math.random())); } } catch (InterruptedException exception){ } If a thread is interrupted while it is sleeping, } } an InterruptedException is thrown. A simplified version of the way BallWorlds does animation When balls are created, they are given position, velocity, and color Our run() method tells each of the balls to move, then redraws them Clicking the mouse turns movement off/on. Think about: could this application be written without creating the new thread? Demonstrate the program! public class AnimatedBallViewer { static final int FRAME_WIDTH = 600; static final int FRAME_HEIGHT = 500; public static void main(String[] args){ JFrame frame = new JFrame(); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("BallAnimation"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); AnimatedBallComponent component = new AnimatedBallComponent(); frame.add(component); frame.setVisible(true); new Thread(component).start(); } } This class has all of the usual stuff, plus this last line of code that starts the animation. class Ball private private private private { double centerX, centerY, velX, velY; Ellipse2D.Double ellipse; Color color; static final double radius = 15; public Ball(double cx, double cy, double vx, double vy, Color c){ this.centerX = cx; this.centerY = cy; this.velX = vx; this.velY = vy; this.color = c; this.ellipse = new Ellipse2D.Double ( this.centerX-radius, this.centerY-radius, 2*radius, 2*radius); } public void fill (Graphics2D g2) g2.setColor(this.color); { g2.fill(ellipse); } public void move (){ this.ellipse.x += this.velX; this.ellipse.y += this.velY; } } Everything here should look familiar, similar to code that you wrote for BallWorlds. public class AnimatedBallComponent extends JComponent implements Runnable, MouseListener { private ArrayList<Ball> balls = new ArrayList<Ball>(); private boolean moving = true; public static final long DELAY = 30; Again, public static final int ITERATIONS = 300; public AnimatedBallComponent() { super(); balls.add(new Ball(40, 50, 8, 5, Color.BLUE)); balls.add(new Ball(500, 400, -3, -6, Color.RED)); balls.add(new Ball(30, 300, 4, -3, Color.GREEN)); this.addMouseListener(this); } there should be no surprises here! public void run() { Each time through for (int i=0; i<ITERATIONS; i++) { if (moving){ the loop (if moving), for (Ball b:balls) tell each ball to b.move(); this.repaint(); move, then repaint } try { Thread.sleep(DELAY); Sleep for a while } catch (InterruptedException e) {} } } public void paintComponent(Graphics g){ Graphics2D g2 = (Graphics2D)g; for (Ball b:balls) b.fill(g2); } Draw each ball Toggle "moving" when the mouse is pressed public void mousePressed (MouseEvent arg0) { moving = !moving; } Could this program have been written without creating the new thread? With regular buttons With radio buttons How many threads does this application appear to have? public class CounterThreads { public static void main (String []args) { JFrame win = new JFrame(); Same old stuff! Container c = win.getContentPane(); win.setSize(600, 250); c.setLayout(new GridLayout(2, 2, 10, 0)); c.add(new CounterPane(200)); c.add(new CounterPane(500)); c.add(new CounterPane(50)); // this one will count fast! c.add(new CounterPane(1000)); win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); win.setVisible(true); } } class CounterPane extends JComponent implements Runnable { private int delay; // sleep time before changing counter private int direction = 0; // current increment of counter private JLabel display = new JLabel("0"); // Constants to define counting directions: private static final int COUNT_UP = 1; // Declaring these private static final int COUNT_DOWN = -1; // constants avoids private static final int COUNT_STILL = 0; // "magic numbers" private static final int BORDER_WIDTH = 3; private static final int FONT_SIZE = 60; public CounterPane(int delay) { JButton upButton = new JButton("Up"); JButton downButton = new JButton("Down"); JButton stopButton = new JButton("Stop"); // Note that these do // NOT have to be fields // of this class. this.delay = delay; // milliseconds to sleep this.setLayout(new GridLayout(2, 1, 5, 5)); // top row for display, bottom for buttons. JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(1, 3, 8, 1)); display.setHorizontalAlignment(SwingConstants.CENTER); display.setFont(new Font(null, Font.BOLD, FONT_SIZE)); // make the number display big! Put a simple border this.add(display); this.add(buttonPanel); this.setBorder(BorderFactory.createLineBorder(Color.blue, BORDER_WIDTH)); // Any Swing component can have a border. this.addButton(buttonPanel, upButton, Color.orange, COUNT_UP); this.addButton(buttonPanel, downButton, Color.cyan, COUNT_DOWN); this.addButton(buttonPanel, stopButton, Color.pink, COUNT_STILL); Thread t = new Thread(this); t.start(); around the panel. There are also more complex border styles that you can use. A lot of the repetitive work is done by the calls to addButton(). // Adds a control button to the panel, and creates an // ActionListener that sets the count direction. private void addButton(Container container, JPanel is a subclass JButton button, of Container Color color, final int dir) { The value of dir will be 1, 1, or 0, to indicate counting container.add(button); up, down, or neither. button.setBackground(color); button.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent e) { direction = dir; } }); } The action listener added here is an anonymous in...

Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

Rose-Hulman - ECE - 520
Real-Time Windows Target, Simulink, and the ECP Model 210 and Model 205Every station in C-116 should be set up to run the ECP Model 210 (rectilinear system) and/or Model 205 (torsional system) from Simulink using the Real-Time Windows Target softwar
Rose-Hulman - ECE - 520
ECE-520 Lab 6State Variable Feedback and Integral Control For One and Two Degree of Freedom SystemsOverview In this lab you will be utilizing state variable feedback control to place the poles in a closed loop system to improve the performance of y
Rose-Hulman - ECE - 520
ECE-520 Lab 5State Variable Feedback Control For One and Two Degree of Freedom SystemsOverview In this lab you will be utilizing state variable feedback control to place the poles in a closed loop system to improve the performance of your open loop
Rose-Hulman - CSSE - 490
Real Time Linux Workshop, Vienna University of Technology, Dec. 1999DIAPM RTAI for Linux: WHYs, WHATs and HOWsPaolo MantegazzaDipartimento di Ingegneria Aerospaziale Politecnico di Milano1REAL TIME AT DIAPMData acquisition Digital implemen
Rose-Hulman - ECE - 520
ECE-520 Lab 4Discrete-Time PI-D and I-PD Controllers and sisotoolOverview In this lab you will be controlling the one degree of freedom systems you previously modeled using PI-D and I-PD controllers. Both one degree of freedom systems must be contr
Rose-Hulman - ECE - 300
Rose-Hulman - ME - 422
ME422 FEM Homework #6 Distributed: January 28, 2008 Due: February 5, 2008 Problem 1: Consider quenching of a steel slab. Initially, the slab is at a temperature T0 . It is suddenly plunged into oil at Toil .to infinitywslab T2 T1 T3convection
Rose-Hulman - CSSE - 232
CSSE 232 Computer Architecture I Winter 2003-2004Rose-Hulman Institute of Technology Computer Science and Software Engineering Prof. Archana ChidanandanExam 2 Name _Solutions_ Section _Instructions: Write all answers on these pages. Use th
Rose-Hulman - ECE - 521
Real-Time Windows Target, Simulink, and the ECP Model 210The Long Version Every station in C-116 should be set up to run the ECP Model 210 from Simulink using the Real-Time Windows Target software. However, it often takes a little effort to make sur
Rose-Hulman - ECE - 250
V.Diode Switching Speed and BJT Transistor SwitchesECE 250 Lab 5 Diode Switching Speed and BJT Transistor SwitchesIn this lab we will look at the switching speed of a diode and at using a BJT as a switch or high current driver.V.A. Pre-Lab Pro
Rose-Hulman - ECE - 250
ECE Department ECE250 Electronic Device Modeling Fall 2008Instructor Information Instructor: Marc E. Herniter Office Hours : See Schedule. Office Phone Number : 877-8512 Office Number : C211 WEB Address: http:/www.rose-hulman.edu/~herniter E-mail:
Rose-Hulman - EE - 456
International RectifierThe IGBT SIP &amp; HEXPak NavigatorEffective 8 September,EXISTING Products NEW Products released to production in last 6-9 months UPCOMING Products to be released within next 3-4 months POTENTIAL Products no current plans. see
Rose-Hulman - EE - 349
VI.Basic Operational Amplifier CircuitsEE 349 Lab 6In this lab we will verify the operation of the inverting and non-inverting amplifiers constructed using Operational Amplifiers. We will also observe the frequency response of these circuits. Th
Rose-Hulman - EE - 349
V.Design of a DC Power SupplyThis lab requires the design, analysis, and measurements of a voltage regulated power supply. You will construct the D.C. power supply designed in the homework using a transformer, diode bridge rectifier, and an IC vo
Rose-Hulman - EE - 349
III.Zener CircuitsIn this lab we will look at several Zener circuits covered in class. You may wish to use the same Zener diode as used in Lab 1 so that you know the diode turn-on voltage and the Zener breakdown voltage.III.A.Pre-LabIn this la
Rose-Hulman - EE - 349
IX.NMOS InverterWe will be testing an NMOS circuit using the ZVN3306A N-channel enhancement MOSFET.IX.A.Measurement of Device ParametersUnfortunately, the data sheets do not give us enough information about the threshold voltage VT and K so we
Rose-Hulman - EE - 349
EE 349 Lab 7VII. Design of a Schmidt Trigger OscillatorWe will design the Schmidt trigger oscillator shown in Figure VII-1. Note that this is an inverting Schmidt trigger with an RC circuit tied from the output to the input. This circuit is simila
Rose-Hulman - EE - 388
7.Common Emitter Amplifier with a jFET Current SourceEE 388 Lab 7 Common Emitter Amplifier with a jFET Current SourceIn this lab you will design and test the amplifier shown in Figure 7-1.7.A.Pre-Lab Vo with a maximum lower 3 dB frequency of
Rose-Hulman - EE - 349
EGR349 Power Supply Design ExampleThis file solves the equations for designing a power supply with a Linear voltage regulator. Specify the output voltage and current V supply 5 . volt I supply 300 . mAAssume a full-wave rectifier T 1 . sec 120 2
Rose-Hulman - EE - 349
Copyright 1999 Marc E. HerniterCopyright 1999 Marc E. HerniterCopyright 1999 Marc E. HerniterCopyright 1999 Marc E. HerniterCopyright 1999 Marc E. HerniterCopyright 1999 Marc E. HerniterCopyright 1999 Marc E. HerniterCopyright 1999 Marc
Rose-Hulman - EE - 456
Rose-Hulman - EE - 456
EE 456Boost Regulator Design - Continuous Mode Operation For Second Lab Projects 10 6 .sec Specify Input Voltage V D Specify Switching Frequency 12 .volt 20 .kHz 1 FS T S = 50 s Specify Output Voltage V o 25 .voltFS TSSpecify the Assumed Effici
Rose-Hulman - EE - 456
Rose-Hulman - EE - 456
Rose-Hulman - EE - 456
EE 456Buck Regulator Design - Continuous Mode OperationProblem 1 of HW # 2 - Compare calculations of RMS current of the capacitor.Define useful units for Electrical Engineering6 s 10 .secSpecify Input Voltage V D24 .volt 20 .kHz 1 FSSpecif
Rose-Hulman - EE - 456
* D:\transfer\HW\EE459 Exam2.sch Date/Time run: 11/28/100 13:21:28 (B) EE459 Exam2.dat 24V Temperature: 27.020V16V12VExample Plots. You will need two sets of plots. One set for8VVin = 120 Volts, and a second set for Vin = 165 Volts.4V-
Rose-Hulman - EGR - 286
MEMOTO: MAC Technical Staff FROM: MAC Management RE: Homework #2: E-mail Introduction DATE: January 21, 2000MAC Inc.ASSIGNMENT PURPOSE: This assignment is used to set up communications between the team members, division managers, and company pre
Rose-Hulman - EE - 456
Copyright 2000 Marc E. HerniterCopyright 2000 Marc E. HerniterCopyright 2000 Marc E. HerniterCopyright 2000 Marc E. HerniterCopyright 2000 Marc E. HerniterCopyright 2000 Marc E. HerniterCopyright 2000 Marc E. HerniterCopyright 2000 Marc
Rose-Hulman - EGR - 286
MEMOTo: MAC Engineering Staff From: MAC Management Re: Date: Due: Design Document D5 Guidelines Wednesday, February 23, 2000 Draft: Monday, March 20. Final: Friday, March 31.MAC IncPurpose of this document: This document needs to communicate the
Rose-Hulman - EE - 388
EE 388 HW 3 - Problem 4 Self-Bias ExampleConstraints I Cmax I Cmin V CEmin 20 . mA 10 . mA 5. V max min V BEmin 350 50 0.7 . V V CCmax V CCmin V BEmax 30 . V 30 . V 0.7 . VChoose a value for Rc and Re V CCmax 2 V CEmin V CCminV CCnomR total
Rose-Hulman - EE - 388