Java代写 | Programming Project 4

本次Java作业是实现排序算法并用Junit进行单元测试

Programming Project 4

Due Wednesday, November 20 at 11:59pm

IMPORTANT: Read the Do’s and Dont’s in the Course Honor Policy found on the Canvas class site.

I. Overview

The purpose of this project is to practice using linked lists, generic types, iterators, and comparable.

The theme of this project is sorting railcars on a train.


II. Code Readability (20% of your project grade)

To receive the full readability marks, your code must follow the following guideline:

  • All variables (fields, parameters, local variables) must be given appropriate and descriptive names.
  • All variable and method names must start with a lowercase letter. All class names must start with an uppercase letter.
  • The class body should be organized so that all the fields are at the top of the file, the constructors are next, the non-static methods next, and the static methods at the bottom with the main method last.
  • There should not be two statements on the same line.
  • All code must be properly indented (see Appendix F of the Lewis book for an example of good style). The amount of indentation is up to you, but it should be at least 2 spaces, and it must be used consistently throughout the code.
  • You must be consistent in your use of {, }. The closing } must be on its own line and indented the same amount as the line containing the opening {.
  • There must be an empty line between each method.
  • There must be a space separating each operator from its operands as well as a space after each comma.
  • There must be a comment at the top of the file that is in proper JavaDoc format and includes both your name and a description of what the class represents. The comment should include tags for the author. (See Appendix J of the Lewis book of pages 226-234 if the Evans and Flanagan book.)
  • There must be a comment directly above each method (including constructors) that is in proper JavaDoc format and states what task the method is doing, not how it is doing it. The comment should include tags for any parameters, return values and exceptions, and the tags should include appropriate comments that indicate the purpose of the inputs, the value returned, and the meaning of the exceptions.
  • There must be a comment directly above each field that, in one line, states what the field is storing.
  • There must be a comment either above or to the right of each non-field variable indicating what the variable is storing. Any comments placed to the right should be aligned so they start on the same column. Variables that are only used as an index in a loop do not need a comment.
  • There must be a comment above each loop that indicates the purpose of the loop. Ideally, the comment would consist of any preconditions (if they exist) and the subgoal for the loop iteration.
  • Any code that is complicated should have a short comment either above it or aligned to the right that explains the logic of the code.

III. Program Testing (20% of your project grade)

You are to create JUnit tests. If you added additional methods to the linked list class (including methods you added in a lab), you should submit a separate JUnit test class for the linked list that tests those methods. You do not have to write a JUnit tests for the methods you did not write. For the other class or classes of this project, you may submit one or multiple JUnit test classes. It is your choice.

Your report should be a short document that explains why the JUnit tests you provide thoroughly test your program. However, you should not JUnit test the main method or methods that specifically read from a file or methods that write to the screen. For these methods, your testing report should describe the tests you did to judge the correctness of your program.

Additional testing requirement: You need to include two additional JUnit tests. The first test consists of a railyard and a train where the cycle sort is able to sort the train while the closest sort fails to do so. The other test is a railyard and a train where the closest sort is able to sort the train while the cycle sort fails to do so. Detail the tests in your report and include the code to run the tests in your JUnit testing class.


IV. Java Programming (60% of your grade)

Remember that in the homework, you will be writing JUnit test cases. A strong recommendation is to write your JUnit test cases at the same time as you complete each part of the program.

Very Important Programming Rules

  • The main purpose of this project is to gain practice using linked lists and arrays. You have your choice of whether you want to use arrays or the ArrayList class from the API, and whether you want to use the LinkedList or the DoubleLinkedList class from class or the LinkedList class from the Java API.

    You must use arrays and ArrayList explicitly as arrays. Therefore you may not use the automatic resize feature of ArrayList, but you must instead set its initial capacity appropriately.

    You must use the linked list explicitly as a linked list. You may not use code that is inefficient in a linked list even if the Java API optimizes that code. Therefore, you can not access a linked list element by its index nor request the size of the list. Instead, you may use methods like adding and removing from the ends, testing if a list is empty, or using an iterator for the list. Similarly, if you are using our linked list or double linked list class, you should not use or add inefficient helper methods.

  • The second purpose of the homework is to use generic types appropriately. Your code should not generate any warning messages when compiled. (If you are not using DrJava, be sure to have it set so your IDE shows generic type warnings.) There is one exception, if you are using arrays, your code may generate warning message for unchecked typecasts from the array.

What you are to do

You may add any classes or methods to the following as you see fit in order to complete the given tasks.

Trains are an essential part of the transportation network. A train will have a large number of places to pick up cargo as well as a large number of places to deliver the cargo. To make the train operation more efficient, train companies have built railyards where the trains can be disassembled, cars can be sorted, and trains can be reassembled. The goal of a railyard is to separate and reorder the cars of a train so that cars that are going to the same location are together on the outgoing train. The part of a railyard used to sort the cars is called a classification yard. A classification yard consists of a collection of parallel tracks. Cars entering the classification yard can be directed to any of the parallel tracks, and when exiting the classification yard, the cars on the parallel tracks are merged together onto the single exiting track.

Classification yards can have variations in the number of parallel tracks and how they are connected. For this homework, we will assume that a classification yard has a single entrance and a single exit. We will also allow a railyard to have multiple classification yards connected in serial (one after the other).

Sorting train cars is quite challenging because there are a fixed number of parallel tracks in a trainyard. (Also, each track has a fixed length, but we will ignore that issue in this homework.) In this homework you will implement a railyard simulation and you will run different algorithms for sorting the cars of an incoming train. You will analyze the algorithms to try to determine when one algorithm is superior to another (see the additional requirement for the JUnit tests below).

You will create a class RailYard to simulate a railyard. The class will take a constructor that has an an array of int. The size of the array indicates the number of classification yards in the railyard, and the value of each entry in the array is the number of parallel tracks in that classification yard. For example, if the input is the array [5], then the RailYard consists of a single classification yard with five parallel tracks, and if the input is the array [4,3], then the RailYard consists of two classification yards. The first has four parallel tracks and the second has three parallel tracks.

How to design your class

Because the number of classification yards and the number of tracks in each classification yard is fixed, you will use an array and/or ArrayList to model both the classification yards and the tracks inside each yard. You can also use a two-dimensional array, if you wish, with one dimension being the classification yards and the other being the tracks in each yard. Because the number of cars that will be placed on each track is undefined, and because the tracks have essentially unlimited capacity, you will use a linked list to model each track in the classification yard. You may use either the double linked list we created in class and lab or you may use the LinkedList class from the Java API.

To model the train, we will use a list of objects that are Comparable, for example String or Integer. Your railyard should work with any possible Comparable type. The Comparable object represents the destination for a car, and an optimal ordering of the train will be one that places the list in order. For example, if we are using Integer for the rail cars, and the train is a list in the following order: 5, 1, 3, 9, 2, we want to use the railyard to reorder the cars so that the final result is a train in the order 1, 2, 3, 5, 9.

Your RailYard class should have the following four methods:

  1. void cycleSort(T[] train): takes a train that is represented as an array of appropriate objects. Sorts the array (i.e. cars of the train) using the “cycle sort” algorithm described below.
  2. void cycleSort(List<T> train): takes a train that is represented as a List (from the Java API) of appropriate objects. Sorts the list (i.e. cars of the train) using the “cycle sort” algorithm described below.
  3. void closestSort(T[] train): takes a train that is represetned as an array of appropriate objects. Sorts the array (i.e. cars of the train) using the “closest sort” algorithm described below.
  4. void closestSort(List<T> train): takes a train that is represetned as a List (from the API) of appropriate objects. Sorts the list (i.e. cars of the train) using the “closest sort” algorithm described below.
  5. a main method. Create a main method for the class. The main method should accept the following command line arguments. First is a string that is either "closest" or "cycle", second is an integer which is the number of classification yards. Then there should be a number of integers, one for each classification yard, that indicates the number of tracks. Finally, there are a bunch of strings. The main should create a rail yard with the given number of classification yards and tracks per yard, and then runs the desired sort on the input strings, and outputs the result.

    For example:
    java RailYard cycle 3 5 2 4 banana apple cherry pear orange pineapple blueberry peach cantalope apricot durian honeydew cranberry grapefruit watermelon dragonfruit kiwi grape quince raspberry kumquat plum
    should create a rail yard with three classification yards. The first has 5 parallel tracks, the second 2 parallel tracks, and the third 4 parallel tracks. It then does a cycle sort on the strings starting with “banana” and ending with “plum” and prints the final “train” ordering of those strings.

    The main method should not crash if the user enters bad data, but should instead give a useful non-Java error message to the user.

You are welcome to create additional helper methods (and you probably should because if the four listed methods are the only methods of your class, you will have both very long methods and lots of duplicate code).

You are welcome to create additional classes, either nested or regular classes, to model the different parts of a railyard.

Sorting Algorithms

Both sorting algorithms have the following structure: for each classification yard in the railyard, the train enters the classification yard, the sorting algorithm below is used to take each car, one at a time, from the front of the train and place it onto the back of the appropriate track of the classification yard. Once all cars of the train are on tracks of the classification yard, the merge algorithm is used to take each car, one by one, out of the front of the tracks of the classification yard and place the car onto the back of an initially empty train. This process repeats through each of the classification yards, in order, in the railyard. Trains may not go through classification yards out of order nor go through the same classification yard twice nor skip a classification yard.

Cycle Sort: When the train enters the classification yard, the first car is placed on the first track. For each following car, if the car is greater than or equal to the car that preceded it in the train, it is placed on the same track as the preceding car. If the car is smaller than the preceding car, it is placed on the next track of the classification yard, if there is a next track, and the first track if the preceding car was placed on the last track of the classification yard.

Closest Sort: To determine the track to place a car, we examine the cars at the back of each track. We chose the track whose back-most car is the largest car that is less than or equal to the car being placed. If there is no such track, the car is placed on an empty track, if one exists, or onto the track for which the back car has the smallest value. For example, if the back cars on the four tracks are 5, 9, 3, 7 and we are placing car 6, it is placed on the same track as 5 because that is the largest back car smaller than 6. On the other hand, if we are placing 2 we place it after 3 (assuming there are no empty tracks) because that is the track with the smallest back car.

Merge: Until all the tracks are empty, the front car of each track is considered and of those cars, the one with the least value is removed and is placed at the back of the train.

Reminder of the additional testing requirement: You need to include two additional JUnit tests. The first test consists of a railyard and a train where the cycle sort is able to sort the train while the closest sort fails to do so. The other test is a railyard and a train where the closest sort is able to sort the train while the cycle sort fails to do so. Detail the tests in your report and include the code to run the tests in your JUnit testing class.


程序代写代做C/C++/JAVA/安卓/PYTHON/留学生/PHP/APP开发/MATLAB


本网站支持淘宝 支付宝 微信支付  paypal等等交易。如果不放心可以用淘宝交易!

E-mail: [email protected]  微信:dmxyzl003


如果您使用手机请先保存二维码,微信识别。如果用电脑,直接掏出手机果断扫描。

发表评论