Needing some help with array getter and setter methods!!
Hello, hello. So I am having some challenges with trying to set an array up with setting values at a position of the array. My array size is 10. *see code below* but I have some getter and setter value methods that I cant seem to get right. Any help would be much appreciated. Below I have the methods I have been trying to set right. The parameters have to be as are. Thanks! :)
// class, private instance variables and default constructor
public class StatsArray
{
private int size; //how big is the array
private int[] stats; // an array of integers
StatsArray()
{
this.size = 10;
this.stats = new int[size] ; //instantiate the array called stats
}
// random generator to fill the array
public void fillArray()
{
Random random = new Random();
for(int index = 0; index < stats.length; index++)
stats[index] = random.nextInt(101);
}
// These are the methods I am trying to work with and need help on: I'm not too sure on how to set the value to the position in the int array.
public void setValue(int position, int value)
{
for(int index = 0; index < stats.length; index++)
position = this.stats[index] + value;
}
public int getValue(int position)
{
return this.stats[position];
}
Re: Needing some help with array getter and setter methods!!
Quote:
Originally Posted by
BlackShadow
Hello, hello. So I am having some challenges with trying to set an array up with setting values at a position of the array. My array size is 10. *see code below* but I have some getter and setter value methods that I cant seem to get right. Any help would be much appreciated. Below I have the methods I have been trying to set right. The parameters have to be as are. Thanks! :)
Code java:
// These are the methods I am trying to work with and need help on: I'm not too sure on how to set the value to the position in the int array.
public void setValue(int position, int value)
{
for(int index = 0; index < stats.length; index++)
position = this.stats[index] + value;
}
public int getValue(int position)
{
return this.stats[position];
}
Hello BlackShadow!
If i understood well, you only want to set and get a value in a specified position of your stats array. In your setValue(int position, int value) method you don't need a loop. You can just use position as the index of stats and then assign it to value. I think the getValue(int position) method is ok.
Hope it helps.
Re: Needing some help with array getter and setter methods!!
Okay thanks, but now I have a problem with my Stats Array Tester class in trying to get my methods to work right. I keep getting a zero for my output in whatever I input as my supposed position and value for my setter and then when I ask it to get it at a particular position it just spits back 0.... :/
Here is that code:
public class StatsArrayTester
{
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
StatsArray stats = new StatsArray(); //stats object
System.out.println("Welcome to our StatsArray\n");
stats.setValue(3, 52);
System.out.println("test" + stats.getValue(3));
//output always is: test0.
//My change to my other class:
public void setValue(int position, int value)
{
value = this.stats[position];
}
public int getValue(int position)
{
return this.stats[position];
}
Re: Needing some help with array getter and setter methods!!
Quote:
Originally Posted by
BlackShadow
Okay thanks, but now I have a problem with my Stats Array Tester class in trying to get my methods to work right. I keep getting a zero for my output in whatever I input as my supposed position and value for my setter and then when I ask it to get it at a particular position it just spits back 0.... :/
Code java:
public void setValue(int position, int value)
{
value = this.stats[position];
}
The above code assigns stats[position] to value. You need the opposite, assign value to stats[position]
Re: Needing some help with array getter and setter methods!!
You mean like this??
this.stats[position] = value;
I'm still only getting zero.... :/
Re: Needing some help with array getter and setter methods!!
Quote:
Originally Posted by
BlackShadow
You mean like this??
this.stats[position] = value;
I'm still only getting zero.... :/
I tried it and it works properly.
Can you post the exact code of both classes that gives you 0.
Re: Needing some help with array getter and setter methods!!
Here is code 1 without the main:
import java.awt.*;
import java.util.Random; //for our random number generator
public class StatsArray
{
private int size; //how big is the array
private int[] stats; // an array of integers
StatsArray()
{
this.size = 10;
this.stats = new int[size] ; //instantiate the array called stats
}
public void display(Graphics g)
{
int x = 50; //coordinates for displaying
int y = 40;
for(int i = 0; i < stats.length; i++)
{
g.drawString("Stats [" + i + "] = "+ stats[i], x, (y + 15 * i));
}
}
public void fillArray()
{
Random random = new Random();
for(int index = 0; index < stats.length; index++)
stats[index] = random.nextInt(101);
}
public void setValue(int position, int value)
{
this.stats[position] = value;
}
public int getValue(int position)
{
return this.stats[position];
}
// Here is the second class I have that I am trying to execute the methods get and set to test out the above methods.
import java.util.Scanner;
import java.util.Random;
public class StatsArrayTester
{
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
StatsArray stats = new StatsArray(); //stats object
System.out.println("Welcome to our StatsArray\n");
StatsArray storage = new StatsArray();
stats.setValue(5, 34);
System.out.println(" GIVE ME SOME VALUE!!! " + stats.getValue(6));
Re: Needing some help with array getter and setter methods!!
So it looks like it works so long as I change the parameter positions in my set and getValue methods.
Re: Needing some help with array getter and setter methods!!
Quote:
Originally Posted by
BlackShadow
Here is code 1 without the main:
Code java:
public class StatsArrayTester
{
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
StatsArray stats = new StatsArray(); //stats object
System.out.println("Welcome to our StatsArray\n");
StatsArray storage = new StatsArray();
stats.setValue(5, 34);
System.out.println(" GIVE ME SOME VALUE!!! " + stats.getValue(6));
Code java:
stats.setValue(5, 34);
System.out.println(" GIVE ME SOME VALUE!!! " + stats.getValue(6));
You set the value 34 in stats[5] but then you print stats[6] which is 0. You should print the same stats index that you set.
And please wrap your code with highlight tags.