Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

TASKS You will create a class called Linkage that characterizes the motion of a

ID: 3605776 • Letter: T

Question

TASKS

You will create a class called Linkage that characterizes the motion of a pinned four-bar linkage. Your class must have the following attributes:

Your class must also have the following methods and constructors:

Linkage class constructors

Linkage(double S, double P, double Q, double L) -This is the object constructor that should initialize the objects member values. Member values MUST be provided through console input. The user must be prompted to enter the link lengths from shortest to longest.

Linkage class accessors

double getLongest() – returns length of the longest link

double getP() – returns length of the first intermediate link

double getQ() – returns length of the second intermediate link

double getShortest() – returns length of the shortest link

string Motion() – returns the type of motion (ie, crank rocker, triple rocker, change point)

Linkage class mutators

void setLongest(double L) – sets longest length

void setQ(double Q) – sets first intermediate length

void setP(double P) – sets second intermediate length

void setShortest(double S) – sets shortest length

A Linkage object constructed from a Linkage class exists to characterize motion type. Thus, for the purposes of this class you are required to use the following strings as the return values of the motion accessor.

You must implement the functions for the Linkage class in the file Linkage.cpp and define the class in Linkage.h. The file main.cpp will only be used for testing purposes, no code should be written in main.cpp.

You can determine which of the three types of motion will happen by applying the following formula:

S + L < = > P + Q

If S + L < P + Q, and the input link is the short link, you will have a crank rocker.

If S + L > P + Q, you will have a triple rocker.

If S + L = P + Q, you will have a change-point linkage.

SAMPLE INPUT AND OUTPOUT

Enter the link lengths in descending order from longest to shortest. Units are meters.
> 14.6
> 10.8
> 9.1
> 7.2
> You entered s1:14.6 s2:10.8 s3:9.1 s4:7.2
> Linkage Constructor was called
> Enter in a NEW longest length
> 15.1
> Called setLongest method
> Called getLongest method
> In main: calling getLongest on the link object: 15.1
> Called getShortest method
> In main: calling getShortest on the link object: 7.2
> Called getP method
> In main: calling getP on the link object: 10.8
> Called getQ method
> In main: calling getQ on the link object: 9.1
> Called Motion method
> This pinned four-bar linkage is a triple rocker.

> Linkage Constructor was called
> Creating a new linkage with L,P,Q,S as 50,40,30,20
> Calling Motion() expecting a change-point linkage
> Called Motion method
> This pinned four-bar linkage is a change-point linkage.

> Shortening longest to 45, creating a crank rocker linkage
> Called setLongest method
> Calling Motion() expecting a crank Rocker linkage
> Called Motion method
> This pinned four-bar linkage is a crank rocker.

> Lengthening shortest to 26, creating a triple rocker linkage
> Called setShortest method
> Calling Motion() expecting a triple rocker linkage
> Called Motion method
> This pinned four-bar linkage is a triple rocker.

SAMPLE STARTER CODE

#ifndef LINKAGE_H
#define LINKAGE_H

#include

class Linkage{

public:
//This is the class constructor
Linkage(double L, double P, double Q, double S);
//returns length of the longest link
double getLongest();
//returns length of the first intermediate link
double getP();
//returns length of the second intermediate link
double getQ();
//returns length of the shortest link
double getShortest();
//returns the type of motion (ie, crank rocker, triple rocker, change point)
std::string Motion();

//sets longest length
void setLongest(double L);
//sets first intermediate length
void setP(double P);/** updated      **/
//sets second intermediate length
void setQ(double Q);/** updated      **/
//sets shortest length
void setShortest(double S);

private:
static const std::string triple;
static const std::string crank;
static const std::string change;
double longest_length_L;
double intermediate_length_P;
double second_length_Q;
double shortest_length_S;



};

#endif



//Linkage.cpp is below this comment
#include
#include "Linkage.h"

using namespace std;

const std::string Linkage::triple = "This pinned four-bar linkage is a triple rocker .";
const std::string Linkage::crank = "This pinned four-bar linkage is a crank rocker .";
const std::string Linkage::change = "This pinned four-bar linkage is a change-point linkage.";


Linkage::Linkage(double L, double P, double Q, double S)
{
std::cout << "Linkage Constructor was called" << std::endl;
this->longest_length_L = L;
this->intermediate_length_P = P;
this->second_length_Q = Q;
this->shortest_length_S = S;
}


double Linkage::getLongest()
{
std::cout << "Called getLongest method" << std::endl;
return this->longest_length_L;
}

void Linkage::setLongest(double L)
{
std::cout << "Called setLongest method" << std::endl;
this->longest_length_L = L;

}



/**
*
* main.cpp is below this comment
*
**/
int main(void)
{ /** updated      **/
std::cout << "Enter the link lengths in descending order from longest to shortest. Units are meters." << std::endl;
double s1,s2,s3,s4;
std::cin >> s1;
std::cin >> s2;
std::cin >> s3;
std::cin >> s4;
std::cout << "You entered s1:" << s1 << " s2:"< Linkage link(s1,s2,s3,s4);
std::cout << "Enter in a NEW longest length" << std::endl;
double newLongest;
std::cin >> newLongest;

//pass the new longest to the set method!!
link.setLongest(newLongest);

std::cout << "In main: calling getLongest on the link object: " << link.getLongest() << std::endl;
std::cout << "In main: calling getShortest on the link object: " << link.getShortest() << std::endl;
std::cout << "In main: calling getP on the link object: "<< link.getP() << std::endl;
std::cout << "In main: calling getQ on the link object: "<< link.getQ() << std::endl;
std::cout << link.Motion() << std::endl << std::endl;

Linkage newLink(50, 40, 30, 20);          /** updated        **/
std::cout << "Creating a new linkage with L,P,Q,S as 50,40,30,20" << std::endl;
std::cout << "Calling Motion() expecting a change-point linkage" << std::endl;
std::cout << newLink.Motion() << std::endl << std::endl;

/** updated      **/
std::cout << "Shortening longest to 45, creating a crank rocker linkage" << std::endl;
newLink.setLongest(45);
std::cout << "Calling Motion() expecting a crank Rocker linkage" << std::endl;
std::cout << newLink.Motion() << std::endl << std::endl;

/** updated      **/
std::cout << "Lengthening shortest to 26, creating a triple rocker linkage" << std::endl;
newLink.setShortest(26);
std::cout << "Calling Motion() expecting a triple rocker linkage" << std::endl;
std::cout << newLink.Motion() << std::endl;


return 0;
}

Explanation / Answer

// File name: Linkage.h
#ifndef LINKAGE_H
#define LINKAGE_H
#include <string>
using namespace std;

// class Linkage definition
class Linkage
{
public:
//This is the class constructor
Linkage(double L, double P, double Q, double S);
//returns length of the longest link
double getLongest();
//returns length of the first intermediate link
double getP();
//returns length of the second intermediate link
double getQ();
//returns length of the shortest link
double getShortest();
//returns the type of motion (ie, crank rocker, triple rocker, change point)
std::string Motion();

//sets longest length
void setLongest(double L);
//sets first intermediate length
void setP(double P);/** updated **/
//sets second intermediate length
void setQ(double Q);/** updated **/
//sets shortest length
void setShortest(double S);

private:
// Data member to store data
static const std::string triple;
static const std::string crank;
static const std::string change;
double longest_length_L;
double intermediate_length_P;
double second_length_Q;
double shortest_length_S;
};// End of class

#endif

---------------------------------------------------------------------------------------------------------

// File name: Linkage.cpp
#include <iostream>
#include <string>
// Includes header file Linkage.h
#include "Linkage.h"
using namespace std;

// Assigns string data to the member
const std::string Linkage::triple = "This pinned four-bar linkage is a triple rocker .";
const std::string Linkage::crank = "This pinned four-bar linkage is a crank rocker .";
const std::string Linkage::change = "This pinned four-bar linkage is a change-point linkage.";

// Parameterized constructor
Linkage::Linkage(double L, double P, double Q, double S)
{
std::cout << "Linkage Constructor was called" << std::endl;
// Assigns parameter data to data member of the class
this->longest_length_L = L;
this->intermediate_length_P = P;
this->second_length_Q = Q;
this->shortest_length_S = S;
}// End of constructor

// Returns length of the longest link
double Linkage::getLongest()
{
std::cout << "Called getLongest method" << std::endl;
return this->longest_length_L;
}// End of function

// Sets longest length
void Linkage::setLongest(double L)
{
std::cout << "Called setLongest method" << std::endl;
this->longest_length_L = L;
}// End of function

// Returns length of the first intermediate link
double Linkage::getP()
{
std::cout << "Called getP method" << std::endl;
return this -> intermediate_length_P;
}// End of function

// Sets first intermediate length
void Linkage::setP(double P)
{
std::cout << "Called setP method" << std::endl;
this -> intermediate_length_P;
}// End of function

// Returns length of the second intermediate link
double Linkage::getQ()
{
std::cout << "Called getQ method" << std::endl;
return this -> second_length_Q;
}// End of function

// Sets second intermediate length
void Linkage::setQ(double Q)
{
std::cout << "Called setQ method" << std::endl;
this -> second_length_Q = Q;
}// End of function

// Returns length of the shortest link
double Linkage::getShortest()
{
std::cout << "Called getShortest method" << std::endl;
return this -> shortest_length_S;
}// End of function

// Sets shortest length
void Linkage::setShortest(double S)
{
std::cout << "Called setShortest method" << std::endl;
this -> shortest_length_S = S;
}// End of function

// Returns the type of motion (i.e, crank rocker, triple rocker, change point)
std::string Linkage::Motion()
{
std::cout << "Called Motion method "<<std::endl;
// Checks If S + L < P + Q
if((shortest_length_S + longest_length_L) < (intermediate_length_P + second_length_Q))
return crank;
// Otherwise checks If S + L > P + Q
else if((shortest_length_S + longest_length_L) > (intermediate_length_P + second_length_Q))
return triple;
// Otherwise S + L = P + Q
else
return change;
}// End of function

------------------------------------------------------------------------------------------------------------------------------

// File name: mainLinkage.cpp
#include<iostream>
#include "Linkage.cpp"
using namespace std;

// main method definition to test the functions
int main(void)
{
/** updated **/
std::cout << "Enter the link lengths in descending order from longest to shortest. Units are meters." << std::endl;
double s1,s2,s3,s4;
std::cin >> s1;
std::cin >> s2;
std::cin >> s3;
std::cin >> s4;
std::cout << "You entered s1: " << s1 << " s2: "<< s2 << " s3: "<<s3<<" s4: "<<s4<<endl;
Linkage link(s1,s2,s3,s4);
std::cout << "Enter in a NEW longest length" << std::endl;
double newLongest;
std::cin >> newLongest;

//pass the new longest to the set method!!
link.setLongest(newLongest);

std::cout << "In main: calling getLongest on the link object: " << link.getLongest() << std::endl;
std::cout << "In main: calling getShortest on the link object: " << link.getShortest() << std::endl;
std::cout << "In main: calling getP on the link object: "<< link.getP() << std::endl;
std::cout << "In main: calling getQ on the link object: "<< link.getQ() << std::endl;
std::cout << link.Motion() << std::endl << std::endl;

Linkage newLink(50, 40, 30, 20); /** updated **/
std::cout << "Creating a new linkage with L,P,Q,S as 50,40,30,20" << std::endl;
std::cout << "Calling Motion() expecting a change-point linkage" << std::endl;
std::cout << newLink.Motion() << std::endl << std::endl;

/** updated **/
std::cout << "Shortening longest to 45, creating a crank rocker linkage" << std::endl;
newLink.setLongest(45);
std::cout << "Calling Motion() expecting a crank Rocker linkage" << std::endl;
std::cout << newLink.Motion() << std::endl << std::endl;

/** updated **/
std::cout << "Lengthening shortest to 26, creating a triple rocker linkage" << std::endl;
newLink.setShortest(26);
std::cout << "Calling Motion() expecting a triple rocker linkage" << std::endl;
std::cout << newLink.Motion() << std::endl;

return 0;
}

Sample Run:

Enter the link lengths in descending order from longest to shortest. Units are meters.
14.6
10.8
9.1
7.2
You entered s1: 14.6 s2: 10.8 s3: 9.1 s4: 7.2
Linkage Constructor was called
Enter in a NEW longest length
15.1
Called setLongest method
Called getLongest method
In main: calling getLongest on the link object: 15.1
Called getShortest method
In main: calling getShortest on the link object: 7.2
Called getP method
In main: calling getP on the link object: 10.8
Called getQ method
In main: calling getQ on the link object: 9.1
Called Motion method
This pinned four-bar linkage is a triple rocker .

Linkage Constructor was called
Creating a new linkage with L,P,Q,S as 50,40,30,20
Calling Motion() expecting a change-point linkage
Called Motion method
This pinned four-bar linkage is a change-point linkage.

Shortening longest to 45, creating a crank rocker linkage
Called setLongest method
Calling Motion() expecting a crank Rocker linkage
Called Motion method
This pinned four-bar linkage is a crank rocker .

Lengthening shortest to 26, creating a triple rocker linkage
Called setShortest method
Calling Motion() expecting a triple rocker linkage
Called Motion method
This pinned four-bar linkage is a triple rocker .