In c# programming: A) Write a class called Robot with the following three instan
ID: 3763160 • Letter: I
Question
In c# programming:
A) Write a class called Robot with the following three instance variables: name (string), age (ushort), isOn (bool). Make the program initialize the instance variables to “unknown”, 0, and false, respectively (without using constructors). Include accessor and mutator methods to assign and retrieve the values of these instance variables.
B) Allow the users of the Robot class to set the initial values of the three instance variables when a Robot object is created. To this end declare one constructor with three formal parameters and one default constructor.
C) Include a member variable called robotsCreated that keeps track of the number of Robot objects created. Make the constructors update the robotsCreated variable so it is always up to date. Implement the following logic: If robotsCreated is less than 5 when the constructor is called, set isOn to true; otherwise false
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Robot_appli1
{
class Robot
{
public String name = "unkonw";
public int age = 0;
public Boolean isOn = false;
public int robotsCreated = 0;
public Robot()
{
}
public Robot(String s, int a, Boolean i)
{
name = s;
age = a;
isOn = i;
robotsCreated++;
if (robotsCreated < 5)
{
isOn = true;
}
else
{
isOn = false;
}
}
}
class Program
{
static void Main(string[] args)
{
Robot o = new Robot("aaa",23,true);
Robot o = new Robot("bbb",25,false);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.