Hi friends,

Currently doing an assignment where we have to calculate fractions using Classes (this is our first time using them, so the code might appear really basic).

Here's the basic assignment: Fraction Class The fraction class represents a simple fraction (e.g 2 / 4) consisting of a numerator (2) and a denominator (4).

`Data members: m_numerator(int) and m_denumerator (int) Constructor: The constructor has 2 arguments and initializes these data members. Methods: The display method has 4 arguments: float x, float y, float size, color fillColour. It draws the fraction as a portion of a rectangle and next to this as an arc. The size parameter (20px) indicates the size of one cell. Use a for-loop to display the rectangles. The getValue method returns the value of the fraction as a float. Main Application Now make 4 instances of the Fraction class giving each object different numerator and denominator values: Call the display method to display them below each other on the window as you can see in this screenshot. Also, show the sum of the fraction values. `

Here's my main tab:

`Fraction f1, f2, f3, f4; void setup() { size(500, 200); f1 = new Fraction(1, 3); f2 = new Fraction(1, 4); f3 = new Fraction(3, 4); f4 = new Fraction(4, 10); textSize(15); } void draw() { background(120); f1.display(20, 20, 20, color(255)); f2.display(20, 50, 20, color(255)); f3.display(20, 80, 20, color(255)); f4.display(20, 110, 20, color(255)); fill(255); text("Sum of all fractions: " + (f1.getValue() + f2.getValue() + f3.getValue() + f4.getValue()), 20, 160); } `

And here's my class Fraction:

`class Fraction { int m_numerator, m_denumerator; //Initializing data members. Fraction(int numerator, int denumerator) { m_numerator = numerator; m_denumerator = denumerator; } //Draw display containing the for-loops for te rectangle and the arc. void display(float posX, float posY, float size, color fillColour) { fillColour = color(245, 12, 12); rectMode(CENTER); //Draw the red inside for the numerator. for (int j=0; j<m_numerator; j++) { fill(fillColour); noStroke(); rect(posX+j*size, posY, size, size); } //Draw the white outline for the denumerator. for (int i=0; i<m_denumerator; i++) { noFill(); stroke(255); rect(posX+i*size, posY, size, size); } //Load the data type before the calculation. getValue(); //Draw an arc with the calculated value. fill(fillColour); stroke(255); arc(posX+m_denumerator*size+size*2, posY, size, size, -HALF_PI, getValue(), PIE); } //Get value from the fractions to calculate the sum. float getValue() { float value = m_numerator / m_denumerator; return value; } } `

Now for my problem: I can't seem to calculate getValue(). It doesn't matter where I put the calculation, or if I put it as a separate value, even if I hardcode the fraction it always gives me zero if I println() it. Can anyone point me in the right direction?

[link] [comments]