Question

Hello, I need some help creating this class in Java. This is for a larger project....

Hello, I need some help creating this class in Java. This is for a larger project. I am using Eclipse if that helps

1. Create a class named State that will store information about a state and provide methods to get, and set the data, and compare the states by several fields.

a. Fields: Name, Capital City, Abbreviation, Population, Region, US House Seats

b. Constructor

c. Get and set methods for each field

d. Compare method to compare State objects based on Name string (expects a State object as a parameter)

e. Method to print a state

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

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// State.java

public class State {
   private String name;
   private String capitalCity;
   private String abbreviation;
   private int Population;
   private String region;
   private int usHouseSeats;

   public State(String name, String capitalCity, String abbreviation,
           int population, String region, int usHouseSeats) {

       this.name = name;
       this.capitalCity = capitalCity;
       this.abbreviation = abbreviation;
       this.Population = population;
       this.region = region;
       this.usHouseSeats = usHouseSeats;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public String getCapitalCity() {
       return capitalCity;
   }

   public void setCapitalCity(String capitalCity) {
       this.capitalCity = capitalCity;
   }

   public String getAbbreviation() {
       return abbreviation;
   }

   public void setAbbreviation(String abbreviation) {
       this.abbreviation = abbreviation;
   }

   public int getPopulation() {
       return Population;
   }

   public void setPopulation(int population) {
       Population = population;
   }

   public String getRegion() {
       return region;
   }

   public void setRegion(String region) {
       this.region = region;
   }

   public int getUsHouseSeats() {
       return usHouseSeats;
   }

   public void setUsHouseSeats(int usHouseSeats) {
       this.usHouseSeats = usHouseSeats;
   }

   public int compare(State s) {
       int res = 0;
       if (this.name.compareTo(s.getName()) < 0)
           res = -1;
       else if (this.name.compareTo(s.getName()) > 0)
           res = 1;
       else if (this.name.compareTo(s.getName()) == 0)
           res = 0;
       return res;
   }

   @Override
   public String toString() {
       return "State [Name=" + name + ", Capital City=" + capitalCity
               + ", Abbreviation=" + abbreviation + ", Population="
               + Population + ", region=" + region + ", US House Seats="
               + usHouseSeats + "]";
   }

}
___________________________

// Test.java

public class Test {

   public static void main(String[] args) {
       State s1 = new State("West Virginia", "Richmond", "WV", 45023,"The South", 5);
       State s2 = new State("Alaska", "Juneau", "AL", 54345,"Western Alaska", 6);

       System.out.println(s1);
       System.out.println(s2);

   }

}
____________________________

Output:

State [Name=West Virginia, Capital City=Richmond, Abbreviation=WV, Population=45023, region=The South, US House Seats=5]
State [Name=Alaska, Capital City=Juneau, Abbreviation=AL, Population=54345, region=Western Alaska, US House Seats=6]

___________________________Thank You

Add a comment
Know the answer?
Add Answer to:
Hello, I need some help creating this class in Java. This is for a larger project....
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
  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class...

    Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class and then use it in a Java program. The program should create two Rectangle objects and find their area and perimeter. Instructions Make sure the class file named Rectangle.java is open. In the Rectangle class, create two private attributes named lengthand width. Both length and width should be data type double. Write public set methods to set the values for length and width. Write...

  • Java - Object Oriented Programming Declare a class named Customer that has two private fields? Write...

    Java - Object Oriented Programming Declare a class named Customer that has two private fields? Write a set method to make sure that if age > 125 years or less than 0 then it is set to 0 (default value) What does it indicate when declaring a class's instance variables with private access modifier? What is the role of a constructor? The data part of by an object's instance variables is known as? Do all methods have to always return...

  • Hello, In need of help with this Java pa homework. Thank you! 2. Create a normal...

    Hello, In need of help with this Java pa homework. Thank you! 2. Create a normal (POJo, JavaBean) class to represent, i. e. model, varieties of green beans (also known as string beans or snap beans). Following the steps shown in "Assignment: Tutorial on Creating Classes in IntelliJ", create the class in a file of its own in the same package as class Main. Name the class an appropriate name. The class must have (a) private field variables of appropriate...

  • IN JAVA Write a class Store which includes the attributes: store name, city. Write another class...

    IN JAVA Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method...

  • Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class...

    Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...

  • how would i write code in java/netbeans for this: You will be creating the driver class...

    how would i write code in java/netbeans for this: You will be creating the driver class for this homework (PA05_TVs.java) and the object/element class, TV.java. Your TV class should include only a channel value for instance data (3-99) and the following methods: constructor [requires an initial channel value], getter, setter, randomChannel() [does not return the new channel value], and toString(). The driver class must complete the following: Instantiate a TV object and set the initial channel to 97. Instantiate a...

  • Start a new project in NetBeans that defines a class Person with the following: Instance variables...

    Start a new project in NetBeans that defines a class Person with the following: Instance variables firstName (type String), middleInitial (type char) and lastName (type String) (1) A no-parameter constructor with a call to the this constructor; and (2) a constructor with String, char, String parameters (assign the parameters directly to the instance variables in this constructor--see info regarding the elimination of set methods below) Accessor (get) methods for all three instance variables (no set methods are required which makes...

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

  • Part I: Problem: Code the following in Java: Candy Class -price -number of pounds sold Create...

    Part I: Problem: Code the following in Java: Candy Class -price -number of pounds sold Create the following methods: -Two constructors – default and second - accessor and mutator methods for each of the fields. - check for invalid price. Negative price is invalid (use if then else and display error message) - a method named userInput to get user input for all the fields. - check for invalid price. Negative price is invalid (use a loop to prompt the...

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