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.
|
private final String code
private int majorCount
findMajor(String)
,
majorDemographics
private final MajorDemographics[] majorDemographics
private final String name
public CollegeDemographics(String code, String name)
code
- the unique college codename
- the friendly name of the collegepublic 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 MajorDemographicprivate 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()
,
majorCount
public String getCode()
code
public String getName()
name
public 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%%%nWhich 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 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