Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment #12 Due: Friday, April 29th, at 5:30pm Important: This is an individu

ID: 3695954 • Letter: A

Question

Assignment #12 Due: Friday, April 29th, at 5:30pm Important: This is an individual assignment. Please do not collaborate. No late assignment will be accepted. Make sure that you write every line of your code. Using code written by someone else will be considered a violation of the academic integrity and will result in a report to the Dean's office. Requirements to get full credits in Documentation The assignment number, your name, student ID, lecture number, and a class/file description need to be included at the top of each file/class. A description of each method is also needed. Some additional comments inside of methods to explain code that are hard to follow should be written. You can look at Java programs in the text book to see how comments are added to programs. Minimal Submitted Files You are required, but not limited, to turn in the following source files: Assignment12.java (The Assignment12 class extends JApplet -- no need to change) ControlPanel.java (It extends JPanel -- no need to change) BeamsControlPanel.java (It extends JPanel, to be completed) BeamsPanel.java (It extends JPanel) You may add more classes or more methods than the specified ones. (You might need them.) Skills to be Applied: Swing/AWT, Animation/Multi-Threads Classes may be needed: Timer in javax.swing package, JApplet, JButton, Container, JPanel, Color, Graphics, JSlider, JLabel, JColorChooser, ActionListener, ActionEvent, ChangeListener, ChangeEvent. You may use other classes. Program Description Suggested Class Diagram: Write a Java program that constructs an Applet. The Applet (JApplet) of your program should contain three buttons for beams, "Start", "Stop", and "Color". These three buttons will be organized vertically. Next to each set of buttons, there will be a label "Delay", then below it, there will be a slider that a user can use to change the delay (speed) of beams. Note than if the delay (interval) of beams decreases, its speed will be faster. Next to the slider for delay, there will be another label "Beam Num", and below it, there will be a slider that a user can use to change the number of beams for beams. Below the buttons and sliders, there are panels, each panel showing beams with their initial color. The left beams should be red and the right one should be blue initially. The picture below shows a snap shot of beams. Beams consist of multiple filled arcs determined by the number of beams, and its background is black. When "Stop" button is pushed, corresponding beams stop. When "Start" button is pushed, the beams start moving, i.e., their beams will start extending from its center towards outside. Pushing the "Color" button opens up a color chooser. Note that a pop-up blocker in your machine might need to be disable for this color chooser to pop up. By moving each slider, a user should be able to change the delay interval of beams or their number of beams. When a delay decreases, the corresponding beams should move faster, and when a delay increases, the beams should move slower. Note that these two sets of beams can have a completely independent movement of each other. (The size of the applet here is approximately 450 X 340). You can click on this site to see how the applet should work You need to create the following classes. BeamsPanel class BeamsPanel class a subclass of JPanel class. It is used to define a panel where beams are moving. It has the following additional attributes: Attribute name Attribute type Description color Color color of the beams. timer Timer An object of Timer in javax.swing package. This is used to control the beams' movement. delay int delay of the timer. step int each step that the beams move, i.e. how much diameter increases for each timer tick centerX int x coordinate of the center of the beams. centerY int y coordinate of the center of the beams. diameter int the diameter of the beams. diameterLimit int The longest the diameter of the beams can become. beamNum int The number of beams. angleSize int the angle size of each beam, determined by the number of beams. The following constructor method should be provided: public BeamsPanel(Color color, int width) The color is initialized to the value of the color parameter. The delay should be initialized to 20, and the step should be initialized to 3. The background should be set to black color. The centerX and centerY should be initialized to (width/2). The diameterLimit should be initialized to (width-10)/2, and the diameter should be initialized to 0 since the beams will spread from inside to outside. The beamNum should be initialized to 8. The angleSize should be initialized to 360/(beamNum*2). The timer should be instantiated with "delay" and the listener object created from MoveListener class. Then it should start by calling start() method in the constructor. The following method should be implemented: public void resume() The timer should start again using its start method. public void suspend() The timer should stop using its stop method. public void changeColor(Color anotherColor) The changeColor method sets the color of beams using the parameter public void setBeamNumber(int beam) It sets the number of beams using the parameter. public void setDelay(int delayValue) This method set the delay of the timer using its parameter. (Hint: setDelay method of the Timer class) public void paintComponent(Graphics page) A color of the beams (arcs) should be set, and beams should be drawing using the coordinate (centerX, centerY) and diameter. Since the beamNum might have been changed, the angleSize should be re-set to 360/(beamNum*2) (-- example, to have 8 beams, each angle size will be 360 degree/(8*2) since there will be a space between beams.) The fillArc method should be used, but the first two parameters are the upper left corner of the bounding rectangle, so its radius (half of diameter) should be subtracted as: page.fillArc(centerX-diameter/2, centerY-diameter/2, diameter, diameter, currentAngle, angleSize); where "currentAngle" starts with 0 and goes up to 360 degrees, and increased by 2*angleSize in each step. Thus this line should be repeated in a loop to draw all beams. MoveListener class This class can be defined as a private class of the BeamsPanel. It implements ActionListener interface. public void actionPerformed(ActionEvent event) Its actionPerformed method defines how the beams (arcs) should move next, by changing its diameter by adding "step" value to it, and re-paint the BeamsPanel after such change. Note that once the diameter reaches the diameterLimit value, then it should be re-set to 0 so that the beams start again from its center to outside. BeamsControlPanel class The BeamsControlPanel is a subclass of JPanel class. It contains 3 buttons including a start button, a stop button, and a color button. It also contains two labels and two JSlider objects. It also contains one panel -- an object of BeamsPanel class. Note that two objects of this class will be used, one for the red beams and one for the blue beams in the ControlPanel class. The following constructor method is already given: public BeamsControlPanel(int width, int height, Color initialColor) Its parameters are width and height of the applet, and the initial color of the beams for the panel. It should instantiate each components and arrange them using layout managers. The JSlider for delay should have its range from 0 to 50, and the initial delay should be 20. Its major tick spacing should be 10, and it should be shown vertically. The JSlider for beam number should have its range from 4 to 36, with the initial value 8. Its major tick spacing should be 4, and it should be shown vertically. You can use other methods defined in JSlider to make your Slider look like the one shown in this page. An object of the ButtonListener class should be added to each button, and an object of the SpeedListener class should be added to the delay JSlider object, and an object of the BeamListener class should be added to the beam number JSlider. ButtonListener class This class can be defined as a private class of the BeamsControlPanel. It implements ActionListener interface. public void actionPerformed(ActionEvent event) Its actionPerformed method should define an action for each button (There are 3 buttons). To distinguish buttons, you can use getSource() method of the ActionEvent object. For instance, if "start" is the start button for the beams panel, we can check if that button is pushed as: public void actionPerformed(ActionEvent event) { if (event.getSource() = = start) { ... } } SpeedListener class This class can be defined as a private class of the BeamsControlPanel. It implements ChangeListener interface. You need to provide a definition for the following: public void stateChanged(ChangeEvent event) by getting the selected value of the slider, and assign it as a delay of the corresponding beams. BeamListener class This class can be defined as a private class of the BeamsControlPanel. It implements ChangeListener interface. You need to provide a definition for the following: public void stateChanged(ChangeEvent event) by getting the selected value of the slider, and assign it as a number of beams.

Explanation / Answer

Ans;

/***
* Assignment number:
* Your Name:
* StudentID:
* Lecture number(time):
* Description: A program will continue to read in numbers until the number
*                0 is entered and compute the minimum, count how many even integers
*                are in the sequence, and compute the sum of negative integers.
*/

import java.util.Scanner;

public class Assignment2 {

   /***
   *
   * @param args
   */
   public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);
       int sumOfNegative = 0; //Store sum of negative numbers
       int evenCount = 0;        //Store even numbers in sequence
       int min = 0;           //Store min number in sequence
       int number = -1;       //Store a number
       while (true) {
           number = scan.nextInt();

           //Check number is mininum
           if (min > number) {
               min = number;
           }
           //Check number is even
           if (number % 2 == 0 && number!=0) {
               evenCount++;
           }
          
           //Check number is negative or not
           if (number < 0) {
               sumOfNegative += number;
           }
          
           //break the loop
           if (number == 0) {
               break;
           }
       }

       System.out.println("The minimum integer is " + min);
       System.out.println("The count of even integers in the sequence is "
               + evenCount);
       System.out.println("The sum of negative integers is " + sumOfNegative);

       scan.close();

   }

}