Create a program that has an App Class which holds your main

Create a program that has an App Class which holds your main() method and create a Band class.

Create an array of 5 Band objects in your main() method.

Each band should have it\'s own compete() method along with a a drummer, vocalist, and piano player.

Each band should have the names of the drummer, vocalist, and piano player.

The complete method() assigns a random score between 0 and 20 to to each band.

Create a method to calculate the top 2 winners from highest score to least and write each band\'s information to a \"winners.txt\" file.

A grabInfo() method should be called to display a band\'s information.

Solution

// Band.java

import java.util.Random;

public class Band implements Comparable<Band> {

   // constructor with all the fields
   public Band(String drummer, String vocalist, String piano) {
       this.drummer = drummer;
       this.vocalist = vocalist;
       this.piano = piano;
   }
  
   // default constructor
   public Band() {}

   private String drummer;
   private String vocalist;
   private String piano;
  
   private int score;

   private Random rand = new Random();
  
   // assign a random score between 0 to 20.
   public void complete()
   {
       this.score = rand.nextInt(20);
   }
  
   // getter and setters
   public String getDrummer() {
       return drummer;
   }

   public void setDrummer(String drummer) {
       this.drummer = drummer;
   }

   public String getVocalist() {
       return vocalist;
   }

   public void setVocalist(String vocalist) {
       this.vocalist = vocalist;
   }

   public String getPiano() {
       return piano;
   }

   public void setPiano(String piano) {
       this.piano = piano;
   }

   public int getScore() {
       return score;
   }

   public void setScore(int score) {
       this.score = score;
   }

   // compare to method to sort array for highest to lowest
   @Override
   public int compareTo(Band arg0) {
       if(getScore() > arg0.getScore())
       {
           return -1;
       }
       if (getScore() < arg0.getScore())
       {
           return 1;
       }
       return 0;
   }

   @Override
   public String toString() {
       return \"Band [drummer=\" + drummer + \", vocalist=\" + vocalist
               + \", piano=\" + piano + \", score=\" + score + \"]\";
   }
  
   public void grabInfo() {
       System.out.println(\"Band [drummer=\" + drummer + \", vocalist=\" + vocalist
               + \", piano=\" + piano + \", score=\" + score + \"]\");
   }
  
  
  
}

// App.java

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;


public class App {

   private static void getTopBand(Band[] bands )
   {
       Arrays.sort(bands);
      
       System.out.println(\"Top two bands:\");
      
       for(int i = 0; i < 2; i++)
       {
           bands[i].grabInfo();
       }
      
       try{
       PrintWriter writer = new PrintWriter(\"winners.txt\", \"UTF-8\");
       for(int i = 0; i < 5; i++)
           {
               writer.println(bands[i]);
           }
       writer.close();
       } catch (IOException e) {
           System.out.println(\"Unable to open file\");
       }
   }
  
   public static void main(String[] args) {

       Band[] bands = new Band[5];
      
       bands[0] = new Band(\"Drummer1\", \"Vocalist1\", \"Piano1\");
       bands[0].complete();
      
       bands[1] = new Band(\"Drummer2\", \"Vocalist2\", \"Piano2\");
       bands[1].complete();
      
       bands[2] = new Band(\"Drummer3\", \"Vocalist3\", \"Piano3\");
       bands[2].complete();
      
       bands[3] = new Band(\"Drummer4\", \"Vocalist4\", \"Piano4\");
       bands[3].complete();
      
       bands[4] = new Band(\"Drummer5\", \"Vocalist5\", \"Piano5\");
       bands[4].complete();
      
      
       getTopBand(bands);
      
   }

}

/*

Sample output

Top two bands:
Band [drummer=Drummer1, vocalist=Vocalist1, piano=Piano1, score=19]
Band [drummer=Drummer5, vocalist=Vocalist5, piano=Piano5, score=19]

winners.txt

Band [drummer=Drummer1, vocalist=Vocalist1, piano=Piano1, score=19]
Band [drummer=Drummer5, vocalist=Vocalist5, piano=Piano5, score=19]
Band [drummer=Drummer4, vocalist=Vocalist4, piano=Piano4, score=13]
Band [drummer=Drummer2, vocalist=Vocalist2, piano=Piano2, score=7]
Band [drummer=Drummer3, vocalist=Vocalist3, piano=Piano3, score=7]

*/

Create a program that has an App Class which holds your main() method and create a Band class. Create an array of 5 Band objects in your main() method. Each ban
Create a program that has an App Class which holds your main() method and create a Band class. Create an array of 5 Band objects in your main() method. Each ban
Create a program that has an App Class which holds your main() method and create a Band class. Create an array of 5 Band objects in your main() method. Each ban

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site