0

so Ive been trying to set values to an object within an object with no luck. This is what I need to do.

Class 1: Interface (main) With MovieDatabase object

Class 2: MovieDatabase with Movie Object

Class 3: Movie with 4 values needed. Name of movie,director etc.

I've previously only used one object and accessed that through another class but I'm not sure how to do this when it's an object within an object. Any examples using the classes above would be appreciated.

6
  • 2
    What you are looking is a Java OO Tutorial/guide. This is not on topic : help center Commented Apr 12, 2017 at 10:29
  • can u explain more in generalized way and currently it is looking simple OOPs concept Commented Apr 12, 2017 at 10:29
  • You don't need an interface per se imo, and it's really not difficult. You need two classes, Movie with 4 variables, MovieDatabase with at least one variable (an instance of Movie). Best practice is declaring these as private, and using getters (& setters, if needed) to access/modify the data. I'd recommend reading about encapsulating data, general object-oriented programming etc. The accessing will be quite easy: MyMovieDB.getMovie().getDirector(). Commented Apr 12, 2017 at 10:30
  • The thing is that this is for an assignment and i need to have all these classes with the objects in those specific classes mentioned. Im not sure how to approach this. Ive had a look at some composition tutorials but they create the objects in one class only, whereas i need the first object in class 1, second object in class 2. I have already used set/get methods, data encapsulation etc but only with 2 classes, like youve shown. Commented Apr 12, 2017 at 10:32
  • Good, now try with more .. we are not going to do this assignment for you, this won't help you. Try and try again. There is a lot of guide (that take more than 10min to read) that will give you a good knowledge to answer this. Commented Apr 12, 2017 at 10:43

1 Answer 1

4

A quick example

Movie Class

public class Movie {


    private String name;
    private String director;


    public Movie(String name,String director){

        this.name=name;
        this.director=director;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDirector() {
        return director;
    }

    public void setDirector(String director) {
        this.director = director;
    } 

}

MovieDatabase which contains a list of Movie objects

public class MovieDatabase {


    List<Movie> movies = new ArrayList<>();

    public List<Movie> getMovies() {
        return movies;
    }

    public void setMovies(List<Movie> movies) {
        this.movies = movies;
    }


}

Now let's try to create some Movie objects and add them to the list of Movie objects which is a field in MovieDatabase Class

    public class stackoverflow {

        public static void main(String[] args) {
            // TODO code application logic here

            Movie a,b,c,d;
            MovieDatabase db =  new MovieDatabase();


            a = new Movie("Movie1","Director1");
            b = new Movie("Movie2","Director2");
            c = new Movie("Movie3","Director3");
            d = new Movie("Movie4","Director4");

            db.movies.add(a);
            db.movies.add(b);
            db.movies.add(c);
            db.movies.add(d);


            for(Movie movie : db.movies)

 System.out.println("the movie "+movie.getName()+" is directed by "+movie.getDirector());



        }

    }

The output :

the movie Movie1 is directed by Director1

the movie Movie2 is directed by Director2

the movie Movie3 is directed by Director3

the movie Movie4 is directed by Director4

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.