CS 185C/286 - Homework 1

Your task is to write an Android application that displays a quiz question, like this:

The question and four answer options should be read in from here. Of course, the question will change when your problem is graded. You will need to read the file.

You check in a project that solves the assignment together with a report in a file report.txt. Use plain text, not Word or RTF, or you won't get credit for the report. Make sure that report.txt and report.png are in the workspace/hw01 directory.

In the report, answer the questions given in the step-by-step instructions below.

  1. Make a new project with application and project name hw01 in the package edu.sjsu.cs185c.hw01.
  2. Using the graphical layout builder, set up the question text and four answers. Make your own question, not the one in question.txt. Make sure each element has an ID. Add a screen capture of the interface builder when you are done in report.png.
  3. When I did this, I got XML with messy alignment. I wanted the buttons below each other, and each option to the right of its button, aligned on the baseline. I had to fix up a couple of them, and it's pretty likely you will need to do the same. Explain the changes that you made.
  4. Why did you get warning triangles next to each text view? (You don't need to fix them now.)
  5. Run your application. When you are done, commit and push your git repo. Paste the terminal contents from the push into your report. (This way, I'll be able to see your intermediate stages.)
  6. Now let's set the strings programmatically, not with the UI builder. Add code to set each textfield so that a different question and different options are displayed. Make sure it works and push your git repo. Paste the terminal contents from the push into your report. (Always do that when I ask you to push something. That way, I get the ID of the snapshot.)
  7. Enough hardcoding. Here is the code for reading the contents of a web page into a list, a line at a time:
                    ArrayList<String> result = new ArrayList<String>();                
                    try {
                        URL url = new URL(urlString);
                        Scanner in = new Scanner(url.openStream());
                        while (in.hasNextLine()) 
                    } catch (IOException e) {
                        Log.e(getClass().toString(), e.getMessage());
    Place this code in your onCreate method, and then set each TextView to results.get(0), results.get(1), and so on. What happens when you run your program?
  8. Look in the LogCat output. What error message do you see that is related to the problem?
  9. Do you see any output that could have come from the Log.e call? How do you know for sure?
  10. Put breakpoints in the three red lines above. Run the debugger. The first breakpoint will hit. Then click on Continue in the debugger. Which of the breakpoints get hit next?
  11. What do you conclude from these observations?
  12. Use an AsyncTask to solve this problem. In your report, explain how you figured out how to do that. What books or web sites did you consult? What mistakes did you make?
  13. Now clean up your program. Remove hardwired strings in the UI resource and in the Java code (except for the URL which can stay hardwired for now).
  14. When you are all done, commit and push your git repo.

Make sure you push multiple stages. You will lose points if you don't do it, and I would also be very suspicious of any answer that fully develops without intermediate work. Also, be sure to to push any junk (classfiles, generated files, metadata). The .gitignore from the lab should take care of that.