Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Needing some help with array getter and setter methods!!

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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];
    }


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Needing some help with array getter and setter methods!!

    Quote Originally Posted by BlackShadow View Post
    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!
    // 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.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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];
    }

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Needing some help with array getter and setter methods!!

    Quote Originally Posted by BlackShadow View Post
    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.... :/
    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]

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Needing some help with array getter and setter methods!!

    You mean like this??

    this.stats[position] = value;

    I'm still only getting zero.... :/

  6. #6
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Needing some help with array getter and setter methods!!

    Quote Originally Posted by BlackShadow View Post
    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.

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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));

  8. #8
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  9. #9
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Needing some help with array getter and setter methods!!

    Quote Originally Posted by BlackShadow View Post
    Here is code 1 without the main:
    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));
    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.

Similar Threads

  1. New student needing help!
    By newstudent in forum Object Oriented Programming
    Replies: 2
    Last Post: January 30th, 2012, 12:49 AM
  2. [SOLVED] Project help...confusion with changing a getter/setter for a driver class
    By coolidge in forum Java Theory & Questions
    Replies: 1
    Last Post: September 30th, 2011, 11:08 PM
  3. What's wrong with my date getter?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 8th, 2010, 08:39 PM
  4. [SOLVED] [Method] needing some help
    By Perplexing in forum Object Oriented Programming
    Replies: 4
    Last Post: December 2nd, 2010, 05:03 PM
  5. Grade averaging program with array and multiple methods.
    By jeremykatz in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 9th, 2009, 09:44 PM