# I need help with coding this assignment :)

Page 1 of 1

## 5 Replies - 1737 Views - Last Post: 24 August 2011 - 06:43 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=244459&amp;s=ff43070247c69d5d75d22e3ef00245ac&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jennymay

Reputation: 0
• Posts: 7
• Joined: 22-August 11

# I need help with coding this assignment :)

Posted 22 August 2011 - 02:01 PM

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
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

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
```

Is This A Good Question/Topic? 0

## Replies To: I need help with coding this assignment :)

### #2 SixOfEleven

• using Caffeine;

Reputation: 947
• Posts: 6,342
• Joined: 18-October 08

## Re: I need help with coding this assignment :)

Posted 22 August 2011 - 02:06 PM

What you need to do is set the Text property of your text boxes to the appropriate values. After the calculations you would for example set textOut.Text as follows:

```textOut.Text = totalLenght.ToString()

```

You would set the other two text boxes using the appropriate variable from the method.

### #3 Psyguy

• D.I.C Regular

Reputation: 81
• Posts: 345
• Joined: 12-January 11

## Re: I need help with coding this assignment :)

Posted 22 August 2011 - 02:07 PM

Why do you exit the btnCalculate_Click method prior to reaching the end of it? If you want to display both the width and length then proceed to the CalculateAndDisplay method, you will need to remove the "Exit Sub" from the length display code:

```If IsValidInput(txtLength.Text) then
length=txtLength.Text
End If

```

thats what I've got for you so far.

EDIT: ...and what SixOfEleven said.

This post has been edited by Psyguy: 22 August 2011 - 02:09 PM

### #4 jennymay

Reputation: 0
• Posts: 7
• Joined: 22-August 11

## Re: I need help with coding this assignment :)

Posted 23 August 2011 - 12:41 AM

Thanks! I'm getting the textboxes to output data now but the data is incorrect. Here's what I changed:

```    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 = strips.ToString()

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

### #5 Psyguy

• D.I.C Regular

Reputation: 81
• Posts: 345
• Joined: 12-January 11

## Re: I need help with coding this assignment :)

Posted 23 August 2011 - 11:51 AM

...why would you want to calculate a number by multiplying it by 1?

```totalLength = strips * 1

```

It appears that your incorrect values are a math issue rather than a coding issue. I could be wrong though. For example, you use width to find the number of strips, but then you calculate the rolls by using totalLength (which is a function of the number of strips) and rollLength. I would consider revising your math.

This post has been edited by Psyguy: 23 August 2011 - 11:51 AM

### #6 Nightfish

Reputation: 74
• Posts: 158
• Joined: 24-May 11

## Re: I need help with coding this assignment :)

Posted 24 August 2011 - 06:43 AM

Is it possible that strips * 1 should be strips * l? As has been pointed out, strips * 1 only makes a limited amount of sense.

I generally would not advise to use l as a variable name, nor to use single letters for variable names at all. Around my company, it's good practice to give variables names so that you can tell what they are supposed to represent instantly.