Virtual Trebuchet 2.0. VirtualTrebuchet is a web based trebuchet simulator that will allow you to quickly evaluate different trebuchet configurations. To begin, enter the parameters of your trebuchet in the input boxes on the left. Next, press the submit button located under the inputs. Finally, watch your trebuchet go.
Trebuchet Project
|
When a speaker is connected to an amplifier using a speaker wire, a small portion of the voltage provided by the amplifier is dropped by the speaker wire and the remainder of the voltage is delivered to the speaker. Given the voltage output by an amplifier, it is possible to calculate the power dissipated by the wires and speakers in a system consisting of one amplifier, two speakers (Left and Right) and two wires (Left and Right).
The amount of voltage dropped by a wire is directly proportional to the resistance of the wire. The resistance of a speaker wire depends upon the length of the wire, its cross sectional area, and a number of other factors. For the purpose of this assignment, three gauges of speaker wire will be used. The resistivity of these speaker wires specified in units of Ohms/km are provided for you.
Goal:
In this assignment, you will complete the development of an object-oriented C# program to calculate the power dissipated by speakers and wires in a system consisting of one amplifier, two speakers (Left and Right) and two wires (Left and Right). Your program will consist of four object classes (Speaker, SpeakerWire, Amplifier, and TestProgram). A portion of the source code for these classes has been provided on UW-ACE for your convenience. The program should prompt a user for the amplifier voltage, the resistance of the two (identical) speakers, and the lengths of the left and right wires. Using this information, a static method called byt eh Main() program entry point in the TestProgram class will calculate the combined power dissipated by the speakers and the combined power dissipated by the wires for three different gauges of wire (12 AWG, 16 AWG, 20AWG) and display in the format suggested.
Additional Comments:
Your program needs to verify that the user specified inputs lie within the specified ranges. If the user enters a non-numeric value, the program is allowed to crash (terminated with an exception). However, if the user enters a numeric value, the program should verify that the input is within the desired range. If it is not within the desired range, the program should prompt the user for a new value and loop until the user enters a numeric value within the expected range or the user enters a non-numeris value causing the program to crash. More complex error handling approaches will be examined in future assignments.
Source Code:
/* ------------------------------------------------------------------------- ||
|| Department of Electrical and Computer Engineering ||
|| University of Waterloo ||
|| ||
|| Student Name: Nate Wennyk ||
|| UWDIR Userid: ndwennyk ||
|| ||
|| Assignment: Programming Assignment #2 ||
|| Submission Date: October 6th, 2008 ||
|| ||
|| I declare that, other than the acknowledgements listed below, ||
|| this program is my original work. ||
|| ||
|| Acknowledgements: ||
|| The formulas used were found in the assignment outline. ||
|| This program was coded by me, excluding the code provided on UW-ACE. ||
|| ------------------------------------------------------------------------- ||
*/
|| Department of Electrical and Computer Engineering ||
|| University of Waterloo ||
|| ||
|| Student Name: Nate Wennyk ||
|| UWDIR Userid: ndwennyk ||
|| ||
|| Assignment: Programming Assignment #2 ||
|| Submission Date: October 6th, 2008 ||
|| ||
|| I declare that, other than the acknowledgements listed below, ||
|| this program is my original work. ||
|| ||
|| Acknowledgements: ||
|| The formulas used were found in the assignment outline. ||
|| This program was coded by me, excluding the code provided on UW-ACE. ||
|| ------------------------------------------------------------------------- ||
*/
using System;
/*
Creates an enumerated type where left = 0 and right = 1.
*/
enum AVPosition
{
left, // At position 0.
right // At position 1.
};
Creates an enumerated type where left = 0 and right = 1.
*/
enum AVPosition
{
left, // At position 0.
right // At position 1.
};
/*
Holds the reference to the speaker's resistance, in Ohms.
*/
class Speaker
{
private float resistance; // Measured in Ohms.
AVPosition side; // References the side that the speaker is on.
Holds the reference to the speaker's resistance, in Ohms.
*/
class Speaker
{
private float resistance; // Measured in Ohms.
AVPosition side; // References the side that the speaker is on.
// Default constructor for the class Speaker that requires 2 values; the
// first being the resistance of the speaker (in Ohms), the second being
// the side that the speaker is on (either left or right).
public Speaker(float pResistance, AVPosition pSide)
{
resistance = pResistance;
side = pSide;
}
// Provides access to the value of the speaker's resistance only.
public float Resistance
{
get
{
return resistance; // In Ohms.
}
set
{
// Sets nothing.
}
}
}
// first being the resistance of the speaker (in Ohms), the second being
// the side that the speaker is on (either left or right).
public Speaker(float pResistance, AVPosition pSide)
{
resistance = pResistance;
side = pSide;
}
// Provides access to the value of the speaker's resistance only.
public float Resistance
{
get
{
return resistance; // In Ohms.
}
set
{
// Sets nothing.
}
}
}
/*
Calculates the resistance of the wire.
*/
class SpeakerWire
{
private int gauge; // Measured in AWg.
private float length; // Measured in metres.
AVPosition side; // Initializes a variable of type AVPosition.
private float resistivity; // Measured in Ohms/m.
private float resistance; // Measured in Ohms.
private const float resAwg12km = 5.20f; // Measured in Ohms/km.
private const float resAwg16km = 13.17f; // Measured in Ohms/km.
private const float resAwg20km = 33.31f; // Measured in Ohms/km.
Calculates the resistance of the wire.
*/
class SpeakerWire
{
private int gauge; // Measured in AWg.
private float length; // Measured in metres.
AVPosition side; // Initializes a variable of type AVPosition.
private float resistivity; // Measured in Ohms/m.
private float resistance; // Measured in Ohms.
private const float resAwg12km = 5.20f; // Measured in Ohms/km.
private const float resAwg16km = 13.17f; // Measured in Ohms/km.
private const float resAwg20km = 33.31f; // Measured in Ohms/km.
// Default constructor fot the class SpeakerWire that requires 3 values;
// the first being the gauge of the wire (in Awg), the second being the
// length of the wire (in metres), the third being the side that the wire
// is on (left or right).
public SpeakerWire(int pGauge, float pLength, AVPosition pSide)
{
gauge = pGauge; // Assigns the gauge of the wire to the gauge variable.
length = pLength; // Assigns the length to the length variable.
side = pSide; // Assigns the side to the side variable.
switch (gauge)
{
case 12:
// Assigns the constant for 12 Awg wire to resistivity.
resistivity = resAwg12km/1000f;
break;
case 16:
// Assigns the constant for 16 Awg wire to resistivity.
resistivity = resAwg16km/1000f;
break;
case 20:
// Assigns the constant for 20 Awg wire to resistivity.
resistivity = resAwg20km/1000f;
break;
default:
// Displays an error message since the gauge is not 12, 16, or 20.
Console.WriteLine('An error has occured.');
break;
}
// Assigns the product of resisitivity with length to resistance.
resistance = resistivity * length;
}
// the first being the gauge of the wire (in Awg), the second being the
// length of the wire (in metres), the third being the side that the wire
// is on (left or right).
public SpeakerWire(int pGauge, float pLength, AVPosition pSide)
{
gauge = pGauge; // Assigns the gauge of the wire to the gauge variable.
length = pLength; // Assigns the length to the length variable.
side = pSide; // Assigns the side to the side variable.
switch (gauge)
{
case 12:
// Assigns the constant for 12 Awg wire to resistivity.
resistivity = resAwg12km/1000f;
break;
case 16:
// Assigns the constant for 16 Awg wire to resistivity.
resistivity = resAwg16km/1000f;
break;
case 20:
// Assigns the constant for 20 Awg wire to resistivity.
resistivity = resAwg20km/1000f;
break;
default:
// Displays an error message since the gauge is not 12, 16, or 20.
Console.WriteLine('An error has occured.');
break;
}
// Assigns the product of resisitivity with length to resistance.
resistance = resistivity * length;
}
// Provides access to the value of the SpeakerWire's resistance only.
public float Resistance
{
get
{
return resistance; // In Ohms.
}
set
{
// Sets nothing.
}
}
}
/*
Holds reference to the Amplifier's voltage, in volts.
*/
public class Amplifier
{
private float voltage; // Measured in volts.
public float Resistance
{
get
{
return resistance; // In Ohms.
}
set
{
// Sets nothing.
}
}
}
/*
Holds reference to the Amplifier's voltage, in volts.
*/
public class Amplifier
{
private float voltage; // Measured in volts.
// Default constructor for the class Amplifier the requires 1 value, the
// voltage of the Amplifier (in volts).
public Amplifier(float pVoltage)
{
voltage = pVoltage; // Assigns the voltage to the voltage variable.
}
// voltage of the Amplifier (in volts).
public Amplifier(float pVoltage)
{
voltage = pVoltage; // Assigns the voltage to the voltage variable.
}
// Provides access to the value of the Amplifier's voltage only.
public float Voltage
{
get
{
return voltage; // In volts.
}
set
{
// Sets nothing.
}
}
}
/*
Calculates the speaker output and the wire power loss (in watts) for 12, 16,
and 20 Awg wire from the user's inputs. The user will input the amplifier's
voltage, the speaker's resistance, the length of the left and right wires.
*/
class TestProgram
{
// The entry point of the program.
static void Main()
{
float amplifierVoltage; // Measured in volts.
float speakerResistance; // Measured in Ohms.
float wireLengthLeft; // Measured in metres.
float wireLengthRight; // Measured in metres.
public float Voltage
{
get
{
return voltage; // In volts.
}
set
{
// Sets nothing.
}
}
}
/*
Calculates the speaker output and the wire power loss (in watts) for 12, 16,
and 20 Awg wire from the user's inputs. The user will input the amplifier's
voltage, the speaker's resistance, the length of the left and right wires.
*/
class TestProgram
{
// The entry point of the program.
static void Main()
{
float amplifierVoltage; // Measured in volts.
float speakerResistance; // Measured in Ohms.
float wireLengthLeft; // Measured in metres.
float wireLengthRight; // Measured in metres.
Console.Write('nAmplifier System Power Calculatorn');
// Assigns the user's input for amplifier voltage to amplifierVoltage.
amplifierVoltage = GetAmplifierVoltage(0.0f, 1000.0f);
// Assigns the user's input for amplifier voltage to amplifierVoltage.
amplifierVoltage = GetAmplifierVoltage(0.0f, 1000.0f);
// Assigns the user' input for speaker resistance to speakerResistance.
speakerResistance = GetSpeakerResistance(0.0f, 20.0f);
// Assigns the user's input for length of left wire to wireLengthLeft.
wireLengthLeft = GetWireLength(0.0f, 100.0f, AVPosition.left);
//Assigns the user's input for length of right wire to wireLengthRight.
wireLengthRight = GetWireLength(0.0f, 100.0f, AVPosition.right);
speakerResistance = GetSpeakerResistance(0.0f, 20.0f);
// Assigns the user's input for length of left wire to wireLengthLeft.
wireLengthLeft = GetWireLength(0.0f, 100.0f, AVPosition.left);
//Assigns the user's input for length of right wire to wireLengthRight.
wireLengthRight = GetWireLength(0.0f, 100.0f, AVPosition.right);
// Calulates the speaker output and wire power loss from the user's
// input for 12, 16, and 20 Awg wire, then displays the result on the
// screen.
CalculateAndDisplayPower(amplifierVoltage, speakerResistance,
wireLengthLeft, wireLengthRight);
}
// input for 12, 16, and 20 Awg wire, then displays the result on the
// screen.
CalculateAndDisplayPower(amplifierVoltage, speakerResistance,
wireLengthLeft, wireLengthRight);
}
// Receives the user's input for the amplifier's voltage, and returns the
// value.
static float GetAmplifierVoltage(float min, float max)
{
float voltage; // Measured in volts.
// value.
static float GetAmplifierVoltage(float min, float max)
{
float voltage; // Measured in volts.
do
{
Console.WriteLine('nEnter the amplifier voltage in Volts:');
Console.WriteLine(
'(Real quantity -> {0} V <= voltage <= {1} V)',
min, max);
// Assigns the input to the voltage variable.
voltage = float.Parse(Console.ReadLine());
if((voltage < min) || (voltage > max))
{
Console.WriteLine('nNumber exceeds range, please try again.');
}
// Checks to see if the input is within range.
} while ((voltage < min) || (voltage > max));
//Declares a new instance of class Amplifier with the amplifier voltage.
Amplifier amplifier = new Amplifier(voltage);
{
Console.WriteLine('nEnter the amplifier voltage in Volts:');
Console.WriteLine(
'(Real quantity -> {0} V <= voltage <= {1} V)',
min, max);
// Assigns the input to the voltage variable.
voltage = float.Parse(Console.ReadLine());
if((voltage < min) || (voltage > max))
{
Console.WriteLine('nNumber exceeds range, please try again.');
}
// Checks to see if the input is within range.
} while ((voltage < min) || (voltage > max));
//Declares a new instance of class Amplifier with the amplifier voltage.
Amplifier amplifier = new Amplifier(voltage);
return amplifier.Voltage; // Returns the voltage, in volts.
}
}
// Recieves the user's input for the speaker's resistance, and returns the
// value.
static float GetSpeakerResistance(float min, float max)
{
float resistance; // Measured in Ohms.
// value.
static float GetSpeakerResistance(float min, float max)
{
float resistance; // Measured in Ohms.
do
{
Console.WriteLine(
'nEnter the resistance of the speakers in Ohms:');
Console.WriteLine(
'(Real quantity -> {0} Ohms <= resistance <= {1} Ohms)',
min, max);
// Assigns the input to the resistance variable.
resistance = float.Parse(Console.ReadLine());
if((resistance < min) || (resistance > max))
{
Console.WriteLine('nNumber exceeds range, please try again.');
}
// Checks to see if the input is within range.
} while ((resistance < min) || (resistance > max));
// Declares a new instance of class Speaker with the speaker's
// resistance and side.
Speaker speakerLeft = new Speaker(resistance, AVPosition.left);
// Declares a new instance of class Speaker with the speaker's
// resistance and side.
Speaker speakerRight = new Speaker(resistance, AVPosition.right);
{
Console.WriteLine(
'nEnter the resistance of the speakers in Ohms:');
Console.WriteLine(
'(Real quantity -> {0} Ohms <= resistance <= {1} Ohms)',
min, max);
// Assigns the input to the resistance variable.
resistance = float.Parse(Console.ReadLine());
if((resistance < min) || (resistance > max))
{
Console.WriteLine('nNumber exceeds range, please try again.');
}
// Checks to see if the input is within range.
} while ((resistance < min) || (resistance > max));
// Declares a new instance of class Speaker with the speaker's
// resistance and side.
Speaker speakerLeft = new Speaker(resistance, AVPosition.left);
// Declares a new instance of class Speaker with the speaker's
// resistance and side.
Speaker speakerRight = new Speaker(resistance, AVPosition.right);
return speakerLeft.Resistance; // Returns the resistance, in Ohms.
}
}
![Trebuchet Calculator Programming Trebuchet Calculator Programming](http://2.bp.blogspot.com/-0JtGUiRcrOU/Ua11eoVY_EI/AAAAAAAABvA/EqOwcKIAwVo/s1600/comathi-developer-save+as.png)
// Recieves the user's input for the wire's length, and returns the value.
static float GetWireLength(float min, float max, AVPosition sideWire)
{
float length; // Measures in metres.
static float GetWireLength(float min, float max, AVPosition sideWire)
{
float length; // Measures in metres.
do
{
Console.WriteLine(
'nEnter the length of the ' + sideWire + ' wire: ');
Console.WriteLine(
'(Real quantity -> {0} m <= length <= {1} m)',
min, max);
// Assigns the input to the length variable.
length = float.Parse(Console.ReadLine());
if((length < min) || (length > max))
{
Console.WriteLine('nNumber exceeds range, please try again.');
}
// Checks to see if the input is within range.
} while ((length < min) || (length > max));
return length; // Returns the length, in metres.
}
{
Console.WriteLine(
'nEnter the length of the ' + sideWire + ' wire: ');
Console.WriteLine(
'(Real quantity -> {0} m <= length <= {1} m)',
min, max);
// Assigns the input to the length variable.
length = float.Parse(Console.ReadLine());
if((length < min) || (length > max))
{
Console.WriteLine('nNumber exceeds range, please try again.');
}
// Checks to see if the input is within range.
} while ((length < min) || (length > max));
return length; // Returns the length, in metres.
}
// Uses the user's inputs to calculate the speaker output and the wire
// power loss in Watts.
static void CalculateAndDisplayPower(
float pAmplifierVoltage, float pSpeakerResistance,
float pWireLengthLeft, float pWireLengthRight)
{
// Assigns pAmplifierVoltage to ampVolts.
float ampVolts = pAmplifierVoltage;
// Assigns pSpeakerResistance to speakResist.
float speakResist = pSpeakerResistance;
// Assigns pWireLengthLeft to wireLeft.
float wireLeft = pWireLengthLeft;
// Assigns pWireLengthRight to wireRight.
float wireRight = pWireLengthRight;
Console.WriteLine('nSpeaker Output Power and Wire Power Lossn');
for(int i = 12; i <= 20; i += 4)
{
// Declares a new instance of SpeakerWire with the gauge, wire
// length, and the side (left).
SpeakerWire resSpeakLeft = new SpeakerWire(
i, wireLeft, AVPosition.left);
// Delcares a new instance on SpeakerWire with the gauge, wire
// length, and the side (right).
SpeakerWire resSpeakRight = new SpeakerWire(
i, wireRight, AVPosition.right);
// Assigns the resistance of the left wire to resistanceL.
float resistanceL = resSpeakLeft.Resistance;
// Assigns the resistance of the right wire to resistanceR.
float resistanceR = resSpeakRight.Resistance;
/*
Following calculations were provided in the outline in detail.
*/
float currentLeftWire = ampVolts/(resistanceL + speakResist);
double leftPower = Math.Pow(currentLeftWire, 2)*resistanceL;
float currentRightWire = ampVolts/(resistanceR + speakResist);
double rightPower = Math.Pow(currentRightWire, 2)*resistanceR;
double power = leftPower + rightPower;
double leftSpeakPow = Math.Pow(currentLeftWire, 2)*speakResist;
double rightSpeakPow = Math.Pow(currentRightWire, 2)*speakResist;
double speakerPower = leftSpeakPow + rightSpeakPow;
// Outputs the gauge, speaker output, and the wire power loss
// to the display for the user to see.
Console.WriteLine('Gauge {0}: Speaker output = {1:F2} W; ' +
'Wire power loss = {2:F2} W', i, speakerPower, power);
}// For-loop End Brace.
}// Method CalculateAndDisplayPower End Brace.
}// Class TestProgram End Brace.
// power loss in Watts.
static void CalculateAndDisplayPower(
float pAmplifierVoltage, float pSpeakerResistance,
float pWireLengthLeft, float pWireLengthRight)
{
// Assigns pAmplifierVoltage to ampVolts.
float ampVolts = pAmplifierVoltage;
// Assigns pSpeakerResistance to speakResist.
float speakResist = pSpeakerResistance;
// Assigns pWireLengthLeft to wireLeft.
float wireLeft = pWireLengthLeft;
// Assigns pWireLengthRight to wireRight.
float wireRight = pWireLengthRight;
Console.WriteLine('nSpeaker Output Power and Wire Power Lossn');
for(int i = 12; i <= 20; i += 4)
{
// Declares a new instance of SpeakerWire with the gauge, wire
// length, and the side (left).
SpeakerWire resSpeakLeft = new SpeakerWire(
i, wireLeft, AVPosition.left);
// Delcares a new instance on SpeakerWire with the gauge, wire
// length, and the side (right).
SpeakerWire resSpeakRight = new SpeakerWire(
i, wireRight, AVPosition.right);
// Assigns the resistance of the left wire to resistanceL.
float resistanceL = resSpeakLeft.Resistance;
// Assigns the resistance of the right wire to resistanceR.
float resistanceR = resSpeakRight.Resistance;
/*
Following calculations were provided in the outline in detail.
*/
float currentLeftWire = ampVolts/(resistanceL + speakResist);
double leftPower = Math.Pow(currentLeftWire, 2)*resistanceL;
float currentRightWire = ampVolts/(resistanceR + speakResist);
double rightPower = Math.Pow(currentRightWire, 2)*resistanceR;
double power = leftPower + rightPower;
double leftSpeakPow = Math.Pow(currentLeftWire, 2)*speakResist;
double rightSpeakPow = Math.Pow(currentRightWire, 2)*speakResist;
double speakerPower = leftSpeakPow + rightSpeakPow;
// Outputs the gauge, speaker output, and the wire power loss
// to the display for the user to see.
Console.WriteLine('Gauge {0}: Speaker output = {1:F2} W; ' +
'Wire power loss = {2:F2} W', i, speakerPower, power);
}// For-loop End Brace.
}// Method CalculateAndDisplayPower End Brace.
}// Class TestProgram End Brace.