Matthew Lo PBL 2

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

Cambridge A Level

Subject : Computer Science Visual Basic : Introduction Date : 9/7/2024

1. Write a program that will request the user to enter their name, gender(M/F) and if the
they are vaccinated status(Yes/No).
The program will then output the name, gender and True( if answer is Yes for
vaccinated) or False( if answer is No for vaccinated) for vaccinated status.

Note : Use string data type for name, Character data type for gender, Boolean data
type for vaccinated)

Sub Main()
Dim Name As String
Dim Gender As Char
Dim Status As Boolean

Console.WriteLine("Input name:")
Name = Console.ReadLine()

Console.WriteLine("Input gender (M/F)")


Gender = Console.ReadLine()

Console.WriteLine("Input True/False status of vaccination:")


Status = Console.ReadLine()

Console.WriteLine("{0}, {1}, {2}", Name, Gender, Status)


Console.ReadLine()
End Sub

Lecturer : V.Subramaniam Page 1 of 6


Taylor’s University Subang Jaya
Cambridge A Level
Subject : Computer Science Visual Basic : Introduction Date : 9/7/2024

2. Write a program to calculate the total area of a circle. The program will ask the user to
enter the Radius of the circle.
Formula :Area = Pi * Radius * Radius , Pi = 3.14159

Note : Use Integer data type for Radius, Real data type for Area

Sub Main()
Dim Radius As Integer
Dim Pi As Single = 3.14159

Console.WriteLine("Enter Radius: ")


Radius = Console.ReadLine()

Console.WriteLine("Area: {0} units", Pi * Radius * Radius)

Console.ReadLine()
End Sub

2. Write a program to calculate the hypotenuse of right triangle. Ask the user to input the
2 sides.

Sub Main()
Dim Side1 As Integer
Dim Side2 As Integer

Console.WriteLine("Input 1st Side: ")


Side1 = Console.ReadLine()

Console.WriteLine("Input 2nd Side: ")


Side2 = Console.ReadLine()

Console.WriteLine("Area of triangle: {0} units", (Side1 * Side2) / 2)

Console.ReadLine()

End Sub

Lecturer : V.Subramaniam Page 2 of 6


Taylor’s University Subang Jaya
Cambridge A Level
Subject : Computer Science Visual Basic : Introduction Date : 9/7/2024

3. Write a program to calculate the volume of a cylinder.

Sub Main()
Dim h, r As Integer
Console.WriteLine("Height of cylinder: ")
h = Console.ReadLine()
Console.WriteLine("Radius of cylinder: ")
r = Console.ReadLine()

Console.WriteLine("Volume: {0} units", 3.14159 * r * r * h)


Console.ReadLine()

End Sub

4. According to Plato, a man should marry a woman whose age is half his age plus seven
years. Write a program that requests a man’s age as input and gives the ideal age of
his wife.

Sub Main()
Dim Age As Integer

Console.WriteLine("Plato: Pray, disclose unto me, good sir, thy years upon
this earthly realm? ")
Age = Console.ReadLine

Console.WriteLine("Plato: Ah, wisdom dictates that a woman of {0} years


shall be your concubine.", Age / 2 + 7)
Console.ReadLine()
End Sub

5 (a) A marathon runner records their time for a race in hours, minutes and seconds.

Lecturer : V.Subramaniam Page 3 of 6


Taylor’s University Subang Jaya
Cambridge A Level
Subject : Computer Science Visual Basic : Introduction Date : 9/7/2024

An algorithm is shown below in structured English.

INPUT race time as hours, minutes and seconds


CALCULATE race time in seconds
STORE race time in seconds
OUTPUT race time in seconds
Sub Main()
Dim RaceHours, RaceMins, RaceSecs As Integer
Console.WriteLine("Input race time in hour(s): ")
RaceHours = Console.ReadLine()

Console.WriteLine("Input race time in minute(s): ")


RaceMins = Console.ReadLine()

Console.WriteLine("Input race time in sec(s): ")


RaceSecs = Console.ReadLine()

Console.WriteLine("Total race time in seconds: {0}", RaceHours * 3600 +


RaceMins * 60 + RaceSecs)
Console.ReadLine()
End Sub

(b) The identifier table needs to show the variables required to write a program for this
algorithm.
Complete the table.

Identifier Data type Description


RaceHours INTEGER The hours part of the race time.
RaceMinutes INTEGER The minutes part of the race time.
RaceSeconds INTEGER The seconds part of the race time.

6. (a) A program stores data about hospital patients.

Lecturer : V.Subramaniam Page 4 of 6


Taylor’s University Subang Jaya
Cambridge A Level
Subject : Computer Science Visual Basic : Introduction Date : 9/7/2024

Give a suitable identifier name for each of the data items.

Description of data item Suitable identifier name


The temperature of the patient PatientTemperature
The temperature of the room RoomTemperature
The patient identification number PatientID
The name of the nurse taking the NurseName
measurement

(b) Write a program to display the variables with the given values as follows:

Sub Main()

Dim MyGreeting As String


Dim MyInitials As Char
Dim AgeInYears As Integer
Dim Weight As Single
Dim Married As Boolean
Dim Children As Boolean

MyGreeting = "Happy Birthday"


MyInitials = "C"
AgeInYears = 27
Weight = 60.5
Married = True
Children = True

Console.WriteLine("My greeting is {0}", MyGreeting)

Console.WriteLine("My initial is {0}", MyInitials)

Console.WriteLine("My age is {0}", AgeInYears)

Console.WriteLine("My mass * gravitational acceleration is {0}", Weight)


Console.WriteLine("Do I have a wife? {0}", Married)
Console.WriteLine("Do I have kids to feed? {0}", Children)
Console.ReadLine()
End Sub

Lecturer : V.Subramaniam Page 5 of 6


Taylor’s University Subang Jaya
Cambridge A Level
Subject : Computer Science Visual Basic : Introduction Date : 9/7/2024

Lecturer : V.Subramaniam Page 6 of 6


Taylor’s University Subang Jaya

You might also like