logo

Assignment 4 Solutions

Demonstrate the ability to write classes with public and private content, use different method declaration parameter types, create instances of classes, and invoke their membership.

6 Pages1440 Words1 Views
   

Added on  2022-12-19

About This Document

This document provides solutions for Assignment 4 problems in C# programming. It includes coding examples and explanations for each problem. The problems cover topics such as pass by value, pass by reference, and using params keyword. Download the solutions now from Desklib.

Assignment 4 Solutions

Demonstrate the ability to write classes with public and private content, use different method declaration parameter types, create instances of classes, and invoke their membership.

   Added on 2022-12-19

ShareRelated Documents
Assignment 4
Problem 1:
Coding:
using System;
namespace Assignment_4_Problem_1_solution
{
class PassDemo
{
// Divide() is used to divide two value and return the answer
public double Divide(double numerator, double denominator = 10)
{
// return 0 if denominator is 0
if (denominator == 0)
{
return 0;
}
// return the result of division
return numerator / denominator;
}
// Arithmetic() is used to perform arithmeric operation on its parameter
public void Arithmetic(int value1,int value2,out int minimum, out int
maximum, out int sum, out int product)
{
// determine min of value1 and value2
minimum = Math.Min(value1, value2);
// determine max of value1 and value2
maximum = Math.Max(value1, value2);
// determine sum of value1 and value2
sum = value1+value2;
// determine product of value1 and value2
product = value1 * value2;
}
}
class Program
{
static void Main(string[] args)
{
// create objects of the class PassDemo
PassDemo passdemo = new PassDemo();
// test Divide() of PassDemo with pass by value
Console.WriteLine("pass by value...");
Console.WriteLine("\tnumerator = 10.00000, denominator = 20.00000,
result = " +passdemo.Divide(10.00000, 20.00000).ToString("0.00000"));
// test Divide() of PassDemo with pass by default
Console.WriteLine("pass using default...");
Console.WriteLine("\tnumerator = 10.00000, denominator defaulted,
result = " + passdemo.Divide(10.00000).ToString("0.00000"));
Assignment 4 Solutions_1
// test Divide() of PassDemo with zero divide
Console.WriteLine("zero divide..");
Console.WriteLine("\tnumerator = 10.00000, denominator = 0.00000,
result = " + passdemo.Divide(10.00000, 0.00000).ToString("0.00000"));
// test Divide() of PassDemo with named parameters
Console.WriteLine("named parameters...");
double numerator = 30.00000;
double denominator = 100.00000;
double result = passdemo.Divide(numerator, denominator);
Console.WriteLine("\tnumerator = {0}, denominator = {1}, result =
{2}", numerator, denominator, result.ToString("0.00000"));
// test Arithmetic() of PassDemo with out parameters
Console.WriteLine("arithmetics using out parameters..");
int min, max, sum, product;
passdemo.Arithmetic(50, 20, out min, out max, out sum, out product);
Console.WriteLine("\tval1 = 50, val2 = 20, min = {0}, max = {1}, sum
= {2}, product = {3}", min, max, sum, product);
Console.ReadKey();
}
}
}
Output:
Problem 2:
Coding:
using System;
namespace Assignment_4_Problem_2_solution
{
class RefDemo
{
// AddToVals() is used to increment value 1 and value 2 by a increment
value
public void AddToVals(ref int val1,ref int val2, int increment)
{
// increment value 1 and value 2
val1 += increment;
val2 += increment;
}
Assignment 4 Solutions_2
// Swap() is used to swap value 1 and value 2
public void Swap(ref int val1,ref int val2)
{
int temp = val1;
val1 = val2;
val2 = temp;
}
}
class Program
{
static void Main(string[] args)
{
// create object of the class RefDemo
RefDemo refDemo = new RefDemo();
int val1 = 10;
int val2 = 20;
int increment = 25;
// display data in value1 and value2 before adding
Console.WriteLine("AddToVals...");
Console.WriteLine("\tCalling with val1 = {0}, val2 = {1}", val1,
val2);
// call AddToVals()
refDemo.AddToVals(ref val1, ref val2, increment);
// display data in value1 and value2 after adding
Console.WriteLine("After AddToVals has been called...");
Console.WriteLine("\tval1 is now {0}, val2 is now = {1}", val1,
val2);
// call Swap() to swap values
refDemo.Swap(ref val1, ref val2);
// display value1 and value2 after swapping
Console.WriteLine("After SwapVals has been called...");
Console.WriteLine("\tval1 is now {0}, val2 is now = {1}", val1,
val2);
Console.ReadKey();
}
}
}
Output:
Assignment 4 Solutions_3

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
Solved Assignments and Coding Problems
|7
|1222
|1