Question

In: Computer Science

1-Using the Note class and make it Comparable. A Note is larger than another note if...

1-Using the Note class and make it Comparable. A Note is larger than another note if it is higher in frequency. 2-In the driver program create a few objects and compare them . then create a list of those objects and sort them. 3. Rewrite the Note class so that a list of Notes is sorted first by length of note, then within that by frequency.

import java.io.*;
import java.math.*;

public class NoteTester
{
    public static void main(String[] args) throws IOException
    {
        //Declare variables to hold user input
        double noteValue;
        String noteLength;
        int tempLength;
  
        //Create BufferedReader object wrapped with InputStreamReader to hold user input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
        //Retrieve starting value from user
        System.out.println("Please enter a value for your note\n"+
                           "between -48 and 39:");
        noteValue = Double.parseDouble(br.readLine());
  
        //While loop ensures a valid number
        while(noteValue < -48 || noteValue > 39)
        {
            System.out.println("Please enter a valid value for your note\n"+
                               "between -48 and 39:");
            noteValue = Double.parseDouble(br.readLine());
        }
  
        //Retrieve starting length from user
        System.out.println("Please enter the number between 1-5\n" +
                           "you would like to represent\n" +
                           "the length of your note.\n"+
                           "1. Whole\n" +
                           "2. Half\n" +
                           "3. Quarter\n" +
                           "4. Eighth\n" +
                           "5. Sixteenth");
        tempLength = Integer.parseInt(br.readLine());
  
        //While loop ensures a valid length
        while(tempLength > 5 || tempLength < 1)
        {
            System.out.println("Please enter the number between 1-5\n" +
                               "you would like to represent\n" +
                               "the length of your note.\n"+
                               "1. Whole\n" +
                               "2. Half\n" +
                               "3. Quarter\n" +
                               "4. Eighth\n" +
                               "5. Sixteenth");
            tempLength = Integer.parseInt(br.readLine());
        }

        //If else statement assigns strings to the user's choice of int
        if(tempLength == 1)
        {
            noteLength = "whole";
        }
        else if(tempLength == 2)
        {
            noteLength = "half";
        }
        else if(tempLength == 3)
        {
            noteLength = "quarter";
        }
        else if(tempLength == 4)
        {
            noteLength = "eighth";
        }
        else if(tempLength == 5)
        {
            noteLength = "sixteenth";
        }
        else
        {
            noteLength = "Invalid";
        }
  
        //Create demoNote object
        Note demoNote = new Note(noteValue, noteLength);
  
        //Print user the information about their note
        System.out.print("Your value was " + demoNote.getValue() + ", your length was a(n) " + demoNote.getLength()
                         + " note, your note's \nletter value was " + demoNote.getLetter() + " which is a " +
                         demoNote.getSharp() + " note. \nThe frequency of your" +
                         " note is " + demoNote.getFreq() + " hz." );
    }
}


class Note
{
    /*Declare variables privately so they cannot be accessed from outside the class. Set the value default
      to middle C and the length default to quarter.*/
    private double value = 0;
    private String length = "Quarter";
    private String letter;
    private String sharp;
    private double freq;

    /**
     * Constructor
     * @param value the note's starting value
     * @param length the note's starting length
    */
    public Note(double value, String length)
    {
        this.value = value;
        this.length = length;
    }
  
    //Setters

    //Assigns this.value to variable value
    void setValue(double value)
    {
        this.value = value;
    }

    //Assigns this.length to variable length
    void setLength(String x)
    {
        this.length = length;
    }

    //Getters
    
    //@return The value of the note
    public double getValue()
    {
        return value;
    }
    
    //@return The length of the note
    public String getLength()
    {
        return length;
    }
    
    //@return The letter of the chromatic scale the value is assigned to
    public String getLetter()
    {
        if(value == 0 || value == 12 || value == 24 || value == 36 ||
           value == -12 || value == -24 || value == -36 || value == -48)
        {
            letter = "A";
        }
        else if(value == 1 || value == 13 || value == 25 || value == 37 ||
                value == -11 || value == -23 || value == -35 || value == -47)   
        {
            letter = "A#";
        }
        else if(value == 2 || value == 14 || value == 26 || value == 38 ||
                value == -10 || value == -22 || value == -34 || value == -46)
        {
            letter = "B";
        }
        else if(value == 3 || value == 15 || value == 27 || value == 39 ||
                value == -9 || value == -21 || value == -33 || value == -45)
        {
            letter = "C";
        }
        else if(value == 4 || value == 16 || value == 28 || value == -8 ||
                value == -20 || value == -32 || value == -44)
        {
            letter = "C#";
        }
        else if(value == 5 || value == 17 || value == 29 || value == -7 ||
                value == -19 || value == -31 || value == -43)
        {
            letter = "D";
        }
        else if(value == 6 || value == 18 || value == 30 || value == -6 ||
                value == -18 || value == -30 || value == -42)
        {
            letter = "D#";
        }
        else if(value == 7 || value == 19 || value == 31 || value == -5 ||
                value == -17 || value == -29 || value == -41)
        {
            letter = "E";
        }
        else if(value == 8 || value == 20 || value == 32 || value == -4 ||
                value == -16 || value == -28 || value == -40)
        {
            letter = "F";
        }
        else if(value == 9 || value == 21 || value == 33 || value == -3 ||
                value == -15 || value == -27 || value == - 39)
        {
            letter = "F#";
        }
        else if(value == 10 || value == 22 || value == 34 || value == -2 ||
                value == -14 || value == -26 || value == -38)
        {
            letter = "G";
        }
        else if(value == 11 || value == 23 || value == 35 || value == -1 ||
                value == -13 || value == -25 || value == -36)
        {
            letter = "G#";
        }
        else
        {
            letter = "invalid";
        }
        return letter;
    }

    //@return Whether or not the note is sharp
    public String getSharp()
    {
        if(value == 1 || value == 13 || value == 25 || value == 37 ||
           value == -11 || value == -23 || value == -35 || value == -47 ||
           value == 4 || value == 16 || value == 28 || value == -8 ||
           value == -20 || value == -32 || value == -44 ||
           value == 6 || value == 18 || value == 30 || value == -6 ||
           value == -18 || value == -30 || value == -42 ||
           value == 9 || value == 21 || value == 33 || value == -3 ||
           value == -15 || value == -27 || value == - 39 ||
           value == 11 || value == 23 || value == 35 || value == -1 ||
           value == -13 || value == -25 || value == -36)
        {
            sharp = "sharp";
        }
        else
        {
            sharp = "natural";
        }
        return sharp;
    }

    //@return The frequency of the note
    public double getFreq()
    {
        freq = (440 * Math.pow(2, value / 12));
        return freq;
    }
}

Solutions

Expert Solution

**Points for the Solution :
1.Submitted the solution along in given classes only , and my solution contains the starting comment - //New
2.Implemented new Class just the same as Note with name NewNote to implement comparable and soring according to that
3.In Driver class cooented out the given solution and added new static code just to understande the concept you can change that as per your requirements
Please give thumps up for the solution

Class 1 :

/*Note :: here in Comparable use NewNote(he whole class type) because here our comparision will be
with same type of class*/
class Note implements Comparable<Note>//New
{
/*Declare variables privately so they cannot be accessed from outside the class. Set the value default
to middle C and the length default to quarter.*/
private double value = 0;
private String length = "Quarter";
private String letter;
private String sharp;
private double freq;

/**
* Constructor
* @param value the note's starting value
* @param length the note's starting length
*/
public Note(double value, String length)
{
this.value = value;
this.length = length;
}
  
//Setters

//Assigns this.value to variable value
void setValue(double value)
{
this.value = value;
}

//Assigns this.length to variable length
void setLength(String x)
{
this.length = length;
}

//Getters
  
//@return The value of the note
public double getValue()
{
return value;
}
  
//@return The length of the note
public String getLength()
{
return length;
}
  
//@return The letter of the chromatic scale the value is assigned to
public String getLetter()
{
if(value == 0 || value == 12 || value == 24 || value == 36 ||
value == -12 || value == -24 || value == -36 || value == -48)
{
letter = "A";
}
else if(value == 1 || value == 13 || value == 25 || value == 37 ||
value == -11 || value == -23 || value == -35 || value == -47)   
{
letter = "A#";
}
else if(value == 2 || value == 14 || value == 26 || value == 38 ||
value == -10 || value == -22 || value == -34 || value == -46)
{
letter = "B";
}
else if(value == 3 || value == 15 || value == 27 || value == 39 ||
value == -9 || value == -21 || value == -33 || value == -45)
{
letter = "C";
}
else if(value == 4 || value == 16 || value == 28 || value == -8 ||
value == -20 || value == -32 || value == -44)
{
letter = "C#";
}
else if(value == 5 || value == 17 || value == 29 || value == -7 ||
value == -19 || value == -31 || value == -43)
{
letter = "D";
}
else if(value == 6 || value == 18 || value == 30 || value == -6 ||
value == -18 || value == -30 || value == -42)
{
letter = "D#";
}
else if(value == 7 || value == 19 || value == 31 || value == -5 ||
value == -17 || value == -29 || value == -41)
{
letter = "E";
}
else if(value == 8 || value == 20 || value == 32 || value == -4 ||
value == -16 || value == -28 || value == -40)
{
letter = "F";
}
else if(value == 9 || value == 21 || value == 33 || value == -3 ||
value == -15 || value == -27 || value == - 39)
{
letter = "F#";
}
else if(value == 10 || value == 22 || value == 34 || value == -2 ||
value == -14 || value == -26 || value == -38)
{
letter = "G";
}
else if(value == 11 || value == 23 || value == 35 || value == -1 ||
value == -13 || value == -25 || value == -36)
{
letter = "G#";
}
else
{
letter = "invalid";
}
return letter;
}

//@return Whether or not the note is sharp
public String getSharp()
{
if(value == 1 || value == 13 || value == 25 || value == 37 ||
value == -11 || value == -23 || value == -35 || value == -47 ||
value == 4 || value == 16 || value == 28 || value == -8 ||
value == -20 || value == -32 || value == -44 ||
value == 6 || value == 18 || value == 30 || value == -6 ||
value == -18 || value == -30 || value == -42 ||
value == 9 || value == 21 || value == 33 || value == -3 ||
value == -15 || value == -27 || value == - 39 ||
value == 11 || value == 23 || value == 35 || value == -1 ||
value == -13 || value == -25 || value == -36)
{
sharp = "sharp";
}
else
{
sharp = "natural";
}
return sharp;
}

//@return The frequency of the note
public double getFreq()
{
freq = (440 * Math.pow(2, value / 12));
return freq;
}


   //New:: Comparing Frequency
/*Explanation : here -1 value implies smaller and act accordingly and 1 implies greater*/
   @Override
   public int compareTo(Note ob) {
       //if current object is greater than given object it will return 1
               if(this.getFreq() > ob.getFreq()) return 1;
               //if current object is less than given object it will return -1
               else if(this.getFreq() < ob.getFreq()) return -1;
               //if current object is equals to given object it will return -1
               else return 0;
   }
}


Class 2 :

/*Note :: here in Comparable use NewNote(he whole class type) because here our comparision will be
with same type of class*/
public class NewNote implements Comparable<NewNote>
{
/*Declare variables privately so they cannot be accessed from outside the class. Set the value default
to middle C and the length default to quarter.*/
private double value = 0;
private String length = "Quarter";
private String letter;
private String sharp;
private double freq;

/**
* Constructor
* @param value the note's starting value
* @param length the note's starting length
*/
public NewNote(double value, String length)
{
this.value = value;
this.length = length;
}
  
//Setters

//Assigns this.value to variable value
void setValue(double value)
{
this.value = value;
}

//Assigns this.length to variable length
void setLength(String x)
{
this.length = length;
}

//Getters
  
//@return The value of the note
public double getValue()
{
return value;
}
  
//@return The length of the note
public String getLength()
{
return length;
}
  
//@return The letter of the chromatic scale the value is assigned to
public String getLetter()
{
if(value == 0 || value == 12 || value == 24 || value == 36 ||
value == -12 || value == -24 || value == -36 || value == -48)
{
letter = "A";
}
else if(value == 1 || value == 13 || value == 25 || value == 37 ||
value == -11 || value == -23 || value == -35 || value == -47)   
{
letter = "A#";
}
else if(value == 2 || value == 14 || value == 26 || value == 38 ||
value == -10 || value == -22 || value == -34 || value == -46)
{
letter = "B";
}
else if(value == 3 || value == 15 || value == 27 || value == 39 ||
value == -9 || value == -21 || value == -33 || value == -45)
{
letter = "C";
}
else if(value == 4 || value == 16 || value == 28 || value == -8 ||
value == -20 || value == -32 || value == -44)
{
letter = "C#";
}
else if(value == 5 || value == 17 || value == 29 || value == -7 ||
value == -19 || value == -31 || value == -43)
{
letter = "D";
}
else if(value == 6 || value == 18 || value == 30 || value == -6 ||
value == -18 || value == -30 || value == -42)
{
letter = "D#";
}
else if(value == 7 || value == 19 || value == 31 || value == -5 ||
value == -17 || value == -29 || value == -41)
{
letter = "E";
}
else if(value == 8 || value == 20 || value == 32 || value == -4 ||
value == -16 || value == -28 || value == -40)
{
letter = "F";
}
else if(value == 9 || value == 21 || value == 33 || value == -3 ||
value == -15 || value == -27 || value == - 39)
{
letter = "F#";
}
else if(value == 10 || value == 22 || value == 34 || value == -2 ||
value == -14 || value == -26 || value == -38)
{
letter = "G";
}
else if(value == 11 || value == 23 || value == 35 || value == -1 ||
value == -13 || value == -25 || value == -36)
{
letter = "G#";
}
else
{
letter = "invalid";
}
return letter;
}

//@return Whether or not the note is sharp
public String getSharp()
{
if(value == 1 || value == 13 || value == 25 || value == 37 ||
value == -11 || value == -23 || value == -35 || value == -47 ||
value == 4 || value == 16 || value == 28 || value == -8 ||
value == -20 || value == -32 || value == -44 ||
value == 6 || value == 18 || value == 30 || value == -6 ||
value == -18 || value == -30 || value == -42 ||
value == 9 || value == 21 || value == 33 || value == -3 ||
value == -15 || value == -27 || value == - 39 ||
value == 11 || value == 23 || value == 35 || value == -1 ||
value == -13 || value == -25 || value == -36)
{
sharp = "sharp";
}
else
{
sharp = "natural";
}
return sharp;
}

//@return The frequency of the note
public double getFreq()
{
freq = (440 * Math.pow(2, value / 12));
return freq;
}

  
   //New:: Comparing length and then Frequency
/*Explanation : here -1 value implies smaller and act accordingly and 1 implies greater
based on this values it will sort In ascending and descending order*/
   @Override
   public int compareTo(NewNote ob) {
       //Firstly we have to compare objects with the length
       //Since, length is string we will us COmpare to to comapre lengths
       if(this.length.length() > ob.length.length()) return 1;
       else if(this.length.length() < ob.length.length()) return -1;
       else {
           //Now if lengths are similar we need sort them further by frequency
           if(this.getFreq() > ob.getFreq()) return 1;
           else if(this.getFreq() < ob.getFreq()) return -1;
           else return 0;
       }
   }

}

Class 3:

import java.io.*;
import java.math.*;
import java.util.ArrayList;
import java.util.Collections;

public class NoteTester
{
public static void main(String[] args) throws IOException
{
   /**Note : Code for creating few Objects and compare them
   * I am creating objects statically just to save the time and better understanding of implementation of comaparable interface
   * ou can change the Code to ask for the input from the user.
   * */
  
Note demoNote1 = new Note(20, "whole");
Note demoNote2 = new Note(34, "quarter");
Note demoNote3 = new Note(33, "sixteenth");
Note demoNote4 = new Note(28, "Invalid");
  
//1st. Comparing these 5 objects using Note class frequency implementation
  
//Choosing objects randomly
if(demoNote1.compareTo(demoNote4) ==1)System.out.println("demoNote1 is greater than demoNote4");
else if(demoNote1.compareTo(demoNote4) ==-1)System.out.println("demoNote1 is less than demoNote4");
else System.out.println("demoNote1 is equals to demoNote4");

if(demoNote2.compareTo(demoNote3) ==1)System.out.println("demoNote2 is greater than demoNote3");
else if(demoNote2.compareTo(demoNote3) ==-1)System.out.println("demoNote2 is less than demoNote3");
else System.out.println("demoNote2 is equals to demoNote3");
  
  
//2nd. Creating a list of objects and sorting them based on length an further by frequency
//here choosing class NewNote - implements comparable to compare length and frequency
  
NewNote newNote1 = new NewNote(20, "whole");
NewNote newNote2 = new NewNote(34, "quarter");
NewNote newNote3 = new NewNote(33, "quarter");
NewNote newNote4 = new NewNote(28, "Invalid");
NewNote newNote5 = new NewNote(38, "eighth");
  
ArrayList<NewNote> listOfNote = new ArrayList<>();
listOfNote.add(newNote1);
listOfNote.add(newNote2);
listOfNote.add(newNote3);
listOfNote.add(newNote4);
listOfNote.add(newNote5);
  
Collections.sort(listOfNote);//Since we have implemented Comparable it will automatically sort according to that
System.out.println("Sorted List : " );
for(NewNote ob : listOfNote) {
   System.out.print("Your value was " + ob.getValue() + ", your length was a(n) " + ob + " note, your note's \nletter value was " + ob.getLetter() + " which is a " +
ob.getSharp() + " note. \nThe frequency of your" +
" note is " + ob.getFreq() + " hz." );
  
}
  
   /*
//Declare variables to hold user input
double noteValue;
String noteLength;
int tempLength;
  
//Create BufferedReader object wrapped with InputStreamReader to hold user input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

//Retrieve starting value from user
System.out.println("Please enter a value for your note\n"+
"between -48 and 39:");
noteValue = Double.parseDouble(br.readLine());
  
//While loop ensures a valid number
while(noteValue < -48 || noteValue > 39)
{
System.out.println("Please enter a valid value for your note\n"+
"between -48 and 39:");
noteValue = Double.parseDouble(br.readLine());
}
  
//Retrieve starting length from user
System.out.println("Please enter the number between 1-5\n" +
"you would like to represent\n" +
"the length of your note.\n"+
"1. Whole\n" +
"2. Half\n" +
"3. Quarter\n" +
"4. Eighth\n" +
"5. Sixteenth");
tempLength = Integer.parseInt(br.readLine());
  
//While loop ensures a valid length
while(tempLength > 5 || tempLength < 1)
{
System.out.println("Please enter the number between 1-5\n" +
"you would like to represent\n" +
"the length of your note.\n"+
"1. Whole\n" +
"2. Half\n" +
"3. Quarter\n" +
"4. Eighth\n" +
"5. Sixteenth");
tempLength = Integer.parseInt(br.readLine());
}

//If else statement assigns strings to the user's choice of int
if(tempLength == 1)
{
noteLength = "whole";
}
else if(tempLength == 2)
{
noteLength = "half";
}
else if(tempLength == 3)
{
noteLength = "quarter";
}
else if(tempLength == 4)
{
noteLength = "eighth";
}
else if(tempLength == 5)
{
noteLength = "sixteenth";
}
else
{
noteLength = "Invalid";
}
  
//Create demoNote object
Note demoNote = new Note(noteValue, noteLength);
  
//Print user the information about their note
System.out.print("Your value was " + demoNote.getValue() + ", your length was a(n) " + demoNote.getLength()
+ " note, your note's \nletter value was " + demoNote.getLetter() + " which is a " +
demoNote.getSharp() + " note. \nThe frequency of your" +
" note is " + demoNote.getFreq() + " hz." );*/
}
}


Related Solutions

Using the Note class and the Quadratic classes you have already developed, make each one Comparable....
Using the Note class and the Quadratic classes you have already developed, make each one Comparable. A Note is larger than another note if it is higher in frequency. A Quadratic is bigger than another Quadratic if it opens faster. 2. Write a driver for each (Note and for Quadratic). In the driver program create a few objects and compare them . then create a list of those objects and sort them. 3. Rewrite the Note class so that a...
Write a program to have a Car class which is comparable. Make the Car class comparable...
Write a program to have a Car class which is comparable. Make the Car class comparable and use speed to compare cars. Write a method in Main class that finds the minimum of 4 things (generically). Create 4 cars and pass the cars to the minimum method and find the smallest car. Have a toString method for the Car class. The main program should be able to find out how many cars are created so far by calling a static...
lowering the pH of the solution inside the battery will: a. make E larger than E...
lowering the pH of the solution inside the battery will: a. make E larger than E b. make E smaller than E c. have no effect on E vs. E d. make the battery last longer
Using the Comparable Interface Write a class Compare3 that provides a static method largest. Method largest...
Using the Comparable Interface Write a class Compare3 that provides a static method largest. Method largest should take three Comparable parameters and return the largest of the three (so its return type will also be Comparable). Recall that method compareTo is part of the Comparable interface, so largest can use the compareTo method of its parameters to compare them. Write a class Comparisons whose main method tests your largest method above. First prompt the user for and read in three...
4) Define an abstract class Name Java class that implements interface Comparable   
4) Define an abstract class Name Java class that implements interface Comparable   
A monopoly is likely —- and —- than a comparable perfectly competirive form
A monopoly is likely —- and —- than a comparable perfectly competirive form
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE-...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE- class-level attribute initialized to 1000 -acctID: int -bank: String -acctType: String (ex: checking, savings) -balance: Float METHODS: <<Constructor>>Account(id:int, bank: String, type:String, bal:float) +getAcctID(void): int                        NOTE: retrieving a CLASS-LEVEL attribute! -setAcctID(newID: int): void           NOTE: PRIVATE method +getBank(void): String +setBank(newBank: String): void +getBalance(void): float +setBalance(newBal: float): void +str(void): String NOTE: Description: prints the information for the account one item per line. For example: Account #:        ...
Using this BubbleSort implementation in Java: public class BubbleSort<T extends Comparable<T>> {    private static <T...
Using this BubbleSort implementation in Java: public class BubbleSort<T extends Comparable<T>> {    private static <T extends Comparable<T>>    void swap(T[] data, int index1, int index2)    {       T temp = data[index1];       data[index1] = data[index2];       data[index2] = temp;    }    public void sort(T[] data)    {       int position, scan;       for (position = data.length - 1; position >= 0; position--)       {          for (scan = 0; scan <= position - 1; scan++)          {...
1. when you create a class by making it inherit from another class, the new class...
1. when you create a class by making it inherit from another class, the new class automatically, contains the data fields and _____ of the original class a. fonts b. methods c. class names d. arrays 2. if a programming language does not support ________, the language is not considered object oriented a. syntax b. applets c. loops d. polymorphism 3. when you create a class and do not provide a ______, java automatically supplies you with a default one...
1. Design a Java CartesianPoint class for a Cartesian Point which implements both cloneable and comparable...
1. Design a Java CartesianPoint class for a Cartesian Point which implements both cloneable and comparable interfaces The class should have the following private member variables: • the x value of a point: int • the y value of a point: int and the class should have the following public member functions: • default constructor which initializes the point to the origin of the Cartesian coordinate system • explicit constructor which initializes the point to a pair of given value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT