MP0: Location

Let’s start hacking! Our goal with MP0 is to provide a gentle but realistic introduction to Android development. On one hand, most of the assignment is writing small pieces of code of the kind you should be increasingly familiar with. On the other hand, MP0 also challenges you to hack on an larger and scarier chunk of code—the guts of a simple Android application. It also introduces you to the development workflow we’ll use throughout the rest of the semester.

MP0 is due Monday 2/4/2019 @ 5PM. To receive full credit, you must submit by this deadline. In addition, 10% of your grade on MP0 is for submitting code that earns at least 30 points by Friday 2/1/2019 @ 5PM. Late submissions will be subject to the MP late submission policy.

1. Learning Objectives

The core objectives of MP0 are both to keep you practicing writing simple functions and to get you started working with Android. We’ve brought both of those goals together in the form of location tracking app which is almost complete but needs a bit of help from you.

Specifically, MP0 continues training you to:

  1. initialize and use variables and arrays of appropriate types

  2. perform simple calculations using integer and floating point arithmetic

  3. use simple loops and conditional statements to process arrays containing data

  4. translate program descriptions into working code, and

  5. work with test suites as part of your development process.

2. Assignment Structure

Like all the MPs this semester MP0 is an Android app. It’s divided into two parts:

  • /lib/: a library that performs various computations on location data collected by the app.

  • /app/: an Android app for you to use for your own interactive testing. The Android app is almost complete, but needs a few modifications from you. And, to work correctly, you need to finish the library in lib.

Most of the points for MP0 are for completing the three short functions in Locator.java in the library. These problems are a bit of an upgrade from what you have been doing on the homework, but not a dramatic change.

In contrast, MainActivity.java in the app tosses you into the deep end of Android app development. This is not a complicated app, but it does contain a lot of new ideas and scary code. Don’t worry—you’ll understand all of it by the end of the semester. But the app development part of the MP is designed to force you to get started on the uncomfortable task of hacking on large pieces of unfamiliar software.

There are a few points for finishing the app, and that only requires a few lines of code 1. But you will spend a while poring over the app source code figuring out what on Earth is going on and what you need to do. Again, this is not uncommon. Modern software development involves working with large codebase with many contributors, meaning that it is rare to unheard of to understand every last bit of something you are working on. You may find our official MP0 online documentation helpful in understanding what each function and class is supposed to do.

2.1. MP0 Preparation

Before starting MP0 you should make sure that you have completed installing Android Studio, and have either an emulator or real device that you can run Android apps on. If you don’t please contact a course staff member immediately.

Next, work through our instructions on installing and using Git. We will provide time during lab this week to review how to use Git to submit MP0, but you are welcome to go through our instructions beforehand if you are raring to get going.

2.2. Obtaining MP0

First, sign up for a GitHub account if you haven’t already. Then use this GitHub Classroom invitation link to obtain your copy of MP0. Once your repository has been created, import it into Android Studio following our assignment Git workflow guide.

Note that the first time you clone MP0 with Android Studio configuration may appear to fail with an "IDE Error". This is OK. Just restart Android Studio at this point and you should be able to continue working. If that doesn’t work check the troubleshooting section below.

2.3. Your Goal

Your goal is to complete the helper functions so that the app works properly and then make one small modifications to the user interface to fix a non-functional button.

All of the core app code is in MainActivity.java in the app directory. That’s the scary stuff—although we have tried to comment it heavily to help you understand what’s going on.

But to work properly the app relies on three helper functions from Locator.java in the lib directory that process location data:

  1. farthestNorth: given an array of positions, find the one that’s the furthest north.

  2. beenHere: given a current position and an array of positions, determine whether the current position already exists in the array—meaning that we’ve been there before.

  3. nextRandomLocation: given a current position, randomly generate a new position to move to—or stay put. This is used by a wandering feature of the app designed to help you see how it works without trekking all over campus.

We have provided test suites for all of the helper functions. Our test suites contains both simple tests for each method for you to use during development and then a more rigorous set of tests.

Your goal is to correctly implement the missing methods so that all of the tests pass. Doing so will earn you almost full credit on the assignment—but see the section on style below.

3. How to Approach MP0

Although MP0 may seem daunting at first, do not get discouraged! Focus on identifying what you need to do and understanding the requirements of each function. There is really not a huge amount of code for you to write—our solution adds only several dozen lines, although yours may be slightly longer.

3.1. Understanding What You Need to Do

A core task when approaching any programming assignment or task is to identify what you need to do. To finish the MP0 library there are three—and only three—functions that you need to change. Begin by identifying those functions and understanding their requirements. This will go a long way to helping you complete the MP. Once you are done with the library then you can tackle the more daunting change to MainActivity.java.

3.2. Getting Started

Your initial clone of MP0 will receive 0 points. That’s intentional! But once you start working you’ll find that your score will go up quickly. You only have to finish one of the helper functions and correct the checkstyle error to get to 30 points by Friday.

Use Android Studio to help you here. It can intelligently identify problems with your MP and guide you toward solutions.

3.3. Test-driven Development

We have provided you with testing suites that you can use to perform iterative test-driven development. You should integrate this process into your normal CS 125 Git workflow. Here’s how that works:

  1. Start with one function that you need to write for MP0—say farthestNorth in Locator.java.

  2. Open Locator.java. Run the assignment test suite, which is installed as run configuration "Test Library". Without changes to Locator.java, it should fail.

  3. Begin modifying the farthestNorth function. When you think that you have a solution, re-run the test suite. Better yet, run just one part of the test suite—for example, the test that tests simple inputs to farthestNorth.

  4. If the test suite succeeds, you’re almost done—congratulations!

  5. But make sure to run the full grading test suite to ensure that you get full marks! There may be a case that the simple test suites are not trying that you are not handling correctly.

  6. If the test suite fails, try to diagnose the problem. Interactive testing can help diagnose these kinds of problems.

In general the fewer lines of code you write before running a test, the better. When you are starting out, it is easy to introduce bugs into your code. Bugs are easiest to catch one-by-one, and so the fewer lines of untested code the more likely you are to identify errors in your logic or implementation.

3.4. Getting Help

The course staff is ready and willing to help you every step of the way! Please come to office hours, or post on the forum when you need help. You should also feel free to help each other, as long as you do not violate the academic integrity requirements.

4. Android

Android is a Java-based framework for building smartphone apps that run on the Android platform. By learning how to build Android apps, your programs can have enormous impact. As of a year ago, Google estimated that there were 2 billion active Android devices. That’s over 25% of people on Earth—and several times more than iOS.

However, Android is also a huge and complex system. It’s easy to feel lost when you are getting started. Our best advice is to just slow down, take a deep breath, and try to understand a bit of what is going on at a time. We’ll try to walk you through a few of the salient bits for MP0 below. Google also maintains a great set of tutorials on beginning Android development.

Note that you will use Android for all MPs this semester and for your final project, so put in some time to familiarizing yourself with it now. It’s simply the best way to build exciting things in Android—programs that you can share with your friends and family.

4.1. Logging

Like any other computer program, an important part of developing on Android is generating debugging output. On Android, our familiar System.out.println doesn’t quite work the same way we’re used to.

However, Android has a simple yet powerful logging system. Unlike System.out.println, logging systems allow you to specify multiple log levels indicating the kind of output that you are generating. This allows you to distinguish between, for example, debugging output that might only be useful during development and a warning message that might indicate a more serious problem or failure. The Android logger also allows you to attach a String tag to each message to help separate them when you are debugging or developing. So the final syntax of the call to generate a debugging message, for example, is Log.d(TAG, message).

Do you need to know this to complete MP0? Probably, since you need to determine what you app is doing or how things are going wrong.

4.2. Activitys and Activity Lifecycle

The Android Activity class corresponds to a single screen that the user can interact with. Our simple app contains only one activity, but most apps consist of several: maybe an activity corresponding to the app’s main screen, another for a settings dialog, and still others for other parts of the app.

As you might expect, there are a few important moments for an activity: when it is created, when it appears on the screen, and when it leaves the screen. Android provides functions that you can override to handle both of these events: onCreate, onResume, and onPause. It is typical for on onCreate method to perform tasks required to make the activity ready for a user to use, such as configuring buttons and other UI elements.

For more information review Android’s official Activity information.

Do you need to know this to complete MP0? No. But you may be confused by the overall app structure if you don’t review it.

4.3. UI Events

Why does code in your app run? In many cases it’s because a user interacted with an activity—clicked a button, entered text into a dialog box, or adjusted an on-screen control. Android provides a way for each app to register handlers: functions that run when various user interface (UI) events take place.

Our app uses these to:

  • enable and disable location tracking

  • enable and disable randomly-generated fake locations, or wandering

  • center the map—although this doesn’t quite work yet

Do you need to know this to complete MP0? Yes! And it will be hard to understand how your app works without reviewing it.

5. Troubleshooting

When building apps with Android Studio any one of a number of things can go wrong. We’ll try to keep an updated list here of troubleshooting strategies.

5.1. General Advice

If your app won’t build, try the following steps:

  1. Restart Android Studio. Yes, this does in fact sometimes do the trick.

  2. Invalidate Caches / Restart. You’ll find this under the "File" menu. Again, sometimes it seems to help.

  3. Rebuild Project. You’ll find this under the "Build" menu. Sometimes rebuilding will help you pinpoint errors that are preventing your app from starting or the test suites from running.

5.2. MP0 Specific Advice

We’ll add things here as we go, but please check the forum first.

5.2.1. Fix build.gradle

The following fix corrects the following error message you may see when you run the test suites:

NoTest

It also causes Android Studio to correctly display errors when you run the test suites. So it is important to apply this change! Read the instructions below or watch the screencast above. If you need help, post on the forum or come to office hours.

To fix this error we’re going to make two small changes to your root build.gradle file. Here’s how to do that. First, switch the left pane to Project view. Next, expand the top (MP0-Spring2019-…​) folder, then double click build.gradle to open it. Note that there two other build.gradle files in the app and lib directory. You do not need to modify these files.

There are two changes you need to make to the file. First, find the part that looks like this at the top:

buildscript {
    repositories {
        google()
        jcenter()
        maven {
            url 'https://jitpack.io'
        }
    }
    dependencies {
        classpath 'org.eclipse.jgit:org.eclipse.jgit:5.2.1.201812262042-r'
        classpath 'com.github.cs125-illinois:gradlegrader:0.13.0'
        classpath 'com.android.tools.build:gradle:3.3.0'
    }
}

We want to change the version of our CS 125 grading plugin from 0.13.0 to 0.13.4. So change this line:

        classpath 'com.github.cs125-illinois:gradlegrader:0.13.0'

so that it looks like this:

        classpath 'com.github.cs125-illinois:gradlegrader:0.13.4'

Android Studio will ask you to sync the Gradle configuration, but don’t yet. Instead, make the next change first.

Next, look at the bottom of the file for the portion that looks like this:

tasks.grade.dependsOn('checkstyleMain')
defaultTasks 'grade'

Remove the first line. So afterward you should only have:

defaultTasks 'grade'

At this point you should use the yellow dialog to sync your Gradle configuration. Once the synchronization completes you’ll be good to go.

6. Grading

MP0 is worth 100 points total, broken down as follows:

  1. 20 points: farthestNorth

    • 10 points for passing the simple tests

    • 10 points for passing the randomized tests

  2. 20 points: beenHere

    • 10 points for passing the simple tests

    • 10 points for passing the randomized tests

  3. 20 points: nextRandomLocation

    • 10 points for passing the simple tests

    • 10 points for passing the randomized tests

  4. 20 points: testCenterButton: note that passing this test requires modifying MainActivity.java

  5. 10 points for no checkstyle violations

  6. 10 points for submitting code that earns at least 30 points before Friday 2/1/2019 @ 5PM.

6.1. Submitting Your Work

Follow the instructions from the submitting portion of the CS 125 workflow instructions. Note that the first time you do this you’ll want to pay careful attention to the common submission pitfalls, particularly if your submission doesn’t show up when you think it should.

6.2. Test Cases

You should carefully review the test cases in LocatorTest.java. The MP0 library testing suite tests your code using random inputs and pre-computed correct outputs. So, for example, when testing beenHere, we have pre-computed the correct answer for a small subset of test cases and use this to determine whether your solution works in all cases.

In contrast, testing the user-facing parts of Android apps is notoriously difficult. This semester we’ll be using Robolectric to write lightweight interface tests. For MP0 all we are testing is that you’ve hooked up the "Center" button properly, but in future MPs these tests will become more comprehensive.

Automated testing is a hugely important part of modern software development. Just like computers are good at running programs, they are also good at running programs to debug other programs. Independently developing a method and the function that tests it allows the two to support each other. The test may find errors in the method, and, the method may also identify errors in the test.

For all three library functions we provide you with both simple test cases using a small number of select inputs and exhaustive test cases that use many randomly-generated inputs. The former should be used during iterative development. For MP0, both are used during grading—but on future MPs there will be few to no points for the simpler test suites. The local autograder we provide will be able to help you estimate your score before you submit.

6.3. Autograding

We have provided you with an autograding script that you can use to estimate your current grade as often as you want. Your Android Studio project contains run configuration called "Grade" that will run the autograder for MP0. You can also run the grader by installing our plugin and then pressing the button that looks like the CS 125 shield.

Unless you have modified the test cases or autograder configuration files, the autograding output should approximate the score that you will earn when you submit. If you modify our test cases or the autograding configuration, all bets are off. You may also lose points if your solution runs too slowly and exceeds the testing timeouts.

6.4. Style Points

90 points on MP0 are for correctly implementing the required functions. The other 10 points are for style. Writing readable code according to a style guideline is extremely important, and we are going to help you get into this habit right from the start. Every software development company and most active open-source projects maintain style guidelines. Adhering to them will help others understand and integrate your contributions.

We have configured the checkstyle plugin to enforce a variant of the Sun Java coding style. Android Studio should naturally generate code that meets this standard. So you should not have to fight with IntelliJ too much to avoid checkstyle violations.

However, the checkstyle plugin does require you to add Javadoc comments, and also avoid the use of so-called magic numbers. You may find these requirements a bit annoying at first, but we trust that you will get used to them. Once you build good style habits, you won’t have to think about it anymore, and will just go on with your business writing beautiful code.

7. Cheating

Please review the CS 125 cheating policies.

All submitted MP source code will be checked by automated plagiarism detection software. Cheaters will receive stiff penalties—the hard-working students in the class that are willing to struggle for their grade demand it.


Created 7/14/2020
Updated 7/14/2020
Commit 2091ea3 // History // View
Built 7/14/2020 @ 18:28 EDT