Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
176 views

Assignment 1

The document describes 3 C# programs: 1. A program that copies elements from one array into another array by taking user input for the number of elements and values and printing the original and copied arrays. 2. A program demonstrating method overloading by changing the number of parameters in methods called "add" to return the sum of parameters. 3. A program implementing an IVehicle interface with Drive and Refuel methods, creating a Car class that implements these to drive if fuel is above 0 and refuel by increasing the fuel amount. It tests refueling and driving a car starting with 0 fuel.

Uploaded by

manoj manu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
176 views

Assignment 1

The document describes 3 C# programs: 1. A program that copies elements from one array into another array by taking user input for the number of elements and values and printing the original and copied arrays. 2. A program demonstrating method overloading by changing the number of parameters in methods called "add" to return the sum of parameters. 3. A program implementing an IVehicle interface with Drive and Refuel methods, creating a Car class that implements these to drive if fuel is above 0 and refuel by increasing the fuel amount. It tests refueling and driving a car starting with 0 fuel.

Uploaded by

manoj manu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

• Write a program in C# Sharp to copy the elements one array into another

array.   
Test Data : 
Input the number of elements to be stored in the array :3 
Input 3 elements in the array : 
element - 0 : 15 
element - 1 : 10 
element - 2 : 12 
Expected Output: 
The elements stored in the first array are : 
15 10 12 
The elements copied into the second array are : 
15 10 12 
 
Program.cs
using System;
namespace Task2
{
class Program
{
static void Main(string[] args)
{

int[] a1 = new int[100];


int[] a2 = new int[100];
int i, n;
Console.Write("Enter the number of elements to be stored in the
array :");
n = Convert.ToInt32(Console.ReadLine());
Console.Write("Input {0} elements in the array :\n", n);
for (i = 0; i < n; i++)
{
Console.Write("element - {0} : ", i);
a1[i] = Convert.ToInt32(Console.ReadLine());
}
for (i = 0; i < n; i++)
{
a2[i] = a1[i];
}
Console.Write("\nThe elements stored in the first array are :\n");
for (i = 0; i < n; i++)
{
Console.Write("{0} ", a1[i]);
}
Console.Write("\n\nThe elements copied into the second array are :\n");
for (i = 0; i < n; i++)
{
Console.Write("{0} ", a2[i]);
}
Console.Write("\n\n");
}
}
}
 
 
 

2.Write a C# program to demonstrate the function  overloading by changing the


Number  of parameters 
Program.cs
using System;
namespace Task2
{
class Program
{
static void Main(string[] args)
{
MethodOverloading obj = new MethodOverloading();
int i = obj.add(10, 10);
int j = obj.add(10, 10, 10);
int k = obj.add(10, 10 ,10,10);
Console.WriteLine("i:" + i);
Console.WriteLine("j:" + j);
Console.WriteLine("k:" + k);

}
}
}
MethodOverloading.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Task2
{
class MethodOverloading
{
public int add(int a, int b) //two int type Parameters method
{
return a + b;
}
public int add(int a, int b, int c) //Three parameters with same method same
as above
{
return a + b + c;
}
public int add(int a, int b, int c, int d) //four Parameters with same method
same as above two method
{
return a + b + c + d;
}
}
}

3. Create a C# program that implements an IVehiculo interface with two


methods, one for Drive of type void and another for Refuel of type bool that
has a parameter of type integer with the amount of gasoline to refuel. Then
create a Car class with a builder that receives a parameter with the car's starting
gasoline amount and implements the Drive and Refuel methods of the car.
The Drive method will print on the screen that the car is Driving, if the gasoline
is greater than 0. The Refuel method will increase the gasoline of the car and
return true.
To carry out the tests, create an object of type Car with 0 of gasoline in the
Main of the program and ask the user for an amount of gasoline to refuel,
finally execute the Drive method of the car.

using System;

public class FirstInterface


{
public static void Main(string[] args)
{
Car car = new Car(0);

int fuel = int.Parse(Console.ReadLine());


if (car.Refuel(fuel))
{
car.Drive();
}
}

public interface IVehicle


{
void Drive();
bool Refuel(int amount);
}

public class Car : IVehicle


{
public int Fuel { get; set; }

public Car(int fuel)


{
Fuel = fuel;
}

public void Drive()


{
if (Fuel > 0)
{
Console.WriteLine("Driving");
}
else
{
Console.WriteLine("Not fuel");
}
}

public bool Refuel(int amount)


{
Fuel += amount;
return true;
}
}
}

You might also like