Question

In: Computer Science

C++ QUESTION ABOUT FIXING CODE AND RUNNING TESTS: class SpeakerSystem { public: void vibrateSpeakerCones(unsigned signal) const...

C++ QUESTION ABOUT FIXING CODE AND RUNNING TESTS:

class SpeakerSystem {
public:
void vibrateSpeakerCones(unsigned signal) const {

cout << "Playing: " << signal << "Hz sound..." << endl;
cout << "Buzz, buzzy, buzzer, bzap!!!\n";
}
};

class Amplifier {
public:
void attachSpeakers(const SpeakerSystem& spkrs)
{
if(attachedSpeakers)
cout << "already have speakers attached!\n";
else
attachedSpeakers = &spkrs;
}
  
void detachSpeakers() { // should there be an "error" message if not attached?
attachedSpeakers = nullptr;
}
  
void playMusic( ) const {
if (attachedSpeakers)
attachedSpeakers -> vibrateSpeakerCones(440);
else
cout << "No speakers attached\n";
}
private:
SpeakerSystem* attachedSpeakers = nullptr;
};

QUESTION:

In the code above, you will find a problem compiling. What is it? Note what the method attachSpeakers is trying to do with the address of spkrs. Why is this a problem?

What do you think is the right way to fix it? There is more than one way and how you want to fix it depends on what you think the relationship will be between the amplifier and the speaker system. Know why you choose to pick one solution or the other is at least as important as making a change that gets the code working.

Create some SpeakerSystem and Amplifier variables.
Test by playing music on various speaker system configurations connected at various times to different speaker systems and trying to play some amps without speakers and by attaching another set of speakers to an already complete system (complete here means has speakers already attached - we're skipping the part about audio input devised.)
How do you cause a speaker system to become attached to an amplifier?

In a one-way association, the associated object (SpeakerSystem here) does not need to know anything about or communicate with the object it is associated with (Amplifier here).
If either of these situations is needed, then two-way association would be required (one-way would be the wrong design decision).
Are there any issues that might need to be considered that a one-way association cannot deal with in this problem?

Solutions

Expert Solution

#include <iostream>
class SpeakerSystem 
{
  public:
  void vibrateSpeakerCones(unsigned signal) const {
    std::cout << "Playing: " << signal << "Hz sound..." << std::endl;
    std::cout << "Buzz, buzzy, buzzer, bzap!!!\n";
  }
};

class Amplifier {
  public:
  Amplifier( ) : attachedSpeakers( NULL ) {}
  void attachSpeakers(const SpeakerSystem& spkrs)
  {
  if(attachedSpeakers)
    std::cout << "already have speakers attached!\n";
  else
    attachedSpeakers = &spkrs;
  }
  
  void detachSpeakers() { 
    attachedSpeakers = nullptr;
  }
   // should there be an "error" message if not attached?

  void playMusic( ) const {
    if (attachedSpeakers)
      attachedSpeakers -> vibrateSpeakerCones(440);
    else
      std::cout << "No speakers attached\n";
  }
 
  private:
    //SpeakerSystem* attachedSpeakers = nullptr;
    const SpeakerSystem* attachedSpeakers;
};

//main function
int main()
{
  //create object spk for class SpeakerSystem
  SpeakerSystem spk;
  spk.vibrateSpeakerCones(11);

  //create object amp for class Amplifier
  Amplifier amp;
  std::cout << "\nattachSpeakers and playMusic call :"<<std::endl;
  amp.attachSpeakers(spk);
  amp.playMusic();

   std::cout << "\ndetachSpeakers and playMusic call :"<<std::endl;
  amp.detachSpeakers();
  amp.playMusic();
  return 0;
}

output ::

I did some changes you can see in above code as well as given output methods calls from class(refer screenshot ).


Related Solutions

CODE: C# using System; public static class Lab6 { public static void Main() { // declare...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare variables int hrsWrked; double ratePay, taxRate, grossPay, netPay=0; string lastName; // enter the employee's last name Console.Write("Enter the last name of the employee => "); lastName = Console.ReadLine(); // enter (and validate) the number of hours worked (positive number) do { Console.Write("Enter the number of hours worked (> 0) => "); hrsWrked = Convert.ToInt32(Console.ReadLine()); } while (hrsWrked < 0); // enter (and validate) the...
C++ programming question class Address { public: Address(const std::string& street, const std::string& city, const std::string& state,...
C++ programming question class Address { public: Address(const std::string& street, const std::string& city, const std::string& state, const std::string& zip) : StreetNumber(street), CityName(city), StateName(state), ZipCode(zip) {} std::string GetStreetNumber() const { return StreetNumber; } void SetStreetNumber(const std::string& street) { StreetNumber = street; } std::string GetCity() const { return CityName; } void SetCity(const std::string& city) { CityName = city; } std::string GetState() const { return StateName; } void SetState(const std::string& state) { StateName = state; } std::string GetZipCode() const { return ZipCode; }...
Given the root C++ code: void sort() {    const int N = 10;    int...
Given the root C++ code: void sort() {    const int N = 10;    int x[N];    for(int i = 0; i < N; i++)    {        x[i] = 1 + gRandom-> Rndm() * 10;        cout<<x[i]<<" "; }    cout<<endl;    int t;       for(int i = 0; i < N; i++)    {    for(int j = i+1; j < N; j++)    {        if(x[j] < x[i])        {   ...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
C++ Class involving Set intersection The goal is to overload the function: void Bag::operator/=(const Bag& a_bag)...
C++ Class involving Set intersection The goal is to overload the function: void Bag::operator/=(const Bag& a_bag) // Bag<int> bag1 = 1,2,3,4 //Bag<int> bag2 = 2,5,6,7 bag1+=bag2; bag1.display() should return 2 //Since type is void, you should not be returning any array but change the bag itself. #include <iostream> #include <string> #include <vector> using namespace std; template<class T> class Bag { public: Bag(); int getCurrentSize() const; bool isEmpty() const; bool add(const T& new_entry); bool remove(const T& an_entry); /** @post item_count_ ==...
Create a new Java file, containing this code public class DataStatsUser { public static void main...
Create a new Java file, containing this code public class DataStatsUser { public static void main (String[] args) { DataStats d = new DataStats(6); d.append(1.1); d.append(2.1); d.append(3.1); System.out.println("final so far is: " + d.mean()); d.append(4.1); d.append(5.1); d.append(6.1); System.out.println("final mean is: " + d.mean()); } } This code depends on a class called DataStats, with the following API: public class DataStats { public DataStats(int N) { } // set up an array (to accept up to N doubles) and other member...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {   ...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {    Scanner console = new Scanner(System.in);    double radius; double height; System.out.println("This program can calculate "+ "the area of a rectangle, the area "+ "of a circle, or volume of a cylinder."); System.out.println("To run the program enter: "); System.out.println("1: To find the area of rectangle."); System.out.println("2: To find the area of a circle."); System.out.println("3: To find the volume of a cylinder."); System.out.println("-1: To terminate the...
Fix the following java code package running; public class Run {    public double distance; //in...
Fix the following java code package running; public class Run {    public double distance; //in kms    public int time; //in seconds    public Run prev;    public Run next;    //DO NOT MODIFY - Parameterized constructor    public Run(double d, int t) {        distance = Math.max(0, d);        time = Math.max(1, t);    }       //DO NOT MODIFY - Copy Constructor to create an instance copy    //NOTE: Only the data section should be...
C++ Class involving union. The goal is to overload the function: void Bag<T>::operator+=(const Bag<T>& a_bag) //...
C++ Class involving union. The goal is to overload the function: void Bag<T>::operator+=(const Bag<T>& a_bag) // The union of two sets A and B is the set of elements which are in A, in B, or in both A and B. For instance, Bag<int> bag1 = 1,2,3 and Bag<int> bag2 = 3,4,5 then bag1+=bag2 should return 1,2,3,4,5. //Since type is void, it should not return an array. #include <iostream> #include <string> #include <vector> using namespace std; template<class T> class Bag...
Consider the following code: public class Bay extends Lake { public void method1() { System.out.println("Bay 1");...
Consider the following code: public class Bay extends Lake { public void method1() { System.out.println("Bay 1"); super.method2(); } public void method2() { System.out.println("Bay 2"); } } //********************************* class Pond { public void method2() { System.out.println("Pond 2"); } } //************************** class Ocean extends Bay { public void method2() { System.out.println("Ocean 2"); } } //********************************* class Lake extends Pond { public void method3() { System.out.println("Lake 3"); method2(); } } //**************************** class Driver { public static void main(String[] args) { Object var4 =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT