Question
public void setInit() { states.add(alabama); states.add(alaska); states.add(arizona); states.add(arkansas); states.add(california); states.add(colorado); states.add(connecticut); states.add(delaware); states.add(florida); states.add(georgia); states.add(hawaii); states.add(idaho); states.add(illinois); states.add(indiana); states.add(iowa); states.add(kansas); states.add(kentucky); states.add(louisiana); states.add(maine); states.add(maryland); states.add(massachusetts);
public void setInit() { states.add("alabama"); states.add("alaska"); states.add("arizona"); states.add("arkansas"); states.add("california"); states.add("colorado"); states.add("connecticut"); states.add("delaware"); states.add("florida"); states.add("georgia"); states.add("hawaii"); states.add("idaho"); states.add("illinois"); states.add("indiana"); states.add("iowa"); states.add("kansas"); states.add("kentucky"); states.add("louisiana"); states.add("maine"); states.add("maryland"); states.add("massachusetts"); states.add("michigan"); states.add("minnesota"); states.add("mississippi"); states.add("missouri"); states.add("montana"); states.add("nebraska"); states.add("nevada"); states.add("new hampshire"); states.add("new jersey"); states.add("new mexico"); states.add("new york"); states.add("north carolina"); states.add("north dakota"); states.add("ohio"); states.add("oklahoma"); states.add("oregon"); states.add("pennsylvania"); states.add("rhode island"); states.add("south carolina"); states.add("south dakota"); states.add("tennessee"); states.add("texas"); states.add("utah"); states.add("vermont"); states.add("virginia"); states.add("washington"); states.add("west virginia"); states.add("wisconsin"); states.add("wyoming"); }
Steps: 1. Modify your Main class to look like the following: import java.util.Scanner; public class Main \{ public static void main(String[] args) \{ Scanner sc = new Scanner(System. in); States st = new States( ); sc.close( ); \} Here we are adding the Scanner that will take in the users input of States. As a reminder, the Scanner class is used to take input from a user using the console. By calling various methods on our Scanner Object, we will be able to take input from the console and store those expression into variables. We are also going to create our States object as the class we are going to create is going to be called States. All of the logic in this program will be done in a different class and the main method is just going to refer to those behaviors as it is best practice for a our main methods to hold minimal code. 2. Create a States class in the States. java file. 3. Add the following code inside the class. private HashSet> states = new HashSet( (); private HashSet> answers = new HashSet> (); Here, we are creating two new HashSet object and storing the reference in a variable called 'states' and another reference variable 'answers'. Additionally, notice the angle brackets '' which contain the word 'String'. These are known as 'Generics'. Different classes, particularly those in the Collection API of Java, use 'Generics' to specify what Datatype can be used with an Object that has been created. In this case, we are specifying that our HashSet 'states' and 'answers' can only contain String objects. If we try to place other Objects within this HashSet, we would be met with a compilation error. 4. Be sure to include your import statements for the HashSet. You may be wondering why we need two HashSets...don't worry, we'll explain that shortly! 5. Now add the following code to your States class underneath the created HashSet objects: Here, we are creating a method and adding all 50 states to the 'states' HashSet. The add () method allows us to insert values into the collection. 6. Now, create a new method called checkForstate. Remember that Java does not support nested methods. Meaning methods being made inside other methods. We need to create a method below our setInit0. Reminder the start and end to methods are signified by the opening and closing curley brackets. As mentioned we are creating a game in which a user can try to guess all 50 states! So we are going to make a method that does that logic for us. 7. Make the checkForState method take in a Scanner object as a parameter. Example method: public int age(Scanner sc ){ int age = sc. sextInt(); return age; \} Note the above is an example method not the code you need for this activity. You may be wondering why or how this is possible. Remember that Scanner is a class in Java. Classes can be used to create objects. We are doing that here we are simply saying we need a Scanner object called sc that will represent the placeholder for the users input. We can then use that object in our method to get the users input in our checkForState method. To do this however, we must also import in Scanner to our class. 8. Add your scanner import below your HashSet import at the top of the file. 9. In our checkForState method create a while loop that checks that the size of our answers Set is smaller than the size of our states Set. The purpose of this is to tell us if the user has guessed all 50 states correctly. If not we need the loop to continue till they are all guessed. - Create a print statement that asks the user to "Enter a State:" - Create a variable that stores the users input and converts their answer to all lower case. (Few things to remember: 1)We have a Scanner object being passed into our method. We can use this to get the users input we do not need to create a new Scanner. 2)There is a method that will convert Strings to all lower case letters. - Now we need to check if the users input is an actual state. - If our states collection contains the users input. - If the users input is not in the collection we need to add the state to our answers collection and print System. out.println( "You've added: " + + " to the collection!"); Otherwise the input was already entered into our answers collection we need to print. System. out.println( "Sorry, you've already added: " + + " previously!"); - Otherwise the users input is not a state we need to print. System.out.println( "Sorry: " + + " is not a State! (double check your spelling)"); - Finally end the method with telling the users how many more states they have left to guess. Fill in the blank with the correct equation. System.out.println(_______+ + " more to go!" ); There are many improvements that could be made, and this code can definitely be expended upon, but hopefully this showcases how we can start to apply some of these concepts and combine all of the tools we've been working with in interesting waysStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started