Programming Assignment 4 (PA4) – StringSorter, Critters
编程 Assignment代写 StringSorter:A console application that reads strings from the keyboard into an array and checks to see if the strings are palindromes.
Due Date: Saturday, July 27 @ 11:59pm
Assignment Overview Programming Assignment1代写
PA4 consists of two separate programs.
StringSorter:
A console application that reads strings from the keyboard into an array and checks to see if the strings are palindromes.
Critters:
A graphical user interface (GUI) program that runs a simulation that controls the movement of three types of Critters.
Grading 编程 Assignment代写
- README: 10 points – See README Requirementshere and questions below
- http://www.gregmiranda.com/cse11-readme-requirements
- Style: 20 points – See Style Requirementshere
- http://www.gregmiranda.com/cse11-style-guidelines
- Correctness: 70 points
- Extra Credit: 5 points – View Extra Credit section for more information.
NOTE: If what you turn in does not compile, you will receive 0 points for that part of the assignment.
Getting Started 编程 Assignment代写
Follow these steps to acquire the starter files Gathering Starter Files:
You will need to create a new directory named pa3 and go into that directory. The $ represents your command prompt. What you type in is in bold.
$ mkdir ~/pa4
$ cd ~/pa4
Copy the starter files from the public directory:
$ cp ~/../public/objectdraw.jar .
$ cp ~/../public/Acme.jar .
Starter files provided:
objectdraw.jar
Acme.jar
Background Info — Command Line Arguments
Entering Command Line Arguments:
In Part of this assignment, we will be passing the maximum number of strings as a command line argument.
This means when we run the program, rather than typing:
java StringSorter
we will add the value for the maximum number of strings as an argument at the end of the run command:
java StringSorter 5 编程 Assignment代写
So in this case, 5 would be used as maximum number of strings the user can enter. Processing Command Line Arguments:
Now that we know how to enter command line arguments as the user, we need to learn how to process
those command line arguments in our code. In Java, the command line arguments are automatically passed as an array of Strings to main():
public static void main(String[] args) {…}
For example (command line arguments are bold):
1) |
command: | java StringSorter 5 |
args: |
0
length = 1 “5” |
|
2) |
command: | java StringSorter I love cse 11 |
args: |
0 1 2 3
length = 4 编程 Assignment代写 |
|
3) |
command: | java StringSorter |
args: | length = 0 |
Note that any whitespace between the command line arguments is trimmed and not stored in the args array.
To access these values within main(), we can just treat it as a regular array (because that’s what it is). In the case of example 1, we can access the String “5” by writing args[0] because “5” is stored at index 0 of the args array; In the case of example 2, we can access the String “cse” by writing args[2] because “cse” is stored at index 2 of the args array; and so on.
To use the maximum number of strings in your code, you will need to convert the String to an int, which you can do with Integer.parseInt().编程 Assignment代写
Program 1: StringSorter (50%)编程 Assignment代写
In this program, you will use your knowledge of recursion to identify palindromes. Your program will take in a command line argument specifying the maximum number of strings the user can enter. Then the program will take in a list of strings from the user, convert them to lowercase, sort them, and then
output the palindromes and the non-palindromes separately. See the sample output section for details. This program will be written in one file: StringSorter.java, and will contain the following two methods.
public static void main(String[] args)编程 Assignment代写
Parse Command Line Arguments:
Main will parse the command line arguments. There should only be one positive integer input (the maximum number of strings the user can enter). If any error occurs, the program will output the usage message, an error message, and exit with code 1.
Error Conditions:
- Wrong number ofarguments
- The argument is not a validinteger
- The argument is a negativenumber
Read User Input:
After parsing the argument, you will use a Scanner object to read in user input from standard input just as in previous assignments. This time around, the user will need to press enter after typing in each string, so you should use hasNextLine() and nextLine() for reading the user’s input. Stop reading input once the user enters <Ctrl>+D to indicate end of file (EOF). Note: use <Ctrl>+Z for Windows.
As you read in the strings from the user, you will need to convert them to lowercase and store them in an array (you know the size of the array from the command line argument). If the user enters fewer strings than the number they specified, there will just be some empty spots in the array (this is fine). If you read in the maximum number of strings allowed, don’t allow the user to enter any more input (because we don’t have any place to put it). Do not use an ArrayList for this!编程 Assignment代写
Process the Strings:
Next, you need to sort the array in alphabetical order. You can use a static method provided in the Arrays class for this. You’ll then need to check if each string is a palindrome using the function below. Finally, print out the palindromes and non-palindromes as shown in the sample output section below.
public static boolean palindromeCheck(String str)
This is a recursive method that will check if the string passed in is a palindrome. If the string is a palindrome, return true; if the string is not a palindrome, return false.
In this function, you will perform outside-in recursion: if the first and last character of the string are the same, call this function recursively to see if the middle substring (str without the first and last character) is a palindrome. Make sure you think about what your base case is so you don’t infinitely recurse!编程 Assignment代写
Note: This method must be recursive and you may NOT add any extra parameters to the method. If this method is not recursive or you change the parameters, you will receive 0 points for this program!
Sample Output:
The following are some examples of how to run StringSorter. Note that the output of your program must match the output below *exactly*, character-for-character, newline-for-newline. What you type in is shown in bold. ^D represents the user entering <Ctrl>+D.
Example 1: (note that <Ctrl>+D is not entered here because we entered the max number of strings)
$ java StringSorter 6 I 编程 Assignment代写
drive my racecar at noon
Sorted List of Palindromes: i
noon racecar
Sorted List of Non-Palindromes: at
drive my
$
Example 2: (note that <Ctrl>+D is entered here because we entered less than the max number of strings)
$ java StringSorter 6编程 Assignment代写
I
drive my racecar
^D
Sorted List of Palindromes: i racecar
Sorted List of Non-Palindromes: drive
my
$
Example 3: (note that more than one word can be entered per line, and the whole line will be considered a single string)
$ java StringSorter 2
ASanta dog lived asA devil god atNASA see, palindromes are fun
Sorted List of Palindromes:
asanta dog lived asa devil god atnasa编程 Assignment代写
Sorted List of Non-Palindromes: see, palindromes are fun
$
More Examples for You to Try:
You should also test error cases where an error is encountered while parsing the command line arguments. This list is NOT exhaustive–that’s your job (here’s a fun wikipedia page if you need some inspiration).
$ java StringSorter
$ java StringSorter 3 4
$ java StringSorter notInteger
$ java StringSorter 420abc
Note that we will not be testing empty strings with StringSorter.编程 Assignment代写
Program 2 is a simulation program that controls the movement of three types of Critters. The program will allow any number of Critters to be added to the world, which is the canvas of the applet. The Critter to be added is selected by pressing on the corresponding button for the Critter. The simulation can be started or stopped at any time, using the Start and Stop buttons. Additionally, all Critters can be removed from the simulation and from the canvas with the Clear button. Critters can be added at any time, whether the simulation is running or not. If added in the running state, new Critters should start responding immediately. The simulation should only run if there are two or more Critters currently existing on the canvas.
GUI and Controller
Your controller class should be named CrittersController.java. As the very first step, create all the GUI elements and lay them out as shown in the screenshot above. You will need to make use of multiple JPanels to create the layout and add the panels to CrittersController. The CrittersController should extend WindowController and implement the ActionListener interface. Make sure to register with the 6 buttons that will be created for the GUI. In addition to the 6 buttons, there are two JLabels: one at the top indicating the status of the simulation, and one at the bottom indicating which Critter is currently selected to be placed on the canvas.
The status label at the top will contain one of three status messages at any given time.
Use the following rules to determine what status message to display:
- If the simulation is in the stopped state, the status should read “Simulation is”
- If the simulation is in the started state, but there are less than 2 Critters on the canvas, the status should read “Please add two or more”
- If the simulation is in the started state, and there are 2 or more Critters on the canvas, the status should read “Simulation is running.”编程 Assignment代写
Note that it is possible for the status message to change by clicking ANY of the three control buttons on the top or by clicking on the canvas to place new Critters. Thus, you will probably want to create a method to which you delegate the task of figuring out and setting the status message, instead of having this logic spread out in different places. For example, hitting the Clear button in the started state should set the status to tell the user to add more critters. Adding two or more Critters in the started state to an initially empty canvas should change the status to “Simulation is running”. And so on. Simply call your method any such time it is needed, which will set the status based on the three rules above.
Once the Chaser,编程 Assignment代写
Runner, or Random button is clicked, clicking anywhere on the canvas will place that selected Critter on the clicked location. A Critter’s location is the point corresponding to its center.
When placing Critters, you can switch between one Critter and another at any time. Also, when a Critter button is clicked, the creature selection label, which initially reads “Select which critter to place:”, should be changed to say “Click on canvas to place a [Critter]”, where [Critter] is Chaser, Runner, or Random, depending on which Critter was clicked.
When starting up, the program should begin in the start state.
Critter appearance and movement 编程 Assignment代写
Chaser
The Chaser will be drawn as a cyan square using a FilledRect object . The size of the square is 15×15 pixels. The Chaser’s behavior is to move towards the Critter that is closest to it. However, a Chaser should NEVER chase after another Chaser. Thus, in determining which Critter is the closest to a Chaser, you should ignore all other Chasers.
Runner
The Runner will be drawn as a magenta circle using a FilledOval object . The diameter of the circle should be 15 pixels. The Runner’s behavior is to move away from the Critter that is closest to it.
However, if a Runner ever touches any of the four borders of the canvas, it should be randomly moved to a different location inside the canvas.
Random 编程 Assignment代写
The Random will be drawn as an orange ‘plus sign’ using two individual Line objects . The bounding box formed by the ‘plus sign’ should be 15×15 pixels. The Random’s behavior is to move randomly from its current location to a nearby location. Thus, the movement of the Random is not influenced by any other Critters around it. However, Chasers and Runners can still be influenced by nearby Randoms.
No Critter should be allowed to move outside of the canvas boundaries. This means that you need to use the Critter’s perimeter – NOT its center – to detect if a Critter is about to move outside of the view of the canvas. You do not need to worry about the applet resizing once it is started, so assume that the canvas size will not change.
The Chaser, Runner, and Random should each subclass a parent Critter.java abstract class. The Critter class should contain a constructor with the specified arguments, like so:编程 Assignment代写
public Critter(Location loc, DrawingCanvas canvas)
You can add more constructor arguments to this constructor or have other constructors in addition to this one if you so need, but at a minimum you need to have the above constructor. Also, the Critter.java should have at least the following abstract method:
public abstract void reactTo(Critter other);编程 Assignment代写
This method will be passed in a reference to a Critter that is determined to be the closest to this Critter on which reactTo() is called. This way, the current Critter can update its location based on the location of the other Critter that is closest to it. (Note that the Random will not need to use the “other” Critter argument, since a Random determines its next location randomly, without any influence from surrounding Critters). Refer to the objectdraw documentation for the distanceTo() method of Location, which you will very likely need to use to calculate distances between two Critters. Also, the Double.MAX_VALUE constant in Java will be useful.
Every time a Runner and Chaser need to react, they can move to one of 8 surrounding locations. For the Chaser, this will be whichever of the 8 locations brings it the closest to the “other” Critter. For the Runner, this will be whichever of the 8 locations puts it furthest away from the “other” Critter. To understand this better, imagine a 3×3 grid, with the center cell representing the Critter’s current location, and the 8 surrounding cells are the possible new locations:
As a result, both the x coordinate and the y coordinate of the Critter will change by -1 or 1.编程 Assignment代写
You need to loop through each of the 8 locations and figure out which is best for the Critter, depending on whether it is a Chaser or a Runner. All of this has to do only with Chaser and Runner. The movement behavior of the Random will be very different. For Random, the x and y coordinates should each be changed by a random value between -10 and 10, inclusive.
Make sure you generate random values in that range separately for the x coordinate and for the y coordinate, instead of using a single random value for both coordinates. When figuring out the new location of ANY Critter, whether it is a Chaser, Runner, or Random, be very careful to ensure that the new location does not cause the Critter to move outside the canvas boundaries. This will probably be trickier for the Random.
Keeping track of Critters and running the simulation
In CrittersController, you will want to use a Java ArrayList or Vector to hold all the Critters that are added to the canvas. For example,
ArrayList<Critter> critters = new ArrayList<Critter>();编程 Assignment代写
To run the simulation, you will need something that extends ActiveObject. This is where you will create a CrittersSimulator.java class, which will extend ActiveObject and define the corresponding run() method. CrittersController should create an instance of CrittersSimulator, passing in a reference to the critters ArrayList. Thus, both the Controller and the Simulator will act on the same reference to the critters ArrayList. You will likely need to use a boolean flag in CrittersSimulator, along with a setter and getter for the flag, to allow the simulation to be stopped and started. CrittersSimulator should run the simulation only in the started state, and only when there are 2 or more Critters on the canvas. The bulk of the work that CrittersSimulator’s run() method has to do is to calculate the distance from every
Critter to every other Critter (with the Chaser-Chaser exception mentioned earlier), and then call the reactTo() method for each Critter after having determined the closest Critter to it.
For the run method, please stick to the following structure:
public void run() { while(true) { /* All the rest of your work should go here */ pause(DELAY); } }
Note the pause(DELAY). Please leave this pause() at the bottom level of the infinite while-loop as you see here, and nowhere else. Regardless of whether the simulation is currently running or stopped, the pause() should always be executed inside the infinite while-loop for the run() method. You can play around with different values of DELAY when you are doing the assignment, but please make sure to set DELAY to somewhere around 40-50 before you turn in the assignment.编程 Assignment代写
README File P编程 Assignment代写
Remember to follow all of the guidelines outlined in the README Guidelines. If you did the extra credit, write a program description for it in the README file as well.
Questions to Answer in your README:
- List at least two ways in which Java Interfaces and Java abstract classes are different.
- List at least two ways in which Java Interfaces and Java abstract classes are alike.
- Howcan you create an array of ints in Java and initialize it with the values of all single digit odd positive numbers (between 0-9), all in one step/line?
- In vim/gvim, what commands will indent N consecutive lines (starting from the cursor line) by one level where the indent level is defined to be two spaces? This will take two vim commands: one to set the number of spaces to indent with each indent level (default is 8), and one to actually indent N consecutive lines. Likewise what command will shift N lines left (de-indent N lines)?
- In vim/gvim, what command will indent an entire curly-bracket block one level, while the cursor is currently on either the open or close curly bracket of the block? Likewise what command will shift an entire curly-bracket block one level left (de-indentblock)?编程 Assignment代写
- List 3 examples of input that you tried with the public executable for StringSorter that aren’t already given in the writeup.
- Why do the helper methods used for parsing command line arguments need to be static methods?
- How can you run vim through the command line to open all Java source code files in the current directory, each file in its owntab?
- Suppose you are currently inside a directory and in there you want to make a new directory called fooDir. And inside fooDir, you want another directory called barDir. Using only asingle mkdir command, how can you create a directory called fooDir with a directory called barDir inside it?
- How do you maintain your integrity when you are stressed, pressured, ortired?
Extra Credit – Imitator (Critters)编程 Assignment代写
Getting Started:
Make copies of the following files to do the extra credit in.
$ cd ~/pa4
$ cp CrittersController.java EC_CrittersController.java
Important: Your original CrittersController.java must remain unchanged. You need both the regular and the EC
versions of these file for turnin编程 Assignment代写
[2 Points] Add a fourth Critter called
The Imitator will be drawn as a black triangle using three Line objects. The bounding box created by the triangle should be 15×15 pixels. The Imitator’s behavior is to mimic the movement of the Critter that is closest to it. While imitating that Critter, the Imitator should change its color to the color of the Critter that it is imitating (but the shape should always stay a triangle). So, the Imitator should make its next move be exactly what the closest Critter would do if the closest Critter were at the same position as the Imitator. Like the Chaser-Chaser exception mentioned earlier in the assignment, all other Imitators should be ignored when finding the closest Critter to an Imitator. Remember to add a corresponding Imitator button and select creature text to the GUI controller.
● [2 Points] Spinning Imitator. 编程 Assignment代写
For the first extra credit part, all other Imitators were ignored when finding the closest Critter to an Imitator. For this extra credit, change this so that if the closest Critter to an Imitator is another Imitator, then the current Imitator starts spinning around in confusion. To simulate the spinning of the triangle, you will need to change the orientation of the triangle to one of four possibilities, cycling from one orientation to the next:
● [2 Points] Imitating a Chaser.编程 Assignment代写
If an Imitator is imitating a Chaser, prevent actual Chasers from chasing this Imitator and prevent this Imitator from chasing other Chasers. As you might have realized, this is to keep our Chaser-Chaser interaction the same for an Imitator for the times that it impersonates a Chaser.
Turnin Summary
See the turnin instructions here. Your file names must match the file names below *exactly*.
Due Date: Saturday night, July 27 @ 11:59 pm
Files required for the Turn-in:
StringSorter.java
CrittersController.java编程 Assignment代写
CrittersSimulator.java
Critter.java
Chaser.java
Runner.java
Random.java
README
Acme.jar
Objectdraw.jar
Extra Credit Files:编程 Assignment代写
EC_CrittersController.java
Imitator.java
If there is anything in these procedures which needs clarifying, please feel free to ask any tutor, the instructor, or post on the Piazza Discussion Board.
NO EXCUSES!
NO EXTENSIONS! NO EXCEPTIONS!
NO LATE ASSIGNMENTS ACCEPTED!
DO NOT EMAIL US YOUR ASSIGNMENT!
Start Early, Finish Early, and Have Fun!
其他代写:考试助攻 计算机代写 java代写 algorithm代写 代写CS function代写paper代写 r代写 金融经济统计代写 web代写 数学代写 essay代写 编程代写 report代写 algorithm代写 Data Analysis代写