This is probably an easy one that i know but its not coming to me. Im supposed to be hardcoding the 6 instances of a movie class into the array that i have set up. I am unsure where i would do that in my actuall code.

public class movie {

	private String title;
	private int fightScenes;
	private int romanticScenes;
	private int musicalScenes;
	
	public movie (String name, int fight, int romance, int musical){
		title = name;
		fightScenes = fight;
		romanticScenes = romance;
		musicalScenes = musical;
	}
	
	public String getTitle(){
		return title;
	}
	
	public int getFightScenes(){
		return fightScenes;
		
	}
	
	public int getRomanticScenes(){
		return romanticScenes;
		
	}
	
	public int getMusicalScenes(){
		return musicalScenes;
		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		final int numMovies = 6;
		int[] movies = new int[numMovies];
		
		 
		
		
		
		
	}

}

Recommended Answers

All 5 Replies

Just assign the elements your new instances

movies[0] = new Movie(...);

or you can initialize them all at once

int[] movies = {
  new Movie(...),
  new Movie(...),
...
};

How can you set the elements of an int array to objects?

commented: Glad someone was paying attention ;) +14

LOL.. shows how much attention I was really paying to the code :)

That's what I get for hammering out a quick answer before I take care of something else at work.

Better check that declaration as Norm said.

Another potential problem is that the array is local to the main method. It will go away when main exits unless connected a variable that has larger scope.

Yes, I assume the array is to be used for some demo output within main(), though the intended usage is not stated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.