In: Computer Science
Windows Forms application using Visual Basic:
Create a class called Character that a role-playing game might use to represent a character within the game.
A character should include six stats as instance variables
–
strength, dexterity, constitution, intelligence, wisdom and
charisma (all types are int).
Your class should have a constructor that initializes these six
instance variables. Provide Properties with an
appropriate set and get block for each instance variable.
In addition, provide a method named getStatsTotal that calculates
the total of these six stats, then returns the amount as an int
value.
If the total is not positive, it should be set to 0.
Create a form that can be used to create a character, assign the stats and display the stats total.
Program:
Character.vb
Public Class Character
Public strength, dexterity, constitution, intelligence, wisdom,
charisma As Integer
Public totalStats As Integer
Sub New(strength1 As Integer, dexterity1 As Integer,
constitution1 As Integer, intelligence1 As Integer, wisdom1 As
Integer, charisma1 As Integer)
setStrength(strength1)
setDexterity(dexterity1)
setConstitution(constitution1)
SetIntelligence(intelligence1)
setWisdom(wisdom1)
setCharisma(charisma1)
End Sub
Sub setStrength(strength1 As Integer)
strength = strength1
End Sub
Sub setDexterity(deterity1 As Integer)
dexterity = deterity1
End Sub
Sub setConstitution(constitution1 As Integer)
constitution = constitution1
End Sub
Sub SetIntelligence(intelligence1 As Integer)
intelligence = intelligence1
End Sub
Sub setCharisma(charisma1 As Integer)
charisma = charisma1
End Sub
Sub setWisdom(wisdom1 As Integer)
wisdom = wisdom1
End Sub
Function getStrength()
Return strength
End Function
Function getDexterity()
Return dexterity
End Function
Function getConstitution()
Return constitution
End Function
Function getIntelligence()
Return intelligence
End Function
Function getWisdom()
Return wisdom
End Function
Function getCharisma()
Return charisma
End Function
Function getStatsTotal()
totalStats = getStrength() + getDexterity() + getConstitution() +
getIntelligence() + getWisdom() + getCharisma()
If (totalStats < 0) Then
totalStats = 0
End If
Return totalStats
End Function
End Class
Form1.vb
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
Dim s, d, c, i, w, charm As Integer
s = Convert.ToInt32(txtStrength.Text)
d = Convert.ToInt32(txtDeterity.Text)
c = Convert.ToInt32(txtConstitution.Text)
i = Convert.ToInt32(txtIntelligence.Text)
w = Convert.ToInt32(txtWisdom.Text)
charm = Convert.ToInt32(txtCharisma.Text)
Dim ch As New Character(s, d, c, i, w, charm)
txtTotalStats.Text = ch.getStatsTotal()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
txtTotalStats.Enabled = False
End Sub
End Class
Screen (form):