public class CollegeDemographics
extends Object
| Modifier and Type | Field and Description |
|---|---|
private String |
code
The college code (NS/EG,etc).
|
private int |
majorCount
The total number of majorDemographics *currently* stored in the array.
|
private MajorDemographics[] |
majorDemographics
An array that contains the majorDemographics.
|
private String |
name
The college name (Natural Sciences/Engineering,etc).
|
| Constructor and Description |
|---|
CollegeDemographics(String code,
String name)
|
| Modifier and Type | Method and Description |
|---|---|
void |
addGenderDemographic(String major,
String gender)
Increases the gender demographic count for a major.
|
private MajorDemographics |
findMajor(String major)
Finds a MajorDemographic in the
majorDemographics array based
on the major name. |
String |
getCode()
Accessor for getting the unique college code.
|
String |
getName()
Accesso for the friendly college name
|
String |
getTable()
A String 'table' representation of all the majorDemographics in the college.
|
static void |
main(String[] args)
The main in this class is used for testing.
|
String |
toHtmlTable()
Builds an HTML table representation of the data.
|
private final String code
private final String name
private final MajorDemographics[] majorDemographics
private int majorCount
findMajor(String),
majorDemographicspublic CollegeDemographics(String code,
String name)
code - the unique college codename - the friendly name of the collegepublic String getCode()
codepublic String getName()
namepublic void addGenderDemographic(String major,
String gender)
findMajor(String), and then adds to the demographic
by calling MajorDemographics.addGenderDemographic(String) passing
in the gender M/F/Nmajor - the major to assign demographicgender - M/F/N to be used by MajorDemographicpublic String getTable()
CollegeName: Major Male Female
MajorName Male% Female%
Major2Name Male% Female%
For example:
Natural Sciences: Major Male Female
Psychology 23.70% 76.30%
Zoology 17.61% 82.39%
Data Science 70.27% 29.73%
Biological Science 27.71% 72.29%
The spacing is accomplished using String format. For the
majorDemographics lines we used
%30s%10.2f%%%10.2f%%%n
Which forced 2 decimal places, with a padding of 10 characters. Though
it could be implemented differently.
Looping from i to i < majorCount will be helpful
in building this, and while not required, we used StringBuilder
to concatenate the Strings (more memory efficient). Notice
the See section for more methods that will help.
Please note: We won't be grading on spaces, but you should try to make it readable for your use.
MajorDemographics.getPercents(),
MajorDemographics.getName()public String toHtmlTable()
NEW to implement in p4 - remember to add tests to your main for it.
This method loops through all the majorDemographics objects, and builds an HTML table to display them later. It will not include the college name, just the major data stored in the college. For example:
<table>
<tr><th>Major</th> <th>Male</th> <th>Female</th></tr>
<tr><td>Psychology</td> <td>23.70%</td> <td>76.30%</td></tr>
<tr><td>Zoology</td> <td>17.61%</td> <td>82.39%</td> </tr>
<tr><td>Data Science</td> <td>70.27%</td> <td>29.73%</td></tr>
<tr><td>Biological Science</td> <td>27.71%</td> <td>72.29%<td></tr>
</table>
Spacing and line returns are optional, but they may help you read what you are doing.Tag.td(String),
Tag.tr(String),
Tag.th(String),
Tag.tableClose(),
Tag.tableOpen(),
MajorDemographics.toHtmlRow()private MajorDemographics findMajor(String major)
majorDemographics array based
on the major name. If
the MajorDemographic is not found, it builds a new one with the
name of major, adds it to the array,
increments the majorCount, and returns the new MajorDemographic.
Implementation Hints
majorCount. majorCount location, and then
increment major countmajor - the name of the major to findMajorDemographics.getName(),
majorCountpublic static void main(String[] args)
TEST: getCode() returns NS - NS TEST: getName() returns Natural Sciences - Natural Sciences TEST: findMajor, set value - Major1 TEST: findMajor, add value - Major2
Here are some example tests we wrote
// Notice we built a CollegeDemographics object to help with tests
// this essentially tests the constructor also
CollegeDemographics demo = new CollegeDemographics("NS", "Natural Sciences");
System.out.println("TEST: getCode() returns NS - " + demo.getCode());
// create an empty MajorDemographic - does need it implemented with
// constructor and accessor
MajorDemographics major1 = new MajorDemographics("Major1");
// force adding a major, and incrementing count.
demo.majorDemographics[0] = major1;
demo.majorCount = 1;
MajorDemographics majorTest = demo.findMajor("Major1");
System.out.println("TEST: findMajor, set value - " + majorTest.getName());
// now trying with auto add
majorTest = demo.findMajor("Major2");
System.out.println("TEST: findMajor, add value - " + majorTest.getName());
args - required for completeness, not used