Uploaded by Cyrus Cyrus

Course work to submit in

advertisement
Course work
NAME: MUTEBBE CYRUS
COURSE: PSCE
SEMESTER: TWO
YEAR: 2023
Question one
(a) Explain these terms in as applied in C# programming.
1. Data type: specifies the kind of value a variable can hold for example integers, floats, strings
and Booleans.
2. On Click Event: is a type of event that is triggered when a user clicks on a graphical user
interface.
3. High Level language; is a programming language that is closer to human language and less
abstract than a low-level language.
4. Operator; is a symbol that is used to perform an operation on two or more operands for
example arithmetic operators, comparison operators, and logical operators.
(b) Three examples of operators and when they are used:
Arithmetic operators; perform mathematical operations on numeric values for example
addition (+), subtraction (-), multiplication (*), and division (/).
Comparison operators: these compare two values and return a Boolean value for example
equality (==), inequality (=).
Assignment operators: these assign a value to a variable for example =.
(c) Explain what is meant by inheritance?
Inheritance is a concept in object-oriented programming that allows a class to inherit the
properties and methods of another class.
(d) Discuss two types of inheritance.
Single inheritance where a class can inherit from only one superclass. This means that a
subclass can have only one immediate parent class.
Multiple inheritance allows a class to inherit properties and behaviors from more than one
superclass. In other words, a subclass can have multiple parent classes.
Course work
Question two
(a) Describe what is meant by a variable.
A variable is data item whose value does not change when the program is executed.
(b) Explain the purpose of the following access modifier keywords within a C# program.
Private; restricts the visibility of class members.
Public; allows the access of a member from anywhere in the program.
Protected: restricts the access of a member to the class in which it is declared and its subclasses
and the member cannot be accessed from outside of the class or its subclasses.
(c) Discuss two commonly encountered programming errors in C#.
Syntax errors: s a type of error that occurs in computer programming when the code violates the
grammatical rules of the programming language and the compiler cannot understand the code
correctly.
Runtime errors: Runtime errors are errors that occur while the program is running for example
it may happen when dividing a number by zero.
(d) Examine four rules to be taken into consideration when naming controls in Visual
Studio IDE using C#.
Use meaningful names: The name of a control should be descriptive and clearly
Indicate what the control does for example Ok Button for Button1.
Use appropriate naming conventions: For example, variables should start with a lowercase
letter, and controls should start with an uppercase letter.
Avoid using spaces in names: Spaces in names can make the code more difficult to be executed
by the compiler.
Course work
Question four
(a) Explain four different literals at C programming language.
Integer Literals: these represent whole numbers and can be written in decimal, octal,
hexadecimal, or binary form. For example, 10, 012, 0x1A etc.
Floating-Point Literals: These represent real numbers and can be written in decimal or
hexadecimal form. For example, 3.14, 1.2e3, etc. . . .
Character Literals: These represent single characters enclosed in single quotes. For example,
' a&#3.
String Literals: These represent sequences of characters and enclosed in double quotes. For
example, " Hello, world! "
(b) Examine four types of operators used in C#.
Arithmetic Operators: Arithmetic operators perform arithmetic operations, such as addition,
subtraction, multiplication, and division. For example, +, -, *, and /.
Comparison Operators: Comparison operators compare two operands and return a Boolean
value (true or false). For example, ==,! =.
Logical Operators: Logical operators perform logical operations, such as AND, OR, and
NOT. For example, & &, ||, and! Are all logical operators.
Assignment Operators: Assignment operators assign a value to a variable. For example, =, +=,
-=, *=, and /= are all assignment operators
Course work
Question six (a)
(a) Define the term Loop as used in C# programming.
Is a control flow statement that allows you to execute a block of code repeatedly until a certain
condition is met?
(b) With C# code illustrations, write the structure of the following conditional statements:
(i) if-else
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
(ii) Do-while
do {
// Code to execute
(iii) while
while (condition) {
// Code to execute
}
(c) Explain terms as used in inheritance:
(i) Derived class: A derived class is a class that inherits the properties and methods of another
class, called the base class.
(ii) Base class; A base class is a class that is inherited by one or more derived classes.
(iii) Sealed; A sealed class is a class that cannot be inherited from. This means that no derived
classes can be created from a sealed class
Course work
Question seven (a)
(a) State the parts that make up the basic structure of a program.
Documentation: This section provides an overview of the program's purpose, functionality, and
usage guidelines.
Data Declarations: This part defines the variables and data types used within the program.
.Input and Output: This section handles the program's interaction with the user or external
systems. It allows for receiving input data and presenting output results.
Processing Logic: This component represents the core functionality of the program.
Error Handling: This section addresses potential errors that may occur during program
execution.
(b) Explain three features of C# programming language.
Object-Oriented Programming (OOP): C# is a fully object-oriented language, enabling
developers to create modular, reusable, and maintainable code by encapsulating data and
functionality within objects.
Strong Typing: C# enforces strict type checking, ensuring that data is handled in a type-safe
manner, preventing runtime errors and enhancing code reliability.
Garbage Collection (GC): C# features automatic memory management through garbage
collection.
(c) Discuss five reasons that make C# a widely used professional programming language.
Versatility: C# is a versatile language suitable for developing a wide range of applications, from
desktop and web applications to mobile apps and enterprise solutions.
Cross-Platform Development: C# supports cross-platform development through .NET Core,
enabling developers to create applications that run on various operating systems, including
Windows, macOS, and Linux.
Robust Language Features: C# offers a rich set of language features, including OOP, strong
typing, generics, and exception handling, making it a powerful and expressive tool for software
development.
Online Documentation: Microsoft provides comprehensive online documentation for C# and the
.NET framework.
Forums and Communities: Numerous online forums and communities are dedicated to C#, where
developers can ask questions, share knowledge, and collaborate on projects.
Course work
Question Five
(a) AnnualInterest = 600000;
QualifiedMembers = 0;
SharedInterest = 0;
if (qualifiedMembers > 0) {
SharedInterest = annualInterest / qualifiedMembers;
}
Console.WriteLine ("The shared interest is: " + sharedInterest);
(b)
AnnualInterest = 600000;
QualifiedMembers = 0;
SharedInterest = 0;
try {
SharedInterest = annualInterest / qualifiedMembers;
} catch (DivideByZeroException) {
Console.WriteLine ("There are no qualified members to share the interest
with.");
}
Console.WriteLine ("The shared interest is: " + sharedInterest);
(C)
annualInterest = 600000
qualifiedMembers = 0
sharedInterest = 0
try:
sharedInterest = annualInterest / qualifiedMembers
except ZeroDivisionError:
print("Error, can't divide by Zero")
print("The shared interest is:", sharedInterest)
Course work
Question Three
(a) private void btnSubmit_Click(object sender, EventArgs e)
{
double bodyTemp = double.Parse(txtBodyTemp.Text);
if (bodyTemp >= 34 && bodyTemp <= 37)
{
txtComment.Text = "Normal Temperature";
txtBodyTemp.BackColor = Color.Green;
lblComment.Text = "Normal Temperature";
}
else if (bodyTemp >= 37.5 && bodyTemp <= 38.5)
{
txtComment.Text = "High Temperature. Must be having Fever";
txtBodyTemp.BackColor = Color.Yellow;
lblComment.Text = "High Temperature. Must be having Fever";
MessageBox.Show("Please consult a doctor immediately!");
}
else
{
txtComment.Text = "Out of Range Temperature";
txtBodyTemp.BackColor = Color.Red;
lblComment.Text = "Out of Range Temperature";
}
}
(b)
private void btnSubmit_Click(object sender, EventArgs e)
{
double bodyTemp = double.Parse(txtBodyTemp.Text);
if (bodyTemp >= 34 && bodyTemp <= 37)
{
txtComment.Text = "Normal Temperature";
txtBodyTemp.BackColor = Color.Green;
txtComment.ForeColor = Color.Black;
lblComment.Text = "Normal Temperature";
}
else if (bodyTemp >= 37.5 && bodyTemp <= 38.5)
{
txtComment.Text = "High Temperature. Must be having Fever";
txtBodyTemp.BackColor = Color.Yellow;
txtComment.ForeColor = Color.Black;
lblComment.Text = "High Temperature. Must be having Fever";
MessageBox.Show("Please consult a doctor immediately!");
}
else
{
txtComment.Text = "Out of Range Temperature";
txtBodyTemp.BackColor = Color.Red;
txtComment.ForeColor = Color.White;
Course work
lblComment.Text = "Out of Range Temperature";
}
}
(C)
private void btnSubmit_Click(object sender, EventArgs e)
{
double bodyTemp = double.Parse(txtBodyTemp.Text);
if (bodyTemp >= 34 && bodyTemp <= 37)
{
txtComment.Text = "Normal Temperature";
txtBodyTemp.BackColor = Color.Green;
txtComment.ForeColor = Color.Black;
lblComment.Text = "Normal Temperature";
}
else if (bodyTemp >= 37.5 && bodyTemp <= 38.5)
{
txtComment.Text = "High Temperature. Must be having Fever";
txtBodyTemp.BackColor = Color.Yellow;
txtComment.ForeColor = Color.Red;
lblComment.Text = "High Temperature. Must be having Fever";
MessageBox.Show("Please consult a doctor immediately!");
}
else
{
txtComment.Text = "Out of Range Temperature";
txtBodyTemp.BackColor = Color.Red;
txtComment.ForeColor = Color.White;
lblComment.Text = "Out of Range Temperature";
}
}
(d)
private void btnClear_Click(object sender, EventArgs e)
{
txtBodyTemp.Text = "";
txtComment.Text = "";
txtBodyTemp.BackColor = Color.White;
txtBodyTemp.ForeColor = Color.Black;
txtComment.ForeColor = Color.Black;
}
Download