Question

C# Goals: Practice building a class Use object oriented programming to solve a problem Problem Description:...

C#

Goals:

Practice building a class

Use object oriented programming to solve a problem

Problem Description:

Create a Windows Console Application using the .NET Framework For this application you will be designing a program that allows

users to keep track of robot inventory. First, you will design a Robot class, and then

build an application that creates Robot objects.

Important formulas / relationships:

Before we begin designing robots, we should understand a few key electrical terms and

relationships.

Terms:

Voltage (V)

- The electrical potential between two points, measured in volts.

Current (I)

- The flow of electrons through a material, measured in amps.

Resistance (R)

- A measure of the restriction of current flow through a material,

measured in ohms.

Current = Voltage / Resistance

Power (P)

- The measure of electrical energy over time, measured in watts.

Power = Voltage * Current

Watt-Hours (Wh)

- A Measure of how much power could be supplied in 1 hour. A

typical AA battery has a voltage of 1.5 Volts, and a supply of 3.75 Watt-Hours. This

means a AA battery could supply 3.75 Watts of power for 1 hour, or 7.5 Watts of power

for 30 minutes.

Power supply length ( in hours ) = Total Watt-Hours / Power

Summary:

I = V / R

P = V * I

Time = Wh / P

1 AA Battery = 1.5 Volts, 3.75 Wh

Robot Class Details:

Add a

Robot

class to your project. Include all fields, properties, methods and

constructors to properly build a Robot object with the following attributes and behaviors:

Attributes:

ID (string value) -- must be a length of 4 or 5 characters

Number of AA Batteries (int value) -- must be between 2 and 6

Resistance (double value) -- can be any value greater than 0

Behaviors:

GetPower() -- returns the power, in Watts, of this robot.

GetRunTime() -- returns how long this robot will run, given its number of batteries

(!) Remember -- each AA battery can produce 3.75 Watt-Hours of energy

Summary() -- prints the ID, Number of Batteries, Power, and RunTime values of

this robot

NOTE: Protect your class against invalid values!

Main() Program Details:

In Main(), create a program that allows the user to to enter in robot information for a

number of robots, and then calculates and prints a robot summary report.

Prompt the user to enter in the number of robots they wish to build.

For each robot, Prompt for the robot’s ID, Number of AA Batteries, and

Resistance

After all robots have been entered, output the following:

How many total batteries are required for all the robots entered?

What is the average Power of all robots entered?

Which Robot ID has the longest runtime?

Print the Summary for all robots entered.

Hint: Use object’s built from the Robot class to solve these problems!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

--------------------------I used Visual Studio 2013, C# language, Console App-----------

------OUTPUT-------

file:///D:/Hari/Ro Enter the number of robots wish to build:3 bin Debu Enter Robot 1 details Enter ID[Max character length is

CODE:

I created 2 Classes, one is Program(Contains main method) and another Robot class

-------Program.cs-----

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RobotProject
{
class Program
{
static int Main(string[] args)
{
Robot[] robotArray;
int result;
double Resistance;
int totalBatteries = 0;
double totalPower = 0;
double longestRunTime = 0;
string IDs = string.Empty;
int count = 0;
Console.Write("Enter the number of robots wish to build:");
if (int.TryParse(Console.ReadLine(), out result))
robotArray = new Robot[result];
else
{
Console.WriteLine("Enter integer values only");
return 0;
}
for (int i = 0; i < robotArray.Length; i++)
{
robotArray[i] = new Robot();
count = i + 1;
Console.WriteLine("------Enter Robot " + count + " details------");
Console.Write("Enter ID[Max character length is 5]: ");
string ID = Console.ReadLine();
if (ID.Length <= 5)
robotArray[i].ID = ID;
else
{
Console.WriteLine("Maximum character length is 5");
return 0;
}

Console.Write("Enter no of AA batteries[Integer between 2 and 6]: ");
if (int.TryParse(Console.ReadLine(), out result) && result >= 2 && result <= 6)
{
totalBatteries += result;
robotArray[i].noofAAbatteries = result;
}
else
{
Console.WriteLine("Enter integer only, in range of 2 to 6");
return 0;
}

Console.Write("Enter resistance: ");
if (double.TryParse(Console.ReadLine(), out Resistance) && Resistance > 0)
robotArray[i].Resistance = Resistance;
else
Console.WriteLine("Enter values greater than zero");
totalPower += robotArray[i].GetPower();
}
Console.WriteLine("Total batteries required for all the robots entered is " + totalBatteries);
Console.WriteLine("Average Power of all robots entered is "+(totalPower/robotArray.Length).ToString("0.00")+" watt");
foreach (Robot robot in robotArray)
{
double RunTime = robot.GetRunTime();
if (RunTime > longestRunTime)
{
longestRunTime = RunTime;
IDs = robot.ID;
}
}
Console.WriteLine("Robot ID: " + IDs + " has the longest runtime of " + longestRunTime.ToString("0.00")+" hour's");
Console.WriteLine("Summary for all robots entered is: ");
count = 0;
foreach (Robot robot in robotArray)
{
count++;
Console.WriteLine("------Robot " + count + " details------");
robot.Summary();
}
  
Console.ReadLine();
return 0;
}
}
}

--------Robot.cs----------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RobotProject
{
class Robot
{
double power;
// ID (string value) -- must be a length of 4 or 5 characters
public string ID{set;get;}
//Number of AA Batteries (int value) -- must be between 2 and 6
public int noofAAbatteries{set;get;}
//Resistance (double value) -- can be any value greater than 0
public double Resistance{set;get;}

//GetPower() -- returns the power, in Watts, of this robot.

public double GetPower()
{
//Power = Voltage * Current
//Current = Voltage / Resistance
//AA battery has a voltage of 1.5 Volts
//P=(V*V)/R
power = ((1.5 * 1.5) / this.Resistance);
return power;
}
//GetRunTime() -- returns how long this robot will run, given its number of batteries
public double GetRunTime()
{
//Each AA battery can produce 3.75 Watt-Hours of energy
double time = (this.noofAAbatteries * 3.75) / power;
  
return time;
}

//Summary() -- prints the ID, Number of Batteries, Power, and RunTime values of this robot
public void Summary()
{
Console.WriteLine("ID: "+this.ID);
Console.WriteLine("Number of Batteries: " + this.noofAAbatteries);
Console.WriteLine("Power: " + GetPower().ToString("0.00")+" watt");
Console.WriteLine("RunTime: " + GetRunTime().ToString("0.00")+" hour's");
}
}
}

Add a comment
Know the answer?
Add Answer to:
C# Goals: Practice building a class Use object oriented programming to solve a problem Problem Description:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT