Instead of having WriteLine() method three times I wish to know whether is there any way to deploy display() method as shown in the following and develop the program appropriately.
// Creates a Boat class
// And instantiates three Boat objects
using System;
public class DebugFour1
{
public static void Main()
{
Boat aRowBoat = new Boat();
Boat aSkiBoat = new Boat();
Boat aYacht = new Boat();
aRowBoat.SetLicense("W2453");
aRowBoat.SetState("WI");
aSkiBoat.SetLicense("M6120");
aSkiBoat.SetState("MI");
aYacht.SetLicense("M5322");
aYacht.SetState("MA");
display(aRowBoat);
display(aSkiBoat);
display(aYacht);
}
static void display(object aBoat)
{
Console.WriteLine("Boat {0} from {1} has a {2} HP motor.", aBoat.GetLicense(), aBoat.GetState(), aBoat.GetMotor());
}
}
class Boat
{
private string licenseNum;
private string state;
public string GetLicense()
{
return licenseNum;
}
public string GetState()
{
return state;
}
public void SetLicense(string licNum)
{
licenseNum = licNum;
}
public void SetState(string st)
{
state = st;
}
}
Loading
Posted Dec 3, 2011, 6:05 PM
Sam HobbsPosted Dec 3, 2011, 5:51 PM
In case anyone is wondering why my post is formatted in such a mess, the format is caused by the forum's software. I am doing my best to get around problems with the software but it is a moving target; what worked in the past does not work today.
Posted Dec 3, 2011, 5:05 PM
Sam HobbsPosted Dec 3, 2011, 4:07 PM
The following is another way. It is similar to what Vulpes shows. It is not
exactly what you asked for but it is object-oriented. Note that I am using
properties but you do not have to use properties. I am sorry if the properties
syntax is confusing for you; perhaps I should not have done that. Note also that
a constructor could make the code even smaller, but I don't know if that would
be practical for the data you actually have.
using System;
namespace _151724
{
class Program
{
static void Main(string[] args)
{
Boat aRowBoat = new Boat();
Boat aSkiBoat = new Boat();
Boat aYacht = new Boat();
aRowBoat.License = "W2453";
aRowBoat.State = "WI";
aRowBoat.Motor = 10;
aSkiBoat.License = "M6120";
aSkiBoat.State = "MI";
aSkiBoat.Motor = 10;
aYacht.License = "M5322";
aYacht.State = "MA";
aYacht.Motor = 10;
Boat[] Boats = new Boat[] {aRowBoat, aSkiBoat, aYacht};
foreach (Boat b in Boats)
b.Display();
}
}
class Boat
{
public string License
{
get;
set;
}
public string State
{
get;
set;
}
public int Motor
{
get;
set;
}
public void Display()
{
Console.WriteLine("Boat {0} from {1} has a {2} HP motor.", License, State, Motor);
}
}
}
Posted Dec 3, 2011, 1:36 PM
VulpesPosted Dec 3, 2011, 1:25 PM