Learning to Program with Robots (JAVA) The link of the textbook: http://www.lear
ID: 3866838 • Letter: L
Question
Learning to Program with Robots (JAVA)
The link of the textbook: http://www.learningwithrobots.com/textbook/PDFs/WholeThing.pdf
Write patterns, in the same style as Section 10.8, for the following: a. Declaring, allocating, and initializing a filled array where the initial values are read from a file b. Finding an extreme element c. Deleting an element from a specified index in an unsorted, partially filled array d. Inserting an element into a sorted, partially filled array e. Enlarging a partially filled array Section 10. 8 Patterns Many patterns involve arrays. They include initialization and changing the size of an array, as well as many algorithms. This section contains only a sampling of what could be considered patterns in this chapter.Explanation / Answer
import java.awt.AWTException; import java.awt.Robot; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; /** * A Java Robot example class. * * Caution: Using the Java Robot class improperly can cause * a lot of system problems. I had to reboot my Mac ~10 * times yesterday while trying to debug this code. * * I created this class to demonstrate the Java Robot * class on a Mac OS X system, though it should run on Linux * or Windows as well. * * On a Mac system, I place the TextEdit text editor in the * upper-left corner of the screen, and put a bunch of blank lines * in the editor. Then I run this Java Robot example from * Eclipse or the Unix command line. * * It types the three strings shown in the code below into * the text editor. * * Many thanks to the people on the Mac Java-dev mailing list * for your help. * * @author Alvin Alexander, http://devdaily.com * */ public class JavaRobotExample { Robot robot = new Robot(); public static void main(String[] args) throws AWTException { new JavaRobotExample(); } public JavaRobotExample() throws AWTException { robot.setAutoDelay(40); robot.setAutoWaitForIdle(true); robot.delay(4000); robot.mouseMove(40, 130); robot.delay(500); leftClick(); robot.delay(500); leftClick(); robot.delay(500); type("Hello, world"); robot.mouseMove(40, 160); robot.delay(500); leftClick(); robot.delay(500); leftClick(); robot.delay(500); type("This is a test of the Java Robot class"); robot.delay(50); type(KeyEvent.VK_DOWN); robot.delay(250); type("Four score and seven years ago, our fathers ..."); robot.delay(1000); System.exit(0); } private void leftClick() { robot.mousePress(InputEvent.BUTTON1_MASK); robot.delay(200); robot.mouseRelease(InputEvent.BUTTON1_MASK); robot.delay(200); } private void type(int i) { robot.delay(40); robot.keyPress(i); robot.keyRelease(i); } private void type(String s) { byte[] bytes = s.getBytes(); for (byte b : bytes) { int code = b; // keycode only handles [A-Z] (which is ASCII decimal [65-90]) if (code > 96 && code < 123) code = code - 32; robot.delay(40); robot.keyPress(code); robot.keyRelease(code); } } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.