Skip to content

Commit

Permalink
Week 2 completed
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlgo committed Mar 14, 2019
1 parent e400572 commit 655bea4
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
File renamed without changes.
99 changes: 99 additions & 0 deletions Week 2/Birthday Problem/Logic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
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
}
Binary file added Week 2/Quiz on Structured Data.pdf
Binary file not shown.

3 comments on commit 655bea4

@manisha069
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey in the end, for the most accurate result, typecast count to double. Currently your method is returning a double value but the vale after division is in int format since count is int.
thank you for the code though!!

@TheAlgo
Copy link
Owner Author

@TheAlgo TheAlgo commented on 655bea4 Sep 10, 2020 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manisha069
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please send a PR.

On Thu, 10 Sep 2020, 21:45 manisha069, @.***> wrote: hey in the end, for the most accurate result, typecast count to double. Currently your method is returning a double value but the vale after division is in int format since count is int. thank you for the code though!! — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub <655bea4#commitcomment-42198322>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGGWXE3FYFRBAW7EZIHHKF3SFD3RVANCNFSM4RFLNUEA .

hey, sent you a PR

Please sign in to comment.