views:

17

answers:

1

For this project I'm not allowed to use generic classes keep in mind. I have tried copying all my classes into another friends computer and he gets the same error.

I cannot make calls like..

int counter = ((Movie) movieList.get(movListIndex)).getShowList().size();

Where getShowList() is a method in my Movie class that returns a LinkedList.

Eclipse says:

Cannot caste from Object to Movie

Additionally

movieList.add(objMovie, counter);

where objMovie is a Movie object.

Eclipse says:

The method add(Object, int) in the type LinkedList is not applicable for the arguments (Movie, int)

-Suggestion is change add method to take in Movie.

I need to be able to keep LinkedList and Node in a way that they can take in anything that is an Object, which when I create a Movie class I cannot cast to an Object.

Linked List Class

public class LinkedList {
 Node head;
 public LinkedList() {
  head = null;
 }

 public boolean isEmpty() {
  return(head==null);
 }

 public int size() {
  int count = 0;
  Node current = head;
  while (current != null) {
   count++;
   current = current.getNext();
  }
  return count;
 }

 public Object get(int index) {
  if ((index < 0) || (index > size()))
   throw new IndexOutOfBoundsException();
  Node current = head;
  int count = 0;
  while (count > 0) {
   count ++;
   current = current.getNext();
  }
  return current.getData();
 }

 public void add (Object data, int index) {
  if ((index < 0) || (index > size())){
   throw new IndexOutOfBoundsException();
  }
  Node previous = null;
  Node current = head;
  while (index > 0) {
   index --;
   previous = current;
   current = current.getNext();
  }

  Node splice = new Node();
  splice.setData(data);
  splice.setNext(current);

  if (previous == null)
   head = splice;
  else
   previous.setNext(splice);
 }

 public Object remove (int index) {
  if ((index < 0) || (index >= size()))
   throw new IndexOutOfBoundsException();
  Node previous = null;
  Node current = head;
  while (index > 0) {
   index --;
   previous = current;
   current = current.getNext();
  }
  if (previous == null)
   head = current.getNext();
  else
   previous.setNext(current.getNext());

  return current.getData();
 }
}

Node class

public class Node {
 private Object data;
 private Node next;

 public Object getData() {
  return data;
 }
 public Node getNext() {
  return next;
 }
 public void setData(Object data) {
  this.data = data;
 }
 public void setNext(Node next) {
  this.next = next;
 }
}

If needed Movie Class

public class Movie{
 private int filmNumber; //Movie ID
 private String filmTitle; //Title of Movie
 private int filmTime; //Movie Runtime
 private String filmRating; //Movie Rating
 private LinkedList showingList = new LinkedList();

 public Movie (int filmNumber, String filmTitle, int filmTime, String filmRating) {
  this.filmNumber = filmNumber; //sets global variable to parameter being passed
  this.filmTitle = filmTitle;   //                    |
  this.filmTime = filmTime;     //                    |
  this.filmRating = filmRating; //                    V
 }

 public int getFilmNumber() {
  return filmNumber; 
 }

 public String getFilmTitle() {
  return filmTitle;
 }

 public int getFilmTime(){
  return filmTime;
 }

 public String getFilmRating() {
  return filmRating;
 }

 public String getStringFilmTime() {
  String runtime = ""; //returned value
  int hours; //variable to hold 2 hour digits
  int minutes; //variable to hold minute digits

  hours = filmTime / 60; //formats original input to just hours
  minutes = filmTime % 60; //formats original input to just minutes

  if (minutes < 10) {
   runtime += ( Integer.toString(hours) + ":0" + Integer.toString(minutes));
   //Mod will only return a single digit if under 10
  }

  if (minutes > 10) {
   runtime += ( Integer.toString(hours) + ":" + Integer.toString(minutes));
   //Same statement without adding a '0'
  }

  return runtime;
 }

 public void setFilmNumber(int newFilmNumber) { 
  filmNumber = newFilmNumber;
 }

 public void setFilmTitle(String newFilmTitle) {
  filmTitle = newFilmTitle;
 }

 public void setFilmTime(int newFilmTime) {
  filmTime = newFilmTime;
 }

 public void setFilmRating(String newFilmRating) {
  filmRating = newFilmRating;
 }

 //public void setShowList()

 public LinkedList getShowList() {
  return showingList;
 }
}
A: 

Are these three classes all you have? I'm asking because if -- for some reason -- you have a class named Object next to your other classes, I can reproduce the error messages.

It may be as simple as:

public class Object {
}

If that's the case you should never name your classes Object, String, Integer or whatever else appears in the package java.lang.

Roland Illig