7

I am currently studying Java and have been asked to write a program that deals with actors and films as classes.

The actor class has the following attributes:
Name, Address, age, myFilm (an array or arraylist to hold all the films a particular actor has starred in.

The film class has these attributes:
Name, Code (String, String)

I have implemented these classes with getter and setter methods to handle the data:
My actor class so far:

public class actor {
    private String name;
    private String address;
    private int age;
    int[] myFilms = new int[3];

     public actor (String name, String address, int age) {
     }


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

     public void setAddress (String address) {
          this.address = address;
     }

     public void setAge (int age) {
          this.age = age;
     }

     public void setFilm () {

     }

     public String getName () {
         return name;
     }

     public String getAddress () {
         return address;
     }  
}

My film class:

public class film {

    private String name;
    private String code;

    //Constructor
    public film () {

    }

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

    public String getName (){
        return name;
    }

    public String getCode (String name) {
        //Get code:
        //Split Function
        String[] words = name.split("\\s+");
        String code = "";
        for (int i=0; i < words.length; i++) {
             code = code + words[i].charAt(0);
             code = code.toUpperCase();
        }
        return code;
    }
}

I'm hitting a brick wall with how to approach making the program dynamic to display each actors total films. This is for a college assingnment and I am also required to do a deep copy of the array at some point. I am almost completely new to OO so this is proving a tricky task for me.

Any words of advice or a point in the right direction would be hugely appreciated.

8
  • 2
    Why is the actors myFilms an array of int? Why not an array of film? Commented Oct 17, 2013 at 12:31
  • Yeah, array of films (Film[]). And also there is an agreement that each class name must start with capital letter. Commented Oct 17, 2013 at 12:33
  • ArrayList<Film> would be even better Commented Oct 17, 2013 at 12:34
  • I would use List<Film> instead of an array to hold films. Or at least an ArrayList. This way you are limiting the max number of film per actor to 3. Commented Oct 17, 2013 at 12:34
  • Correction: Yes the myFilm array should be a String. The trouble I'm having is figuring out how to make each actors myFilm array dynamic to display film titles relavent to only them. Commented Oct 17, 2013 at 12:36

4 Answers 4

3

Two comments about your classes...

  1. Why not declare the films of an actor like this:

    private List<Film> myFilms = new ArrayList<Film>();

    This way, you will be able to add and remove film object references dinamically from your actor objects. You can implement getter and setter for the list and manipulate it from outside, for example. Lists (or Collections in general) are much easier to manipulate than primitive arrays.

  2. Consider declaring classes with first capital letter, it's a convention. For example: Actor, Film.

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

4 Comments

"Two comments about your classes".
Thanks for the reply Martin, Would the arrayList being private not allow me to access it from the main class though? I need to copy an actors films from the film array in the actor class into 1 array in the main.
@AnonOmus You access it through public methods. For example, addFilm could be public (callable from main), which then adds an item to the List of films. You can also have a public getFilms which returns the list(or a copy of it). Same with remove film. In Java we generally give access to everything through methods. object.field = value is almost never done.
You can access the List from outside if you declare getter/setter methods, and you can add/remove elements from the list without worrying (as long as you do not do anything multithreaded, but I guess that's not part of your assignment)...
2

Consider extracting a "actor that play in film" to another class, to decouple film out of actor (actor can also do theathre spectacles, vioce dubbig etc, not specialy movies.)

class ActorRole {
   private Actor actor;
   private Movie movie;
   private int dollarsSallary;
   private int scenesPlayed;
   // etc.
}

If you don't want to, I'm almost sure that better create dependency from Movie to Actor than from Actor to Movie, because Movies almost surely have actos:

class Movie {
  private List<Actor> actors = new ArrayList<Actor>();
}

This makes harder to count actor statistics (you have to iterate over all Movies) but I think this is a better design.

To count single actor shows:

for ( Movie movie : listOfAllMovies ) {
   if ( movie.getActors().contains( myActor ) ) { // read about equals() in Java !
     timesPlayed++;
   }
}

If you want to make a ranking for more actors, you can use Map<Actor,Integer> to map actors to they times played counters.

This can be a lengthy operation, so you can think about cashing the results (like in above map) - the solution can be map, ActorStatistics class, simple timesPlayed field in actor etc. etc.

Don't be afraid to objects

Don't do a hard workaround to mape films to id (like your id, which is propably connected to your film code String, wich add another type-incompatibility issue. Try to use more object references instead of workarounds, and List instead of array.

Generally, read about Collections in Java, like ArrayList and HashMap and also overriding equals() and hashCode(), and in general OOP Single responsibility principle and Class cohesion

1 Comment

I agree that it's better to have a dependency from movie to actor; after all, if an actor plays in another movie, it doesn't actually change him from a domain standpoint. However, there no good reason for an Actor class); as is, it's just a simple Person . You could then reuse the class to add more team members to the movie, eg producers, musicians etc.
1

You can compose a auto-increment container inside class actor, like vector ArrayList and so on. Or you could implement a dynamic array by yourself.

1 Comment

Why on earth would anyone implement a dynamic array themselves? Just about every desired implementation is offered to date.
0

If u have a database which has a table with username and film(ID) & etc, u can create a class as follow,

Class Film{

private String actorName;

private String filmCode;

private String filmName;

....getter & setter methods
...
}

then u can create a method to get data list of Film class. Eg:

List<Film> filmList = new ArrayList<Film>();
String actorName = "Arnold";
filmList = dbCon.getFilmListByActor(actorName);

your getFilmListByActor method should be like this,

public List<Film> getFilmListByActor(String actorName){

List<Film> filmList = new ArrayList<Film>();
//Query for get film list by actor
 //assigne filmList to result set
enter code here
 return filmList; 

}

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.