Car Instrument Simulator For this assignment you will design

Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you will design arc the following: The FuelGauge Class: This class will simulate a fuel gauge. Its responsibilities are as follows: To know the car\'s current amount of fuel, in gallons. To report the car\'s current amount of fuel, in gallons. To be able to increment the amount of fuel by 1 gallon. This simulates putting fuel in the car. (The car can hold a maximum of 15 gallons.) To be able to decrement the amount of fuel by 1 gallon, if the amount of fuel is greater than 0 gallons. This simulates burning fuel as the car runs. The Odometer Class: This class will simulate the car\'s odometer. Its responsibilities are as follows: To know the car\'s current mileage. To report the car\'s current mileage. To be able to increment the current mileage by 1 mile. The maximum mileage the odometer can store is 999, 999 miles. When this amount is exceeded, the odometer resets the current mileage to 0. To be able to work with a FuelGauge object. It should decrease the FuelGauge object\'s current amount of fuel by 1 gallon for every 24 miles traveled. (The car\'s fuel economy is 24 miles per gallon.) Demonstrate the classes by creating instances of each. Simulate filling the car up with fuel, and then run a loop that increments the odometer until the car runs out of fuel. During each loop iteration, print the car\'s current mileage and amount of fuel.

Solution

//FuelGauge class
class FuelGauge
{
    private int fuel;
    public static int max_capacity = 15;
  
    public FuelGauge()
    {
        fuel = 0;
    }

    // parameterized constructor
    public FuelGauge(int oil)
    {
        if (oil <= max_capacity)
        {
            fuel = oil;
        }
        else
        {
            fuel = max_capacity;
        }
    }

    // return amount of fuel
    public int getfuel()
    {
        return fuel;
    }

  
    // add fuel till max capacity is reached
    public void addfuel()
    {
        if (fuel < max_capacity)
        {
            fuel++;
        }

        else
        {
            System.out.println(\"Maximum capacity reached, Burn some fuel\ \");
        }
    }

   // burn the fuel
    public void burnFuel()
    {
        if (fuel > 0)
        {
            fuel--;
        }

        else
        {
            System.out.println(\"No fuel left, Add some fuel\ \");
        }
    }
}

//Odometer class

class Odometer
{
    // maximum mileage
    public int max_mileage = 999999;
    // miles per gallon
    public int fuel_economy = 22;
    private int startmileage;
    private int mileage;

    // FuelGauge object
    private FuelGauge fuelgauge;

    public Odometer(int mlg, FuelGauge fg) {
        startmileage = mlg;
        mileage = mlg;
        fuelgauge = fg;
    }


    public int getMileage()
    {
        return mileage;
    }


    public void addMileage()
    {

        if (mileage < max_mileage)
        {
            mileage++;
        }
        else
        {
            mileage = 0;
        }

        int d = startmileage - mileage;
        if (d % fuel_economy == 0)
        {
            fuelgauge.burnFuel();
        }
    }
}

//Test class
public class Test
{
   public static void main(String[] args)
   {

        FuelGauge f = new FuelGauge();
        Odometer o = new Odometer(0, f);

        for (int i = 0; i < FuelGauge.max_capacity; i++)
        {
            f.addfuel();
        }

        while (f.getfuel() > 0)
        {
            o.addMileage();
            System.out.println(\"Mileage: \" + o.getMileage());
            System.out.println(\"Fuel: \" + f.getfuel() + \" gallons\");
            System.out.println(\"\ \");
        }
   }
}

/*
output:

Mileage: 1
Fuel: 15 gallons


Mileage: 2
Fuel: 15 gallons


Mileage: 3
Fuel: 15 gallons


Mileage: 4
Fuel: 15 gallons


Mileage: 5
Fuel: 15 gallons


Mileage: 6
Fuel: 15 gallons


Mileage: 7
Fuel: 15 gallons


Mileage: 8
Fuel: 15 gallons


Mileage: 9
Fuel: 15 gallons


Mileage: 10
Fuel: 15 gallons


Mileage: 11
Fuel: 15 gallons


Mileage: 12
Fuel: 15 gallons


Mileage: 13
Fuel: 15 gallons


Mileage: 14
Fuel: 15 gallons


Mileage: 15
Fuel: 15 gallons


Mileage: 16
Fuel: 15 gallons


Mileage: 17
Fuel: 15 gallons


Mileage: 18
Fuel: 15 gallons


Mileage: 19
Fuel: 15 gallons


Mileage: 20
Fuel: 15 gallons


Mileage: 21
Fuel: 15 gallons


Mileage: 22
Fuel: 14 gallons


Mileage: 23
Fuel: 14 gallons


Mileage: 24
Fuel: 14 gallons


Mileage: 25
Fuel: 14 gallons


Mileage: 26
Fuel: 14 gallons


Mileage: 27
Fuel: 14 gallons


Mileage: 28
Fuel: 14 gallons


Mileage: 29
Fuel: 14 gallons


Mileage: 30
Fuel: 14 gallons


Mileage: 31
Fuel: 14 gallons


Mileage: 32
Fuel: 14 gallons


Mileage: 33
Fuel: 14 gallons


Mileage: 34
Fuel: 14 gallons


Mileage: 35
Fuel: 14 gallons


Mileage: 36
Fuel: 14 gallons


Mileage: 37
Fuel: 14 gallons


Mileage: 38
Fuel: 14 gallons


Mileage: 39
Fuel: 14 gallons


Mileage: 40
Fuel: 14 gallons


Mileage: 41
Fuel: 14 gallons


Mileage: 42
Fuel: 14 gallons


Mileage: 43
Fuel: 14 gallons


Mileage: 44
Fuel: 13 gallons


Mileage: 45
Fuel: 13 gallons


Mileage: 46
Fuel: 13 gallons


Mileage: 47
Fuel: 13 gallons


Mileage: 48
Fuel: 13 gallons


Mileage: 49
Fuel: 13 gallons


Mileage: 50
Fuel: 13 gallons


Mileage: 51
Fuel: 13 gallons


Mileage: 52
Fuel: 13 gallons


Mileage: 53
Fuel: 13 gallons


Mileage: 54
Fuel: 13 gallons


Mileage: 55
Fuel: 13 gallons


Mileage: 56
Fuel: 13 gallons


Mileage: 57
Fuel: 13 gallons


Mileage: 58
Fuel: 13 gallons


Mileage: 59
Fuel: 13 gallons


Mileage: 60
Fuel: 13 gallons


Mileage: 61
Fuel: 13 gallons


Mileage: 62
Fuel: 13 gallons


Mileage: 63
Fuel: 13 gallons


Mileage: 64
Fuel: 13 gallons


Mileage: 65
Fuel: 13 gallons


Mileage: 66
Fuel: 12 gallons


Mileage: 67
Fuel: 12 gallons


Mileage: 68
Fuel: 12 gallons


Mileage: 69
Fuel: 12 gallons


Mileage: 70
Fuel: 12 gallons


Mileage: 71
Fuel: 12 gallons


Mileage: 72
Fuel: 12 gallons


Mileage: 73
Fuel: 12 gallons


Mileage: 74
Fuel: 12 gallons


Mileage: 75
Fuel: 12 gallons


Mileage: 76
Fuel: 12 gallons


Mileage: 77
Fuel: 12 gallons


Mileage: 78
Fuel: 12 gallons


Mileage: 79
Fuel: 12 gallons


Mileage: 80
Fuel: 12 gallons


Mileage: 81
Fuel: 12 gallons


Mileage: 82
Fuel: 12 gallons


Mileage: 83
Fuel: 12 gallons


Mileage: 84
Fuel: 12 gallons


Mileage: 85
Fuel: 12 gallons


Mileage: 86
Fuel: 12 gallons


Mileage: 87
Fuel: 12 gallons


Mileage: 88
Fuel: 11 gallons


Mileage: 89
Fuel: 11 gallons


Mileage: 90
Fuel: 11 gallons


Mileage: 91
Fuel: 11 gallons


Mileage: 92
Fuel: 11 gallons


Mileage: 93
Fuel: 11 gallons


Mileage: 94
Fuel: 11 gallons


Mileage: 95
Fuel: 11 gallons


Mileage: 96
Fuel: 11 gallons


Mileage: 97
Fuel: 11 gallons


Mileage: 98
Fuel: 11 gallons


Mileage: 99
Fuel: 11 gallons


Mileage: 100
Fuel: 11 gallons


Mileage: 101
Fuel: 11 gallons


Mileage: 102
Fuel: 11 gallons


Mileage: 103
Fuel: 11 gallons


Mileage: 104
Fuel: 11 gallons


Mileage: 105
Fuel: 11 gallons


Mileage: 106
Fuel: 11 gallons


Mileage: 107
Fuel: 11 gallons


Mileage: 108
Fuel: 11 gallons


Mileage: 109
Fuel: 11 gallons


Mileage: 110
Fuel: 10 gallons


Mileage: 111
Fuel: 10 gallons


Mileage: 112
Fuel: 10 gallons


Mileage: 113
Fuel: 10 gallons


Mileage: 114
Fuel: 10 gallons


Mileage: 115
Fuel: 10 gallons


Mileage: 116
Fuel: 10 gallons


Mileage: 117
Fuel: 10 gallons


Mileage: 118
Fuel: 10 gallons


Mileage: 119
Fuel: 10 gallons


Mileage: 120
Fuel: 10 gallons


Mileage: 121
Fuel: 10 gallons


Mileage: 122
Fuel: 10 gallons


Mileage: 123
Fuel: 10 gallons


Mileage: 124
Fuel: 10 gallons


Mileage: 125
Fuel: 10 gallons


Mileage: 126
Fuel: 10 gallons


Mileage: 127
Fuel: 10 gallons


Mileage: 128
Fuel: 10 gallons


Mileage: 129
Fuel: 10 gallons


Mileage: 130
Fuel: 10 gallons


Mileage: 131
Fuel: 10 gallons


Mileage: 132
Fuel: 9 gallons


Mileage: 133
Fuel: 9 gallons


Mileage: 134
Fuel: 9 gallons


Mileage: 135
Fuel: 9 gallons


Mileage: 136
Fuel: 9 gallons


Mileage: 137
Fuel: 9 gallons


Mileage: 138
Fuel: 9 gallons


Mileage: 139
Fuel: 9 gallons


Mileage: 140
Fuel: 9 gallons


Mileage: 141
Fuel: 9 gallons


Mileage: 142
Fuel: 9 gallons


Mileage: 143
Fuel: 9 gallons


Mileage: 144
Fuel: 9 gallons


Mileage: 145
Fuel: 9 gallons


Mileage: 146
Fuel: 9 gallons


Mileage: 147
Fuel: 9 gallons


Mileage: 148
Fuel: 9 gallons


Mileage: 149
Fuel: 9 gallons


Mileage: 150
Fuel: 9 gallons


Mileage: 151
Fuel: 9 gallons


Mileage: 152
Fuel: 9 gallons


Mileage: 153
Fuel: 9 gallons


Mileage: 154
Fuel: 8 gallons


Mileage: 155
Fuel: 8 gallons


Mileage: 156
Fuel: 8 gallons


Mileage: 157
Fuel: 8 gallons


Mileage: 158
Fuel: 8 gallons


Mileage: 159
Fuel: 8 gallons


Mileage: 160
Fuel: 8 gallons


Mileage: 161
Fuel: 8 gallons


Mileage: 162
Fuel: 8 gallons


Mileage: 163
Fuel: 8 gallons


Mileage: 164
Fuel: 8 gallons


Mileage: 165
Fuel: 8 gallons


Mileage: 166
Fuel: 8 gallons


Mileage: 167
Fuel: 8 gallons


Mileage: 168
Fuel: 8 gallons


Mileage: 169
Fuel: 8 gallons


Mileage: 170
Fuel: 8 gallons


Mileage: 171
Fuel: 8 gallons


Mileage: 172
Fuel: 8 gallons


Mileage: 173
Fuel: 8 gallons


Mileage: 174
Fuel: 8 gallons


Mileage: 175
Fuel: 8 gallons


Mileage: 176
Fuel: 7 gallons


Mileage: 177
Fuel: 7 gallons


Mileage: 178
Fuel: 7 gallons


Mileage: 179
Fuel: 7 gallons


Mileage: 180
Fuel: 7 gallons


Mileage: 181
Fuel: 7 gallons


Mileage: 182
Fuel: 7 gallons


Mileage: 183
Fuel: 7 gallons


Mileage: 184
Fuel: 7 gallons


Mileage: 185
Fuel: 7 gallons


Mileage: 186
Fuel: 7 gallons


Mileage: 187
Fuel: 7 gallons


Mileage: 188
Fuel: 7 gallons


Mileage: 189
Fuel: 7 gallons


Mileage: 190
Fuel: 7 gallons


Mileage: 191
Fuel: 7 gallons


Mileage: 192
Fuel: 7 gallons


Mileage: 193
Fuel: 7 gallons


Mileage: 194
Fuel: 7 gallons


Mileage: 195
Fuel: 7 gallons


Mileage: 196
Fuel: 7 gallons


Mileage: 197
Fuel: 7 gallons


Mileage: 198
Fuel: 6 gallons


Mileage: 199
Fuel: 6 gallons


Mileage: 200
Fuel: 6 gallons


Mileage: 201
Fuel: 6 gallons


Mileage: 202
Fuel: 6 gallons


Mileage: 203
Fuel: 6 gallons


Mileage: 204
Fuel: 6 gallons


Mileage: 205
Fuel: 6 gallons


Mileage: 206
Fuel: 6 gallons


Mileage: 207
Fuel: 6 gallons


Mileage: 208
Fuel: 6 gallons


Mileage: 209
Fuel: 6 gallons


Mileage: 210
Fuel: 6 gallons


Mileage: 211
Fuel: 6 gallons


Mileage: 212
Fuel: 6 gallons


Mileage: 213
Fuel: 6 gallons


Mileage: 214
Fuel: 6 gallons


Mileage: 215
Fuel: 6 gallons


Mileage: 216
Fuel: 6 gallons


Mileage: 217
Fuel: 6 gallons


Mileage: 218
Fuel: 6 gallons


Mileage: 219
Fuel: 6 gallons


Mileage: 220
Fuel: 5 gallons


Mileage: 221
Fuel: 5 gallons


Mileage: 222
Fuel: 5 gallons


Mileage: 223
Fuel: 5 gallons


Mileage: 224
Fuel: 5 gallons


Mileage: 225
Fuel: 5 gallons


Mileage: 226
Fuel: 5 gallons


Mileage: 227
Fuel: 5 gallons


Mileage: 228
Fuel: 5 gallons


Mileage: 229
Fuel: 5 gallons


Mileage: 230
Fuel: 5 gallons


Mileage: 231
Fuel: 5 gallons


Mileage: 232
Fuel: 5 gallons


Mileage: 233
Fuel: 5 gallons


Mileage: 234
Fuel: 5 gallons


Mileage: 235
Fuel: 5 gallons


Mileage: 236
Fuel: 5 gallons


Mileage: 237
Fuel: 5 gallons


Mileage: 238
Fuel: 5 gallons


Mileage: 239
Fuel: 5 gallons


Mileage: 240
Fuel: 5 gallons


Mileage: 241
Fuel: 5 gallons


Mileage: 242
Fuel: 4 gallons


Mileage: 243
Fuel: 4 gallons


Mileage: 244
Fuel: 4 gallons


Mileage: 245
Fuel: 4 gallons


Mileage: 246
Fuel: 4 gallons


Mileage: 247
Fuel: 4 gallons


Mileage: 248
Fuel: 4 gallons


Mileage: 249
Fuel: 4 gallons


Mileage: 250
Fuel: 4 gallons


Mileage: 251
Fuel: 4 gallons


Mileage: 252
Fuel: 4 gallons


Mileage: 253
Fuel: 4 gallons


Mileage: 254
Fuel: 4 gallons


Mileage: 255
Fuel: 4 gallons


Mileage: 256
Fuel: 4 gallons


Mileage: 257
Fuel: 4 gallons


Mileage: 258
Fuel: 4 gallons


Mileage: 259
Fuel: 4 gallons


Mileage: 260
Fuel: 4 gallons


Mileage: 261
Fuel: 4 gallons


Mileage: 262
Fuel: 4 gallons


Mileage: 263
Fuel: 4 gallons


Mileage: 264
Fuel: 3 gallons


Mileage: 265
Fuel: 3 gallons


Mileage: 266
Fuel: 3 gallons


Mileage: 267
Fuel: 3 gallons


Mileage: 268
Fuel: 3 gallons


Mileage: 269
Fuel: 3 gallons


Mileage: 270
Fuel: 3 gallons


Mileage: 271
Fuel: 3 gallons


Mileage: 272
Fuel: 3 gallons


Mileage: 273
Fuel: 3 gallons


Mileage: 274
Fuel: 3 gallons


Mileage: 275
Fuel: 3 gallons


Mileage: 276
Fuel: 3 gallons


Mileage: 277
Fuel: 3 gallons


Mileage: 278
Fuel: 3 gallons


Mileage: 279
Fuel: 3 gallons


Mileage: 280
Fuel: 3 gallons


Mileage: 281
Fuel: 3 gallons


Mileage: 282
Fuel: 3 gallons


Mileage: 283
Fuel: 3 gallons


Mileage: 284
Fuel: 3 gallons


Mileage: 285
Fuel: 3 gallons


Mileage: 286
Fuel: 2 gallons


Mileage: 287
Fuel: 2 gallons


Mileage: 288
Fuel: 2 gallons


Mileage: 289
Fuel: 2 gallons


Mileage: 290
Fuel: 2 gallons


Mileage: 291
Fuel: 2 gallons


Mileage: 292
Fuel: 2 gallons


Mileage: 293
Fuel: 2 gallons


Mileage: 294
Fuel: 2 gallons


Mileage: 295
Fuel: 2 gallons


Mileage: 296
Fuel: 2 gallons


Mileage: 297
Fuel: 2 gallons


Mileage: 298
Fuel: 2 gallons


Mileage: 299
Fuel: 2 gallons


Mileage: 300
Fuel: 2 gallons


Mileage: 301
Fuel: 2 gallons


Mileage: 302
Fuel: 2 gallons


Mileage: 303
Fuel: 2 gallons


Mileage: 304
Fuel: 2 gallons


Mileage: 305
Fuel: 2 gallons


Mileage: 306
Fuel: 2 gallons


Mileage: 307
Fuel: 2 gallons


Mileage: 308
Fuel: 1 gallons


Mileage: 309
Fuel: 1 gallons


Mileage: 310
Fuel: 1 gallons


Mileage: 311
Fuel: 1 gallons


Mileage: 312
Fuel: 1 gallons


Mileage: 313
Fuel: 1 gallons


Mileage: 314
Fuel: 1 gallons


Mileage: 315
Fuel: 1 gallons


Mileage: 316
Fuel: 1 gallons


Mileage: 317
Fuel: 1 gallons


Mileage: 318
Fuel: 1 gallons


Mileage: 319
Fuel: 1 gallons


Mileage: 320
Fuel: 1 gallons


Mileage: 321
Fuel: 1 gallons


Mileage: 322
Fuel: 1 gallons


Mileage: 323
Fuel: 1 gallons


Mileage: 324
Fuel: 1 gallons


Mileage: 325
Fuel: 1 gallons


Mileage: 326
Fuel: 1 gallons


Mileage: 327
Fuel: 1 gallons


Mileage: 328
Fuel: 1 gallons


Mileage: 329
Fuel: 1 gallons


Mileage: 330
Fuel: 0 gallons

*/

 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you
 Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car\'s fuel gauge and odometer. The classes you

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site