Question

JAVA Create a Governor class with the following attributes: name : String party - char (the...

JAVA

Create a Governor class with the following attributes:

name : String

party - char (the character will be either D, R or I)

ageWhenElected : int

Create a State class with the following attributes:

name : String

abbreviation : String

population : long

governor : Governor

Your classes will have all setters, getters, typical methods to this point (equals(), toString()) and at least 3 constructors (1 must be the default, 1 must be the copy).

You will be turning in the UML diagram - it behooves you to diagram first.

Test your classes with hard-coded data before attempting to create the data structure from the file.

The file that we will be using to create the data structure: governerAndState.csv

The data structure will be an array of objects, NOT an ArrayList. We know that there are 50 states (we are not including US territories or the District of Columbia), so we will use a data structure that contains exactly 50 State objects.

The required operations on the data structure - these are in a services/utilities class NOT in the driver:

display all governors by name -- self-explanatory

display all governors by party --- your method will display a list of options ( Democratic, Republican or Independent) and the user will select (you may consider using JOptionPane buttons for this)

determine and display the youngest governor (when elected since that's what we know) -- self-explanatory

determine and display the oldest governor (when elected) -- self-explanatory

search by governor name, display state --- your method will request of the user a portion of a name, search all of the states for the governor's name and display the state and the state abbreviation where that governor is

change governor --- your method will allow the user to enter the state (you may go by abbreviation here) and the new governor's name, age and party --- your method will change the governor in that state

display state by population --- your method will allow the user to enter a population and display all states that have a population greater than the user request.

display state by first letter --- your method will request a letter of the user, the method will display all states that begin with that letter or a message stating that no state starts with that letter.

display all --- display all information for the state object --- the population should be well formatted, that is with commas every 3rd digit.

Deliverables (zipped)

Governor.java

State.java

StateUtilities.java

StateDriver.java

governorAndState.csv (the data file)

_______________________________________________________________________________________

((governorAndState.csv)

State,State abbreviation,population,Governor name,party,age when elected
Alabama,AL,4858979,Kay Ivey,R,73
Alaska,AK,738432,Bill Walker,I,63
Arizona,AZ,6828065,Doug Ducey,R,51
Arkansas,AR,2978204,Asa Hutchinson,R,65
California,CA,39144818,Jerry Brown,D,73
Colorado,CO,5456574,John Hickenlooper,D,59
Connecticut,CT,3590886,Dan Malloy,D,56
Delaware,DE,945934,John Carney,D,61
Florida,FL,20271272,Rick Scott,R,59
Georgia,GA,10214860,Nathan Deal,R,69
Hawaii,HI,1431603,David Ige,D,57
Idaho,ID,1654930,C.L. “Butch” Otter,R,65
Illinois,IL,12859995,Bruce Rauner,R,58
Indiana,IN,6619680,Eric Holcomb,R,49
Iowa,IA,3123899,Kim Reynolds,R,58
Kansas,KS,2911641,Jeff Colyer,R,58
Kentucky,KY,4425092,Matt Bevin,R,48
Louisiana,LA,4670724,John Bel Edwards,D,50
Maine,ME,1329328,Paul LePage,R,63
Maryland,MD,6006401,Larry Hogan,R,59
Massachusetts,MA,6794422,Charlie Baker,R,59
Michigan,MI,9922576,Rick Snyder,R,53
Minnesota,MN,5489594,Mark Dayton,D,64
Mississippi,MS,2992333,Phil Bryant,R,58
Missouri,MO,6083672,Eric Greitens,R,53
Montana,MT,1032949,Steve Bullock,D,47
Nebraska,NE,1896190,Pete Ricketts,R,51
Nevada,NV,2890845,Brian Sandoval,R,48
New Hampshire,NH,1330608,Chris Sununu,R,53
New Jersey,NJ,8958013,Phil Murphy,D,61
New Mexico,NM,2085109,Susana Martinez,R,52
New York,NY,19795791,Andrew Cuomo,D,54
North Carolina,NC,10042802,Roy Cooper,D,60
North Dakota,ND,756927,Doug Burgum,R,60
Ohio,OH,11613423,John Kasich,R,59
Oklahoma,OK,3911338,Mary Fallin,R,57
Oregon,OR,4028977,Kate Brown,D,55
Pennsylvania,PA,12802503,Tom Wolf,D,67
Rhode Island,RI,1056298,Gina Raimondo,D,44
South Carolina,SC,4896146,Henry McMaster,R,70
South Dakota,SD,858469,Dennis Daugaard,R,58
Tennessee,TN,6600299,Bill Haslam,R,53
Texas,TX,27469114,Greg Abbott,R,58
Utah,UT,2995919,Gary R. Herbert,R,62
Vermont,VT,626042,Phil Scott,R,59
Virginia,VA,8382993,Ralph Northam,D,59
Washington,WA,7170351,Jay Inslee,D,62
West Virginia,WV,1844128,Jim Justice,R,66
Wisconsin,WI,5771337,Scott Walker,R,44
Wyoming,WY,586107,Matthew Mead,R,49

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE TO COPY:

Governor.java

class Governor{

private String name;

private char party;

private int ageWhenElected;

public Governor()

{

this.name="";

this.party='\0';

this.ageWhenElected=0;

}

public Governor(String name, char party, int age)

{

this.name=name;

this.party=party;

this.ageWhenElected=age;

}

public Governor(Governor g)

{

this.name=g.name;

this.ageWhenElected=g.ageWhenElected;

this.party=g.party;

}

/**

* @return the name

*/

public String getName() {

return name;

}

/**

* @param name the name to set

*/

public void setName(String name) {

this.name = name;

}

/**

* @return the party

*/

public char getParty() {

return party;

}

/**

* @param party the party to set

*/

public void setParty(char party) {

this.party = party;

}

/**

* @return the ageWhenElected

*/

public int getAgeWhenElected() {

return ageWhenElected;

}

/**

* @param ageWhenElected the ageWhenElected to set

*/

public void setAgeWhenElected(int ageWhenElected) {

this.ageWhenElected = ageWhenElected;

}

public String toString()

{

return "Governor Name: "+name+"\nParty: "+party+"\nAge when elected: "+ageWhenElected;

}

}

State.java

class State{

private String name;

private String abbreviation;

private long population;

private Governor governor;

/**

* @return the name

*/

public String getName() {

return name;

}

/**

* @param name the name to set

*/

public void setName(String name) {

this.name = name;

}

/**

* @return the abbreviation

*/

public String getAbbreviation() {

return abbreviation;

}

/**

* @param abbreviation the abbreviation to set

*/

public void setAbbreviation(String abbreviation) {

this.abbreviation = abbreviation;

}

/**

* @return the population

*/

public long getPopulation() {

return population;

}

/**

* @param population the population to set

*/

public void setPopulation(long population) {

this.population = population;

}

/**

* @return the governor

*/

public Governor getGovernor() {

return governor;

}

/**

* @param governor the governor to set

*/

public void setGovernor(Governor governor) {

this.governor = governor;

}

public String toString()

{

String pp=Long.toString(population);

String resPop="";

for(int i=0; i<pp.length(); i++)

{

if(i!=0 && i%3==0 && i!=pp.length()-1)

{

resPop+=",";

}

resPop+=pp.charAt(i);

}

return "State: "+name+"\nAbbrevaition: "+abbreviation+"\nPopulation: "+resPop+"\n"+governor.toString();

}

public State()

{

//default constructor

population=0;

name="";

abbreviation="";

}

public State(State s)

{

//copy constructor

this.name=s.name;

this.abbreviation=s.abbreviation;

this.population=s.population;

this.governor=s.governor;

}

public State(String sname, String abb, long pop, String gname, char party, int age)

{

Governor g=new Governor(gname, party, age);

this.governor=g;

this.name=sname;

this.abbreviation=abb;

this.population=pop;

}

}

StateUtilities.java

import java.util.*;

class StateUtilities

{

public void displayGovernorsByName(State array[])

{

Scanner obj=new Scanner(System.in);

System.out.println("Enter the name of the governor.");

String name=obj.nextLine();

for(int i=0; i<array.length; i++)

{

if(array[i].getGovernor().getName().equalsIgnoreCase(name))

{

System.out.println(array[i].getGovernor()+"\n");

}

}

}

public void displayGovernorsByParty(State array[])

{

Scanner obj=new Scanner(System.in);

System.out.println("Enter the party name.\nD Democratic\nR Republic\nI Independent");

char party=obj.nextLine().charAt(0);

for(int i=0; i<array.length; i++)

{

if(array[i].getGovernor().getParty() == party)

{

System.out.println(array[i].getGovernor()+"\n");

}

}

}

public void displayTheYoungestGovernor(State array[])

{

State young=array[0];

for(int i=0; i<array.length; i++)

{

if(array[i].getGovernor().getAgeWhenElected()<young.getGovernor().getAgeWhenElected())

young=array[i];

}

System.out.println(young.getGovernor());

}

public void displayTheOldestGovernor(State array[])

{

State old=array[0];

for(int i=0; i<array.length; i++)

{

if(array[i].getGovernor().getAgeWhenElected()>old.getGovernor().getAgeWhenElected())

old=array[i];

}

System.out.println(old.getGovernor());

}

public void searchByNameDisplayState(State array[])

{

System.out.println("Enter the name of the governor.");

Scanner obj=new Scanner(System.in);

String name=obj.nextLine();

System.out.printf("\n\n%30s%30s\n", "State", "Abbreviation");

for(int i=0; i<array.length; i++)

{

if(array[i].getGovernor().getName().equalsIgnoreCase(name))

{

System.out.printf("%30s%30s\n", array[i].getName(), array[i].getAbbreviation());

}

}

System.out.println();

}

public void displayStateByPopulation(State array[])

{

Scanner obj=new Scanner(System.in);

System.out.println("Enter the population of the state.");

long pop=Long.parseLong(obj.nextLine());

for(int i=0; i<array.length; i++)

{

if(array[i].getPopulation()>pop)

System.out.println(array[i]+"\n");

}

}

public void displayAll(State array[])

{

for(int i=0; i<array.length; i++)

{

System.out.println(array[i]+"\n");

}

}

public void changeGovernor(State array[])

{

Scanner obj=new Scanner(System.in);

System.out.println("Enter the abbreviation of the state.");

String ab=obj.nextLine();

State res=null;

for(int i=0; i<array.length; i++) {

if(array[i].getAbbreviation().equalsIgnoreCase(ab))

{

res=array[i];

break;

}

}

if(res==null)

System.out.println("No such state found!");

else {

System.out.println("Enter the new name.");

String name=obj.nextLine();

System.out.println("Enter the party.");

char party=obj.nextLine().charAt(0);

System.out.println("Enter the age.");

int age=Integer.parseInt(obj.nextLine());

Governor g=new Governor(name, party, age);

res.setGovernor(g);

//print the information

System.out.println("New Information: ");

System.out.println(res);

}

}

public void displayStatesByLetter(State array[])

{

Scanner obj=new Scanner(System.in);

System.out.println("Enter the first letter of the state.");

char letter=obj.nextLine().charAt(0);

boolean isFound=false;

for(int i=0; i<array.length; i++)

{

if(array[i].getName().charAt(0)==letter)

{

System.out.println(array[i]);

isFound=true;

}

}

if(!isFound)

System.out.println("No such state found!");

}

}

StateDriver.java

import java.util.*;

import java.io.*;

public class StateDriver {

public static void main(String[] args) throws FileNotFoundException {

System.out.println("\n\n----------------HARD CODED DATA TESTING-----------------------");

State s=new State("Uttar Pradesh", "UP", 10999009, "Ram Naik", 'D', 44);

System.out.println(s);

System.out.println("\n----------------TESTING ON FILE DATA-------------------\n");

State states[]=new State[50];//50 states array

//populate the array

File file=new File("governerAndState.csv");

Scanner obj=new Scanner(file);

int index=0;

while(obj.hasNextLine())

{

String line[]=obj.nextLine().split(",");//split the line by a comma

State state=new State(line[0], line[1], Long.parseLong(line[2]), line[3], line[4].charAt(0), Integer.parseInt(line[5]));

states[index++]=state;

}

//test the utilities methods

StateUtilities util=new StateUtilities();

System.out.println("\n---------------------------Testing Display all governors by name-------------------\n");

util.displayGovernorsByName(states);

System.out.println("\n---------------------------Testing Display all governors by party-------------------\n");

util.displayGovernorsByParty(states);

System.out.println("\n---------------------------Testing Display youngest governor-------------------\n");

util.displayTheYoungestGovernor(states);

System.out.println("\n---------------------------Testing Display oldest governor-------------------\n");

util.displayTheOldestGovernor(states);

System.out.println("\n---------------------------Testing Display all governors state, search by name-------------------\n");

util.searchByNameDisplayState(states);

System.out.println("\n---------------------------Testing change governor-------------------\n");

util.changeGovernor(states);

System.out.println("\n---------------------------Testing Display state by population-------------------\n");

util.displayStateByPopulation(states);

System.out.println("\n---------------------------Testing Display state by first letter-------------------\n");

util.displayStatesByLetter(states);

System.out.println("\n---------------------------Testing Display all states-------------------\n");

util.displayAll(states);

}

}

PROGRAM SCREENSHOTS:

OUTPUT: (Since the output was quite large, i have attached only a few screenshots)

In case of any doubt, do let me know in the comments section. I'll get it resolved :):) Do give a THUMBS UP!! :):)

Add a comment
Know the answer?
Add Answer to:
JAVA Create a Governor class with the following attributes: name : String party - char (the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • State Lookup Table: Below are two tables, stateStrings (50 rows by 6 columns) and stateInts (50...

    State Lookup Table: Below are two tables, stateStrings (50 rows by 6 columns) and stateInts (50 rows by 3 columns), that contain data for the 50 states. Below are examples for the first state, Alabama: stateStrings: State Abbrev Name Capital Largest City Governor Political Party AL Alabama Montgomery Birmingham Robert Julian Bentley Republican stateInts: State Index Population Electoral Votes 0 4,858,979 9 Write a java application that uses the full tables below to look up the corresponding data using the...

  • In Java Create a class Worker. Your Worker class should include the following attributes as String...

    In Java Create a class Worker. Your Worker class should include the following attributes as String variables: Name, Worker ID, Worker Address, Worker City, Worker State Create the constructor that initializes the above worker attributes. Then make another class name HourlyWorker that inherits from the Worker class.   HourlyWOrker has to use the inherited parent class variables and add in hourlySalary and billableHours. Your HourlyWorker class should contain a constructor that calls the constructor from the Worker class to initialize the...

  • Create a java project and create Student class. This class should have the following attributes, name...

    Create a java project and create Student class. This class should have the following attributes, name : String age : int id : String gender : String gpa : double and toString() method that returns the Student's detail. Your project should contain a TestStudent class where you get the student's info from user , create student's objects and save them in an arralist. You should save at least 10 student objects in this arraylist using loop statement. Then iterate through...

  • Create an abstract class Employee. Your Employee class should include the following attributes: First name (string)...

    Create an abstract class Employee. Your Employee class should include the following attributes: First name (string) Last name (string) Employee id (string) Employee home street address (string) Employee home city (string) Employee home state (string) Write a constructor to initialize the above Employee attributes. Create an abstract method called earnings.      Create another class HourlyEmployee that inherits from the abstract Employee class.   HourEmployee must use the inherited parent class variables and add in attributes HourlyRate and HoursWorked. Your HourEmployee class should...

  • Create an abstract class Employee. Your Employee class should include the following attributes: First name (string)...

    Create an abstract class Employee. Your Employee class should include the following attributes: First name (string) Last name (string) Employee id (string) Employee home street address (string) Employee home city (string) Employee home state (string) Write a constructor to initialize the above Employee attributes. Create an abstract method called earnings.      Create another class HourlyEmployee that inherits from the abstract Employee class.   HourEmployee must use the inherited parent class variables and add in attributes HourlyRate and HoursWorked. Your HourEmployee class should...

  • create a java prgram that have UniversityDriver Class The UniversityDriver contains the main method. The following...

    create a java prgram that have UniversityDriver Class The UniversityDriver contains the main method. The following commands can be entered: (a) "hire" //to hire a new faculty member (b) "admit" //to admit a new student (c) "find student" //to display information about a specific student: name, date of birth, and major (d)"find faculty" //display information about a specific faculty: name, date of birth, and courses; (e) “list students” // list the first and last names of all students (f) “list...

  • C++ HELP! Create a class and name it Payslip. This class should have the following attributes...

    C++ HELP! Create a class and name it Payslip. This class should have the following attributes or properties: name, pay grade, basic salary, overtime hours, overtime pay, gross pay, net pay and withholding tax. Define the accessors and mutators of the Payslip class based on its attributes or properties. This class should also have the following methods: determinePayGradeAndTaxRate and computePay. Create another class and name it Employee. This class will contain the main method. In the main method, instantiate an...

  • in Java Create an object class for the Employee Master (Employee). Select data types that will...

    in Java Create an object class for the Employee Master (Employee). Select data types that will best represent the data to be stored. All variables are to be declared with a private access modifier. Provide assessor/mutator methods for each instance field. Also provide a display data method that displays all of the instance fields of the class. Be sure each field is preceded by the text name of the field. Employee Master (Employee) Employee Number                              Numeric                 0 decimals, max size...

  • FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width,...

    FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width, height (in inches), weight (in pounds), address (1 line), city, state, zip code. These variables must be private. Create setter and getter functions for each of these variables. Also create two constructors for the box class, one default constructor that takes no parameters and sets everything to default values, and one which takes parameters for all of the above. Create a calcShippingPrice function that...

  • public class Pet {    //Declaring instance variables    private String name;    private String species;...

    public class Pet {    //Declaring instance variables    private String name;    private String species;    private String parent;    private String birthday;    //Zero argumented constructor    public Pet() {    }    //Parameterized constructor    public Pet(String name, String species, String parent, String birthday) {        this.name = name;        this.species = species;        this.parent = parent;        this.birthday = birthday;    }    // getters and setters    public String getName() {        return name;   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT