Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<math.h>
- using namespace std;
- class Car {
- private:
- int id;
- int speed;
- int yearmodel;
- int x_coordinate;
- int y_coordinate;
- int passweight[5];
- public:
- Car() {
- id = 0;
- speed = 0;
- yearmodel = 0;
- x_coordinate = 0;
- y_coordinate = 0;
- for (int i = 0; i < 5; i++) {
- passweight[i] = 0;
- }
- }
- void setid(int d) {
- if (d > 0) {
- d = id;
- }
- }
- void setspeed(int s) {
- if (speed >= 0) {
- s = speed;
- }
- }
- void setyearmodel(int m) {
- if (m > 0) {
- m = yearmodel;
- }
- }
- void set_x_coordinate(int x) {
- if (x_coordinate > 0) {
- x = x_coordinate;
- }
- }
- void set_y_coordinate(int y) {
- if (y_coordinate > 0) {
- y = y_coordinate;
- }
- }
- void set_passweight(int w[5]) {
- for (int i = 0; i < 5; i++) {
- w[i] = passweight[i];
- int p = w[i];
- }
- }
- int getid() {
- return id;
- }
- int getspeed() {
- return speed;
- }
- int get_yearmodel() {
- return yearmodel;
- }
- int get_x_coordinate() {
- return x_coordinate;
- }
- int get_y_coordinate() {
- return y_coordinate;
- }
- int get_passweight(int index) {
- for (int i = 0; i < 5; i++) {
- if (index == i) {
- return passweight[index];
- }
- }
- }
- Car(int d, int s, int m, int x, int y, int w[]) {
- setid(d);
- setspeed(s);
- setyearmodel(m);
- set_x_coordinate(x);
- set_y_coordinate(y);
- set_passweight(w);
- }
- bool is_idle() {
- if (speed > 0) {
- return true;
- }
- else {
- return false;
- }
- }
- void print_details() {
- cout << "The ID = " << id << endl;
- cout << "The Speed = " << speed << endl;
- cout << "The Year model = " << yearmodel << endl;
- cout << "The X coordinate = " << x_coordinate << endl;
- cout << "The Y coordinate = " << y_coordinate << endl;
- cout << "Passweight values : ";
- for (int i = 0; i < 5; i++) {
- cout << passweight[i] << " " << endl;
- }
- }
- int accelarate() {
- if (speed <= 200) {
- speed = speed + 5;
- return speed;
- }
- else {
- cout << "Slow down the speed please" << endl;
- }
- }
- int brake() {
- if (speed >= 0) {
- speed = speed - 5;
- return speed;
- }
- }
- int computedistance(int x, int y) {
- return sqrt(pow(x - x_coordinate, 2) + pow(y - y_coordinate, 2));
- }
- void moveleftright(int dx) {
- x_coordinate = x_coordinate + dx;
- }
- void moveupdown(int dy) {
- y_coordinate = y_coordinate + dy;
- }
- float weightavg() {
- int sum = 0;
- float avg;
- for (int i = 0; i < 5; i++) {
- sum = sum + passweight[i];
- avg = sum / 5;
- }
- return avg;
- }
- int hashigherweight() {
- for (int i = 0; i < 5; i++) {
- if (passweight[i] >= 100) {
- return passweight[i];
- }
- else {
- cout << "Passweights are less than 100" << endl;
- }
- }
- }
- };
- int main() {
- Car car1;
- int x, y, s, m, d, w[5];
- cout << "Enter ID: ";
- cin >> d;
- car1.setid(d);
- cout << "\nEnter year model : ";
- cin >> m;
- car1.setyearmodel(m);
- cout << "\nEnter speed : ";
- cin >> s;
- car1.setspeed(s);
- cout << "\nEnter X coordinate : ";
- cin >> x;
- car1.set_x_coordinate(x);
- cout << "\nEnter Y coordinate : ";
- cin >> y;
- car1.set_y_coordinate(y);
- cout << "\nEnter passweight values: ";
- for (int i = 0; i < 5; i++) {
- cin >> w[i];
- }
- car1.set_passweight(w);
- car1.print_details();
- int weight[5] = { 200, 120,150,50,250 };
- Car car2(10, 60, 2000, 5, 4, weight);
- car2.print_details();
- if (car1.get_yearmodel() > car2.get_yearmodel()) {
- cout << "car1 is newer than car2" << endl;
- }
- ////
- cout << "car2 x__cordinate = " << car2.get_x_coordinate() << endl;
- cout << "car1 x__coordinate = " << car1.get_x_coordinate() << endl;
- cout << "car2 y__coordinate = " << car2.get_y_coordinate() << endl;
- cout << "car1 y__coordinate = " << car1.get_y_coordinate() << endl;
- if (car1.get_x_coordinate() > car2.get_x_coordinate()) {
- cout << "car1 is right to car2" << endl;
- }
- if (car2.get_x_coordinate() > car1.get_x_coordinate()) {
- cout << "car 1 is left to car2" << endl;
- }
- if (car1.get_y_coordinate() > car2.get_y_coordinate()) {
- cout << "car1 is down comparing to car2" << endl;
- }
- if (car2.get_y_coordinate() > car1.get_y_coordinate()) {
- cout << "car1 is up comparing to car2" << endl;
- }
- cout << "car1 average of weight = " << car1.weightavg() << endl;
- cout << "car2 average of weight = " << car2.weightavg() << endl;
- if (car1.weightavg() > car2.weightavg()) {
- cout << "car1 has the highest average weight" << endl;
- }
- if (car2.weightavg() > car1.weightavg()) {
- cout << "car2 has the highest average weight" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement