-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcarclass.cpp
More file actions
195 lines (158 loc) · 3.52 KB
/
carclass.cpp
File metadata and controls
195 lines (158 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/****************************************
James Bertel
CS111
Lab 14-1
11-27-17
****************************************/
#include <iostream>
using namespace std;
enum direct {NORTH, EAST, SOUTH, WEST};
class Car
{
private:
string make;
string model;
int year;
int mileage;
direct direction;
int X;
int Y;
public:
//prototypes
Car(string mk, string md, int yr);
string getMake();
string getModel();
void turnRight();
void turnLeft();
direct getDirection();
void goForward(int blk);
int getX();
int getY();
int getMileage();
string getDir();
};
Car::Car(string mk, string md, int yr)
{
make = mk;
model = md;
year = yr;
mileage = 0;
direction = NORTH;
X = 0;
Y = 0;
}
string Car::getMake()
{
return make;
}
string Car::getModel()
{
return model;
}
int Car::getMileage()
{
return mileage;
}
void Car::turnRight()
{
if(direction == WEST)
direction = NORTH;
else
direction = (direct)(direction + 1);
}
void Car::turnLeft()
{
if(direction == NORTH)
direction = WEST;
else
direction = (direct)(direction - 1);
}
direct Car::getDirection()
{
return direction;
}
void Car::goForward(int blk)
{
mileage += blk;
if(direction == NORTH)
Y += blk;
else if(direction == SOUTH)
Y -= blk;
else if(direction == EAST)
X += blk;
else //if direction == WEST
X -= blk;
}
int Car::getX()
{
return X;
}
int Car::getY()
{
return Y;
}
string Car::getDir()
{
switch(direction)
{
case NORTH: return "north";
case EAST: return "east";
case SOUTH: return "south";
case WEST: return "west";
}
}
int main() //10 & 13A || 20 & 22A
{
Car c1("Toyota", "Camry", 2017);
cout << c1.getMake() << endl;
c1.turnRight();
cout << c1.getDirection() << endl;
c1.turnRight();
cout << c1.getDirection() << endl;
c1.turnRight();
cout << c1.getDirection() << endl;
c1.turnRight();
cout << c1.getDirection() << endl;
c1.goForward(3); //(0, 3)
cout << "(" << c1.getX() << ", " << c1.getY() << ")" << endl;
c1.turnRight();
c1.goForward(5); //(5, 3)
cout << "(" << c1.getX() << ", " << c1.getY() << ")" << endl;
c1.turnRight();
c1.goForward(7); //(5, -4)
cout << "(" << c1.getX() << ", " << c1.getY() << ")" << endl;
c1.turnRight();
c1.goForward(6); //((-1, -4)
cout << "(" << c1.getX() << ", " << c1.getY() << ")" << endl;
cout << c1.getMake() << " " << c1.getModel() << " is located at (";
cout << c1.getX() << ", " << c1.getY() << ") facing " << c1.getDir();
cout << ". It has " << c1.getMileage() << " miles on it.\n" << endl;
Car c2("Honda", "Odyssey", 2001);
cout << c2.getMake() << endl;
c2.turnRight();
cout << c2.getDirection() << endl;
c2.turnRight();
cout << c2.getDirection() << endl;
c2.turnRight();
cout << c2.getDirection() << endl;
c2.turnRight();
cout << c2.getDirection() << endl;
c2.goForward(5);
cout << "(" << c2.getX() << ", " << c2.getY() << ")" << endl;
c2.turnLeft();
c2.goForward(1);
cout << "(" << c2.getX() << ", " << c2.getY() << ")" << endl;
c2.turnLeft();
c2.goForward(2);
cout << "(" << c2.getX() << ", " << c2.getY() << ")" << endl;
c2.turnLeft();
c2.goForward(4);
cout << "(" << c2.getX() << ", " << c2.getY() << ")" << endl;
c2.turnLeft();
c2.goForward(10);
cout << "(" << c2.getX() << ", " << c2.getY() << ")" << endl;
cout << c2.getMake() << " " << c2.getModel() << " is located at (";
cout << c2.getX() << ", " << c2.getY() << ") facing " << c2.getDir();
cout << ". It has " << c2.getMileage() << " miles on it.\n" << endl;
return 0;
}