### Task 4 - OOP Assignment 1 - Solution

The Assignment description is given at: http://www.bitspedia.com/2017/02/task-4-oop-assignment-1.html. Here I provide you the solution code for reference. There were following objectives of this assignments, I hope you well understand then now:

1. How to decide when we need to hide data and when not to hide. Or when to define a get and set method.
2. How to update object attributes based on other attribute values
3. How to decide right place to implement a functionality

```public class AutomaticCar {
private String model;
private int year;
private int speed;
private int gear;
private int odoMeter;
private double averageGear;

public AutomaticCar(String model, int year) {
this.model = model;
this.year = year;
}

public void setSpeed(int speed) {
if (speed < 0) {
System.out.println("Speed is invalid");
return;
}

this.speed = speed;
if (speed == 0) {
gear = 0;
} else if (speed >= 1 && speed <= 100) {
gear = 1;
} else if (speed > 100 && speed <= 200) {
gear = 2;
} else if (speed > 200 && speed <= 300) {
gear = 3;
} else if (speed > 300 && speed <= 400) {
gear = 4;
} else if (speed > 400 && speed <= 500) {
gear = 5;
}
odoMeter = odoMeter + speed;
averageGear = (averageGear + gear) / 2;
}

public void showTrack() {
System.out.println();
for (int count = 0; count < (int) getOdoMeter() / 50; count++) {
System.out.print("-");
}
System.out.printf(" %d km.  Model: %s", getOdoMeter(), getModel());
}

public int getAverageGear() {
return (int)Math.floor(averageGear);
}

public void printCarInfo() {
System.out.println("{" +
"model=" + model +
", year=" + year +
", averageGear=" + getAverageGear() +
", odoMeter=" + odoMeter +
'}');
}

public String getModel() {
return model;
}

public int getYear() {
return year;
}

public int getSpeed() {
return speed;
}

public int getGear() {
return gear;
}

public int getOdoMeter() {
return odoMeter;
}

public void setYear(int year) {
this.year = year;
}

public void setModel(String model) {
this.model = model;
}
}```
```import java.util.*;

public class LuckyRacer {
private static Random randomNumberGenerator = new Random();

public static void main(String[] args) {
Scanner input1 = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
String model;
int year;

AutomaticCar car1, car2, car3, car4, car5;

System.out.println("Enter the Model and Year of Manufacturing for Car 1: ");
model = input1.nextLine();
year = input2.nextInt();
car1 = new AutomaticCar(model, year);

System.out.println("Enter the Model and Year of Manufacturing for Car 2: ");
model = input1.nextLine();
year = input2.nextInt();
car2 = new AutomaticCar(model, year);

System.out.println("Enter the Model and Year of Manufacturing for Car 3: ");
model = input1.nextLine();
year = input2.nextInt();
car3 = new AutomaticCar(model, year);

System.out.println("Enter the Model and Year of Manufacturing for Car 4: ");
model = input1.nextLine();
year = input2.nextInt();
car4 = new AutomaticCar(model, year);

System.out.println("Enter the Model and Year of Manufacturing for Car 5: ");
model = input1.nextLine();
year = input2.nextInt();
car5 = new AutomaticCar(model, year);

for (int lap = 1; lap <= 10; lap++) {

System.out.printf("\nLap %d Detail.", lap);

car1.setSpeed(getRandomSpeed());
car2.setSpeed(getRandomSpeed());
car3.setSpeed(getRandomSpeed());
car4.setSpeed(getRandomSpeed());
car5.setSpeed(getRandomSpeed());

car1.showTrack();
car2.showTrack();
car3.showTrack();
car4.showTrack();
car5.showTrack();

System.out.println();
}

int winner = 0;
int winnerDistance = 0;

if (car1.getOdoMeter() > winnerDistance) {
winner = 1;
winnerDistance = car1.getOdoMeter();
}
if (car2.getOdoMeter() > winnerDistance) {
winner = 2;
winnerDistance = car2.getOdoMeter();
}
if (car3.getOdoMeter() > winnerDistance) {
winner = 3;
winnerDistance = car3.getOdoMeter();
}
if (car4.getOdoMeter() > winnerDistance) {
winner = 4;
winnerDistance = car4.getOdoMeter();
}
if (car5.getOdoMeter() > winnerDistance) {
winner = 5;
winnerDistance = car5.getOdoMeter();
}

System.out.printf("\n\n Car %d Won the Race. \n", winner);
if (winner == 1) {
car1.printCarInfo();
} else if (winner == 2) {
car2.printCarInfo();
} else if (winner == 3) {
car3.printCarInfo();
} else if (winner == 4) {
car4.printCarInfo();
} else if (winner == 5) {
car5.printCarInfo();
}
}

private static int getRandomSpeed() {
return randomNumberGenerator.nextInt(500) + 1;
}
}```

```import java.util.Random;
import java.util.Scanner;

public class LuckyRacerWithArrays {
private static Random randomNumberGenerator = new Random();

public static void main(String[] args) {
Scanner input1 = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
String model;
int year, carsCount, lapsCount;

System.out.println("Enter the number of cars and number of laps?");
carsCount = input2.nextInt();
lapsCount = input2.nextInt();

AutomaticCar cars[] = new AutomaticCar[carsCount];

for (int i = 0; i < cars.length; i++) {
System.out.println("\nEnter the Model and Year of Manufacturing for Car " + (i + 1));
model = input1.nextLine();
year = input2.nextInt();
cars[i] = new AutomaticCar(model, year);
}

for (int lap = 1; lap <= lapsCount; lap++) {
System.out.printf("\nLap %d Detail.", lap);
for(int i=0; i < cars.length; i++){
cars[i].setSpeed(getRandomSpeed());
cars[i].showTrack();
}
System.out.println();
}

int winner = 0;
int winnerDistance = 0;
for(int i=0; i winnerDistance) {
winner = i + 1;
winnerDistance = cars[i].getOdoMeter();
}
}
System.out.printf("\n\n Car %d Won the Race. \n", winner);
for(int i=0; i < cars . length; i++){
if(winner == i+1){
cars[i].printCarInfo();
}
}
}

private static int getRandomSpeed() {
return randomNumberGenerator.nextInt(500) + 1;
}
}```