# Tag Archives: methods

## My own Coding Problem

I’ve been considering a new classroom exercise to describe genetics and the inheritance of alleles within a population. The idea is that every student draws two coins randomly to start – these represent two alleles for a single gene in that organism. Then each student has the opportunity to ‘breed’ with another student to make two offspring. The genetics of the offspring will be determined randomly from that of the parents (something like each parent flips their coins until they get one heads and one tails – the heads from each parent goes to the offspring).

We’ll do this for several generations and track the frequency of alleles through time.

OK – now to coding:

I thought this sounded like a simple project that could be simulated easily, so that’s a coding challenge for myself. To start with, I thought I would build the program up bit by bit doing unit testing along the way. At this point I am trying to just set up the parental generation. To do this, I:

1. get input from the user: how many people are playing?

how many different alleles for the gene exist?

2. loop through making ‘User’ objects for each person playing. Each User object has an id number for tracking (idNum) and two alleles determined randomly(alleles[0,1]). I was thinking of keeping my list of users as an array called players[0…10max]

I am getting and using my userData with no problem. I think I am even setting up my objects alright and assigning them two randomly determined elements. But instead of those elements being coins (representing alleles), I’m getting the address of a char within the first denomination….

Any help at all is greatly appreciated, but remember – I’m doing this to learn, so I need to be able to understand the solution, not just get it solved.

Here’s the code with some debugging output statements:

//

//  main.cpp

//  CoinsLabSimulation

//

//  Created by Jack on 6/27/13.

//

//A simulation game to test how my coins lab idea will work

#include <iostream>

#include <cmath>

#include <time.h>

using namespace::std;

class User{

private:

int idNum;

string alleles[2];

public:

User(int A){idNum=A;};//int myArray[] = new int[idNum];

char getAlleles();

void setAlleles(string, int[]);

};

void getInput(int userInput[]);

void randomAssignment(int userInput[], string denominations[], int players[]);

int main()

{

srand(time(NULL));

int userInput[2];

char p[10] = {“pennies”};   //I hate character arrays / strings in C++

char n[10] = {“nickels”};   //I want an array of the coins we will use as alleles in this game

char d[10] = {“dimes”};

char q[10] = {“quarters”};

char s[15] = {“SusanBAnthonys”};

string denominations[5] = {p,n,d,q,s}; //this needs to be fixed so I can call on this array for coin types

int players[10];

getInput(userInput);

cout<<“We will be playing with “<<userInput[0]<<” players and “<<userInput[1]<<” alleles”<<endl;

cout<<“Alleles used will be:”<<endl;

for (int i =0; i<userInput[1]; i++) {

cout<<denominations[i]<<endl;

}

randomAssignment(userInput, denominations, players);

return 0;

} //end main

//*****************non-member functions

void getInput(int usersChoices[]){  //fx to retreive user settings –unit test passed

cout<<“How many players will be participating(10 max)?”<<endl;

cin>>usersChoices[0];

cout<<“How many denominations would you like to use?”<<endl;

cin>>usersChoices[1];

}// end getInput

void randomAssignment(int userNumbers[], string allelesUsed[], int player[]){ //will create user objects and assign each alleles

//————-   unit test:

cout<<endl<<endl<<“Unit Test”<<endl;

cout<<allelesUsed[0]<<”    “<<allelesUsed[3]<<endl<<endl;

//————-   pass

User *a[10];

for (int i=0; i<userNumbers[0]; i++) {

a[i] = new User(i);

a[i]->setAlleles(*allelesUsed,userNumbers);  ///trace this

}

}// end randomAssignment

//******************User Member Methods

void User::setAlleles(string allelesUsed,int userNum[]){

int max = userNum[1];

int randomNumber = floor(1 + (rand() % max));

cout<<“Random number: “<<randomNumber<<endl;

string allele1 = &allelesUsed[randomNumber];

randomNumber = floor(1 + (rand() % max));

cout<<“Random number: “<<randomNumber<<endl;

string allele2 = &allelesUsed[randomNumber];

alleles[0] = allele1;

alleles[1] = allele2;

//———–unit test2

cout<<“UNIT TEST2::::Alleles for this player are: “<<allele1<<” and “<<allele2<<endl;

//———–fail – it’s using the random number as an address within ‘pennies’

cout<<“Alleles for this player are: “<<alleles[0]<<” and “<<alleles[1]<<endl;

}

char User::getAlleles(){}

————————————————————-

my output looks like this:

How many players will be participating(10 max)?

2

How many denominations would you like to use?

4

We will be playing with 2 players and 4 alleles

Alleles used will be:

pennies

nickels

dimes

quarters

Unit Test

pennies    quarters

Random number: 4

Random number: 2

UNIT TEST2::::Alleles for this player are: ies and nnies

Alleles for this player are: ies and nnies

Random number: 1

Random number: 2

UNIT TEST2::::Alleles for this player are: ennies and nnies

Alleles for this player are: ennies and nnies

1 Comment

Posted by on June 29, 2013 in Uncategorized

I’m stuck. In working on a section in codecademy’s JavaScript class relating to methods and objects that inherit these methods, I’ve become a bit confused. The problem that I’m on involves an original Object that is a Car that contains the method ‘accelerate.’ – I’m OK with this:

Objects are things within a class, i.e. they are all contain similar property. In this example our Objects are Cars. There can be many different kinds of cars, each with different property such as (different colors, makes, speed, etc). But they are all in the same ‘class’ (i.e. same kind of thing: cars)

Methods are Functions that manipulate Objects. In this example, if we have made a Car Object that has the property Blue and Ford and 10 mph, we can now do something with this object using a method. The codecademy project has us write an ‘accelerate’ Method that adds 10 to the speed. OK – so every time we send our car object to that method, it looks for the speed property and increases it by 10.

Inheritance is a way of making subsets of an Object so that they automatically get the properties that the ‘parent’ Object has, but can also be given new properties that the ‘parent’ Object does not have. In the present codecademy example, we are making an ElectricCar Object that inherits from the Car Object. No problem, this is a good example because we can easily see that it is still a kind of car and that means it makes sense that it should have all the same properties as other cars (color, make, speed – perhaps Blue, Chevy Volt, 20Mph) But we also want to give it properties that other cars do not have, like ‘electricity’ (meaning battery charge).

OK, here’s what I have with some notes to follow along an to indicate where I am having problems. Help is greatly appreciated.

function Car( listedPrice ) {
var price = listedPrice;
this.speed = 0;
this.numWheels = 4;

this.getPrice = function() {
return price;
};
}

Car.prototype.accelerate = function() {
this.speed += 10;
};

function ElectricCar( listedPrice ) {
var price = listedPrice;
this.electricity = 100;
}
ElectricCar.prototype = new Car();

// Write the accelerate method for ElectricCar here
myElectricCar.accelerate = function() {
this.speed += 20;
};

// Write the decelerate method for ElectricCar here
myElectricCar.decelerate = function(secondsStepped) {
this.speed -= (5*secondsStepped);
};

myElectricCar = new ElectricCar(500);

myElectricCar.accelerate();
console.log(“myElectricCar has speed ” + myElectricCar.speed);
myElectricCar.decelerate(3);
console.log(“myElectricCar has speed ” + myElectricCar.speed);

Posted by on August 7, 2012 in Uncategorized

## Am I obsessed with codecademy?

Yes, I think I am obsessed. This has been a lot of fun and I am really learning something every day. Yesterday I hit the first ‘wall’ – like mile 20 in a marathon, I thought it was over for me. I had gone as far as I could and could go no farther.

The wall was Objects and Methods.

Yesterday I even wrote a plaintiff note in the Q&A section bemoaning the fact that this section was just too hard to get into my head. There were a number of other people posting the exact same problems and I thought this was the end of the road. But today, I pushed through almost by luck writing the correct code and then got to the review section to find that it explains the material much better than the first section itself.

hallelujah!