patterncsharpModerate
Verifying if a car is a sports car based on speed and horse power
Viewed 0 times
verifyingcarsportspowerbasedandhorsespeed
Problem
I have an assignment where I am required to verify if a car is a sports car or not by minimum max speed and minimum horse power. I have finished the program and it works great. I was just wanting to see if you can review my assignment and see if there is room for improvement.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Project3
{
class sportsCar
{
public int _maxSpeed = 0;
public int _horsepower = 0;
private static int _MinimumRequiredSpeed = 150;
private static int _MinimumRequiredHorsepower = 250;
public bool IsSportsCar()
{
if ((_maxSpeed >= _MinimumRequiredSpeed) && (_horsepower >= _MinimumRequiredHorsepower))
return true;
else
return false;
}
public void maxSpeed(int newmaxSpeed)
{
_maxSpeed = newmaxSpeed;
}
public void horsepower(int newhorsepower)
{
_horsepower = newhorsepower;
}
}
class Program
{
static void Main(string[] args)
{
sportsCar car = new sportsCar();
Console.Write("What is the max speed? ");
int newmaxSpeed = int.Parse(Console.ReadLine());
Console.Write("What is the max horsepower? ");
int newhorsepower = int.Parse(Console.ReadLine());
car.maxSpeed(newmaxSpeed);
car.horsepower(newhorsepower);
bool sportCar = car.IsSportsCar();
if (sportCar == true)
{
Console.WriteLine("This is Definately a Sports Car");
}
else
{
Console.WriteLine("This is NOT a Sports Car");
}
}
}
}Solution
_maxSpeed and _horsePower should be private.You have (confusingly named) setter methods for public variables, but no getters. That makes for an odd interface.
In C# you by convention use properties for getters and setters. So instead of
public int _maxSpeed = 0;
public int _horsepower = 0;
public void maxSpeed(int newmaxSpeed)
{
_maxSpeed = newmaxSpeed;
}
public void horsepower(int newhorsepower)
{
_horsepower = newhorsepower;
}You have
public int MaxSpeed { get; set; }
public int HorsePower { get; set; }Since these values are unlikely to change, you could make the type immutable:
class Car
{
public readonly int MaxSpeed;
public readonly int HorsePower;
public Car( int maxSpeed, int horsePower )
{
MaxSpeed = maxSpeed;
HorsePower = horsePower;
}
}Instead of checking a boolean expression and then returning a literal
true or false, just return the expression instead. Booleans are a type just like any other. Also, you don't need the comparison to true or false for the same reason.if( someCondition == true )
return true;
else
return false;You have
if( someCondition )
return true;
else
return false;or better yet
return someCondition;So
return _maxSpeed >= _MinimumRequiredSpeed &&
_horsepower >= _MinimumRequiredHorsepower;Whenever you take user input you need to check it. Your program will crash if the user enters something that is not a number. You can check this by using
TryParse instead of Parse.int horsePower;
do
{
string input = Console.ReadLine();
}
while( !int.TryParse( input, out horsePower ) );Other than that it is fine, keep at it ;)
Code Snippets
public int _maxSpeed = 0;
public int _horsepower = 0;
public void maxSpeed(int newmaxSpeed)
{
_maxSpeed = newmaxSpeed;
}
public void horsepower(int newhorsepower)
{
_horsepower = newhorsepower;
}public int MaxSpeed { get; set; }
public int HorsePower { get; set; }class Car
{
public readonly int MaxSpeed;
public readonly int HorsePower;
public Car( int maxSpeed, int horsePower )
{
MaxSpeed = maxSpeed;
HorsePower = horsePower;
}
}if( someCondition == true )
return true;
else
return false;if( someCondition )
return true;
else
return false;Context
StackExchange Code Review Q#5378, answer score: 15
Revisions (0)
No revisions yet.