Question

In: Computer Science

Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed);...

Study the following class definition:

class Car
{
public:
   Car(double speed);
 
   void start();
   void accelerate(double speed);
   void stop();
   double get_speed() const;
 
private:
   double speed;
};

Which of the following options would make logical sense in the definition of the void accelerate(double speed)function?

Group of answer choices

this->speed = this->speed;

this->speed = speed;

this.speed = speed;

speed1 = this->speed;

Flag this Question

Question 131 pts

The Point class has a public function called display_point(). What is the correct way of calling the display_point function in the main function?

int main()
{
   Point* point_ptr = new Point;
   // call the display_point function using point_ptr
   return 0;
}

Group of answer choices

display_point(point_ptr);

point_ptr->display_point();

*point_ptr.display_point();

point_ptr.display_point();

Flag this Question

Question 141 pts

Study the following class definition:

class Athlete
{
public:
   Athlete(double new_speed);
 
   void train();
   void run(double new_speed);
   void rest();
   double get_speed() const;
 
private:
   double speed;
};

Which of the following options would be legal in the definition of the void run(double new_speed)function?

Group of answer choices

this.speed = new_speed;

this->speed = this->new_speed;

speed = this->new_speed;

this->speed = new_speed;

Flag this Question

Question 151 pts

You are given the class definition for CashRegister. One of the member functions of this class is clear(). Furthermore, you have set up and allocated an array of pointers to CashRegister objects. Given the declaration of the array all_registers below, which code snippet correctly calls the clear() function on every object pointed to from the all_registers array?

CashRegister* all_registers[20];

Group of answer choices

for (int i = 0; i < 20; i++) 
{
   all_registers[i]->clear();
}
for (int i = 0; i < 20; i++) 
{
   all_registers[i].clear();
}
for (int i = 0; i < 20; i++) 
{
   all_registers.clear()
}
for (int i = 0; i < 20; i++) 
{
   all_registers->clear();
}

Solutions

Expert Solution

Question 1) this->speed=speed;

Here we have speed as an argument in the function and we have to map that speed argument with the speed of the Car class variable. For any variable which is defined in side the class can be called as this->(name of variable). This happens if we are using the variable inside the class. So for assigning speed we need to do it like this->speed=speed.

Question 131)

The given code snippet is

int main()
{
   Point* point_ptr = new Point;
   // call the display_point function using point_ptr
   return 0;
}

so the variable we have is a pointer of Point type. and we have to call a function on the point_ptr so that means we have to call function on a pointer. So we needed to use it like point_ptr.display_point() if it was a simple variable of that class but here due to pointer variable we will have to use it like (*point_ptr).display_point() which is also written as point_ptr->display_point(); so our option will be point_ptr->display_point().

Question 141) This one is similar to the problem 1st. Here we have the argument given as new_speed and the variable for storing the speed inside the class is speed. So as mentioned earlier we can say that to access the speed of the class we will have to use this->speed and this value is provided as new_speed as argument. So we can our answer will be this->speed = new_speed.

Question 151) Here we will have to clear each variable of the object of the class. We have an array of object of class so we need to go each object using for loop and then use the erase method on the corresponding object. But here we are given the pointer array of that class so we will have to use the array method as mentioned in 2nd question. So basically our answer should be this

for (int i = 0; i < 20; i++) 
{
   (*all_registers[i]).clear();
}

as we know (*all_registers[i]).clear() can be written as all_registers[i]->clear(). So our answer is

for (int i = 0; i < 20; i++) 
{
   all_registers[i]->clear();
}

Related Solutions

import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts)...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts) { return quarts * 0.25; } public static double milesToFeet(double miles) { return miles * 5280; } public static double milesToInches(double miles) { return miles * 63360; } public static double milesToYards(double miles) { return miles * 1760; } public static double milesToMeters(double miles) { return miles * 1609.34; } public static double milesToKilometer(double miles) { return milesToMeters(miles) / 1000.0; } public static double...
import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass;...
import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass; double velocity; double totalkineticEnergy;    Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the objects mass in kilograms"); mass = keyboard.nextDouble();    System.out.println ("Please enter the objects velocity in meters per second: "); velocity = keyboard.nextDouble();    double actualTotal = kineticEnergy (mass, velocity); System.out.println ("Objects Kinetic Energy: " + actualTotal); } public static double kineticEnergy (double mass, double velocity) { double totalkineticEnergy =...
A incomplete definition of a class Temperature is given below: public class Temperature { private double...
A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3}; } [6] (i) Copy and put it in a new class. Write a method toString() of the class, which does not have any parameters and returns a string containing all the values separated by newlines. When the string is printed, each value should appear on a line in the ascending order of their indexes. Copy the content of...
Consider the following interface: public interface Car{ public String getMake(); public void setMake(); public void honk();...
Consider the following interface: public interface Car{ public String getMake(); public void setMake(); public void honk(); public void crash(); public void drive(); } public interface Boat{ public String getMake (); public void setMake (); public void blast_horn(); public void sink(); public void move(); } 1. Create a concrete FamilyCar class from the Car interface.
Make a function definition in C for the following: void insert (double *b, int c, double...
Make a function definition in C for the following: void insert (double *b, int c, double s, int pos); //Insert value s at position pos in array. //needs: // c > 0, pos >= 0, and pos <= c-1. Elements b[0]...b[c-1] exist. //this will do: //Elements from indexes pos up to c-2 have been moved up to indexes pos+1 up to c-1. The value s has been copied into b[pos]. //Note: the data that was in b[c-1] when the function...
public class sales_receipt { public static void main(String[] args) { //declare varables final double tax_rate =...
public class sales_receipt { public static void main(String[] args) { //declare varables final double tax_rate = 0.05; //tax rate String cashier_name = "xxx"; //sales person String article1, article2; //item name for each purchased int quantity1, quantity2; //number of quantities for each item double unit_cost1, unit_cost2; //unit price for each item double price1, price2; //calculates amounts of money for each item. double sub_total; //total price of two items without tax double tax; //amount of sales tax double total; //total price of...
Suppose that Account class has private attributes double balance and two public methods void setBalance(double amount)...
Suppose that Account class has private attributes double balance and two public methods void setBalance(double amount) and double getBalance() const. The method names explain its purpose. Further suppose that child class Savings has one more attribute double interest_rate and a public method void addInterest() which will update the balance according the formula new balance = old balance * (1 + interest_rate/100.0); Implement addInterest method. c++
#ifndef CCALC_HEADER #define CCALC_HEADER    class   CCalc { public:     // member functions     CCalc();     void    Add(double...
#ifndef CCALC_HEADER #define CCALC_HEADER    class   CCalc { public:     // member functions     CCalc();     void    Add(double value);     void    Clear();     void    Divide(double value);     double  GetValue() const;     void    Multiply(double value);     void    SetValue(double  newValue);     void    Subtract(double value);    private:     // data members     double  m_total; };    #endif // CCALC_HEADER int     main() {     CCalc       calculator;     char        choice;        // loop and let the user manipulate the calculator     do {         // display the menu and get the user selection         DisplayMenu();         cout <<...
void printList(double * A, int start, int end ) { if (start == end) //base case...
void printList(double * A, int start, int end ) { if (start == end) //base case { cout << A[start] << endl; } else //recursive case { cout << A[start] << endl; printList(A, start + 1, end); } } int main() { double nums[] = { 13.8, 2.14, 51, 82, 3.14, 1.7, 4.89, 18, 5, 23.6, 17, 48, 5.6 }; printList(nums, 0, 12); //13.8 2.14 51 .... 48 5.6 cout << endl; //HELP HERE: Using a recursive method on C++,...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse();...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse(); //reverses the order of elements in the linked list void insert(int value); private: struct Node{ int data; Node* next; Node* prev; }; Node* head; Node* tail; //Add your helper function here that recursively reverses the order of elements in the linked list }; Write the declaration of a helper function in the class provided above that recursively reverses the order of elements in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT