Please Answer all the questions correctly and complete. Thanks. Identify the err
ID: 3666402 • Letter: P
Question
Please Answer all the questions correctly and complete. Thanks.
Identify the errors in the operator functions in Exercises a) through i) and suggest a solution in each case.
a) void Pixel::operator+(Pixel &p1, Pixel &p2, int a)
{ p1.x = p1.x + a;
p1.y = p1.y + a;
p2.x = p2.x + a;
p2.y = p2.y + a; }
b) Pixel Pixel::operator++(Pixel &p1, Pixel &p2
{ p1.x++;
p1.y++;
p2.x++;
p2.y++; }
c) // function prototype friend void &operator=(Impedance &v1, Impedance &v2);
d) // function prototype friend int &operator?(Resistor &r1, Resistor &r2) const;
e) Pixel Pixel::operator-(int a, Pixel &p)
{ p.x = a - p.x;
p.y = a - p.y;
return *this; }
f) istream &operator<<(istream &ist, impedance imp)
{ cout << "Enter resistance"; ist >> imp.r;
cout << "Enter reactance"; ist >> imp.x; }
g) Pixel Pixel::operator--(int)
{ x--;
y--;
return *this; }
h) ACurrent ACurrent::operator*(double r) const
{ real = real * r;
imag = imag * r;
return *this; }
i) // function prototype
friend void operator/(ACurrent &C);
2. Create a Circuit class that uses three data members to store the resistance, input voltage, and current of a circuit. This class should be defined as an abstract base class with pure virtual function. The Circuit class also has the following member functions:
a. A constructor with arguments to initialize data members.
b. A function that computes and returns current (input voltage divided by resistance)
c. A function that displays values of resistance, input voltage, and current.
d. A pure virtual function, which is redefined in the derived class to compute the efficiency of a specific circuit (in this case, the Amplifier class). Create an Amplifier class that inherits all members from Circuit except constructor (cannot be inherited). In addition to the inherited members, the Amplifier class has some specific members:
A data member gain
A constructor with arguments to initialize data members
A function that computes and returns an output voltage (input voltage multiplied by gain)
A function that returns gain
A function that computes and calculates efficiency (the output power, the input power)
Design the main ( ) function to instantiate an object of the Amplifier class and then use this object to call all member functions. Note: Power (Watts) = V * I = V ^2 / R = I^2 * R, where I is electric current in amperes and V is electric potential or voltage in volts = 100 * (Pout / Pin) , where is the efficiency in percent (%), Pin is the input power consumption in watts (W), Pout is the output power or actual work in watts (W).
Explanation / Answer
abstract public class Circuit
{
int resistance,inputVol,current;
public Circuit(int res, int vol, int cur)
{
resistance= res;
inputVol=vol;
current=cur;
}
int calculateCurrent(int vol, int res)
{
current = vol/res;
return current;
}
void display()
{
System.out.println("The resistance="+resistance);
System.out.println("The input voltage="+inputVol);
System.out.println("The current="+current);
}
abstract int calEffieciency();
} // end of the class Circuit
public class Amplifier extends Circuit
{
int gain;
public Amplifier(int res, int vol, int cur, int g)
{
resistance= res;
inputVol=vol;
current=cur;
gain=g;
}
int calculateOutVol(int vol, int gain)
{
int outputVol;
outputVol= vol*gain;
return outputVol;
}
void displayGain()
{
Systen.out.println("The gain="+gain);
}
int calEfficiency(int outputPow, inputPow)
{
int eff;
eff=100*(outputPow/inputPow);
return eff;
}
} // end of the Amplifier class
public class Test
{
public static void main(String []args)
{
int vol, res, gain;
int powIn, powOut;
Scanner s= new Scanner(System.in);
System.out.println("enter input voltage");
vol=s.nextInt();
System.out.println("enter resistance");
res= s.nextInt();
System.out.println("enter gain");
gain =s.nextInt();
Circuit obj= new Amplifier();
obj.calculateCurrent(vol,res);
obj.display();
obj.displayGain();
powIn= (pow(vol,2))/res;
int op=obj.calculateOutVol(vol,gain);
powOut= (pow(op,2))/res;
System.out.println("the efficiency of circuit is:"+obj.calEfficiency(powOut,powIn);
} // end of the main method
} // end of the Test class
solutions to the errors:
1) void Pixel::operator+(Pixel &p1, Pixel &p2, int a)
{ p1.x = p1.x + a;
p1.y = p1.y + a;
p2.x = p2.x + a;
p2.y = p2.y + a; }
Here name of the method should not end with specialcharacter..So, the solution will be:
void Pixel::operatorPlus(Pixel &p1, Pixel &p2, int a)
{ p1.x = p1.x + a;
p1.y = p1.y + a;
p2.x = p2.x + a;
p2.y = p2.y + a; }
2) Pixel Pixel::operator++(Pixel &p1, Pixel &p2
{ p1.x++;
p1.y++;
p2.x++;
p2.y++; }
Here, again the same error as previous one with the method name. In addition to this one more error of incomplete paranthesis after arguments. and the return type must be void as it is not returning anything
void Pixel::operatorIncrement(Pixel &p1, Pixel &p2)
{ p1.x++;
p1.y++;
p2.x++;
p2.y++; }
3) friend void &operator=(Impedance &v1, Impedance &v2);
The function name cannot start with &..the solution to this is:
@override
friend void operatorEquality(Impedance &v1, Impedence &v2);
4) // function prototype friend int &operator?(Resistor &r1, Resistor &r2) const;
Same error as previous one..The solution is:
@override
friend int operatorIfElse(Resistor &r1, Resistor &r2) const;
5) Pixel Pixel::operator-(int a, Pixel &p)
{ p.x = a - p.x;
p.y = a - p.y;
return *this; }
Here again method name should not end with special character and method should return only this keyword:
Pixel Pixel::operatorMinus(int a, Pixel &p)
{ p.x = a - p.x;
p.y = a - p.y;
return this; }
6) istream &operator<<(istream &ist, impedance imp)
{ cout << "Enter resistance"; ist >> imp.r;
cout << "Enter reactance"; ist >> imp.x; }
Here, again the error exists with the method name and the method must have return type as void as it is only displaying output and not returning anything:So, the solution will be:
@override
void istream:: operatorLeftShift(istream &ist, Impedance imp)
{ cout << "Enter resistance"; (ist << imp.r);
cout << "Enter reactance"; ist << imp.x; }
7) Pixel Pixel::operator--(int)
{ x--;
y--;
return *this; }
Int his case, again the error exists with method name that is ending with special character and the arguments to be passed are 2 while they have passed a single argument to the function, and the method should return this:The solution will be:
Pixel Pixel::operatorDecrement(int x, int y)
{ x--;
y--;
return this; }
8) ACurrent ACurrent::operator*(double r) const
{ real = real * r;
imag = imag * r;
return *this; }
Eeeor with the method name and it should return only this..and the two data members real and imag must be declared previously with the return type as double:
double real=15; // you can put any value here.. I've just created it for demonstrartion.
double imag=10;
ACurrent ACurrent::operatorMult(double r) const
{ real = real * r;
imag = imag * r;
return this; }
9) // function prototype
friend void operator/(ACurrent &C);
again the error with the method Name:
friend void operatorDiv(ACurrent &C);