Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.52 KB | None | 0 0
  1. #include<iostream>
  2. #include<math.h>
  3. using namespace std;
  4. class Car {
  5. private:
  6.     int id;
  7.     int speed;
  8.     int yearmodel;
  9.     int x_coordinate;
  10.     int y_coordinate;
  11.     int passweight[5];
  12. public:
  13.     Car() {
  14.         id = 0;
  15.         speed = 0;
  16.         yearmodel = 0;
  17.         x_coordinate = 0;
  18.         y_coordinate = 0;
  19.         for (int i = 0; i < 5; i++) {
  20.             passweight[i] = 0;
  21.         }
  22.     }
  23.     void setid(int d) {
  24.         if (d > 0) {
  25.             d = id;
  26.         }
  27.  
  28.  
  29.  
  30.     }
  31.     void setspeed(int s) {
  32.         if (speed >= 0) {
  33.             s = speed;
  34.         }
  35.     }
  36.     void setyearmodel(int m) {
  37.         if (m > 0) {
  38.             m = yearmodel;
  39.         }
  40.  
  41.     }
  42.     void set_x_coordinate(int x) {
  43.         if (x_coordinate > 0) {
  44.             x = x_coordinate;
  45.  
  46.         }
  47.  
  48.     }
  49.     void set_y_coordinate(int y) {
  50.         if (y_coordinate > 0) {
  51.             y = y_coordinate;
  52.         }
  53.     }
  54.     void set_passweight(int w[5]) {
  55.         for (int i = 0; i < 5; i++) {
  56.             w[i] = passweight[i];
  57.             int p = w[i];
  58.  
  59.         }
  60.  
  61.     }
  62.     int getid() {
  63.         return id;
  64.     }
  65.     int getspeed() {
  66.         return speed;
  67.     }
  68.     int get_yearmodel() {
  69.         return yearmodel;
  70.     }
  71.     int get_x_coordinate() {
  72.         return x_coordinate;
  73.     }
  74.     int get_y_coordinate() {
  75.         return y_coordinate;
  76.     }
  77.     int get_passweight(int index) {
  78.         for (int i = 0; i < 5; i++) {
  79.             if (index == i) {
  80.                 return passweight[index];
  81.             }
  82.  
  83.         }
  84.     }
  85.    
  86.     Car(int d, int s, int m, int x, int y, int w[]) {
  87.         setid(d);
  88.         setspeed(s);
  89.         setyearmodel(m);
  90.         set_x_coordinate(x);
  91.         set_y_coordinate(y);
  92.         set_passweight(w);
  93.     }
  94.  
  95.     bool is_idle() {
  96.         if (speed > 0) {
  97.             return true;
  98.         }
  99.         else {
  100.             return false;
  101.         }
  102.     }
  103.     void print_details() {
  104.         cout << "The ID = " << id << endl;
  105.         cout << "The Speed = " << speed << endl;
  106.         cout << "The Year model = " << yearmodel << endl;
  107.         cout << "The X coordinate = " << x_coordinate << endl;
  108.         cout << "The Y coordinate = " << y_coordinate << endl;
  109.         cout << "Passweight values : ";
  110.         for (int i = 0; i < 5; i++) {
  111.             cout << passweight[i] << " " << endl;
  112.  
  113.         }
  114.     }
  115.     int accelarate() {
  116.         if (speed <= 200) {
  117.             speed = speed + 5;
  118.             return speed;
  119.         }
  120.         else {
  121.             cout << "Slow down the speed please" << endl;
  122.         }
  123.     }
  124.     int brake() {
  125.         if (speed >= 0) {
  126.             speed = speed - 5;
  127.             return speed;
  128.  
  129.         }
  130.  
  131.     }
  132.     int computedistance(int x, int y) {
  133.         return sqrt(pow(x - x_coordinate, 2) + pow(y - y_coordinate, 2));
  134.     }
  135.     void moveleftright(int dx) {
  136.         x_coordinate = x_coordinate + dx;
  137.     }
  138.     void moveupdown(int dy) {
  139.         y_coordinate = y_coordinate + dy;
  140.     }
  141.     float weightavg() {
  142.         int sum = 0;
  143.         float avg;
  144.         for (int i = 0; i < 5; i++) {
  145.             sum = sum + passweight[i];
  146.             avg = sum / 5;
  147.  
  148.  
  149.         }
  150.         return avg;
  151.  
  152.     }
  153.     int hashigherweight() {
  154.         for (int i = 0; i < 5; i++) {
  155.             if (passweight[i] >= 100) {
  156.                 return passweight[i];
  157.             }
  158.             else {
  159.                 cout << "Passweights are less than 100" << endl;
  160.             }
  161.         }
  162.     }
  163. };
  164. int main() {
  165.     Car car1;
  166.     int x, y, s, m, d, w[5];
  167.     cout << "Enter ID: ";
  168.     cin >> d;
  169.     car1.setid(d);
  170.     cout << "\nEnter year model : ";
  171.     cin >> m;
  172.     car1.setyearmodel(m);
  173.     cout << "\nEnter speed : ";
  174.     cin >> s;
  175.     car1.setspeed(s);
  176.     cout << "\nEnter X coordinate : ";
  177.     cin >> x;
  178.     car1.set_x_coordinate(x);
  179.     cout << "\nEnter Y coordinate : ";
  180.     cin >> y;
  181.     car1.set_y_coordinate(y);
  182.     cout << "\nEnter passweight values:  ";
  183.     for (int i = 0; i < 5; i++) {
  184.         cin >> w[i];
  185.     }
  186.     car1.set_passweight(w);
  187.     car1.print_details();
  188.     int weight[5] = { 200, 120,150,50,250 };
  189.     Car car2(10, 60, 2000, 5, 4, weight);
  190.     car2.print_details();
  191.     if (car1.get_yearmodel() > car2.get_yearmodel()) {
  192.         cout << "car1 is newer than car2" << endl;
  193.     }
  194.     ////
  195.     cout << "car2 x__cordinate = " << car2.get_x_coordinate() << endl;
  196.     cout << "car1 x__coordinate = " << car1.get_x_coordinate() << endl;
  197.     cout << "car2 y__coordinate = " << car2.get_y_coordinate() << endl;
  198.     cout << "car1 y__coordinate = " << car1.get_y_coordinate() << endl;
  199.     if (car1.get_x_coordinate() > car2.get_x_coordinate()) {
  200.         cout << "car1 is right to car2" << endl;
  201.     }
  202.     if (car2.get_x_coordinate() > car1.get_x_coordinate()) {
  203.         cout << "car 1 is left to car2" << endl;
  204.     }
  205.     if (car1.get_y_coordinate() > car2.get_y_coordinate()) {
  206.         cout << "car1 is down comparing to car2" << endl;
  207.     }
  208.     if (car2.get_y_coordinate() > car1.get_y_coordinate()) {
  209.         cout << "car1 is up comparing to car2" << endl;
  210.     }
  211.     cout << "car1 average of weight = " << car1.weightavg() << endl;
  212.     cout << "car2 average of weight = " << car2.weightavg() << endl;
  213.     if (car1.weightavg() > car2.weightavg()) {
  214.         cout << "car1 has the highest average weight" << endl;
  215.     }
  216.     if (car2.weightavg() > car1.weightavg()) {
  217.         cout << "car2 has the highest average weight" << endl;
  218.     }
  219.     return 0;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement