Arrays

Assignment

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

41 Replies - 1848 Views - Last Post: 24 March 2010 - 01:42 PM Rate Topic: -----

#1 jaArch   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Arrays

Posted 23 March 2010 - 07:17 PM

The problem I'm having is that I can't seem to get the output to display the array from the method in the shifter class:

        Scanner input = new Scanner(System.in);
        
        System.out.printf("Array contents: %d\n", Shifter.data);
        System.out.printf("Shift how many positions? " );
       
        int data = input.nextInt();
       
        Shifter shiftpos = new Shifter(data);
        Shifter shiftsize = new Shifter(data);
      
        shiftpos.shift(data); 
        shiftsize.shift(data);

       
    }

}



public class Shifter {
    
    private int data[];
        
    public Shifter ( int size ){
    
        int shift[] = new int [ 15 ];
        for (int counter = 0; counter < shift.length; counter ++) {
        data = shift;  
  
        }
        
    }
    
    public void shift ( int pos ){
        
     

           
    }
    
    public void display () {
        
}



Not sure what I'm doing wrong here

Is This A Good Question/Topic? 0
  • +

Replies To: Arrays

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,879
  • Joined: 27-December 08

Re: Arrays

Posted 23 March 2010 - 07:21 PM

Well, you neither define nor invoke display() from the Shifter object, so nothing will output:
public void display () { 
         
}



Also, here, since you are telling the array data to reference the same Object as shift, you don't need a for loop for that. Just say data = shift; without using a for loop. Note that if you were copying elements over from shift to data one-by-one, you would use a for loop. However, you are not doing this, so you don't need the loop.
for (int counter = 0; counter < shift.length; counter ++) { 
        data = shift;   
} 


Was This Post Helpful? 0
  • +
  • -

#3 jaArch   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Arrays

Posted 23 March 2010 - 07:24 PM

How do I output the array without having to invoke the method?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,879
  • Joined: 27-December 08

Re: Arrays

Posted 23 March 2010 - 07:26 PM

Since the array is a private field, you cannot directly access it from another class. However, you can either define display() (no sense having a method that doesn't do anything) or you can add an accessor method for data and print its contents in your other class. Either way, essentially the same code.

Also, note that if you attempt to print the entire array (and not an individual element) you will get hash code as your output, as arrays do not override the Object toString() method.
Was This Post Helpful? 0
  • +
  • -

#5 jaArch   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Arrays

Posted 23 March 2010 - 07:30 PM

But the lab specifically states that I have to use those methods:

Write a java class to shift the contents of an array of integers. Design your class based on the UML diagram to the right as well as the following notes:


* The field, data, will contain the actual array values
* The constructor takes a parameter representing the size of the array to be created - you may hard code the data into the array using a loop
* The method, shift( ), takes a parameter that indicates how many positions to shift the data in the array.
* The method, display( ), merely displays the current contents of the array


The main program will display the array contents to the user and ask how many positions he would like to shift off the right side of the array, and replace onto the left side. This is called a right circular shift. If the user inputs a negative value, implement a left circular shift instead. Remember, you are not merely printing the values in the array differently, you are physically changing the order in which the values are stored in the array.

Stay in a loop, repeating the same process until the user enters something non-numeric as a value.

This algorithm can be used as the basis for implementing a "lazy susan" type of application, such as cycling forwards or backwards through songs in a song list.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,879
  • Joined: 27-December 08

Re: Arrays

Posted 23 March 2010 - 07:34 PM

View PostjaArch, on 23 March 2010 - 10:24 PM, said:

How do I output the array without having to invoke the method?



View PostjaArch, on 23 March 2010 - 10:30 PM, said:

But the lab specifically states that I have to use those methods:
* The method, display( ), merely displays the current contents of the array


You are contradicting yourself.
Was This Post Helpful? 0
  • +
  • -

#7 jaArch   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Arrays

Posted 23 March 2010 - 07:38 PM

I'm unsure of what to do - can you explain?
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,879
  • Joined: 27-December 08

Re: Arrays

Posted 23 March 2010 - 07:43 PM

What are you unsure about specifically? Invoking the method? Printing each element of an array? Something else?
Was This Post Helpful? 0
  • +
  • -

#9 jaArch   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Arrays

Posted 23 March 2010 - 07:46 PM

Both and this * The method, display( ), merely displays the current contents of the array
It's something I have no idea how to do
Programming is my one weakness, lol
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,879
  • Joined: 27-December 08

Re: Arrays

Posted 23 March 2010 - 07:50 PM

You seem to be able to invoke shift() alright shiftpos.shift(data);. The only difference between shift() and display() is that display() does not accept a parameter.

As for working with arrays, I find it hard to believe that you're working with classes and Objects, but you're unfamiliar with a simple iteration through an array. If this is the case, then you may want to check out TriggaMike's tutorial on arrays.

Link to the tutorial: http://www.dreaminco...showtopic=83615
Was This Post Helpful? 0
  • +
  • -

#11 jaArch   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Arrays

Posted 23 March 2010 - 07:57 PM

Sounds like the Array needs to go inside the main class then I print it in there?
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,879
  • Joined: 27-December 08

Re: Arrays

Posted 23 March 2010 - 08:00 PM

If you have a Shifter object named shiftpos, and you invoke shiftpos.display();, it should output the elements in the array data. This array should not go into the main() method, as that was not what your instructions asked for. Now in display(), you just need to write code that will output the elements in the array data. Go ahead and give it a try.
Was This Post Helpful? 0
  • +
  • -

#13 jaArch   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Arrays

Posted 23 March 2010 - 08:05 PM

This is what I came up with:

System.out.printf("Array contents: ", shift);
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,879
  • Joined: 27-December 08

Re: Arrays

Posted 23 March 2010 - 08:10 PM

The variable shift is local to your constructor, so you cannot access it in your display() method. Also, when you print an array, you will get hash code as your output, not its elements. Hint- use a for loop. Again, take a look at the tutorial I linked to. It is not so important for you to take away from it where the arrays go, but more so how they work.

This post has been edited by macosxnerd101: 23 March 2010 - 08:11 PM

Was This Post Helpful? 0
  • +
  • -

#15 jaArch   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Arrays

Posted 23 March 2010 - 08:15 PM

public Shifter ( int size ){

int shift[] = new int [ 15 ];
for ( int i = 0; i > data.length; i++);
data = shift;



}

Like thatt?
Was This Post Helpful? -1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3