Saturday, May 20, 2017

( 9608 ) Pre-release Material 2017 Paper 4: Solution




OOP ( Containment )


Task 1.2


Module Module1


    Class Test

        Private TestID As String
        Private Questions(10) As Question
        Private NumberOfQuestions As Integer
        Private MaxMarks As Integer
        Private Level As Char
        Private DateSet As Date

        Private markCount As Integer = 0
        Private flag As Boolean = False

        Public Sub DesignTest()
            markCount = 0
            Dim qID As String
            Dim qText As String
            Dim Ans As String
            Dim Mar As Integer = 0
            Dim Top As String

            Console.Write(" Enter test ID: ")
            TestID = Console.ReadLine()



            Console.Write(" Enter number of questions: ")

            Do
                NumberOfQuestions = Console.ReadLine()

                If NumberOfQuestions > 10 Then
                    Console.WriteLine()
                    Console.Write(" Invalid. The maximum number of questions is 10.")
                    Console.Write(" Enter number of questions again: ")
                Else
                    flag = True
                End If

            Loop Until flag = True

            flag = False


            Console.Write(" Enter maximum number of marks: ")
            MaxMarks = Console.ReadLine()

            Console.Write(" Enter the test Level ( A , S, G ): ")
            Do
                Level = Console.ReadLine()
                Level = UCase(Level)
                If Level = "A" Or Level = "S" Or Level = "G" Then
                    flag = True

                Else
                    Console.WriteLine()
                    Console.Write("Invalid. Valid test levels are ( A, S, G): ")
                End If
            Loop Until flag = True
            flag = False


            Console.Write(" Enter the test date: ")
            DateSet = Console.ReadLine()
            Console.WriteLine()

            For counter As Integer = 1 To NumberOfQuestions
                Console.Write(" Enter question {0} QuestionID: ", counter)
                qID = Console.ReadLine()

                Console.Write(" Enter Question Topic: ")
                Top = Console.ReadLine()

                Console.Write(" Enter question {0} Question text: ", counter)
                qText = Console.ReadLine()

                Console.Write(" Enter question {0} question answer: ", counter)
                Ans = Console.ReadLine()

                Console.Write(" Enter question {0} Question mark: ", counter)


                Do
                    Mar = Console.ReadLine()
                    markCount = Mar + markCount
                    If markCount > MaxMarks Then
                        markCount = markCount - Mar
                        Console.WriteLine()
                        Console.Write(" Invalid. You cannot give mark greater than maximum number of marks. Remaining number of marks is {0}. Enter again: ", (MaxMarks - markCount))

                    Else
                        flag = True
                    End If
                Loop Until flag = True

                flag = False

                Questions(counter) = New Question
                Questions(counter).SetQuestion(qID, qText, Ans, Mar, Top)
                Console.WriteLine()
            Next


        End Sub



        Public Sub PrintTest()
            Console.WriteLine("Test ID: " & TestID)
            Console.WriteLine("Test Date: " & DateSet)
            Console.WriteLine("Test Leve: " & Level)
            Console.WriteLine("Number of questions in a test: " & NumberOfQuestions)
            Console.WriteLine()


            For counter As Integer = 1 To NumberOfQuestions
                Console.WriteLine(" Question {0} Topic: {1} ", counter, Questions(counter).GetTopic)
                Console.WriteLine(" Question {0} ID and Text: {1} ", counter, Questions(counter).GetQuestion)
                Console.WriteLine(" Question {0} mark: {1} ", counter, Questions(counter).GetMarks)
                Console.WriteLine()


            Next

        End Sub


        Public Sub PrintAnswers()
            Console.WriteLine("Test ID: " & TestID)
            Console.WriteLine("Test Date: " & DateSet)
            Console.WriteLine("Test Leve: " & Level)
            Console.WriteLine("Number of questions in a test: " & NumberOfQuestions)
            Console.WriteLine()

            For counter As Integer = 1 To NumberOfQuestions
                Console.WriteLine(" Question {0} Topic: {1}", counter, Questions(counter).GetTopic)
                Console.WriteLine(" Answer of Question {0}: {1}", counter, Questions(counter).GetAnswer)
                Console.WriteLine(" Question {0} mark: {1}", counter, Questions(counter).GetMarks)
                Console.WriteLine()
            Next

        End Sub

    End Class



    Class Question

        Private QuestionID As String
        Private QuestionText As String
        Private Answer As String
        Private Marks As Integer
        Private Topic As String

        Public Sub SetQuestion(ByVal qID As String, ByVal qText As String, ByVal Ans As String, ByVal Mar As Integer, ByVal Top As String)
            QuestionID = qID
            QuestionText = qText
            Answer = Ans
            Marks = Mar
            Topic = Top
        End Sub

        Public Function GetQuestion() As String
            Return (QuestionID & " " & QuestionText)
        End Function

        Public Function GetMarks() As Integer
            Return (Marks)
        End Function

        Public Function GetTopic() As String
            Return (Topic)
        End Function

        Public Function GetAnswer() As String
            Return (Answer)
        End Function

    End Class


    Sub Main()
        Dim access As Test = New Test()
        Dim choice As Integer
        choice = 0


        Do

            Console.WriteLine("1. Design Test")
            Console.WriteLine("2. Print Question")
            Console.WriteLine("3. Print Answer")
            Console.WriteLine("4. Exit")

            choice = Console.ReadLine()
            Console.Clear()


            Select Case choice
                Case 1
                    access.DesignTest()
                    Console.ReadLine()
                    Console.Clear()
                Case 2
                    access.PrintTest()
                    Console.ReadLine()
                    Console.Clear()
                Case 3
                    access.PrintAnswers()
                    Console.ReadLine()
                    Console.Clear()
            End Select

        Loop Until choice = 4

    End Sub
End Module



Friday, April 7, 2017

Random Number: Program code ( visual basic )

Computer Science(9608) notes and Resources






Program that randomly generates number from 1 to 100 both inclusive. 




Module Module1

    Sub Main()
        Randomize()
        Dim Temp As Integer
        For i = 1 To 4
            Temp = Int(Rnd() * 100) + 1
            Console.WriteLine(Temp)
        Next
        Console.ReadLine()
    End Sub

End Module





Module Module1

    Sub Main()

        Dim temp As New Random
        Dim num As Integer
        For counter = 1 To 50
            num = temp.Next(1, 101)
            Console.WriteLine(num)
        Next
        Console.ReadLine()
    End Sub

End Module





Dictionary: Program code ( visual basic )



Computer Science(9608) notes and Resources






Implementing a dictionary using Dictionary class in Visual Basic





Module Module1

    Dim dictionary As New Dictionary(Of String, Integer)


    Sub InsertInDictionary(ByVal key As String, ByVal value As Integer)
        If dictionary.ContainsKey(key) Then
            Console.WriteLine(" Key alreadly exist! ")
        Else
            dictionary.Add(key, value)
        End If

    End Sub



    Sub findInDictionary(ByVal key As String)
        If dictionary(key) Then
            Console.WriteLine(" Found and its value is: " & dictionary(key))
        Else
            Console.WriteLine(" Not Found ")
        End If
    End Sub



    Sub removeFromDictionary(ByVal key As String)
        dictionary.Remove(key)
    End Sub



    Sub printValueInDictionary(ByVal key As String)
        Console.WriteLine(dictionary(key))
    End Sub



    Sub printNoOfEntriesInDictionary()
        Console.WriteLine(dictionary.Count)
    End Sub



    Sub printDictionary()
        Dim pair As KeyValuePair(Of String, Integer)

        For Each pair In dictionary

            Console.WriteLine("{0}, {1}", pair.Key, pair.Value)
        Next
    End Sub



    Sub Main()
        Console.WriteLine(" Inserting 4 items in dictionary")
        InsertInDictionary("John", 10)
        InsertInDictionary("maria", 90)
        InsertInDictionary("sofia", 50)
        InsertInDictionary("Mia Kalifa", 69)
        Console.WriteLine()
        Console.Write(" Value of key sofia: ")
        printValueInDictionary("sofia")
        Console.WriteLine()
        Console.Write(" Number of items in dictionary: ")
        printNoOfEntriesInDictionary()
        Console.WriteLine()
        Console.WriteLine(" Remove one item from dictionary")
        removeFromDictionary("Mia Kalifa")
        Console.WriteLine()
        Console.WriteLine(" Print whole dictionary")
        printDictionary()
        Console.WriteLine()
        Console.WriteLine(" find an item:")
        findInDictionary("sofia")
        Console.ReadLine()

    End Sub



End Module




                                                                                                         - CIE A-LEVEL COMPUTER SCIENCE (  9608 )