Assignment 4 Solutions

Verified

Added on  2022/12/19

|6
|1440
|1
AI Summary
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.

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
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"));

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
// 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;
}
Document Page
// 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:
Document Page
Problem 3:
Coding:
using System;
namespace Assignment_4_Problem_3_solution
{
class ParamsDemo
{
// TableSum() is used to sum all the value in the array and return them
public int TableSum(params int[] dataArray)
{
int sum = 0;
// loop for each data in the array
foreach (int data in dataArray)
{
// add data to sum
sum += data;
}
// return sum
return sum;
}
}
class Program
{
static void Main(string[] args)
{
// create objects of the class
ParamsDemo paramsDemo = new ParamsDemo();
// pass 1, 2, 3, 4, 5 implicitly to TableSum() of ParamsDemo class
Console.WriteLine("Passing params 1, 2, 3, 4, 5 implicitly to
TableSum returns a sum of " + paramsDemo.TableSum(1, 2, 3, 4, 5));
int[] dataArray = { 2, 4, 6, 8, 10 };
// pass array explicitly to TableSum() of ParamsDemo class
Console.WriteLine("Passing params 2, 4, 6, 8, 10 explicitly to
TableSum returns a sum of " + paramsDemo.TableSum(dataArray));
Console.ReadKey();
}
}
}
Output:

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
Problem 4:
Coding:
using System;
namespace Assignment_4_Problem_4_solution
{
// Circle class stores the details and methods of class
class Circle
{
// attributes of class
private int radius;
// mutator of class
public void SetRadius(int tradius)
{
radius = Math.Abs(tradius);
}
// accessor of class
public int GetRadius()
{
return radius;
}
// Area() is used to compute area of the circle
public double Area()
{
return Math.PI * Math.Pow(GetRadius(), 2);
}
}
class Program
{
static void Main(string[] args)
{
// create array of objects
Circle[] myCircles = new Circle[] { new Circle(), new Circle() };
// loop through each circle
foreach (Circle circle in myCircles)
{
// get valid radius of the circle
bool valid = false;
int radius = 0;
// loop until valid radius is entered
while (!valid)
{
Document Page
// prompt and get radius
Console.Write("Enter an integer radius: ");
valid = int.TryParse(Console.ReadLine(), out radius);
// if not valid
if (!valid)
{
// display error message
Console.WriteLine("Illegal input, try again!");
}
}
// set radius of circle
circle.SetRadius(radius);
}
// loop through each circle
foreach (Circle circle in myCircles)
{
// display radius and area of the circle
Console.WriteLine("Circle has a radius of {0} and an area of
{1}",circle.GetRadius(),circle.Area().ToString("#.#####"));
}
Console.ReadKey();
}
}
}
Output:
Reference:
Albahari J. & Albahari, B. (2010). C# 7.0 in a Nutshell: The Definitive Reference. Sebastopol,
CA: O'Reilly Publisher
Purdum, P. (2012). Beginning Object-Oriented Programming with C#. Indianapolis, IN: John
Wiley & Sons Publisher.
1 out of 6
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]