安卓代写-Name Guessing Game

Exam: Name Guessing Game

Create a simple game name guessing game. The game will generate a random name. The player will try to guess the name. If the name is two high (later alphabetically), the game will say that the guess was too high. If the guess was too low, the game will say that as well. If the guess was too short (too few letters) or too long, the game will say that as well.

Here is what the basic layout should look like in portrait on a Nexus 7:

I am also providing other examples of the layout in different formats. The layout notes section discusses some issues that you may need to know to complete the layout. There are two activites for the exam. See below for details on the second activity layout.

Please name your project: Examzhaoshiy

Functionality

This is a simple name guessing game. The program chooses a random name and gender. If the gender is male, it displays this image:

If the gender is female, it displays this image:

The user types a name in the EditText control and presses Guess. If the guess is wrong, display one of the following messages in a TextView:

You are too high and too short! You are too high and too long!

You are too high!

You are too low and too short! You are too low and too long!

You are too low!

Too high means you are too high alphabetically. You can determine that using this Java command:

int comp = guess.compareToIgnoreCase(name);

If comp < 0, you are too low. If comp > 0, you are too high. If comp == 0, you have guessed the name correctly. For the purposes of this exam, you may hard-code the strings into your program, but not into your layout. Note that you can change a string in a TextView using the setText function:

resultView.setText(result);

If you get the name correct, switch to an activity that looks like this:

Of course, it should display the correct image and indicate the name and the number of guesses.

At any time, the user can press “Give Up” and it will jump to that activity and display “You gave up!” instead of “Guessed in ? guesses”.

Names

I am providing a file with a simple class you can use to get the names:

Names.java

You can download that file and add it to your project. You will have to change the package name and put it in the right directory. An easier

solution is to create an empty class called Names and then cut and paste the code from Names.java into your copy.

Create a member variable like this:

private Names names = new Names();

Then you can get a new gender and name at any time using this code:

names.choose(); girl = names.isGirl();

name = names.getName();

Layout Notes

Both layouts must work for Nexus 7, Nexus S, and Nexus One. I have provided examples of the main activity layout.

In portrait mode, everything is centered horizontally. In landscape mode, the image is the left and the other fields are to the right and the centers line up among the four fields.

The second activity is almost identical to the first activity. Get the first activity working, then copy it to make the second activity.

You have to create a different layout for landscape than for portrait. Get portrait right first, then copy it into a directory called layout-land and modify it to make the landscape version. I recommend that you complete the program with the portrait layout and add the landscape layout last.

Be sure your program works right when you rotate the screen. It should NOT choose a new name when you rotate the screen.

Android Studio likes to default to padding all around the screen. I deleted that so I could get closer to the edge. I did add 10dp of padding at the bottom.

Do not nest weights.

Ground Rules

You may use class notes, personal notes, and any other resources you wish including web sources such as Wikipedia or online documentation, provided you in no way communicate with any other person (other than the TA for questions related to the exam). You may not accept assistance from another person at any time.

You are not allowed to use any static variables other than constants. There should be no warnings in your solution.

Submission Instructions

In Android Studio, select the Terminal Window. View>Tool Windows>Terminal.

Type: gradlew clean. It may take a long time the first time you do this. Then choose File>Close Project.

Zip the contents of the folder containing your android project. That is the folder that contains all of the files.

In Windows, go into the folder. The folder should have subfolders in it named .gradle, .idea, app, and gradle. Highlight all of the files and folders and right click. Choose Send To>Compressed ZIP folder. Submit the .zip file. There are many other ZIP utilities available as well.

Notice: In Windows, DO NOT right click on the project folder and choose Send To>Compressed ZIP Folder. If you do so, the .idea folder will not be included in the .zip file. I do not know why, but it is true. That file will then not be loadable later on.

Submit your .zip file using this button to upload the file to the server.

After you submit, use the link to download your submission. Extract it to another location, and open it in Android Studio. Be sure you do this to ensure your submission is valid. If your submission is invalid, the TA will contact you about fixing it and will deduct points for being a pain.

On a Macintosh computer you may need to type

./gradlew clean

Submit: no file selected

Submissions

No submissions, yet.

All solution submissions can be accessed on the assignment grading page, which is available here and from the home page grading link.

Assignment Grading Page

Other Notes

Hint: output the chosen name via Log.i so you will know what it is when testing.

When you switch to the new activity, call finish() on the current activity so you do not stack up activities:

public void onNewGame(View view) {
Intent intent = new Intent(this, MainActivity.class); startActivity(intent);
finish();
}

You can change an image in an ImageView this way:

ImageView image = (ImageView)findViewById(R.id.imageView); image.setImageResource(R.drawable.girl);

Be sure you test rotation and using the buttons in either orientation. Be sure the number of guesses is right after rotation.