0% found this document useful (0 votes)
4 views11 pages

A Level Computer Science Programming

The document provides a comprehensive overview of VB.NET console programming, detailing syntax and functionality for modules, input/output operations, data types, arithmetic operations, conditional statements, loops, and arrays. It includes examples of a restaurant billing program that calculates amounts due based on user input and currency selection, as well as an area calculator for different geometric shapes. The document emphasizes procedural programming without the use of object-oriented programming concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

A Level Computer Science Programming

The document provides a comprehensive overview of VB.NET console programming, detailing syntax and functionality for modules, input/output operations, data types, arithmetic operations, conditional statements, loops, and arrays. It includes examples of a restaurant billing program that calculates amounts due based on user input and currency selection, as well as an area calculator for different geometric shapes. The document emphasizes procedural programming without the use of object-oriented programming concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

VB.

NET Console Notes (Syntax + Meaning)

1) Program/structure

vb
Module Program
Sub Main()
' code here
End Sub
End Module

 Module: groups related code.


 Sub Main(): starting point of a console program.
 End Module / End Sub: closes blocks.

2) Output (printing)

vb
[Link]("Hello")
[Link]("Enter your name: ")

 [Link](...) prints text and moves to the next line.


 [Link](...) prints text but stays on the same line.

3) Input (reading user data)

vb
Dim x As String = [Link]()

 [Link]() reads what the user types (always as String).

4) Converting input to numbers

vb
Dim n As Integer = [Link]([Link]())
Dim d As Double = [Link]([Link]())

 [Link](...) converts string to an integer (e.g., "5" → 5).


 [Link](...) converts string to a decimal number (e.g., "2.5" → 2.5).
Example idea:

vb
[Link]("Enter age: ")
Dim age As Integer = [Link]([Link]())

5) Variables

vb
Dim name As String
Dim total As Double
Dim count As Integer
Dim flag As Boolean

 Dim declares a variable.


 Types:
o String text
o Integer whole numbers
o Double decimal numbers
o Boolean true/false

6) Constants

vb
Const PI As Double = 3.14159

 Const means the value cannot change later.

7) Arithmetic operators

vb
+ - * /

 + add, - subtract, * multiply, / divide

Reminder: 15 / 2 in integer math may give integer results (depending on types). Using Double
gives decimals.
8) If/Else conditions

vb
If choice = 1 Then
[Link]("Circle")
ElseIf choice = 2 Then
[Link]("Rectangle")
Else
[Link]("Invalid")
End If

 Runs one block based on conditions.


 ElseIf is used for multiple checks.

9) Comparisons (used inside conditions)

vb
= <> > < >= <=

 = equal
 <> not equal
 > < >= <= normal comparisons

10) Loops (repeat)

a) For loop
vb
For i As Integer = 1 To 5
[Link](i)
Next

 Repeats a known number of times.

b) While loop
vb
While total < 100
total += 10
End While

 Repeats while condition is true.


11) Math formatting

vb
[Link]([Link]("0.00"))

 Formats a number to 2 decimal places.


 Example: 12.3 → 12.30

12) Boolean (true/false)

vb
Dim isValid As Boolean = True
If isValid Then
[Link]("OK")
End If

13) Ending the program / pause

vb
[Link]()

 Waits for a key press (prevents the console from closing immediately).

Common patterns (very useful)


Read integer + use it

vb
[Link]("Enter number: ")
Dim n As Integer = [Link]([Link]())

Output result

vb
[Link]("Result = " & n)

******************************************************************************
A restaurant will provide guests with the following menu
1. Sadza and Guru ZiG 20.4
2. Sadza and Chicken ZiG 27.2
3. Rice and Chicken ZiG 40.8
4. Rice and Beef ZiG 40.8
A guest will make his/her choice and will be prompted to specify the quantity. The cashier will
then calculate the amount due.
i. Using a console application, write a program that will display the menu, allow the
guest to enter his/her choice as well and the quantity. The program must calculate the
amount due. [20]
ii. Extend the program in (i) so that it will prompt the user the currency to pay with and
there are only two currencies available, US Dollar and ZiG. Rate is 1 US Dollar is
equal to 13.6 ZiG [10]
vb
Module Module1

Sub Main()

' Menu prices in ZiG


Dim itemNames() As String = {"", "Sadza and Guru", "Sadza and
Chicken", "Rice and Chicken", "Rice and Beef"}
Dim itemPrices() As Double = {0, 20.4, 27.2, 40.84, 40.8}

[Link]("------ RESTAURANT MENU ------")


For i As Integer = 1 To 4
[Link](i & ". " & itemNames(i) & " ZiG " &
itemPrices(i))
Next

[Link](vbCrLf & "Enter your choice (1-4): ")


Dim choice As Integer = [Link]([Link]())

If choice < 1 Or choice > 4 Then


[Link]("Invalid choice.")
Return
End If

[Link]("Enter quantity: ")


Dim qty As Integer = [Link]([Link]())

Dim amountDueZig As Double = itemPrices(choice) * qty

[Link](vbCrLf & "------ BILL ------")


[Link]("Item: " & itemNames(choice))
[Link]("Quantity: " & qty)
[Link]("Amount Due: ZiG " & [Link]("0.00"))

[Link](vbCrLf & "Press any key to exit...")


[Link]()
End Sub

End Module

Part ii

(ii) Extend: prompt currency (US Dollar or ZiG) and


convert (1 USD = 13.6 ZiG)
vb
Module Module1

Sub Main()

' Menu prices in ZiG


Dim itemNames() As String = {"", "Sadza and Guru", "Sadza and
Chicken", "Rice and Chicken", "Rice and Beef"}
Dim itemPrices() As Double = {0, 20.4, 27.2, 40.84, 40.8}

[Link]("------ RESTAURANT MENU ------")


For i As Integer = 1 To 4
[Link](i & ". " & itemNames(i) & " ZiG " &
itemPrices(i))
Next

[Link](vbCrLf & "Enter your choice (1-4): ")


Dim choice As Integer = [Link]([Link]())

If choice < 1 Or choice > 4 Then


[Link]("Invalid choice.")
Return
End If

[Link]("Enter quantity: ")


Dim qty As Integer = [Link]([Link]())

Dim amountDueZig As Double = itemPrices(choice) * qty

' Currency selection


[Link](vbCrLf & "Choose currency to pay with:")
[Link]("1. US Dollar (USD)")
[Link]("2. ZiG")
[Link]("Enter 1 or 2: ")
Dim currencyChoice As Integer = [Link]([Link]())

Const USD_TO_ZIG As Double = 13.6

[Link](vbCrLf & "------ BILL ------")


[Link]("Item: " & itemNames(choice))
[Link]("Quantity: " & qty)
[Link]("Amount in ZiG: ZiG " &
[Link]("0.00"))
If currencyChoice = 1 Then
Dim amountDueUsd As Double = amountDueZig / USD_TO_ZIG
[Link]("Amount Due: US Dollar " &
[Link]("0.00"))
ElseIf currencyChoice = 2 Then
[Link]("Amount Due: ZiG " &
[Link]("0.00"))
Else
[Link]("Invalid currency choice.")
End If

[Link](vbCrLf & "Press any key to exit...")


[Link]()

End Sub

End Module

1) What programming “type” do the codes follow?


Both codes are written in a procedural style (simple step-by-step logic) using:

 Console I/O ([Link], [Link], [Link])


 Arrays (to store menu items and prices)

They do not use OOP (Object-Oriented Programming) like classes/objects.


So the main structure is procedural + arrays.

2) Explanation of the first (i) code (ZiG only)


Key lines

vb
Dim price() As Double = {20.4, 27.2, 40.84, 40.8}
Dim name() As String = {"Sadza and Guru", "Sadza and Chicken", "Rice and
Chicken", "Rice and Beef"}

 price() is an array of Doubles (the amounts in ZiG).


 name() is an array of Strings (the food names).
 Both arrays have the same length, and the same index matches the same menu item:
o index 0 → choice 1
o index 1 → choice 2
o index 2 → choice 3
o index 3 → choice 4
Example: if user chooses 3, then choice = 3 - 1 = 2, so:

 name(2) = "Rice and Chicken"


 price(2) = 40.84

Reading user input

vb
[Link]("Choice (1-4): ")
Dim choice As Integer = [Link]([Link]()) - 1

 - 1 is needed because arrays in VB start at index 0.

Calculating the bill

vb
Dim totalZig As Double = price(choice) * qty
[Link]("Amount Due = ZiG " & [Link]("0.00"))

 Uses the selected unit price (price(choice)) and multiplies by quantity (qty).

3) Explanation of the second (ii) code (currency conversion)


This code adds currency selection.

Same arrays

vb
Dim price() As Double = {20.4, 27.2, 40.84, 40.8}
Dim name() As String = {...}

Same idea as part (i): arrays store the menu data.

Calculate in ZiG first

vb
Dim totalZig As Double = price(choice) * qty

 The bill is first computed in ZiG because the menu prices are given in ZiG.

Ask which currency to pay with

vb
[Link]("Pay with: 1 = USD, 2 = ZiG")
Dim cur As Integer = [Link]([Link]())
Conversion rule

vb
Const USD_TO_ZIG As Double = 13.6

Given: 1 USD = 13.6 ZiG

So if you have totalZig and want USD:

totalUsd=totalZig13.6\text{totalUsd} = \frac{\text{totalZig}}{13.6}totalUsd=13.6totalZig

That’s exactly what the code does:

vb
If cur = 1 Then
Dim totalUsd As Double = totalZig / USD_TO_ZIG
[Link]("Amount Due = US Dollar " & [Link]("0.00"))

If cur = 2, it just prints ZiG.

4) Arrays vs OOP (why you don’t see classes)


 Arrays: used to store related data (menu names and prices).
 OOP: would usually involve something like:
o a Class MenuItem
o objects like Dim item As New MenuItem(...)

Your code doesn’t do that—there are no Class or Sub/Function methods inside classes.

So: array-based procedural programming, not OOP.

Module Program
Sub Main()
[Link]("=== AREA CALCULATOR ===")
[Link]("1. Circle")
[Link]("2. Rectangle")
[Link]("3. Triangle")
[Link]("4. Square")
[Link]("Enter choice (1-4): ")
Dim choice As Integer = [Link]([Link]())

Dim area As Double = 0

If choice = 1 Then
[Link]("Enter radius: ")
Dim r As Double = [Link]([Link]())
area = 3.14159 * r * r

ElseIf choice = 2 Then


[Link]("Enter length: ")
Dim length As Double = [Link]([Link]())
[Link]("Enter width: ")
Dim width As Double = [Link]([Link]())
area = length * width

ElseIf choice = 3 Then


[Link]("Enter base: ")
Dim b As Double = [Link]([Link]())
[Link]("Enter height: ")
Dim h As Double = [Link]([Link]())
area = 0.5 * b * h

ElseIf choice = 4 Then


[Link]("Enter side: ")
Dim s As Double = [Link]([Link]())
area = s * s
Else
[Link]("Invalid choice.")
[Link]()
Return
End If

[Link]("Area = " & [Link]("0.00"))


[Link]("Press any key to exit...")
[Link]()
End Sub
End Module
vb
Dim choice As Integer = [Link]([Link]())

1) [Link]()

 Waits for the user to type something in the console.


 Returns what the user typed as a String (even if they typed 1).

Example: user types 2


[Link]() returns "2" (string).

2) [Link](...)

 Converts the string into an Integer.


 So "2" becomes 2.

3) Dim choice As Integer = ...

 Declares a variable named choice.


 The variable type is Integer.
 Then it stores the converted value in it.

You might also like