2013-03-30 42 views
0

我有一個來自我的教授的任務,我無法弄清楚如何正確地創建一個對象數組。除了Client類以外,其他類都不能更改;界面也不能改變。我應該能夠從幾個子類創建一個對象數組,並且能夠訪問所有的方法。如何製作一組子對象

這是我的錯誤報告:

MathewBorumP5.java:68: error: cannot find symbol 
        revenue = movies[x].calcRevenue(); 
            ^
symbol: method calcRevenue() 
location: class Movie 
MathewBorumP5.java:70: error: cannot find symbol 
          movies[x].getYear(), movies[x].calcRevenue(), 
                 ^
symbol: method calcRevenue() 
location: class Movie 
MathewBorumP5.java:71: error: cannot find symbol 
          movies[x].calcProfit(revenue), movies[x].categor 
y()); 
            ^
symbol: method calcProfit(double) 
location: class Movie 
MathewBorumP5.java:71: error: cannot find symbol 
          movies[x].calcProfit(revenue), movies[x].categor 
y()); 
                    ^
symbol: method category() 
location: class Movie 
MathewBorumP5.java:78: error: cannot find symbol 
        totalRevenue = totalRevenue + movies[x].calcRevenue(); 
                 ^
symbol: method calcRevenue() 
location: class Movie 
MathewBorumP5.java:81: error: cannot find symbol 
        "%.3f million dollars.", Movie[0].getTotalMovies(), tota 
lRevenue); 
              ^
symbol: variable Movie 
location: class MathewBorumP5 
6 errors 

我的客戶端類:

import java.util.Scanner; 

public class MathewBorumP5 { 
    public static void main(String[] args) { 
     int choice; 
     boolean restart = true; 

     Scanner input = new Scanner(System.in); 

     Movie[] movies = new Movie[6]; 
     movies[0] = new Animated("Beauty and the Beast", "Gary Trousdale", 1991, 
      10.0, 5.0, 2.0); 
     movies[1] = new Animated("Peter Pan", "Clyde Geronimi", 1953, 2.0, 1.2, 
      .5); 
     movies[2] = new Documentary("Planet Earth", "Alastair Fothergill", 2006, 
      10, 20, 5); 
     movies[3] = new Documentary("Drain the Ocean", "Steve Nichols", 2009, 9, 
      2,3); 
     movies[4] = new Drama("The Shawshank Redemption", "Frank Darabont", 
      1994, 89, 7, 2); 
     movies[5] = new Drama("The Godfather", "Francis Coppola", 1972, 10, 3, 
      5); 

     do { 
      menu(); 
      System.out.print("Enter a number from 1 - 5: "); 
      choice = input.nextInt(); 
      System.out.print("\n"); 

      switch(choice) { 
       case 1: 
        item1(movies); 
        break; 
       case 2: 
        item2(movies); 
        break; 
       case 3: 
        break; 
       case 4: 
        break; 
       case 5: 
        restart = false; 
        break; 
       default: 
        System.out.print("You didn't enter a number between 1" 
         + " and 5.\n"); 
        break; 
      } 
     } while(restart == true); 
    } 

    public static void menu() { 
     System.out.print("Warren Moore Movie Menu\n"); 
     System.out.print("1. Show the list of movies in the array\n"); 
     System.out.print("2. Display the total number of movies and the total" + 
      " revenues\n"); 
     System.out.print("3. Search movie by title\n"); 
     System.out.print("4. Display movies sorted by profit in decreasing" + 
      " order\n"); 
     System.out.print("5. Exit\n"); 
    } 

    public static void item1(Movie[] movies) { 
     double revenue; 
     System.out.printf("%-26s%-6s%-10s%-9s%-11s\n", "Title", "Year", 
     "Revenue", "Profit", "Category"); 
     for(int x = 0; x <= 6; x++) { 
      revenue = movies[x].calcRevenue();    
       System.out.printf("%-26s%-6s%-10s%-9s%-11s\n", movies[x].getTitle(), 
       movies[x].getYear(), movies[x].calcRevenue(), 
       movies[x].calcProfit(revenue), movies[x].category()); 
     } 
    } 

    public static void item2(Movie[] movies) { 
     double totalRevenue; 
     for(int x = movies[0].getTotalMovies(); x > 0; x--) { 
      totalRevenue = totalRevenue + movies[x].calcRevenue(); 
     } 
     printf("The total number of moves is %d, and their total revenue is" + 
      "%.3f million dollars.", Movie[0].getTotalMovies(), totalRevenue); 
    } 
} 

我的超類:

public class Movie { 
    protected String title; 
    protected String director; 
    protected int year; 
    protected double productionCost; 
    private static int totalMovies = 0; 

    public Movie() { 
     totalMovies++; 
    } 
    public Movie(String newTitle, String newDirector, int newYear, 
     double newCost) { 
     totalMovies++; 
     title = newTitle; 
     director = newDirector; 
     year = newYear; 
     productionCost = newCost; 
    } 

    public int getTotalMovies() { 
     return totalMovies; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String newTitle) { 
     this.title = title; 
    } 

    public String getDirector() { 
     return director; 
    } 

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

    public int getYear() { 
     return year; 
    } 

    public void setYear(int year) { 
     this.year = year; 
    } 

    public double getProductionCost() { 
     return productionCost; 
    } 

    public void setProductionCost(double productionCost) { 
     this.productionCost = productionCost; 
    } 

    public String toString() { 
     return ""; 
    } 
} 

類模板(我所有的類都是基本日Ë相同):

public class Animated extends Movie implements Profitable { 
    private double rate; 
    private double income; 

    public Animated() { 
     super(); 
    } 

    public Animated(String title, String director, int year, double cost, 
     double rate, double income) { 
     super(title, director, year, cost); 
     this.rate = rate; 
     this.income = income; 
    } 

    public double getRate() { 
     return rate; 
    } 

    public void setRate(double rate) { 
     this.rate = rate; 
    } 

    public double getIncome() { 
     return income; 
    } 

    public void setIncome(double income) { 
     this.income = income; 
    } 

    public String category() { 
     return "Animated"; 
    } 

    public double calcRevenue() { 
     return (income * rate); 
    } 

    public double calcProfit(double revenue) { 
     return (revenue - super.productionCost); 
    } 

    public String toString() { 
     return (super.toString() + ""); 
    } 
} 

我的界面:

public interface Profitable { 
    public abstract String category(); 
    public abstract double calcRevenue(); 
    public abstract double calcProfit(double revenue); 
} 

回答

0

你需要使用instanceof來添加適當的演員和測試。

revenue = movies[x].calcRevenue(); 

必須

if (movies[x] instanceof Profitable) { 
    revenue = ((Profitable)movies[x]).calcRevenue(); 
} else { 
    // decide what to do if movie if not profitable that is to say does not have the calcRevenue method 
} 
被替換