Z. Josh
Z. Josh

Reputation: 5

Need Help fixing incorrect output to console window.

I'm working on an assignment for school that consists of both a Program.cs file and a separate class called Car. I have written all code for the Car class and pasted already provided code into the program.cs file. The resulting output is

2010 Ford Focus is going 20 MPH.
2018 Chevy Cruze is going 0 MPH.

The assignment calls for an expected output of

2010 Ford Focus is going 28 MPH.
2018 Chevy Cruze is going 18 MPH.

I need to know how to get the window to output the expected speed of 28 for Car 1 and 18 for car 2. I'm assuming that I'm not supposed to alter the code that was provided for program.cs to accomplish the right output for the application/assignment.

public class Car
{
    private int Speed;
    private string Make;
    private string Model;
    private int Year;

    public Car (string make, string model, int year, int speed)
    {
        this.Make = make;
        this.Model = model;
        this.Year = year;
        this.Speed = speed;
    }

    public Car(string make, string model, int year)
    {
        this.Make = make;
        this.Model = model;
        this.Year = year;
        this.Speed = 0;
    }

    public int SpeedUp()
    {
        this.Speed = Speed++;
        return (Speed);
    }

    public int SlowDown()
    {
        if (Speed > 0)
        {
            this.Speed = Speed--;   
        }

        return (Speed);
    }

    public void Display()
    {
        Console.WriteLine(Year + " " + Make + " " + Model + " is going " + Speed + " MPH.");
        Convert.ToString(Console.ReadLine());
    }
}

and here is the given code that goes in program.cs

class Program
{
    static void Main(string[] args)
    {
        int car1Speed = 20;
        int car2Speed = 0;
        Car car1 = new Car("Ford", "Focus", 2010, car1Speed);
        Car car2 = new Car("Chevy", "Cruze", 2018, car2Speed);

        for (int i = 0; i < 60; i++)
        {
            if (i % 2 == 0)
            {
               car2Speed = car2.SpeedUp();
            }
            if (i % 3 == 0)
            {
                car1Speed = car1.SpeedUp();
            }
            if (i % 5 == 0)
            {
                car1Speed = car1.SlowDown();
                car2Speed = car2.SlowDown();
            }
        }

        car1.Display();
        car2.Display();
    }
}

Upvotes: 0

Views: 130

Answers (2)

Alex
Alex

Reputation: 36

The line of code

this.Speed = Speed++;

Has no effect on the value of this.Speed.

The line of code is roughly equivalent to

int temp = this.Speed; // We store the original value of Speed.
this.Speed = Speed + 1; // We add one to Speed and assign it back to Speed.
this.Speed = temp; // We immediately replace Speed with the original value of Speed.

This is the due to the behavior of the '++' operator which, when appended to the end of a variable will add 1 to its current value and then assigns the result back to the variable. This operator however temporarily stores the original value and uses that as the result of the expression. This temporary result of the ++ operator is ultimately what you are assigning to this.Speed due to the '=' operator you're using in the same expression, this in turn is what is causing the variable to not actually be modified.

The correct usage would be to simply call

Speed++;

This will perform the addition of 1 and assignment back to Speed, and also discard the temporary variable, as it is not being assigned anywhere.

This issue also exists in your SpeedDown method with the '--' operator.

Upvotes: 2

MaestroNate
MaestroNate

Reputation: 33

In your Car.cs class, here is a better way to increment the speed without modifying the Program.cs file:

public int SpeedUp()
{
    this.Speed += 1;
    return (Speed);
}
public int SlowDown()
{
    if (Speed > 0)
    {
        this.Speed -= 1;
    }
    return (Speed);
}

That produces the output that you're after.

Upvotes: 2

Related Questions