Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Object Oriented Programming using Java With Lego MindStorms Robot Inheritance Lab Topics: Inheritance, Interface, Super class, Sub class, Polymorphism, super, method overloading, method over-riding, Event-Driven Programming. Requirements: You are required to do the mandatory reading and pre-lab activities before you come to lab. Submit your pre-lab work during the Lab. During the Lab, work your way through the exercises. As you complete each step, show the working program to Professor Azhar or Professor Parsons.   You are required to turn in the complete pre-lab and lab exercises at the end of each lab session.   Handouts                                                                     DrivableRobot.java OneBumperRobot.java Inheritance Note. Problem Statement:   You are required to solve the following problem. In previous exercise, we have model our DrivableRobot. DrivableRobot: This robot can be steered in various directions, and be made to move forward, backward, and stop. Goal: We want to improve this DrivableRobot’s capabilities building two new Robots. The new Robots will do all those things that DrivableRobot can do and more. As you realize, current robot cannot sense the obstacles. We like our Robot to avoid obstacles and stops as soon its dark. Here are the descriptions of new Robots you are supposed to create from existing DrivableRobot.   OneBumperRobot: This Robot has everything that has DrivableRobot plus one front bumper equipped with a touch sensor, so that the robot will get around obstacles. The class modeling this robot extends naturally the DrivableRobot, it will therefore be defined as a subclass of DrivableRobot. This subclass, OneBumperRobot, is almost entirely defined. You will complete it. TwoBumperRobot: You will modify OneBumperRobot to make another subclass of the Drivable Robot that will model a robot called TwoBumperRobot with touch sensors on both sides. LightBumperRobot: Finally, you will extend this last TwoBumperRobot subclass to model a robot equipped with two bumpers and a light sensor, so that the robot stops as soon as it gets dark.   Pre-Lab Exercises: Read Inheritance Notes posted on web before you do this Lab.   1. List all the methods available to an object of the class OneBumperRobot. 2. What is the effect of the statement super(); in the first line of the constructor of the subclass? 3. Can the instance variables leftMotor and rightMotor be accessed in the class OneBumperRobot? 4. Add documentation comments to the class OneBumperRobot. 5. Complete the method avoid() in OneBumperRobot. To avoid an obstacle, the robot will move backward a short while, and then turn to one side. Could you make it turn at random to the left or the right? 6. The method run() from the superclass is overridden in the subclass. What does this mean? Describe what should happen when the run() method from each class is called (assuming that the method avoid() is completed). 7. List the changes you have to make to the OneBumperRobot class to transform it into a TwoBumperRobot class for a robot equipped with a left and a right bumper. If the right bumper is touched then the robot will move toward the left to avoid the obstacle, if the left bumper is touched it will move to the right. List any new instance variables, any new methods, as well as the changes that have to be made to existing methods. 8. Design and write a class whose sole purpose is to read and output the light measured by a light sensor attached to port 2. Output the reading on the LCD display. 9. You will extend the TwoBumperRobot to model a robot that has two bumpers and also a light sensor. The robot moves only when there is sufficient light. We will assume that it starts in the light. What is the header of this subclass? What instance variables should be included in this subclass? Do you need to implement a method other than the constructor? You need to implement a SensorListener for the light sensor. Write the implementation of stateChanged(Sensor source, int oldValue, int newValue). Explain event-driven Programming in this context. 10. You are provided with a class called TestDrive that can be used to test the classes. It is written to test the Robot class. How do you modify it to test the OneBumperRobot class?   Lab exercises: Before entering the lab you need to show your pre-lab work. Task 1: Use the TestDrive class to test the robot class. Does it behave as expected? Task 2: Modify the TestDrive class to test the OneBumperRobot class and perform the test. Does it behave as expected? If it does not, it probably comes from the implementation of the avoid() method. Make any necessary corrections. Task 3: Save the file OneBumperRobot.java as TwoBumperRobot.java and modify this class as planned in your pre-lab exercise. You may implement a SensorListener class for each sensor in which the stateChanged(Sensor source, int OldValue, int newValue) will be very similar to that in the OneBumperRobot class. Or, you may use the same listener for both sensors in which case you need more extensive change to stateChanged(Sensor source, int OldValue, int newValue). Change the TestDrive class so that you can test TwoBumperRobot. Task 4: Test the light in different lighting conditions with the class you designed at this effect. Task 5: Write the LightBumperRobot class and test it.     Important Note: It is assumed that the robot will consider the ambient light in the room where it starts as being sufficient to move. So the threshold for darkness should be set when the program starts to run.   Lab report: You must turn in the answer questions to the pre-lab questions number 1, 2, 3, 4, and 6, before the lab, rest of the questions (5, 7, 8, 9, 10) as well as the classes OneBumperRobot, TwoBumperRobot, and LightBumperRobot after the Lab.      The Code: Class DrivableRobot. This is the superclass. It models an object that moves forward, backwards, steers left and right, and stops. The run() method is only used for testing. Begin with this sample program. Go through the whole program and explain what each part does. //DrivableRobot.java import josx.platform.rcx.*;   public class DrivableRobot  {  private Motor leftMotor;      private Motor rightMotor;      public DrivableRobot(){         leftMotor = Motor.A;         rightMotor = Motor.C;    } public Motor getLeftMotor() {     return leftMotor; } public Motor rightMotor() {     return rightMotor; }   public void goForward() { leftMotor.forward(); rightMotor.forward(); }   public void goBackward() { leftMotor.backward(); rightMotor.backward(); } public void turnLeft(int mSecs) {    leftMotor.backward();    rightMotor.forward();    pause(mSecs); }   public void turnRight(int mSecs) {     leftMotor.forward();     rightMotor.backward();     pause(mSecs); } public void stop() {     leftMotor.stop();     rightMotor.stop(); } public void pause(int mSecs) {       try      {        Thread.sleep(mSecs);        }catch (InterruptedException e) {} }    public void run()   {       while(true)       {          goForward();          pause(2000);          goBackward();          pause(2000);          turnLeft(1000);          turnRight(1000);       }   } }   /* Class OneBumperRobot. It is a subclass of DrivableRobot. It models a drivable object, equipped with a front bumper. When it encounters an obstacle it moves back and turns. You are required to complete the avoid() method.*/   import josx.platform.rcx.*;   public class OneBumperRobot extends DrivableRobot {    private Sensor frontSensor;    public OneBumperRobot()    {       super();       frontSensor = Sensor.S2;       frontSensor.setTypeAndMode(SensorConstants.SENSOR_TYP       E_TOUCH, SensorConstants.SENSOR_MODE_BOOL);       frontSensor.setPreviousValue(0);       frontSensor.addSensorListener(new BumperListener());     }   public void avoid() { /*Your version of the code goes here*/ } public void run() {    goForward(); }   private class BumperListener implements SensorListener {    public void stateChanged(Sensor source,int oldValue, int newValue)    {        if (newValue == 1) avoid();     }  }   }   /*Class TestDrive. This is the driver class to test all the other classes.*/   import josx.platform.rcx.*; public class TestDrive {    public static void main(String[ ] args)    {       OneBumperRobot robot = new OneBumperRobot();       robot.run();       try{ Thread.sleep(50000);       }       catch(InterruptedException e){      }   } } Common Problems: If you are using Notepad it automatically saves the file as a text file unless the "Save as Type" is set to "All Files". If leJOS can't find your file try saving it again and make sure "Save as type" is set to "All Files". It’s a same problem when you are writing Java program. If compiling and downloading in DOS and it can't find the file make sure that you are in the same directory as the file. Make sure to initialize all superclass constructors from sub class constructors.