【问题标题】:Interfaces and Polymorphism接口和多态
【发布时间】:2015-04-17 05:34:30
【问题描述】:

我有一个大学作业

到目前为止,这就是我所拥有的

public class Film {

    //Data members
    static int idNumber;
    String title;
    String classification;

    //constructor
    public Film() {
        idNumber = 0;
        title = "Not Given";
        classification = "G";
        classification = "PG";
        classification = "12A";
        classification = "15A";
        classification = "16";
        classification = "18";      
    }

    public Film(int idNumber, String title, String classification) {
        idNumber = 0;
        title = "Not Given";
        classification = "Not Given";
    }

    //Getters
    public int getIdNumber( ) {
        return idNumber;
    }

    public String getTitle( ) {
        return title;
    }

    public String getClassification( ) {
        return classification;
    }

    //Setters
    public void setIdNumber(int IdNumber) {
        IdNumber = idNumber;
    }

    public void setTitle(String Title) {
        Title = title;
    }

    public void setClassification(String Classification) {
        Classification = classification;
    }

    //calculateLateFee method
    double calculateLateFee(int numDaysLate){
        return numDaysLate;
    }

    //toString method
    public String toString(){
        return idNumber + "" + title + "" + classification;
        }

}//end class

public class Drama extends Film {

    int numDaysLate = 3;

    //overridden calculateLateFee method
    double calculateLateFee(int numDaysLate){
        return numDaysLate;     
    }

}//end class

public class Action extends Film {

    int numDaysLate = 5;

    //overridden calculateLateFee method
    double calculateLateFee(int numDaysLate){
        return numDaysLate; 

}

}

public class Comedy extends Film {

    int numDaysLate = 4;

    //overridden calculateLateFee method
    double calculateLateFee(int numDaysLate){
        return numDaysLate;
}

}

import java.util.Scanner;

public class FilmTest {

static int array;

//create method createArray
public static Film[] createArray() {

    Scanner int_input = new Scanner(System.in);

    Film[] array = new Film[0]; 

    //read user input as arraySize
    return new Film[3];

}//end method   

//create method populateArray
public static void populateArray(Film[] array) {

    Scanner string_input = new Scanner(System.in);
    Scanner double_input = new Scanner(System.in);
    Scanner int_input = new Scanner(System.in);

    for (int i = 0; i < 3; i++) {
        Film movies = new Film(); //drama

        //set ID number
        System.out.println("Enter Movie ID Number: ");
        movies.setIdNumber(int_input.nextInt());

        //put new student into array passed to the method
        array[i] = movies; 

    }//end for loop 

}//end method

//create method display Array
public static void displayArray(Film[] array){

        for (Film s : array) {

            System.out.println(String.format("%s %d", s.getTitle(), 
                s.getIdNumber(), s.getClassification()));
        }//end for loop
    }//end method

     public static void main(String [] args) {

        // create array of size specified by user
        Film[] movies = createArray();

        //populate this array with data from user
        populateArray(movies);

        //display array contents
        displayArray(movies);   

    }//end main method

}//end class

好的,所以我只是在学习如何做这些事情,所以有人可以帮助我

我该如何写,这样当我输入 3 个 ID 号码时,电影的详细信息就会出现,如下所示:

Enter Movie ID Number: 
12345
Enter Movie ID Number: 
34567
Enter Movie ID Number: 
95432
12345 - Step Brothers - 15A - Comedy
34567 - Transformers - PG - Action
95432 - The Godfather -18 - Drama

【问题讨论】:

  • 嗨,欢迎来到 stackoverflow。请将您的问题缩短为您遇到的具体问题。如果你先阅读sscce.org会更好。这将节省人们阅读它的时间,他们将以更好的方式帮助您。

标签: java inheritance methods polymorphism


【解决方案1】:

你需要这样的东西......

public Film GetMovieFromID(int id){
     for(Film f: array){
          if(f.getIdNumber == id){
               return f;
          }
     }
     //If it didn't find the movie return null
     return null;
}

然后您需要一个扫描仪来询问用户输入。因此,在您显示 id 和标题以及分类和评级之后。你需要这样称呼......

public void MovieGoerChoosening(){
     String s = "What movie would you like to see? (Type in movie ID.)";
     Film chosenMovie;
     System.out.println(s);
     Scanner sc = new Scanner(System.in);
     try{
     /**
      * Surrounded with a try catch block because users are stoopid
      * and given the chance they will break your program at any
      * opportunity given.
      */
          s = sc.Next();
          chosenMovie = GetMovieFromID(Integer.parseInt(s));
     }catch(NumberFormatException e){
          s = "Type in a number with digits only. Try again."
          System.out.println(s);
     //Recursive call to itself to give the movie goer a second chance
          MovieGoerChoosening();
     }
     //Again users make mistakes and even if they typed only digits it 
     //could have returned nothing.
     if(chosenMovie != null){
          System.out.println(String.format("%s %d", chosenMovie.getTitle(), 
            chosenMovie.getIdNumber(), chosenMovie.getClassification()));
     }else{
          s = "Not a valid movie... Try again.";
          System.out.println(s);
          //Recursive call to itself to give the movie goer a second chance
          MovieGoerChoosening();
     }
}

【讨论】:

    猜你喜欢
    • 2011-02-20
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 2011-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多