Passing Array help

I am trying to pass my array to the constructor.

Page 1 of 1

9 Replies - 724 Views - Last Post: 19 January 2009 - 08:36 PM Rate Topic: -----

#1 mykohlx43  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 13-January 09

Passing Array help

Posted 19 January 2009 - 07:51 PM

I have a homework assignment where I need to pass variables to an object and then sort them. One of them is an array and my teacher never really explained how to pass it to the constructor and then reassign them globally. Can anyone help!? I am not asking for people to solve the homework assignment, just to help me pass the array.


Main

// The "CDCOLLECT" class.
import cs1.Keyboard;
public class CDCOLLECT
{
	public static void main (String[] args)
	{
		System.out.println ("How many Cd's");
		int choice = Keyboard.readInt ();
		CD[] c = new CD [choice];

		int index = 0, cdtracknum;
		String cdname, cdartist;
		boolean answer = false;

		while (answer == false)
		{
			System.out.println ("CD # " + (index + 1));
			System.out.print ("Album's Name?: ");
			cdname = Keyboard.readString ();
			System.out.print ("Artist's Name?: ");
			cdartist = Keyboard.readString ();
			System.out.print ("# of Tracks?: ");
			cdtracknum = Keyboard.readInt ();

			String[] trackname = new String [cdtracknum];
			for (int x = 0; x < cdtracknum; x++)
			{
				System.out.print ("(" + (x + 1) + ")" + "Name of the Tracks?: ");
				trackname [x] = Keyboard.readString ();
			}

			CD cd = new CD (cdname, cdartist, cdtracknum, trackname);
			c [index] = cd;
			if ((index + 1) == choice)
				break;
			index++;

			System.out.println (cd); // this is where i am testing to see if it printed anything
											   // and it doens't print it
		}
	} 
} 








Object File
public class CD implements Comparable
{
	String name, artist;
	String[] trackname = new String [30];
	int tracknum;
	public CD (String cdname, String cdartist, int cdtracknum, String[] trackname1)
	{
		name = cdname;
		artist = cdartist;
		tracknum = cdtracknum;

		// is this correct???

		for (int x = 0; x < trackname1.length; x++)
		{
			trackname [x] = trackname1 [x];
		}
	}

	public int compareTo (Object o)
	{
		return -1;
	}

	public String toString ()
	{
		return trackname [0]; // I am trying to print it out to see if it worked
	}
}





Is This A Good Question/Topic? 0
  • +

Replies To: Passing Array help

#2 TriggaMike  Icon User is offline

  • Has left the building.
  • member icon

Reputation: 81
  • View blog
  • Posts: 1,096
  • Joined: 26-September 08

Re: Passing Array help

Posted 19 January 2009 - 07:57 PM

It looks like you are accepting the array as an argument in the constructor. Can you be more specific? Is something not working in your program, if so what?
Was This Post Helpful? 0
  • +
  • -

#3 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: Passing Array help

Posted 19 January 2009 - 07:57 PM

yea, you got the array part tight ;)
Was This Post Helpful? 0
  • +
  • -

#4 mykohlx43  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 13-January 09

Re: Passing Array help

Posted 19 January 2009 - 08:00 PM

well when i run the program everything works except when i try to print out the variable. It gives me nothing when i print it out, so i feel like something is getting lost when i am reassigning it. I know i'm a beginner, but what does accepting it as an argument mean?
Was This Post Helpful? 0
  • +
  • -

#5 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: Passing Array help

Posted 19 January 2009 - 08:05 PM

well its supposed to print the element, try using the "this" keyword
 for (int x = 0; x < trackname1.length; x++)
		{
		   this.trackname [x] = trackname1 [x];
		}


Was This Post Helpful? 0
  • +
  • -

#6 mykohlx43  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 13-January 09

Re: Passing Array help

Posted 19 January 2009 - 08:13 PM

i still can't print out the array after reassigning it. i think i'm just gonna pass it directly to the method. w/o globally declaring it
Was This Post Helpful? 0
  • +
  • -

#7 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: Passing Array help

Posted 19 January 2009 - 08:15 PM

my apologies but its really late over here now, and am really sleepy..cant think at all..i think other members should be around now like pbl
Was This Post Helpful? 0
  • +
  • -

#8 TriggaMike  Icon User is offline

  • Has left the building.
  • member icon

Reputation: 81
  • View blog
  • Posts: 1,096
  • Joined: 26-September 08

Re: Passing Array help

Posted 19 January 2009 - 08:21 PM

Ok ok, if you're a beginner I'll give you a few pointers that will make your life easier:

this is your general method definition

[public|private|protected|*default*none of the other words*] [static|*default*without static*] [return type]  [method name] ([argument type] [argument name],...,as many arguments as you want, or none if you don't want any)


Public: The Method is accessible to any other class that has access to the class the method is inside of

Private: The method is only accessible to the class itself.

Protected: The method is accessible to the class, or other classes inside of the same package. You will not use this keyword until later on.

Default: More Strict than Protected, Less Strict than private. I don't recommend using default.



Static: This will be complicated to understand right now, but essentially you should only have a static main method and all other methods should not be static. You will not need to use this keyword for a while other than your main method.




Return Type: This can be anything, from primitive data types (ie. int, double) or objects (String, CD, any other objects you create). CONSTRUCTORS CANNOT HAVE A RETURN STATEMENT. THIS IS DUE TO THE FACT THEY AUTOMATICALLY RETURN A REFERENCE NOT A USABLE VALUE.


Arguments: every method can have 0 or more arguments. You must define both the type of argument, and the name of the argument. This is because it acts like a declaration of a new variable, except it is in the method header.

I hope that helps!

EDIT: When passing Array's and Objects, remember they are passed by REFERENCE not by VALUE. This means that you can edit things inside of them and it will carry back to your main method. This is because an address to a memory location is passed, not a new object or array.

This post has been edited by TriggaMike: 19 January 2009 - 08:26 PM

Was This Post Helpful? 0
  • +
  • -

#9 mykohlx43  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 13-January 09

Re: Passing Array help

Posted 19 January 2009 - 08:32 PM

That did help!! thanks!!
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: Passing Array help

Posted 19 January 2009 - 08:36 PM

Yes this is correct
	  for (int x = 0; x < trackname1.length; x++)
		{
			trackname [x] = trackname1 [x];
		}



but you are hardcoding the array length to 30
better to

	  trackname = new String[trackname1.length];
	  for (int x = 0; x < trackname1.length; x++)
		{
			trackname [x] = trackname1 [x];
		}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1