De tweede constructor moet als paremeter een string color en een int bottleCount hebben
In de tweede constructor moeten de properties van d class gezet worden op de waarden uit de parameters
Verwachte output
Crate1 is blue and holds 24 bottles
Crate2 is red and holds 6 bottles
Nu jij
using System;Crate crate1 = new Crate("blue");Crate crate2 = new Crate("red", 6);Console.WriteLine($"Crate1 is {crate1.Color} and holds {crate1.BottleCount} bottles");Console.WriteLine($"Crate2 is {crate2.Color} and holds {crate2.BottleCount} bottles");class Crate{ public string Color; public int BottleCount; //constructor with one parameter public Crate(string color) { Color = color; BottleCount = 24; } //constructor with two parameters ________}
Mogelijke uitwerking
using System;Crate crate1 = new Crate("blue");Crate crate2 = new Crate("red", 6);Console.WriteLine($"Crate1 is {crate1.Color} and holds {crate1.BottleCount} bottles");Console.WriteLine($"Crate2 is {crate2.Color} and holds {crate2.BottleCount} bottles");class Crate{ public string Color; public int BottleCount; //constructor with one parameter public Crate(string color) { Color = color; BottleCount = 24; } //constructor with two parameters public Crate(string color, int bottleCount) { Color = color; BottleCount = bottleCount; }}
Opdracht 2
Voeg twee constructors toe aan de class
Specificaties
Beide nieuwe constructors moeten in minimaal dezelfde parameters hebben als de voorgaande
De tweede constructor moet als extra parameter een string brand hebben
De derde constructor moet als extra parameter ook nog string color hebben.
Verwachte output
Car1 is a Black Mercedes with licenseplate RL-95-NX.
Car2 is a Black Volkswagen with licenseplate 12-BJ-JK.
Car1 is a Grey Skoda with licenseplate 85-SL-GL.
Nu jij
using System;Car car1 = new Car("RL-95-NX");Car car2 = new Car("12-BJ-JK","Volkswagen");Car car3 = new Car("85-SL-GL", "Skoda", "Grey");Console.WriteLine($"Car1 is a {car1.Color} {car1.Brand} with licenseplate {car1.LicensePlate}.");Console.WriteLine($"Car2 is a {car2.Color} {car2.Brand} with licenseplate {car2.LicensePlate}.");Console.WriteLine($"Car1 is a {car3.Color} {car3.Brand} with licenseplate {car3.LicensePlate}.");class Car{ public string LicensePlate { get; set; } public string Brand { get; set; } public string Color { get; set; } //constructor with one parameter public Car(string licensePlate) { LicensePlate = licensePlate; Brand = "Mercedes"; Color = "Black"; } //constructor with two parameters ________ //constructor with three parameters ________}
Mogelijke uitwerking
using System;Car car1 = new Car("RL-95-NX");Car car2 = new Car("12-BJ-JK","Volkswagen");Car car3 = new Car("85-SL-GL", "Skoda", "Grey");Console.WriteLine($"Car1 is a {car1.Color} {car1.Brand} with licenseplate {car1.LicensePlate}.");Console.WriteLine($"Car2 is a {car2.Color} {car2.Brand} with licenseplate {car2.LicensePlate}.");Console.WriteLine($"Car1 is a {car3.Color} {car3.Brand} with licenseplate {car3.LicensePlate}.");class Car{ public string LicensePlate { get; set; } public string Brand { get; set; } public string Color { get; set; } //constructor with one parameter public Car(string licensePlate) { LicensePlate = licensePlate; Brand = "Mercedes"; Color = "Black"; } //constructor with two parameters public Car(string licensePlate, string brand) { LicensePlate = licensePlate; Brand = brand; Color = "Black"; } //constructor with three parameters public Car(string licensePlate, string brand, string color) { LicensePlate = licensePlate; Brand = brand; Color = color; }}
Opdracht 3
Voeg twee constructors toe aan de class
Specificaties
De eerste constructor moet als enige parameter de waarde voor de IsOn property mee krijgen
De tweede constructor moet als enige paramter de waarde voor de Name property meekrijgen
Verwachte output
Lamp: Desk Lamp, Is On: False
Lamp: Unnamed Lamp, Is On: True
Nu jij
using System;class Lamp{ public bool IsOn { get; set; } public string Name { get; set; } // Constructor 1: Accepts a string (lamp name) ________ // Constructor 2: Accepts a boolean (lamp state) ________ public void DisplayInfo() { Console.WriteLine($"Lamp: {Name}, Is On: {IsOn}"); }}class Program{ static void Main() { // Using first constructor (string parameter) Lamp lamp1 = new Lamp("Desk Lamp"); lamp1.DisplayInfo(); // Using second constructor (bool parameter) Lamp lamp2 = new Lamp(true); lamp2.DisplayInfo(); }}
Mogelijke uitwerking
using System;class Lamp{ public bool IsOn { get; set; } public string Name { get; set; } // Constructor 1: Accepts a string (lamp name) public Lamp(string name) { Name = name; IsOn = false; // Default state } // Constructor 2: Accepts a boolean (lamp state) public Lamp(bool isOn) { Name = "Unnamed Lamp"; // Default name IsOn = isOn; } public void DisplayInfo() { Console.WriteLine($"Lamp: {Name}, Is On: {IsOn}"); }}class Program{ static void Main() { // Using first constructor (string parameter) Lamp lamp1 = new Lamp("Desk Lamp"); lamp1.DisplayInfo(); // Using second constructor (bool parameter) Lamp lamp2 = new Lamp(true); lamp2.DisplayInfo(); }}