Question

please help me with this in C# language. Constructors The goal for this exercise is to...

please help me with this in C# language.

Constructors

The goal for this exercise is to understand what constructors are, how to define them, and how to call them, including ‘default’ constructors, and including the use of overloading to provide multiple constructors.

One of the advantages of having a clear separation between the public interface of an object and private internal implementation of an object is that once you've got the data in the object you can then ask the object to do things, such as saying "Hey, object – print yourself!" with a command like theDishwasher.Print();.   This is all well and good, as long as each object has valid data stored inside it. But what if there isn't valid data stored inside it? (Perhaps a co-worker created the object, and forgot to properly initialize it) We can still tell the object to do things, but it won't be nearly as useful.

It would be great if the C# language (and the C# compiler) could be used to help us remember to initialize each object, as we create it. As it turns out, there is a language feature that does this, exactly. A constructor is a special method that will always be called when the object is created (it will be called by the new command, essentially). The method exists to construct (to initialize) each object, so as to ensure that each object has valid data assigned to it. A default constructor is a constructor that has no parameters, and is useful because it allows other people to create instances of your object without knowing much about it (for example, if you wanted to add a control (a software component/block) to Visual Studio, VS may create instances of the object using the default constructor). You can also overload your constructor, which is normally what's done (so that people have several options about how much info they need to provide, in order to properly initialize the new object). One neat trick is that you can use the this(…); statement, as the first line in one of your constructors, in order to call a different constructor.

What you need to do for this exercise:

Within the provided starter project, there is already a class named Constructors_Exercise. You should create a class named Dishwasher below it, in the space indicated. Each Dishwasher object can hold a maximum number of glasses, and is currently holding between zero and that maximum number. For example, a small dishwasher might hold a maximum of 5 glasses, and currently be holding 2, while a larger dishwasher might hold a maximum of 20 glasses, and currently be holding 0 (none). Similarly, each Dishwasher object can hold a maximum number of plates, and a maximum number of bowls, and is currently holding some number of plates, and bowls.

Additionally, you should define a method named Print(), which takes no parameters, and returns no data. When called, Print() will print out how many of each item the Dishwasher object is currently holding, out of what maximum.

What’s new for this exercise:  

  1. Implement the Dishwasher class, as described above.
    1. Don’t forget to make the Dishwasher class public, so that the tests can access the class, like so:
      public class Dishwasher
    2. For this exercise, you should make all the instance variables private, like normal. However, you are not required to create any getter/setter methods. You are welcome to do so (if you want to), but it is not required for this exercise.
  2. Make sure that your Dishwasher class has a default constructor, which initializes the all the ‘maximums’ to be 10, and the ‘currently holdings’ to be 0. So if you create a Dishwasher using the default constructor, and then printed it, it would print out the following:

    Holding 0 of 10 glasses
    Holding 0 of 10 plates
    Holding 0 of 10 bowls
  1. Overload your constructor by adding a second, non-default constructor, which takes 3 pairs of numbers as parameters – one parameter will for the maximum, and one for the current value of, each of the glasses, plates, and bowls fields. So running the following code (which demonstrates the use of this constructor):

    Dishwasher d = new Dishwasher(0, 10, 1, 5, 3, 7);
    d.Print();

    will print:

    Holding 0 of 10 glasses
    Holding 1 of 5 plates
    Holding 3 of 7 bowls

    1. As a side-note, you’ll notice that passing 6 integers into the constructor is getting a bit awkward. It’s hard to remember what they all mean, and even if you do remember that they’re pairs (0/10, 1/5, 3/7), it’s still tricky to remember which one goes first, second and third (Glasses, bowls, plates? Plates, bowls, glasses? Etc). While we won’t stop to explore better ways here, it’s worth noting that 6 parameters of the same type, in a row, is a bit awkward.
  1. Make sure to call each version of the constructor at least once, so that you can verify that each one works.
         
    Notice how the implementation of constructors helps guarantee that other programmers will properly initialize the objects that they create (or else get a compiler error).
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

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

namespace Constructors_Exercise
{
class Program
{
public class Dishwasher
{
private int maximumGlasses, maximumPlates, maximumBowls, currcurrentlyHoldingGlasses, currcurrentlyHoldingPlates, currcurrentlyHoldingBowls;
public Dishwasher()
{
maximumBowls = 10;
maximumGlasses = 10;
maximumPlates = 10;
currcurrentlyHoldingBowls = 0;
currcurrentlyHoldingGlasses = 0;
currcurrentlyHoldingPlates = 0;
}
public Dishwasher(int currentG, int maxG, int currentP, int maxP,int currentB,int maxB)
{
maximumGlasses = maxG;
maximumPlates = maxP;
maximumBowls = maxB;
currcurrentlyHoldingGlasses = currentG;
currcurrentlyHoldingBowls = currentB;
currcurrentlyHoldingPlates = currentP;
}
public void Print()
{
Console.WriteLine("Holding " + currcurrentlyHoldingGlasses + " of " + maximumGlasses + " glasses");
Console.WriteLine("Holding " + currcurrentlyHoldingPlates + " of " + maximumPlates + " plates");
Console.WriteLine("Holding " + currcurrentlyHoldingBowls + " of " + maximumBowls + " bowls");
}
}
static void Main(string[] args)
{
Dishwasher defaultD = new Dishwasher();
defaultD.Print();
Console.WriteLine("\n\n");
Dishwasher d = new Dishwasher(0, 10, 1, 5, 3, 7);
d.Print();
}
}
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
please help me with this in C# language. Constructors The goal for this exercise is to...
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
  • I need help in this please, in the c# language. Objects As Parameters The goal for...

    I need help in this please, in the c# language. Objects As Parameters The goal for this exercise is to define the Television object, which you will be using in several, subsequent exercises. For all the methods that you implement (in this course (not just this exercise, but in this course, as you go forwards)), you should remember that since method is public, anyone, anywhere can call the method. Even people whom you never thought would call this method. Therefore,...

  • Exercise 2: There can be several constructors as long as they differ in number of parameters...

    Exercise 2: There can be several constructors as long as they differ in number of parameters or data type. Alter the program so that the user can enter just the radius, the radius and the center, or nothing at the time the object is defined. Whatever the user does NOT include (radius or center) must be initialized somewhere. There is no setRadius function and there will no longer be a setCenter function. You can continue to assume that the default...

  • Diego’s Bike Lab Lab Objectives • Be able to create both default and non-default constructors •...

    Diego’s Bike Lab Lab Objectives • Be able to create both default and non-default constructors • Be able to create accessor and mutator methods • Be able to create toString methods • Be able to refer to both global and local variables with the same name Introduction Until now, you have just simply been able to refer to any variable by its name. This works for our purposes, but what happens when there are multiple variables with the same name?...

  • Anyone helps me in a Java Languageif it it is possible thanks. Write a class called...

    Anyone helps me in a Java Languageif it it is possible thanks. Write a class called Player that holds the following information: Team Name (e.g., Ravens) . Player Name (e.g., Flacco) . Position's Name (e.g. Wide reciver) . Playing hours per week (e.g. 30 hours per week). Payment Rate (e.g., 46 per hour) . Number of Players in the Team (e.g. 80 players) . This information represents the class member variables. Declare all variables of Payer class as private except...

  • Please help with a source code for C++ and also need screenshot of output: Step 1:...

    Please help with a source code for C++ and also need screenshot of output: Step 1: Create a Glasses class using a separate header file and implementation file. Add the following attributes. Color (string data type) Prescription (float data type) Create a default constructor that sets default attributes. Color should be set to unknown because it is not given. Prescription should be set to 0.0 because it is not given. Create a parameterized constructor that sets the attributes to the...

  • FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width,...

    FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width, height (in inches), weight (in pounds), address (1 line), city, state, zip code. These variables must be private. Create setter and getter functions for each of these variables. Also create two constructors for the box class, one default constructor that takes no parameters and sets everything to default values, and one which takes parameters for all of the above. Create a calcShippingPrice function that...

  • C++ Help ASAP Create a constructor of class which intializes the platform of application. // constructors...

    C++ Help ASAP Create a constructor of class which intializes the platform of application. // constructors based on parameters can intialize array for app purchase

  • Exercise 11 - in Java please complete the following: For this exercise, you need to work...

    Exercise 11 - in Java please complete the following: For this exercise, you need to work on your own. In this exercise you will write the implementation of the pre-written implementation of the class CAR. The class CAR has the following data and methods listed below: . Data fields: A String model that stores the car model, and an int year that stores the year the car was built. . Default constructor . Overloaded constructor that passes values for both...

  • A class can have multiple constructors and each constructor must accept a unique set of parameters....

    A class can have multiple constructors and each constructor must accept a unique set of parameters. True False If the superclass has more than 2 constructors that requires parameters, then the subclasses can inherit these constructors automatically and we donot need to write any constructors in the subclasses. True False An inner class can reference the data and methods defined in the outer class in which it nests, so you do not need to pass the reference of the outer...

  • Please help me with this in C# language. Returning an array from a function The goal...

    Please help me with this in C# language. Returning an array from a function The goal for this exercise is to make sure that you return an array from a method (as a return value). Also: to give you more practice creating and using methods/functions. What you need to do for this exercise: In the starter project, add code to the Returning_An_Array class, so that the RunExercise method does the following: Declare an integer array variable, but DO NOT ALLOCATE...

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