Advanced Application Programming Assignment
Advanced Application Programming Assignment
John Maina
2) Write a program that accepts a set of integers (the user decides how many) and then
stores them in an array. The main function then passes these values one by one to a
method called get_even which returns 1 if the integer is even and 0 if it is odd. The
main function should then specify which numbers were even, which ones were odd
and their respective totals. It should also specify how many numbers were odd and
how many were even. For example, if the user enters 25 34 56 17 14 20, the output
should be: -
25 is an odd number
34 is an even number
56 is an even number
17 is an odd number
14 is an even number
20 is an even number
using System;
1
Advanced Application Programming Lecturer
John Maina
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// Prompt the user to enter a set of integers
Console.WriteLine("Please enter a set of integers, separated by spaces: ");
string input = Console.ReadLine();
// Iterate over the numbers and determine whether each one is even or odd
foreach (string num in numbers)
{
int number = int.Parse(num);
if (get_even(number) == 1)
{
// The number is even, so add it to the evenNumbers array
evenNumbers[evenCount] = number;
evenCount++;
}
else
2
Advanced Application Programming Lecturer
John Maina
{
// The number is odd, so add it to the oddNumbers array
oddNumbers[oddCount] = number;
oddCount++;
}
}
3
Advanced Application Programming Lecturer
John Maina
3) Write a program that accepts the amount of money deposited in a bank account, the
annual interest rate and the target amount (The amount of money the account holder
wants to have in the account after a period of time) and then calculates the number of
years it will take for the money to accumulate to the targeted amount. NB: 1) The
interest being earned is Compound Interest. 2) Don’t use the formula for calculating
compound interest.
For example if the money deposited is 10000 and the target amount is 20000 and the
account earns an interest rate (compound) of 10% pa, then the output should be: -
It will take 8 years for your money to reach your target.
By the end of this period, the amount in your account will be 21435.89 (10 marks)
using System;
namespace BankAccount
4
Advanced Application Programming Lecturer
John Maina
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the amount of money deposited in the account:");
double deposit = double.Parse(Console.ReadLine());
// Convert the interest rate from percentage to a decimal value (e.g. 10% ->
0.1)
interestRate = interestRate / 100;
int years = 0;
double currentAmount = deposit;
Console.WriteLine($"It will take {years} years for your money to reach your
target.");
Console.WriteLine($"By the end of this period, the amount in your account
will be {currentAmount:F2}.");
}
}
}
5
Advanced Application Programming Lecturer
John Maina
4) Making use of object orientation write a program that stores and evaluates the total
cost for items bought from a supermarket. The cashier should enter the following: -
Product code, Price and Quantity. The total price should be evaluated as follows: -
Total cost = Price * Quantity
If the total cost per item is more than 20,000 there is a discount of 14% on that item
and a discount of 10% on an item whose total cost is between 10,000 and 20,000. No
discount is given on items whose total cost is less than 10,000
NB: The cashier should decide how many Items he/she wants to work with. If he/she
chooses 3, for example, the output should take the format shown below.
Item Code Price Quantity Total Cost Discount Net
109 6000 4 24000 3360 20640
201 900 8 7200 0 7200
127 600 20 12000 1200 10800
The total amount payable is 38640
NB: Your class should have constructors (including default constructor) and a
destructor. It should also have properties to set and return price, quantity and total
cost. Set item code to read only. (15 marks)
class Item
{
// read-only property for the product code
public int ProductCode { get; }
// constructor to initialize the object with the product code, price, and quantity
public Item(int productCode, int price, int quantity)
{
ProductCode = productCode;
Price = price;
Quantity = quantity;
}
6
Advanced Application Programming Lecturer
John Maina
class Supermarket
{
// list of Item objects to store the items in the supermarket
private List<Item> items = new List<Item>();
// method to calculate the total amount payable for all the items in the list
public int CalculateTotalAmountPayable()
{
int total = 0;
// calculate the total cost of each item and add any applicable discounts
foreach (Item item in items)
{
int totalCost = item.CalculateTotalCost();
7
Advanced Application Programming Lecturer
John Maina
else if (totalCost > 10000)
{
total += totalCost * (1 - 0.10);
}
else
{
total += totalCost;
}
}
return total;
}
}
class Program
{
static void Main(string[] args)
{
// create a new instance of the Supermarket class
Supermarket supermarket = new Supermarket();
5) Rewrite the program above making use of arrays only i.e. don’t use object
orientation your program. (10 marks)
using System;
using System.Collections;
8
Advanced Application Programming Lecturer
John Maina
namespace Supermarket
{
class Item
{
// Property to get and set the item code
public int Code { get; }
// Property to get and set the total cost for the item
public int TotalCost
{
get
{
return Price * Quantity;
}
}
9
Advanced Application Programming Lecturer
John Maina
else if (total > 10000)
{
return (int)(total * 0.1);
}
else
{
return 0;
}
}
}
// Property to get the net cost for the item after the discount
public int Net
{
get
{
return TotalCost - Discount;
}
}
class Program
{
static void Main(string[] args)
{
// Create an ArrayList to store the items
ArrayList items = new ArrayList();
10
Advanced Application Programming Lecturer
John Maina
11
Advanced Application Programming Lecturer
John Maina
{
total += item.Net;
}
Console.WriteLine("The total amount payable is {0}", total);
}
}
}
6) Write a program that accepts a set of numbers (The user decides how many) and
stores them in array. The program should the sort all the numbers in the array in
ascending order. It should output the numbers in the array before sorting and then
again after sorting. (10 marks)
using System;
namespace SortArray
class Program
int n = int.Parse(Console.ReadLine());
12
Advanced Application Programming Lecturer
John Maina
arr[i] = int.Parse(Console.ReadLine());
Array.Sort(arr);
13
Advanced Application Programming Lecturer
John Maina
}
7) Write a program that accepts two numbers and swaps them. Both input and output
should be done in the main method and the swapping (actual swapping) should be
done in a method called swapNumbers. Output the two numbers in the main method
before and after swapping. (7 marks)
using System;
namespace SwapNumbers
class Program
14
Advanced Application Programming Lecturer
John Maina
num1 = num2;
num2 = temp;
8) Write a (unique) C# program using the concept of object orientation to illustrate the
following concept in Object Oriented programming. Indicate with comments where
each of these concepts/techniques is used in your program. (15 marks)
a) Inheritance.
b) Overloading.
c) Overriding.
d) Encapsulation.
e) Constructors (Including default constructors)
f) Destructor.
g) Properties.
h) Error handling – try catch
using System;
namespace OOPConcepts
{
// Base class "Shape" with protected member variables for height and width
public class Shape
{
// Protected variables can be accessed by derived classes
15
Advanced Application Programming Lecturer
John Maina
protected double height;
protected double width;
// Default constructor
public Shape()
{
this.height = 0;
this.width = 0;
}
// Derived class "Rectangle" that inherits from the base "Shape" class
public class Rectangle : Shape
{
// Default constructor that calls the base class default constructor
public Rectangle() : base()
{
}
16
Advanced Application Programming Lecturer
John Maina
public double Height
{
get
{
return this.height;
}
set
{
this.height = value;
}
}
class Program
{
static void Main(string[] args)
{
try
{
// Create a rectangle object and set its height and width
Rectangle rectangle = new Rectangle(10, 20);
17
Advanced Application Programming Lecturer
John Maina
{
// Catch any exceptions and print the error message
Console.WriteLine("Error: " + ex.Message);
}
}
}
}
18