首页
网站开发
桌面应用
管理软件
微信开发
App开发
嵌入式软件
工具软件
数据采集与分析
其他
首页
>
> 详细
CS 3140代做、代写java语言编程
项目预算:
开发周期:
发布时间:
要求地区:
CS 3140 - SDE Spring 2024
HW 3: Apportionment Refactored
1. Getting Started
In Unit III of our course, you learned that code refactoring is like giving your code a makeover.
It's the process of improving the structure, organization, and functionality of your code without
changing its external behavior. Refactoring can not only make your code easier to read,
maintain, and debug, but it can also increase its performance and efficiency. It's a skill that every
developer should master, as it's often the key to unlocking the true potential of their code.
In this assignment, we will revisit and refactor HW1-C. You are given an implementation of HW1
written by Prof. Will McBurney, the course's original designer. For reasons of academic
honesty, this implementation code may not be reposted anywhere or shared with anyone.
The assignment is divided into 4 parts:
● Part A - Refactor an Apportionment method
● Part B - Add a new concrete implementer to ApportionmentFormat
● Part C - Designed three classes ApportionmentMethodFactory,
StateSupplierFactory, and RepresentationFormatFactory, to instantiate and
return the correct concrete implementation of the functionality of our software.
1
CS 3140 - SDE Spring 2024
● Part D - Reimplement a new command-line arguments system
You may tackle these parts in any order, though doing Part C before you do Part D is
recommended since the new Arguments implementation should use the Factories created in
Part C.
Note on Piazza: For this and future assignments, we are back to our regular Piazza policies,
meaning it's completely okay (and encouraged) to ask public questions on Piazza about the
specification, starter code, etc.
Team Repository: Teams of up to 3 are allowed. You may have teammates from your previous
team or find new ones. However, you can only work with the same person up to 4 times.
1. Decide if you are working alone or if you are working in a group of up to 3 members. All
team members must have their NetID in the team repository name. In this assignment,
you will add the prefix “Refactor” to your team name. For example, a team of netID1,
netID2, and netID3 must follow this format: “Refactor-netID1-netID2-netID3”
2. Click on this GitHub Classroom Invite: https://classroom.github.com/a/fB4JiPvY
and create the git repository you and your team will use. If you miss this step, make a
private "rename repo" request on Piazza.
3. Using IntelliJ, clone the repository to your local computer in the root directory of your
project (by default, named HW3-starter-repo).
2. Program Specifications
This implementation additionally has three extra features, including 2 apportionment methods
that were not in Homework 1:
● AdamsMethod.java, which is used by adding "--adams" to the command line
○ You will rewrite the argument handling in a later part. The implementation will no
longer have the "--adams" flag.
● JeffersonMethod.java, which is used by adding “--jefferson” to the command
line
○ You will rewrite the argument handling in a later part. The implementation will no
longer have the "--jefferson" flag.
● PopulationFormat.java, which can display the apportionment in order of
population (ascending or descending).
As a rule, do not change any existing public or protected class interfaces (that is, do not change
the method declarations, and do not change their behavior) unless explicitly told to do so.
Additionally, do not remove any existing tests except for ArgumentsTest, which you will want
to replace in Part D completely. These tests help ensure you don't "break" any working features
when adding new code. You may add additional tests if they help. However, be aware that we
will run all existing tests except those in ArgumentsTest (as well as new tests) when grading
your code, and you will lose points for any test failures.
2
CS 3140 - SDE Spring 2024
In general, I would expect you only to need to change the following source files:
● Arguments.java (which you will replace completely in part D)
● Any of the classes that you add
● ArgumentsTest.java (which you are encouraged to replace with your tests)
2.1. Part A - Reimplement an Apportionment Method
You will expand some features from Homework 1C in the HuntingtonHill Method. You will
re-design the Huntington-Hill to implement the ApportionmentMethod interface and return a
Representation. You can refer back to HW1-C for an example of implementing the
Huntington-Hill Method and reusing any code from Homework 1C from any team member. The
class declaration should be:
public class HuntingtonHillMethod implements ApportionmentMethod {}
This class should not have any constructors! We will instantiate it with an implicit zero-argument
constructor. As part of the ApportionmentMethod interface, the getRepresentation function
should calculate how many representatives each state receives using the Huntington-Hill
algorithm and return an appropriate Representation.
Implementation Note: These methods may not change any code outside their class. Refrain
from modifying how command line arguments are handled (we will discuss that in Parts C and
D). You can reference existing code, but you must not change it! We intend to use
polymorphism to help ensure our new code can be added without changing existing code in
other software parts (though you can re-use code outside of the class, such as
Apportionment).
2.2. Part B - Add a New Feature: Relative Benefit
Since our code now supports a few apportionment methods, we want to add a Relative Benefit
feature to help users compare different apportionment methods. You will learn how to calculate
the relative benefit of an apportionment strategy and format it for display on the console. To do
this, you will implement a new class called RelativeBenefitFormat.
RelativeBenefitFormat
Looking at the table of the Jefferson method with our starting Divisor based on the Total
Population divided by the number of Representatives (described in Appendix A). The
RoundedDown column was replaced with the number of representatives given to the state.
Name Population Raw Final reps Benefit
Delaware 989948 0.808914 0 -0.808914
Maryland 6177224 5.04758 5 -0.04758
3
CS 3140 - SDE Spring 2024
Pennsylvania 13002700 10.62486 12 +1.37514
Virginia 8631393 7.052948 7 -0.052948
West Virginia 1793716 1.465695 1 -0.465695
From this table, we can see how much a state benefited from or was hurt by the
apportionment method:
Benefit = [final reps] - [raw]
In this case, we can say Pennsylvania benefitted the most from our use of the Jefferson
Apportionment method (then ended up with nearly 1.4 more reps than you would expect from
their raw number), whereas Delaware was hurt the most (they would expect roughly 0.8 reps -
they got 0). Using this information, here is how your printed format should look:
State | Reps|Benefits
Pennsylvania | 12| +1.375
Maryland | 5| -0.048
Virginia | 7| -0.053
West Virginia | 1| -0.466
Delaware | 0| -0.809
Format Description:
● A header row as shown above
● Vertical bars to indicate columns
● 16 characters left-aligned for state name
● 5 right-aligned for Reps. The number must be an integer
● 7 or 8 characters right-aligned for Benefit.(previously said 7, but example was 8, both ok)
○ the number must have exactly 3 decimal places,
○ + sign must be included for positive benefit values
○ - sign for negative numbers
○ 0.000 should have no sign.
That is, states should be sorted by benefit for the given algorithm. While this example uses the
Jefferson method, this relative benefit should work with ANY apportionment method.
The RelativeBenefitFormat class will also use our divisor. This could involve copying
commonly used code to a library accessible by many classes. Consider again how to approach
making our code D.R.Y. “Don’t Repeat Yourself”, and avoiding copying and pasting code.
The RelativeBenefitFormat should have a private field of type DisplayOrder which determines
the displayOrder class should have two constructors:
● public RelativeBenefitFormat() - defaults the DisplayOrder field to
DESCENDING.
4
CS 3140 - SDE Spring 2024
● public RelativeBenefitFormat(DisplayOrder displayOrder) - directly sets
the DisplayOrder field but throws an IllegalArgumentException if DisplayerOrder is
null.
Do not add any additional constructors.
I recommend looking at PopulationFormat when writing this class, as you should find that it
helps you out considerably, especially with how to handle DisplayOrder.
The interesting part of implementing this is getting each state's quota, since the function is
limited to taking in a Representation object. Be aware that you can calculate the quotas from
the Representation (either by writing the code yourself or re-using the default methods in
ApportionmentMethod.java).
2.3. Part C: Extracting Classes
By now, we should note that the Arguments class violates the Single Responsibility Principle:
"A class should only have one reason to change." Arguments could change because
1. We change the argument format
2. We implement new concrete implementations we want our program to use:
a. New input file format
b. New apportionment algorithm
c. New output String format
This means we have 4 reasons to change. Let's fix that before we start making changes to
Arguments class. You will make 3 Factory classes, each with 1-3 methods. These classes are
called Factory because they implement a creational design pattern called the Factory Pattern (to
be revealed later in the course). As a hint, all of these methods should be very simple,
basically just if-statements or switch statements that return new instances of the intended class.
The "default" factory methods should just be one-line return statements! If you find that any
method is more than 8 or so lines of code, you need to do something differently!
Factory 1: StateSupplierFactory
● public StateSupplier getStateSupplier(String filename) - Returns a new
StateSupplier (either CSVStateReader or SpreadsheetStateReader) depending on the
ending of the filename. This method will be very similar to the existing getStateSupplier
method in Arguments, but this method takes in the filename directly, rather than
extracting it from the command-line arguments. It should throw an
UnsupportedFileFormatException for invalid filenames.
5
CS 3140 - SDE Spring 2024
Factory 2: ApportionmentMethodFactory
○ public ApportionmentMethod getDefaultApportionmentMethod()-
Returns a new instance of the default apportionment method, HuntingtonHillMethod
○ public ApportionmentMethod getApportionmentMethod(String method)-
Returns a new instance of ApportionmentMethod (JeffersonMethod, AdamsMethod, or
HuntingtonHillMethod). The input string will be the command-line parameter associated
with the --method argument you will implement in Part D. Specifically, the input String
will be either "adams", "jefferson", or "huntington". Any other string input should
result in an IllegalArgumentException with a meaningful error message.
Factory 3: RepresentationFormatFactory
● public RepresentationFormat getDefaultRepresentationFormat() - Returns a
new instance of the default representation format, AlphabeticalFormat
● public RepresentationFormat getRepresentationFormat(String name) -
Returns a new instance of implementation of RepresentationFormat
(AlphabeticalFormat, RelativeBenefitFormat, or PopulationFormat). The input string will
be the command-line parameter associated with the --format argument that you will
implement in Part D. Specifically, the input String will be either "alpha", "benefit", or
"population". For benefit and population, use their default displayOrder (descending
and ascending respectively). Any other string input should result in an
IllegalArgumentException with a meaningful error message.
● public RepresentationFormat getRepresentationFormat(String name,
DisplayOrder order) - Does the same thing as getRepresentationFormat(String),
but allows the DisplayOrder to be specified (note that for AlphabeticalFormat, the
display order should be ignored.
Do not add any constructors to these Factories! All instantiation should be done using the
implicit zero argument constructor that all classes without an explicit constructor have.
Additionally, the classes shouldn't have any instance variables, though you are welcome to use
static constants. (public static final [type] [variable name])
Example Factory usage: If working correctly, then your usage of a given Factory will look
something like this:
StateSupplierFactory factory = new StateSupplierFactory();
StateSupplier supplier = factory.getStateSupplier(filename);
In this case, if the filename is a .csv file, the supplier will be an instance of CSVStateReader. If
the filename is an .xls or .xlsx file, the supplier will be an instance of SpreadsheetStateReader.
6
CS 3140 - SDE Spring 2024
2.4. Part D - Arguments rewrite
Note: You should implement Part C before attempting to implement this part.
Now that we have extracted the factories, we can re-implement Arguments based on the new
specification for handling requirements and ensure our project is scalable.
Tips: I recommend starting by deleting all of the implementations of all methods in
Arguments.java, but leaving the method declarations. You should also delete/comment
out all of the previous tests in ArgumentsTest.java, as those tests are incompatible with
the format of the new argument.
We can re-implement Arguments based on the new specification for handling requirements.
Arguments will still handle the same basic tasks:
● Take in the String[] args from Main via the Constructor.
● Providing the methods:
○ public StateSupplier getStateSupplier()
○ public int getRepresentatives()
○ public ApportionmentMethod getApportionmentMethod()
○ public RepresentationFormat getRepresentationFormat()
○ (these methods must still be present! - no interface change!)
However, rather than Arguments selecting which concrete implementers to use for
Configuration, it simply interacts with the Factories we wrote in Part C to do so. We are setting
up a framework for having a more customizable approach. So we're going to reset our
command-line arguments. We will still require one argument:
java -jar Apportionment.jar filename
Where filename is the name of the census population file (such as census2020.csv) - this
needs to accept both .csv and .xlsx files. From there, we want all our other arguments done in a
way that many modern programs handle optional arguments: … with flags!
Long Flag arguments
For simplicity, you can assume everything is case sensitive - we will only test your arguments
with lowercase letters.
java -jar Apportionment.jar census2020.xlsx --reps 1000 --format benefit
--algorithm jefferson
In this example, we have three optional arguments defined:
● --reps 1000 - run apportionment with 1000 representatives
● --format benefit - print results using RelativeBenefitFormat
● --algorithm jefferson - use JeffersonMethod
7
CS 3140 - SDE Spring 2024
In this case, the flag specifies the name of the argument, and the next argument specifies the
value of the argument. It's important that the optional arguments can be in any order, but
the argument must follow the flag directly. For example, the following is also a valid command.
java -jar Apportionment.jar census2020.xlsx --algorithm jefferson --reps 1000
List of optional arguments: This lists all optional arguments.
● --reps [integer] - must be followed by a positive (non-zero, non-negative) integer.
Throw a meaningful exception if the input is invalid.
○ Defaults to --reps 435
● --format formatName - set the format name. Format choices:
○ --format alpha - print States (AlphabeticalApportionmentFormat)
○ This is the default value
○ --format benefit - prints States by benefit (RelativeBenefitFormat)
○ --format population - prints States by population (PopulationFormat)
○ Anything else is invalid, a meaningful exception should be thrown
● --algorithm strategyName - set the Apportionment method
○ --algorithm adams - use Adams Apportionment algorithm
○ --algorithm jefferson - use Jefferson Apportionment algorithm
○ --algorithm huntington - use Huntington-Hill Apportionment algorithm
○ This is the default value
Short Flag arguments:
Short flags work largely the same way as long flags. For instance, long flag arguments:
java -jar Apportionment.jar census2020.xlsx --reps 1000 --format benefit
--algorithm jefferson
Could also be written as a short flag form:
java -jar Apportionment.jar census2020.xlsx -r 1000 -f benefit -a jefferson
That is:
● -r is short for --reps
● -f is short for --format
● -a is short for --algorithm
However, unlike long flags, short flags can be combined. For example,
java -jar Apportionment.jar census2020.xlsx -rfa 1000 benefit jefferson
Specifically, -rfa means that:
● The next argument (1000) is -r (--reps)
8
CS 3140 - SDE Spring 2024
● The argument after that (benefit) is -f (--format)
● And the argument after that (jefferson) is -a (--algorithm)
Just like long flags, these can go in any order, and only include some arguments:
java -jar Apportionment.jar census2020.xlsx -af jefferson alpha
The above means:
● Use the Jefferson Apportionment algorithm
● Print states alphabetically
● Use 435 reps (default)
A few words of encouragement: If you have followed the instructions and implemented the
code to the end of this part, congratulations! You have written a pretty complex piece of software
with several long and short flag arguments! This is no small feat, as it demonstrates a mastery
of functional and clean code design. I hope you take pride in what you have accomplished.
Keep up the awesome work, and continue to push yourselves to new heights in your future
projects!
3. Submission and Grading
Ensure all work has been merged to the main branch, then submit, as usual, answer the
questions on the Questions.md.
The coding portion of this assignment will be graded solely based on correct functionality, like
Homework 1 and 2. While we encourage practicing good style and testing (as they will likely
make developing the homework more efficient), we will not be grading anything other than the
functionality for the coding portion. Make sure your class and method names match this
document - we will use automated testing during this assignment to ensure efficient grading.
You should try to make your code DRY, but you will not be graded.
Functionality: We are only grading how correctly your code functions:
● Team Declaration: 5%
● Code Submission
○ Part A - 20%
○ Part B - 20%
○ Part C - 15%
○ Part D - 25%
● Submission and Reflection Questions - 20% (including 5% for Team Declaration)
○ 10% for answering the questions in Questions.md
○ 5% for filling out the Github information and setting up repo correctly
9
CS 3140 - SDE Spring 2024
4. Frequently Asked Questions
Q: Can I add code my team implemented in HW1-C?
A: Yes, you may add code that your team developed from HW1-C
Q: How do we access methods from an Apportionment if they are private?
A: You are welcome to **increase** the visibility level of any method (i.e., any private method
can be made protected or public, and any protected method can be made public).
Q: If a flag is syntactically incorrect/invalid ("-reps", "reps", or "--numreps") or if some
flag is missing, should we throw an error or should we allow the program to continue
running with the default configurations?
A: Any invalid flags or arguments not attached to flags should throw an error.
Q: Can I change the Method Signatures of Factory Classes?
A: No, you must adhere to the specification as given, only because it will make the grading job
more accessible if they can just use the automated test which relies upon a specific interface.
Q: May we use an external library to implement Arguments?
A: Yes. This is acceptable and in my view an intelligent thing to do. If you find the wheel that
works perfectly well, you don't need to invent a new one. Just make sure you do a proper import
through Gradle Dependencies.
Appendix A - Jefferson Apportionment Method
In this assignment, we will extend the pool of Apportionment methods with a new algorithm:
Jefferson Apportionment Method. As in Homework 1 with Hamilton and Huntington-Hill, we will
quickly illustrate the Jefferson Apportionment Method starting with these five states. For the first
few steps, this will look a lot like Hamilton. This Youtube video on Apportionment describes the
Jefferson Apportionment algorithm starting at the 13-minute mark.
In this example, we will be using 25 representatives once again.
Name Population
Delaware 989948
Maryland 6177224
Pennsylvania 13002700
Virginia 8631393
10
CS 3140 - SDE Spring 2024
West Virginia 1793716
Like with the Hamilton Method from HW1-A/B, we are going to get the total population
(30,594,981) and divide by our number of representatives (25) to get our divisor (roughly
1,223,799.24). And just like in Hamilton, we will divide every state by the divisor (Raw, in the
table below) and then Round all states DOWN (RoundedDown in the table below.)
Name Population Raw Rounded Down
Delaware 989948 0.808914 0
Maryland 6177224 5.04758 5
Pennsylvania 13002700 10.62486 10
Virginia 8631393 7.052948 7
West Virginia 1793716 1.465695 1
Now, if we were to allocate reps equal to the number rounded down, we would be allocating 23
representatives, which is two fewer than we should.
This is the part where we break away from Hamilton. In the Jefferson method, rather than
dealing with remainders, we instead change the divisor. That is, we start decreasing the value
of our divisor until we have allocated all 25 representatives.
So, for instance, if we change our divisor to 1.1 million (1,100,000), then our table becomes:
Name Population Raw Rounded Down
Delaware 989948 0.899953 0
Maryland 6177224 5.615658 5
Pennsylvania 13002700 11.82064 11
Virginia 8631393 7.846721 7
West Virginia 1793716 1.630651 1
We've allocated 24 representatives, but we are still short of 25. If we drop our divisor to 1
million (1,000,000), however, then we end up allocating too many representatives:
11
CS 3140 - SDE Spring 2024
Name Population Raw Rounded Down
Delaware 989948 0.989948 0
Maryland 6177224 6.177224 6
Pennsylvania 13002700 13.0027 13
Virginia 8631393 8.631393 8
West Virginia 1793716 1.793716 1
So that means 1 million is too small. I then tried 1.08 million (1,080,000) and got 25:
Name Population Raw Rounded Down
Delaware 989948 0.916619 0
Maryland 6177224 5.719652 5
Pennsylvania 13002700 12.03954 12
Virginia 8631393 7.992031 7
West Virginia 1793716 1.660848 1
Since we now have 25 representatives allocated, we stop here and accept this number. Yes,
this leaves Delaware without representation, but for the sake of simplicity, you should not try to
enforce "every state gets at least one representative" here. This would be the correct
Apportionment for this input.
Fun fact: In 1992, if Congress had been apportioned by the Jefferson method, Al Gore would
have won the 2000 Presidential Election even if no votes were changed. This is because
Jefferson heavily favors larger states.
12
软件开发、广告设计客服
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-23:00
微信:codinghelp
热点项目
更多
代写dts207tc、sql编程语言代做
2024-12-25
cs209a代做、java程序设计代写
2024-12-25
cs305程序代做、代写python程序...
2024-12-25
代写csc1001、代做python设计程...
2024-12-24
代写practice test preparatio...
2024-12-24
代写bre2031 – environmental...
2024-12-24
代写ece5550: applied kalman ...
2024-12-24
代做conmgnt 7049 – measurem...
2024-12-24
代写ece3700j introduction to...
2024-12-24
代做adad9311 designing the e...
2024-12-24
代做comp5618 - applied cyber...
2024-12-24
代做ece5550: applied kalman ...
2024-12-24
代做cp1402 assignment - netw...
2024-12-24
热点标签
mktg2509
csci 2600
38170
lng302
csse3010
phas3226
77938
arch1162
engn4536/engn6536
acx5903
comp151101
phl245
cse12
comp9312
stat3016/6016
phas0038
comp2140
6qqmb312
xjco3011
rest0005
ematm0051
5qqmn219
lubs5062m
eee8155
cege0100
eap033
artd1109
mat246
etc3430
ecmm462
mis102
inft6800
ddes9903
comp6521
comp9517
comp3331/9331
comp4337
comp6008
comp9414
bu.231.790.81
man00150m
csb352h
math1041
eengm4100
isys1002
08
6057cem
mktg3504
mthm036
mtrx1701
mth3241
eeee3086
cmp-7038b
cmp-7000a
ints4010
econ2151
infs5710
fins5516
fin3309
fins5510
gsoe9340
math2007
math2036
soee5010
mark3088
infs3605
elec9714
comp2271
ma214
comp2211
infs3604
600426
sit254
acct3091
bbt405
msin0116
com107/com113
mark5826
sit120
comp9021
eco2101
eeen40700
cs253
ece3114
ecmm447
chns3000
math377
itd102
comp9444
comp(2041|9044)
econ0060
econ7230
mgt001371
ecs-323
cs6250
mgdi60012
mdia2012
comm221001
comm5000
ma1008
engl642
econ241
com333
math367
mis201
nbs-7041x
meek16104
econ2003
comm1190
mbas902
comp-1027
dpst1091
comp7315
eppd1033
m06
ee3025
msci231
bb113/bbs1063
fc709
comp3425
comp9417
econ42915
cb9101
math1102e
chme0017
fc307
mkt60104
5522usst
litr1-uc6201.200
ee1102
cosc2803
math39512
omp9727
int2067/int5051
bsb151
mgt253
fc021
babs2202
mis2002s
phya21
18-213
cege0012
mdia1002
math38032
mech5125
07
cisc102
mgx3110
cs240
11175
fin3020s
eco3420
ictten622
comp9727
cpt111
de114102d
mgm320h5s
bafi1019
math21112
efim20036
mn-3503
fins5568
110.807
bcpm000028
info6030
bma0092
bcpm0054
math20212
ce335
cs365
cenv6141
ftec5580
math2010
ec3450
comm1170
ecmt1010
csci-ua.0480-003
econ12-200
ib3960
ectb60h3f
cs247—assignment
tk3163
ics3u
ib3j80
comp20008
comp9334
eppd1063
acct2343
cct109
isys1055/3412
math350-real
math2014
eec180
stat141b
econ2101
msinm014/msing014/msing014b
fit2004
comp643
bu1002
cm2030
联系我们
- QQ: 9951568
© 2021
www.rj363.com
软件定制开发网!