Question

In: Computer Science

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

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.

Solutions

Expert 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]

*/


Related Solutions

In Java Create a class called "TestZoo" that holds your main method. Write the code in...
In Java Create a class called "TestZoo" that holds your main method. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of Poem objects, read in the information from PoemInfo.txt and create Poem objects to populate the ArrayList. After all data from the file is read in and the Poem objects added to the ArrayList- print the contents of the ArrayList. Paste your PoemDriver.java text (CtrlC to copy, CtrlV to paste) into the open space before. You should not change Poem.java or PoemInfo.txt. Watch your time...
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Using C# Create a class named Inches To Feet. Its Main()method holds an integer variable named...
Using C# Create a class named Inches To Feet. Its Main()method holds an integer variable named inches to which you will assign a value. Create a method to which you pass inches. The method displays inches in feet and inches. For example, 67 inches is 5 feet 7 inches.
Create a class using C# named InchesToFeet. Its Main()method holds an integer variable named inches to...
Create a class using C# named InchesToFeet. Its Main()method holds an integer variable named inches to which you will assign a value. Create a method to which you pass inches. The method uses 2 ref parameters: feet, inches left of type int and a parameter that is not ref of type int to which you pass inchesinches. For example, 67 inches is 5 feet 7 inches.
1. create a class called ArrayStack that is a generic class. Create a main program to...
1. create a class called ArrayStack that is a generic class. Create a main program to read in one input file and print out the file in reverse order by pushing each item on the stack and popping each item off to print it. The two input files are: tinyTale.txt and numbers.txt. Rules: You cannot inherit the StackofStrings class. 2. Using your new ArrayStack, create a new class called RArrayStack. To do this, you need a) remove the capacity parameter...
1. create a class called ArrayStack that is a generic class. Create a main program to...
1. create a class called ArrayStack that is a generic class. Create a main program to read in one input file and print out the file in reverse order by pushing each item on the stack and popping each item off to print it. The two input files are: tinyTale.txt and numbers.txt. Rules: You cannot inherit the StackofStrings class. 2. Using your new ArrayStack, create a new class called RArrayStack. To do this, you need a) remove the capacity parameter...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main()...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main() function to coordinate the execution of the program. We will need methods: Method for Depositing values into the account. What type of method will it be? Method for Withdrawing values from the account. What type of method will it be? Method to output the balance of the account. What type of method will it be? Method that will output all deposits made to the...
Create a Java program. The class name for the program should be 'EncryptText'. In the main...
Create a Java program. The class name for the program should be 'EncryptText'. In the main method you should perform the following: You should read a string from the keyboard using the Scanner class object. You should then encrypt the text by reading each character from the string and adding 1 to the character resulting in a shift of the letter entered. You should output the string entered and the resulting encrypted string. Pseudo flowchart for additional code to be...
Define Loan Class – Add to your project. And write program in main method to test...
Define Loan Class – Add to your project. And write program in main method to test it. Note: Assume year is number of years, rate is the annual interest rate and P is principle is loan amount, then the total payment is Total payment = P *(1+ rate/12)^ year*12; Monthly Payment = TotalPayment/(year*12); java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT