I need help with coding this assignment :)

jenny_may

Member
Joined
Aug 20, 2011
Messages
7
Programming Experience
Beginner
Hi guys, my computer class has been given the following assignment:

Bob is a carpet layer. He needs a way to calculate how much carpet he needs to carpet floors in various rooms. Bob is then able to work out how much to charge his customers. The carpet he has is supplied in rolls that are 5 meters wide and 30 metres long. Any excess width is discarded. This means that rooms that are wider than 5 meters need several strips of carpet. The smallest space that he will carpet is 1m x1m, the biggest is 20m x 20m. At the moment , he has to draw diagrams to work out how much carpet he needs. He needs a program that will tell him:

1) How many metres of carpet he
will need to cover a room.

2) How many rolls of carpet that
will be.

3) How much full-width carpet he
will have left on the last roll.


Bob charges for his carpet by the metre, so he only works in the whole metre lengths. In other words even if he only needed 1.6 meters of carpet for the room, he would charge the customer for 2 meters because he has to discard the unused part of the metre.


My problem is I can't figure out (remember) how to output the results of the calculate button to the appropriate textboxes. I've highlighted in my picture the textboxes that I wish to output to. What I want to happen is once the room dimensions have been entered and the calculate button clicked, for the results to be displayed in the 3 textboxes on the right. Here is what I've already coded:


Public Class Form1
    
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        End 'Exits the program.
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtLength.Text = "" 'Resets value to 0.
        txtWidth.Text = "" 'Resets value to 0.
        txtOut.Text = ""    'Resets value to 0.
        txtOut2.Text = ""   'Resets value to 0.
        txtOut3.Text = ""   'Resets value to 0.
    End Sub

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Dim width, length As Integer 'Defines variables.
        If IsValidInput(txtWidth.Text) Then
            width = txtWidth.Text
        End If
        If IsValidInput(txtLength.Text) Then
            length = txtLength.Text
            Exit Sub
        End If
End Sub

    Sub CalculateAndDisplay(ByVal w, ByVal l)
        Dim strips, totalLength, rolls, carpetLeft As Integer 'Defines variables.
        Const rollwidth = 5 'Sets the constant amount of the variable"
        Const rollLength = 30 'Sets the constant amount of the variable"
        strips = w \ rollwidth
        If (w Mod rollwidth) > 0 Then strips = strips + 1
        totalLength = strips * 1
        rolls = totalLength \ rollLength

        If (totalLength Mod rollLength) > 0 Then
            rolls = rolls + 1
            carpetLeft = 30 - (totalLength Mod rollLength)
        Else : carpetLeft = 0
        End If
    End Sub
    Function IsValidInput(ByVal Value)
        IsValidInput = False
        If IsNumeric(Value) Then
            If Value < 1 Or Value > 20 Then 'Stating the value of the entries allowed.
                IsValidInput = False 'If entry is not between 1 and 20 then it's invalid
            Else : IsValidInput = True 'If it's between 1 and 20 it's correct and therefore valid
            End If
        End If
    End Function

End Class


carpet.jpg
 
You already know how to get data from a TextBox into a variable:
VB.NET:
    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

        Dim width, length As Integer 'Defines variables.

        If IsValidInput(txtWidth.Text) Then

            [B][U]width = txtWidth.Text[/U][/B]

        End If

        If IsValidInput(txtLength.Text) Then

            [B][U]length = txtLength.Text[/U][/B]

            Exit Sub

        End If

End Sub
Getting data from a variable into a TextBox is simply the opposite.
 
Ok I've got it to output (thanks!) but now I'm getting incorrect calculations. What's could be wrong?

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim width, length As Integer 'Defines variables.
If IsValidInput(txtWidth.Text) Then
width = txtWidth.Text
End If
If IsValidInput(txtLength.Text) Then
length = txtLength.Text
End If
CalculateAndDisplay(width, length)
End Sub

Sub CalculateAndDisplay(ByVal w, ByVal l)
Dim strips, totalLength, rolls, carpetLeft As Integer 'Defines variables.
Const rollwidth = 5 'Sets the constant amount of the variable"
Const rollLength = 30 'Sets the constant amount of the variable"
strips = w \ rollwidth
If (w Mod rollwidth) > 0 Then strips = strips + 1
totalLength = strips * 1
rolls = totalLength \ rollLength
txtOut.Text = totalLength.ToString()
txtOut2.Text = rolls.ToString()
txtOut3.Text = carpetLeft.ToString()

If (totalLength Mod rollLength) > 0 Then
rolls = rolls + 1
carpetLeft = 30 - (totalLength Mod rollLength)
Else : carpetLeft = 0
End If
End Sub

Function IsValidInput(ByVal Value)
IsValidInput = False
If IsNumeric(Value) Then
If Value < 1 Or Value > 20 Then 'Stating the value of the entries allowed.
IsValidInput = False 'If entry is not between 1 and 20 then it's invalid
Else : IsValidInput = True 'If it's between 1 and 20 it's correct and therefore valid
End If
End If
End Function

End Class
 
Back
Top