Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
This MOOC teaches you how to program core features and classes from the Java programming language that are used in Android, which is the dominant platform for developing and deploying mobile device apps.
In particular, this MOOC covers key Java programming language features that control the flow of execution through an app (such as Java’s various looping constructs and conditional statements), enable access to structured data (such as Java’s built-in arrays and common classes in the Java Collections Framework, such as ArrayList and HashMap), group related operations and data into classes and interfaces (such as Java’s primitive and user-defined types, fields, methods, generic parameters, and exceptions), customize the behavior of existing classes via inheritance and polymorphism (such as subclassing and overriding virtual methods).
In this assignment, you are to write a handful of methods that perform common geometry calculations such as calculating the area of a circle, or the perimeter of a right triangle, or the volume of a box.
After completing this assignment, you will have experience with:
1. Writing methods that accept parameters and return a value.
2. Creating local variables to hold values.
3. Writing expressions that use various operators and Math functions.
4. Using assignment statements to give new values to variables.
Please download the supplied zip file available with this specification. Extract the files onto your computer. The extracted files contain an Android Studio project. Start Android Studio and open the project (rather than import the project as demonstrated in an earlier module). All your work for this assignment will be in the file Logic.java. Open this file in the IDE and look for the comment:
// TODO — add your code here
As you do your work, be sure to place all your code in the Logic class.
In this assignment, you are to write a program that will print an ASCII art drawing of a diamond in a picture frame that will be displayed on the screen of your Android device in a text box. You should exactly reproduce the format of the output samples in the attached PDF file below. A major part of this assignment is showing that you understand for loops and if statements.
After completing this assignment, you will have experience with:
package mooc.vandy.java4android.diamonds.logic;
import android.util.Log;
import mooc.vandy.java4android.diamonds.ui.OutputInterface;
/**
* This is where the logic of this App is centralized for this assignment.
* <p>
* The assignments are designed this way to simplify your early
* Android interactions. Designing the assignments this way allows
* you to first learn key 'Java' features without having to beforehand
* learn the complexities of Android.
*/
public class Logic
implements LogicInterface {
/**
* This is a String to be used in Logging (if/when you decide you
* need it for debugging).
*/
public static final String TAG = Logic.class.getName();
/**
* This is the variable that stores our OutputInterface instance.
* <p>
* This is how we will interact with the User Interface [MainActivity.java].
* <p>
* It is called 'out' because it is where we 'out-put' our
* results. (It is also the 'in-put' from where we get values
* from, but it only needs 1 name, and 'out' is good enough).
*/
private OutputInterface mOut;
/**
* This is the constructor of this class.
* <p>
* It assigns the passed in [MainActivity] instance (which
* implements [OutputInterface]) to 'out'.
*/
public Logic(OutputInterface out){
mOut = out;
}
/**
* This is the method that will (eventually) get called when the
* on-screen button labeled 'Process...' is pressed.
*/
public void process(int size) {
int height = size * 2 + 1;
int width = size * 2 + 2;
int accumulator = -(size+1);
for(int i=1;i<=height;i++){
accumulator++;
for (int j=1;j<=width;j++) {
if((i == 1 || i == height) && (j == 1 || j == width))
mOut.print("+");
else if((i == 1 || i == height) && !(j == 1 || j == width))
mOut.print("-");
else if(!(i == 1 || i == height) && (j == 1 || j == width))
mOut.print("|");
else {
drawDiamond(size, i, j, accumulator);
}
}
mOut.print("\n");
}
}
public void drawDiamond(int size, int i, int j, int accumulator){
int diamondRowThickness;
if (accumulator <= 0){
diamondRowThickness = i*2-2;
} else {
diamondRowThickness = (i-accumulator*2)*2-2;
}
int diamondMidpoint = size + 1;
int diamondBoundsLeft = diamondMidpoint - (diamondRowThickness/2-1);
int diamondBoundsRight = diamondMidpoint + (diamondRowThickness/2);
int frameTop = 1;
int frameBottom = size * 2 + 1;
if (j >= diamondBoundsLeft && j <= diamondBoundsRight) {
if (j == diamondBoundsLeft || j == diamondBoundsRight) {
if (i < diamondMidpoint && i > frameTop) {
if (j == diamondBoundsLeft) {
mOut.print("/");
} else {
mOut.print("\\");
}
} else if (i == diamondMidpoint) {
if (j == diamondBoundsLeft) {
mOut.print("<");
} else {
mOut.print(">");
}
} else if (i > diamondMidpoint && i < frameBottom) {
if (j == diamondBoundsLeft) {
mOut.print("\\");
} else {
mOut.print("/");
}
}
} else {
if (i % 2 == 0) {
mOut.print("=");
} else {
mOut.print("-");
}
}
} else {
mOut.print(" ");
}
}
}
In probability theory, the birthday problem concerns the probability that, in a set of n randomly chosen people, some pair of them will have the same birthday. By the pigeonhole principle, the probability reaches 100% when the number of people reaches 366 (since there are 365 possible birthdays, excluding February 29th).
It would seem that we would need 183 people (half of 365) to reach a 50% probability. However, 99% probability is reached with just 57 people and 50% probability with just 23 people. These conclusions are based on the assumption that each day of the year (except February 29) is equally probable for a birthday.
After completing this assignment, you will have experience with:
Please download the supplied zip file available with this specification. Extract the files onto your computer. The extracted files contain an Android Studio project. Start Android Studio and open the project. All your work for this assignment will be in the file Logic.java. Open this file in the IDE and look for the comment:
// TODO — add your code here
As you do your work, be sure to place all your code in the Logic class.
This comment is located inside the calculate() method. The calculate() method takes in two parameters: the size of the group of people, and the count of the number of simulations to run. The method returns a value of type double that represents the percent of the simulations that had two people with the same birthday. If/when you add helper methods, be sure to place them in the Logic class after the calculate() method.
package mooc.vandy.java4android.birthdayprob.logic;
import java.util.Random;
import mooc.vandy.java4android.birthdayprob.ui.OutputInterface;
/**
* This is where the logic of this App is centralized for this assignment.
* <p>
* The assignments are designed this way to simplify your early Android interactions.
* Designing the assignments this way allows you to first learn key 'Java' features without
* having to beforehand learn the complexities of Android.
*
*/
public class Logic
implements LogicInterface {
/**
* This is a String to be used in Logging (if/when you decide you
* need it for debugging).
*/
public static final String TAG =
Logic.class.getName();
/**
* This is the variable that stores our OutputInterface instance.
* <p>
* This is how we will interact with the User Interface
* [MainActivity.java].
* <p>
* It is called 'mOut' because it is where we 'out-put' our
* results. (It is also the 'in-put' from where we get values
* from, but it only needs 1 name, and 'mOut' is good enough).
*/
OutputInterface mOut;
/**
* This is the constructor of this class.
* <p>
* It assigns the passed in [MainActivity] instance
* (which implements [OutputInterface]) to 'out'
*/
public Logic(OutputInterface out){
mOut = out;
}
/**
* This is the method that will (eventually) get called when the
* on-screen button labelled 'Process...' is pressed.
*/
public void process() {
int groupSize = mOut.getSize();
int simulationCount = mOut.getCount();
if (groupSize < 2 || groupSize > 365) {
mOut.makeAlertToast("Group Size must be in the range 2-365.");
return;
}
if (simulationCount <= 0) {
mOut.makeAlertToast("Simulation Count must be positive.");
return;
}
double percent = calculate(groupSize, simulationCount);
// report results
mOut.println("For a group of " + groupSize + " people, the percentage");
mOut.println("of times that two people share the same birthday is");
mOut.println(String.format("%.2f%% of the time.", percent));
}
/**
* This is the method that actually does the calculations.
* <p>
* We provide you this method that way we can test it with unit testing.
*/
public double calculate(int size, int count) {
// TODO -- add your code here
int dupcount=0;
Random r = new Random();
for(int i=0;i<count;i++)
{
int arr[]=new int[365];
r.setSeed(i+1);
for(int j=0;j<size;j++)
{
int n=r.nextInt(365);
arr[n]++;
if(arr[n]>=2) {
dupcount++;
break;
}
}
}
return dupcount*100.0/count;
}
// TODO - add your code here
}
In this assignment, you will create a class file, Gate.java. You will use this file in a follow-on assignment, creating two client files that use the class. Submitting this file independently (before working on the client files) will enable you to be sure that you have built the class correctly. This will make the task of creating the client files in the next assignment go much more smoothly. See the attached specification file for more details on this assignment.
package mooc.vandy.java4android.gate.logic;
/**
* This file defines the Gate class.
*/
public class Gate {
public static final int IN = 1;
public static final int OUT = -1;
public static final int CLOSED = 0;
private int mSwing;
public Gate(){
mSwing = CLOSED;
}
public boolean setSwing(int direction) {
if(direction == IN || direction == OUT || direction == CLOSED) {
mSwing = direction;
return true;
}
return false;
}
public boolean open(int direction) {
if (direction == IN || direction == OUT) {
this.setSwing(direction);
return true;
}
return false;
}
public void close() {
mSwing = 0;
}
public int getSwingDirection() {
return mSwing;
}
public int thru(int count) {
if(mSwing == IN)
return count;
else if(mSwing == OUT)
return - count;
else
return 0;
}
@Override
public String toString() {
if(mSwing ==0)
return "This gate is closed";
else if(mSwing == IN)
return "This gate is open and swings to enter the pen only";
else if (mSwing == OUT)
return "This gate is open and swings to exit the pen only";
else
return "This gate has an invalid swing direction";
}
}
In this assignment, you will see for yourself how a well-designed object can be used in two different client files.
See the specification file for more details on this assignment.
For this exercise, you will be creating a hierarchy consisting of several class files. An abbreviated diagram is included in the specification to give you an idea of what you will produce. You will also create one client file that uses the classes you have created.
See the specification file for more details on this assignment.
Rubric:
Here is a PDF of the rubric being used for this assignment. It is the same material in the platform’s peer-assessment. However, we find that it is easier to read & understand in a more traditional ‘rubric’ format.
Overview of Peer Assessment
Peer grading is mandatory for this assignment. Everyone enrolled in the course must review at least five (5) other submissions to ensure everyone receives a grade; however, many learners complete more to help their peers who are still waiting. Any learner who does not complete the 5 reviews will receive a 20% reduction in their score. Therefore, please take peer assessment seriously: it is an important part of the course.
Making sure that class file works correctly (i.e. creates objects as defined in the specification), is only one part of writing “good” class files. Writing code that is readable, easy to reuse, and adheres to principles such as “don’t repeat yourself” is equally as important. By using peer assessment to evaluate correctness and style, we will, therefore, accomplish several goals:
When you give text feedback, please do the following:
Please also keep the following in mind during peer assessments:
Steps for Peer Assessors
Review the submitter’s source code. Since you did your own project, you know where the challenges should be and you have a good idea of the kind of control flow statements that should be in the submitter’s code. Remember that students taking this class have many different backgrounds and may have more or less prior experience than you, and thus they may have done things differently than you did. That’s fine. As you assess a submission and you have doubt about a particular issue you should error on the side of giving too many points, rather than giving too few.
Since the file you are downloading contains a program you did not write, follow general security precautions: Do not run the code without checking that it does no harmful things. For completing peer assessment, you probably do not need to run the code at all since you are only evaluating how the code is written.
The Coursera platform will give you access to the submissions you are to assess. For each submission please open the *.java files in the app/src/main/java/mooc/vandy/java4android/calculator/logic directory in a text editor and look for the items discussed in the rubric below. Please see https://github.com/douglascraigschmidt/Android-App-Development/wiki/FAQ#65 for information on when you’ll receive feedback on your submissions from peer reviewers.
I hope this Java for Android All Programming Assignment Solution would be useful for you to learn something new from this Course. If it helped you then don’t forget to bookmark our site for more peer graded solutions.
This course is intended for audiences of all experiences who are interested in learning about new skills in a business context; there are no prerequisite courses.
Keep Learning!
More Peer-graded Assignment Solutions >>
Boosting Productivity through the Tech Stack Peer-graded Assignment Solutions
Essential Design Principles for Tableau Peer-graded Assignment Solutions
Creating Dashboards and Storytelling with Tableau Peer-graded Assignment Solutions
Intro to AR/VR/MR/XR: Technologies, Applications & Issues Graded Assignments
Conversational Selling Playbook for SDRs Peer-graded Assignment Solution