0% found this document useful (0 votes)
116 views6 pages

Coding Pre Release Nov 2015

The document contains code snippets demonstrating the use of classes, inheritance, and polymorphism in Visual Basic. It defines a base Taxi class with methods to calculate charges and display registration numbers. It then defines Minibus and Car classes that inherit from Taxi, with Minibus overriding the charge calculation and each having their own maximum passenger methods. The Main method creates Minibus and Car objects, calls their methods to calculate charges and display details.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views6 pages

Coding Pre Release Nov 2015

The document contains code snippets demonstrating the use of classes, inheritance, and polymorphism in Visual Basic. It defines a base Taxi class with methods to calculate charges and display registration numbers. It then defines Minibus and Car classes that inherit from Taxi, with Minibus overriding the charge calculation and each having their own maximum passenger methods. The Main method creates Minibus and Car objects, calls their methods to calculate charges and display details.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Pre released solution NOV 2015

CS 9608/04

Task3.1 ----------------------------------------------------------------------------ModuleModule
SubMain()
DimtheaddressAsInteger
DimmyisbnAsString
Console.WriteLine("Enter an ISBN of 10 digits")
myisbn = Console.ReadLine()
theaddress = Hash(myisbn)
Iftheaddress = -1 Then
Console.WriteLine("Invalid isbn....!")
Else
Console.WriteLine("The address for ISBN "&myisbn&"is: "&theaddress)
EndIf
Console.ReadKey()
EndSub
FunctionHash(ByValisbnAsString) AsInteger
Dim address AsInteger
address = LeftToInt(isbn, 3)
Return address
EndFunction
FunctionLeftToInt(ByValThisStringAsString, ByVal n AsInteger) AsInteger
Dim letter AsChar
Dim valid AsBoolean = True
IfLen(ThisString) >= n Then
Forpos = 1 To n
letter = Mid(ThisString, pos, 1)
IfAsc(letter) <= 48 OrAsc(letter) > 57 Then
valid = False
Exit For
EndIf
Next
Else
valid = False
EndIf
If valid = TrueThen
ReturnVal(Left(ThisString, 3))
Else
Return -1
EndIf
EndFunction
EndModule
task 2------------------------------------------------------------------------------Imports System.IO
ModuleModule1
PublicdummyrecordAsBookRecord
SubMain()
DimfileNumberAsInteger = FreeFile()
DimmySizerecordnumberAsInteger = Len(dummyrecord)
dummyrecord.isbn = "0000000000"
FileOpen(fileNumber, "D:/BookFile2", OpenMode.Random, OpenAccess.Write, ,
mySizerecordnumber)
For count = 1 To 1000
FilePut(fileNumber, dummyrecord, count)
Next
FileClose(fileNumber)
Console.ReadKey()
EndSub
EndModule
StructureBookRecord
<VBFixedString(10)>DimisbnAsString
<VBFixedString(24)>Dim title AsString
DimonloanAsBoolean
EndStructure

Mr.Kreshna MUDHOO (TEL: 57145790)

1|P a g e

Pre released solution NOV 2015

CS 9608/04

task 3.3----------------------------------------------------------------------------ModuleModule1
PublicNewBookAsBookRecord
SubMain()
DimnewaddressAsInteger
DimmyisbnAsString
DimfileNumberAsInteger = FreeFile()
DimmySizerecordnumberAsInteger = Len(NewBook)
FileOpen(fileNumber, "D:/BookFile2", OpenMode.Random, OpenAccess.Write, ,
mySizerecordnumber)
For count = 1 To 5
Console.WriteLine("Enter book ISBN: ")
NewBook.isbn = Console.ReadLine()
Console.WriteLine("Enter book Title: ")
NewBook.title = Console.ReadLine()
newaddress = Hash(NewBook.isbn)
Ifnewaddress = -1 Then
Console.WriteLine("Invalid isbn....!")
Else
Seek(fileNumber, newaddress)
FilePut(fileNumber, NewBook, )
EndIf
Next
FileClose(fileNumber)
Console.ReadKey()
EndSub
FunctionHash(ByValisbnAsString) AsInteger
Dim address AsInteger
address = LeftToInt(isbn, 3)
Return address
EndFunction
FunctionLeftToInt(ByValThisStringAsString, ByVal n AsInteger) AsInteger
Dim letter AsChar
Dim valid AsBoolean = True
IfLen(ThisString) >= n Then
Forpos = 1 To n
letter = Mid(ThisString, pos, 1)
IfAsc(letter) < 48 OrAsc(letter) > 57 Then
valid = False
Exit For
EndIf
Next
Else
valid = False
EndIf
If valid = TrueThen
ReturnVal(Left(ThisString, 3))
Else
Return -1
EndIf
EndFunction
EndModule
StructureBookRecord
<VBFixedString(10)>DimisbnAsString
<VBFixedString(24)>Dim title AsString
DimonloanAsBoolean
EndStructure

Mr.Kreshna MUDHOO (TEL: 57145790)

2|P a g e

Pre released solution NOV 2015

CS 9608/04

Task 3.4------------------------------------------------------------------------------Imports System.IO


ModuleModule1
PublicbookrecordAsBookRecord
SubMain()
DimfileNumberAsInteger = FreeFile()
DimmySizerecordnumberAsInteger = Len(bookrecord)
Dim n AsInteger = 1
FileOpen(fileNumber, "D:/BookFile2", OpenMode.Random, OpenAccess.Read, ,
mySizerecordnumber)
DoWhileNotEOF(fileNumber)
FileGet(fileNumber, bookrecord, n)
IfLeft(bookrecord.isbn, 3) <>"000"Then
Console.WriteLine(bookrecord.isbn&" - "&bookrecord.title&" - "&bookrecord.onloan)
EndIf
n=n+1
Loop
FileClose(fileNumber)
Console.ReadKey()
EndSub
EndModule
StructureBookRecord
<VBFixedString(10)>DimisbnAsString
<VBFixedString(24)>Dim title AsString
DimonloanAsBoolean
EndStructure
task 3.5 -----------------------------------------------------------------------------Imports System.IO
ModuleModule1
PublicNewBookAsBookRecord
SubMain()
DimfileNumberAsInteger = FreeFile()
DimmySizerecordnumberAsInteger = Len(NewBook)
Try
FileOpen(fileNumber, "u:/BodsFile.txt", OpenMode.Random, OpenAccess.Read, ,
mySizerecordnumber)
'.......
Console.WriteLine("File Found!!")
FileClose(fileNumber)
Catch ex AsException
Console.WriteLine("File Not Found!!")
EndTry
Console.ReadKey()
EndSub
EndModule
StructureBookRecord
<VBFixedString(10)>DimisbnAsString
<VBFixedString(24)>Dim title AsString
DimonloanAsBoolean
EndStructure
Task 3.6------------------------------------------------------------------------------ModuleModule1
PublicdummyrecordAsBookRecord
SubMain()
DimfileNumberAsInteger = FreeFile()
DimmySizerecordnumberAsInteger = Len(dummyrecord)
dummyrecord.isbn = "9999999999"
dummyrecord.title = "Duplicateman"
FileOpen(fileNumber, "D:/BookFile2", OpenMode.Random, OpenAccess.Write, ,
mySizerecordnumber)

Mr.Kreshna MUDHOO (TEL: 57145790)

3|P a g e

Pre released solution NOV 2015

CS 9608/04

FilePut(fileNumber, dummyrecord, 123)


FileClose(fileNumber)
Console.WriteLine(dummyrecord.isbn)
Console.ReadKey()
EndSub
EndModule
StructureBookRecord
<VBFixedString(10)>DimisbnAsString
<VBFixedString(24)>Dim title AsString
DimonloanAsBoolean
EndStructure
task 3.7------------------------------------------------------------------------------Imports System.IO
' task 3.7------------------------------------------------------------------------------ModuleModule1
PublicNewBookAsBookRecord
SubMain()
DimnewaddressAsInteger
Dim position, count AsInteger
DimfileNumberAsInteger = FreeFile()
DimmySizerecordnumberAsInteger = Len(NewBook)
Dimnewisbn, newtitleAsString
FileOpen(fileNumber, "D:/BookFile", OpenMode.Random, OpenAccess.ReadWrite, ,
mySizerecordnumber)
Console.WriteLine("Enter book ISBN: ")
newisbn = Console.ReadLine()
Console.WriteLine("Enter book Title: ")
newtitle = Console.ReadLine()
newaddress = Hash(newisbn)
Ifnewaddress = -1 Then
Console.WriteLine("Invalid isbn....!")
Else
Seek(fileNumber, newaddress)
FileGet(fileNumber, NewBook, newaddress)
position = newaddress
count = 1
DoWhileLeft(NewBook.isbn, 3) <>"000"And count < 1000
If position >= 1000 Then
position = 1
Else
position = position + 1
EndIf
count = count + 1
FileGet(fileNumber, NewBook, position)
Loop
If count < 1000 Then
NewBook.isbn = newisbn
NewBook.title = newtitle
FilePut(fileNumber, NewBook, position)
Console.WriteLine("success....!")
Else
Console.WriteLine("File is full....!")
EndIf
EndIf
FileClose(fileNumber)
Console.ReadKey()
EndSub
FunctionHash(ByValisbnAsString) AsInteger
Dim address AsInteger
address = LeftToInt(isbn, 3)
Return address

Mr.Kreshna MUDHOO (TEL: 57145790)

4|P a g e

Pre released solution NOV 2015

CS 9608/04

EndFunction
FunctionLeftToInt(ByValThisStringAsString, ByVal n AsInteger) AsInteger
Dim letter AsChar
Dim valid AsBoolean = True
IfLen(ThisString) >= n Then
Forpos = 1 To n
letter = Mid(ThisString, pos, 1)
IfAsc(letter) < 48 OrAsc(letter) > 57 Then
valid = False
Exit For
EndIf
Next
Else
valid = False
EndIf
If valid = TrueThen
ReturnVal(Left(ThisString, 3))
Else
Return -1
EndIf
EndFunction
EndModule
StructureBookRecord
<VBFixedString(10)>DimisbnAsString
<VBFixedString(24)>Dim title AsString
DimonloanAsBoolean
EndStructure

----------------------------------------------------------------------------------------------------------------------------------------Task 2
ModuleModule1
SubMain()
Dim pv1 AsMinibus = NewMinibus("NBR 123", 51, 49, 10)
pv1.showRegNo()
pv1.calcharge()
pv1.showcharge()
pv1.ShowMaxPassenger()
Dim pv2 Ascar = Newcar("mba 143", 21, 30)
pv2.showRegNo()
pv2.CalCharge()
pv2.showcharge()
pv2.ShowCMaxPassenger()
Console.ReadKey()
EndSub
EndModule
MustInheritClassTaxi
ProtectedregNoAsString
Protected charge AsInteger
PublicOverridableSubCalCharge()
charge = 200
EndSub
PublicSubshowRegNo()
Console.WriteLine(regNo)
EndSub
PublicSubshowcharge()
Console.WriteLine(charge)
EndSub
EndClass
ClassMinibus

Mr.Kreshna MUDHOO (TEL: 57145790)

5|P a g e

Pre released solution NOV 2015

CS 9608/04

InheritsTaxi
PrivateextrachargeAsDouble
PrivatemaxpassengerAsInteger
PublicSubNew(ByValregNoValueAsString, ByValChargeValueAsInteger,
ByValextrachargevalueAsDouble, ByValmaxpassengerValueAsInteger)
regNo = regNoValue
charge = ChargeValue
extracharge = extrachargevalue
maxpassenger = maxpassengerValue
EndSub
PublicOverridesSubcalcharge()
charge = charge + extracharge
EndSub
PublicSubShowMaxPassenger()
Console.WriteLine(maxpassenger)
EndSub
EndClass
Classcar
InheritsTaxi
PrivatemaxPassengerAsInteger
PublicSubNew(ByValregNoValueAsString, ByValChargeValueAsInteger,
ByValmaxPassengerValueAsInteger)
regNo = regNoValue
charge = ChargeValue
maxPassenger = maxPassengerValue
EndSub
PublicSubShowCMaxPassenger()
Console.WriteLine(maxPassenger)
EndSub
EndClass

Mr.Kreshna MUDHOO (TEL: 57145790)

6|P a g e

You might also like