r/processing • • 2d ago

Homework hint request showSumOfArray(), showAverageOfArray(), and showCountBetween70and90()

Hey! I was given this assignment from an intro to problem solving class and I was just thrown into it without much help besides the first stuff in it, but that's really it and the assignment wasn't talked about in class. When I ask the professor for help and saying I don't understand she simply says to "play around with it". I don't need the answers, just a push in the right direction if able ^^. It's due Friday and the forums on processing aren't helping at all.

Here it is

// For this programming assignment, you will write the last 3 functions/subprograms.

// at the end of this file.

// Your code will use the array called "numbers" and the variable holding the

// count of the number of values in the array called "numberCount".

// The subprograms you will write are:

// 1. showSumOfArray() - this method adds the values in the array and then

// displays the sum.

// 2. showAverageOfArray() - this method calculates the average of the values

// in the array and then displays the average.

// 3. showCountBetween70and90() - this method counts the number of values in the

// array that are between 70 and 90 and then displays that count.

char c; // holds single character

int mx, my; // assigned mouseX and mouseY

String initialText = "(Enter Numbers)";

String str = ""; // holds string input

int strNumber; // holds numeric representation of string input

int[] numbers = new int[10];

String arrayString = ""; // For displaying the array

int numberCount = 0; // holds number of values in the array

String errorMSG = "";

String sum = ""; // Used to display the sum

String average = ""; // Used to display the average

String between70and90 = ""; // Used to display the count of values between 70 and 90

int tbx = 50, tby = 50, tbw = 200, tbh = 40; // textbox

int dx = 530, dy = 50, dw = 150, dh = 40; // display

int rx = 700, ry = 50, rw = 150, rh = 40; // reset

int b1x = 50, b1y = 140, b1w = 300, b1h = 40; // place in array button

int b2x = 50, b2y = 200, b2w = 300, b2h = 40; // show sum buton

int b3x = 50, b3y = 260, b3w = 300, b3h = 40; // show average button

int b4x = 50, b4y = 320, b4w = 300, b4h = 40; // show count between 70 and 90 button

void setup() {

size(900, 600);

textSize(24);

strokeWeight(3);

}

void draw() {

background(234, 247, 251);

fill(0, 96, 128);

text("Enter a number and click on \"Place In Array\" to put the number in the array",50,30);

// textbox

fill(250);

rect(tbx, tby, tbw, tbh);

fill(100);

text(initialText, tbx + 9, tby + 30);

// text in textbox to show THE number

fill(0);

text(str, tbx + 9, tby + 30);

fill(255,0,0);

text(errorMSG, tbx + tbw + 20, tby + 30); // test msg

fill(0);

// Display Array Button

fill(21, 162, 208);

rect(dx, dy, dw, dh);

fill(0);

text("DisplayArray", dx + 3, dy +30);

text(arrayString, dx + 40, dy + 80);

// Reset Button

fill(21, 162, 208);

rect(rx, ry, rw, rh);

fill(0);

text("Reset Array", rx + 15, ry +30);

// Place in Array Button

fill(21, 162, 208);

rect(b1x, b1y, b1w, b1h);

fill(0);

text("Place In Array", b1x + 2, b1y +30);

// Show Sum Button

fill(21, 162, 208);

rect(b2x, b2y, b2w, b2h);

fill(0);

text("Show Sum", b2x + 2, b2y + 30);

text(sum, b2x + b2w + 5, b2y + 30);

// Show Average Button

fill(21, 162, 208);

rect(b3x, b3y, b3w, b3h);

fill(0);

text("Show Average", b3x + 2, b3y +30);

text(average, b3x + b3w + 5, b3y + 30);

// Number Between 70 & 90 Button

fill(21, 162, 208);

rect(b4x, b4y, b4w, b4h);

fill(0);

text("Number Between 70 & 90", b4x + 2, b4y +30);

text(between70and90, b4x + b4w + 5, b4y + 30);

}

void keyPressed() {

c = key;

initialText = "";

if ((int)c != 8 && (int)c != 65535 && c >= '0' && c <= '9') {

str += c;

}

if ((int)c == 8) {

if (str.length() > 0) {

str = str.substring(0, str.length() - 1);

} else if (str.length() == 0) {

str = "";

}

}

}

void mousePressed() {

mx = mouseX;

my = mouseY;

if ( onDisplayArrayButton(mx, my) ) {

displayArray();

} else if (onResetButton(mx, my)) {

resetArray();

} else if (onPlaceInArrayButton(mx, my)) {

placeInArray();

} else if (onShowSumButton(mx, my)) {

showSumOfArray();

} else if (onShowAverageButton(mx, my)) {

showAverageOfArray();

} else if (onCountBetween70and90Button(mx, my)) {

showCountBetween70and90();

}

}

boolean onPlaceInArrayButton(int x, int y) {

if (x > b1x && x < (b1x + b1w) && y > b1y && y < (b1y + b1h)) {

return true;

} else {

return false;

}

}

boolean onDisplayArrayButton(int x, int y) {

if (x > dx && x < (dx + dw) && y > dy && y < (dy + dh)) {

return true;

} else {

return false;

}

}

boolean onShowSumButton(int x, int y) {

if ( x > b2x && x < (b2x + b2w) && y > b2y && y < (b2y + b2h) ) {

return true;

} else {

return false;

}

}

boolean onShowAverageButton(int x, int y) {

if (x > b3x && x < (b3x + b3w) && y > b3y && y < (b3y + b3h) ) {

return true;

} else {

return false;

}

}

boolean onCountBetween70and90Button(int x, int y) {

if (x > b4x && x < (b4x + b4w) && y > b4y && y < (b4y + b4h)) {

return true;

} else {

return false;

}

}

boolean onResetButton(int x, int y) {

if (x > rx && x < (rx + rw) && y > ry && y < (ry + rh)) {

return true;

} else {

return false;

}

}

void displayArray() {

int c = 0;

String s;

arrayString = "";

while (c < numberCount) {

s = Integer.toString(numbers[c]);

arrayString += s + "\n";

c = c + 1;

}

}

void resetArray() {

arrayString = "";

for (int i = 0; i < numberCount; i++) {

numbers[i] = 0;

}

errorMSG = "";

sum = "";

average = "";

between70and90 = "";

numberCount = 0;

}

void placeInArray() {

if (numberCount == 10) {

errorMSG = "ARRAY IS FULL";

str = "";

}

if (str.length() > 0 && numberCount < 10) {

strNumber = Integer.parseInt(str);

numbers[numberCount] = strNumber;

numberCount = numberCount + 1;

str = "";

}

}

(these three parts are what we have to edit)

void showSumOfArray() {

// Write the code that will summ the values

// in the numbers array.

// Then display the result by rewriting the line below.

sum = "This displays the sum";

}

void showAverageOfArray() {

// Write the code that will summ the values

// in the numbers array and calculate the average.

// Then display the result by rewriting the line below.

average = "This displays the average";

}

void showCountBetween70and90() {

// write the code that will keep a count of the

// number of values in the numbers array that are between 70

// and 90. Then display the count by rewriting

// the line below.

between70and90 = "This displays the count of values between 70 and 90";

}

This is what it looks like when I currently run it.

That's all I have, the professor expects us to know already how to program when coming into the class whenever it's a intro intro to programming class. There's tons of people who aren't becoming programmers who have to take the course like cybersecurity. For whoever is reading this far thank you so much! It means the world

0 Upvotes

9 comments sorted by

2

u/Raurou_Dersal 2d ago

I'll have to link an imgur picture of the code later tonight after work, it' a bit difficult to read through the copy and paste, I'm sorry!

3

u/zck 2d ago

It's a little difficult to help if we don't know where you're currently at. So, a few questions to see what you can already understand, and what you can't.

Can you make it so when the "Show Sum" button is clicked, the This displays the sum text is replaced with "hi there"?

Can you add up the numbers in the numbers array?

Can you check if a number is between 70 and 90?

0

u/DoorCareless8854 2d ago

Hi, classmate of op here that was suffering with them earlier. we can change the text. When the buttons are pressed it will only show the text attached to them, so we cannot add numbers or check if they are between 70 and 90. I took a screen recording of what the program looks like and does at the moment if that makes it any easier to visualize. Thanks a lot for helping us out!

1

u/zck 1d ago

Hi, classmate of op here that was suffering with them earlier. we can change the text. When the buttons are pressed it will only show the text attached to them, so we cannot add numbers or check if they are between 70 and 90.

Where is the text "fuck this" determined?

0

u/DoorCareless8854 1d ago

void showSumOfArray() {

// Write the code that will sum the values

// in the numbers array.

// Then display the result by rewriting the line below.

sum = "fuck this";

I'm mostly confused on how to get the Array things to work in the code, I'm not a computer science major so this is even less up my Ally than op's, sorry if I'm not answering your questions right.

1

u/zck 1d ago

Cool! Yeah, that's where it goes.

Now we just need to figure out how to add up the numbers in the array. If you were working on paper, how would you do that?

Do you know anything about how to use arrays? Here is one explanation of how it works. Read through it and see if that helps

1

u/rusticlypredactious 2d ago

The processing documentation is only going to help with processing specific functionality, this problem is asking for you to perform operations on Arrays in Java.

Use a for loop to iterate over the numbers array. These will be helpful for syntax and functions attached to Java Arrays: https://www.w3schools.com/java/java_for_loop.asp https://www.w3schools.com/java/java_arrays.asp

1

u/DoorCareless8854 2d ago

Tysm, this helped a lot.

1

u/TurtleGraphics64 1d ago

yeah, just to build on this, the main thing you need to know first for these is how to work with all of the elements in the array.

in high level, to get the sum of all the elements in the array you should start with a variable set to zero, then loop through all of the array elements, adding each one to that variable. When you finish looping through, this will total the sum of all the members of the array.