Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic Object Oriented Programming

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.

like image 314
Anon Omus Avatar asked Oct 17 '13 12:10

Anon Omus


1 Answers

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.

like image 182
Martin Avatar answered Oct 13 '22 11:10

Martin