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

textbook= An introduction to an object-oriented / Ch.4 (Level 1 Programming Exer

ID: 3651140 • Letter: T

Question

textbook= An introduction to an object-oriented / Ch.4 (Level 1 Programming Exercises) problem #14
Write a WeightConverter class. An instance of this class is created by passing the gravity of an object relative to the Earth's gravity (see Exercise 16 on page 144). For example, the Moon's gravity is approximately 0.167 of the Earth's gravity, so we create a WeightConverter instance for the Moon as

WeightConverter moonweight;
moonWeight = new WeightConverter(0.167);

This is the incorrect answer:

class WeightConverter
{
private:
float relative_gravity;
public:
WeightConverter(float a)
{
relative_gravity=a;
}
};

Explanation / Answer

class WeightConverter{ double relativeGravity; public WeightConverter(double relativeGravity){ this.relativeGravity = relativeGravity; } //mass is in terms of KG //9.8 is earth's gravity public double convert(double mass){ return relativeGravity * double * 9.8; } }