开发者

How to write a 3 class program and have the main class trigger methods in the other two

I have to write a program with 3 classes and lots of different methods.

I've written a simpler example to try and get an idea where I am going wrong

First class (music) is defining a music object with three data types. And should have a method to print the contents of an array.

the second class (musicArray) has all the data for the array and should build the array when the third class tells it too.

the third class(searchclass) has the main method it should tell the second class to make the array then with user input search the array for songs that match the rating.

        import java.util.Arrays;

public class Music extends musicArray {

        private  String songTitle;
        private  double songLength;
        private  int rating;
        static String everything;

    public Musi开发者_如何学JAVAc(String songTitle, double songLength, int rating) {
           this.songTitle = songTitle;
           this.songLength = songLength;
           this.rating = rating;
        }
    public  String getsongTitle()
    {
        return songTitle;
    }
    public  double getsongLength()
    {
        return songLength;
    }
    public    int rating()
    {
        return rating();
    }
@Override
public String toString() {
    return "music{"+ "songTitle= " + songTitle + ", songLength=  "
    + songLength + ",rating=" + rating + '}';
}
    public Music[] printsonglibrary(char[][] songDetails){

         for (int count = 0; count <= 6; count++)
             {
             return System.out.println(songDetails[count]);
             System.out.println(" ");
             }
         }
}

public class musicArray extends Searchclass{

    static Music song1 = new Music ("achy breaky", 5.32, 10);
    static Music song2 = new Music ("billy",1.2, 8 );
    static Music song3 = new Music ("hello", 1.5, 9 );
    static //Create array and make posistion 0 = song1

    Music[] songDetails ={song1,song2,song3};
        import java.util.Scanner;

        public class Searchclass {
      public static void main(String[] args) {
        for(int count = 1; count <= songDetails.length; count++){
         system out put for debugging
          System.out.println(songDetails.length);
          System.out.println(songDetails[count - 1]);}
        }

        /* public String songSeach(){
        System.out.println("what rating of song do you want to search for?");
        Scanner keyboard = new Scanner(System.in);
        int searchValue = keyboard.nextInt();
         if searchValue == rating in array use the printsonglibrary
        method in the music class to print the array entry
   */
        }
}  

If I have the main method in the musicArray class I can print the array So the question is how do I make the Songdetails array available in the seachclass?


You shouldn't directly expose any variables of one class to another. Instead consider giving the MusicArray class (note that by convention class names should begin with a capital letter) a public method, say public void printOutSongDetails() that would print out the contents of the array. Your main method can then call this method off of the MusicArray object that it has created. e.g.,

Edit 1
Also, the Music class should most definitely not extend the MusicArray class for there is no way that a Music object should behave like a MusicArray object. And on the same token, MusicArray should not extend the SearchClass.

Edit 2
Note that there are several other significant issues with your code that each one would prevent it from compiling, and this suggests that you should modify how you program (if you can't use an IDE). Try to compile early and often, and only add new code to the program after fixing any compilation errors so that the current code base compiles.

Edit 3
A small code example of what I was describing above.

class SearchClass {
   public static void main(String[] args) {
      MusicArray musicArray = new MusicArray();
      musicArray.addMusic(new Music("Foobars Unit", 10.4, 5));
      musicArray.addMusic(new Music("Spamalot", 11.0, 7));
      //... etc ...

      musicArray.printOutSongDetails();
   }
}

Also, you'll probably not want those static Music variables but rather give MusicArray a method to add Music to its array, a public void addMusic method that accepts a Music object as a parameter.


One way is to make it public public Music[] songDetails ={song1,song2,song3};

A better way is to provide getter:

public Music[] getSongDetails() {
    return songDetails;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜