# Need serious help with VB2010 assignment



## Killer_Rubber_Ducky (Jul 12, 2013)

Hey guys,

I have this VB2010 assignment I have to do by sunday.

 I need help writing a Visual Basic program that calculates the area and circumference of a hot tub once I enter the inches and feet.

The professor left us with this clue: 
	

	
	
		
		

		
			




and





We have not covered this information yet so I am stumped. I have no clue where to start.

The instructions for the program state that the application computes the area and circumference of a hot tub by entering the diameter in feet and inches. The area and circumference should go out to 2 decimal places. You should be able to clear the entry, enter a new diameter and exit the form.

I have been trying to do this assignment all week. I am not a programmer. My ASS degree requires I take a programming class.


This is what I have so far. I taught myself Qbasic back in the day but this is definately not QB any more.



> Public Class frmHotTubCalc
> 
> Const diameter As Integer = (feet * 12) + inches
> Const radius As Integer = diameter \ 2
> ...


----------



## FordGT90Concept (Jul 12, 2013)

First, your use of Const is going to create serious problems.  Constants (= Const) which means the value is set at runtime and never changed (it is read only).  Change those to Dim (= dimension) and that should fix that problem.

Second, you have a logical error here with your radius and diameter variables.  You're treating them as functions and they simply aren't.  Either you can move those into functions so they return the appropriate value or, better, you should put all the calculating in a subroutine tied to either a button or radio (in the case of inches or feet).

Third, "decimal places" means you're going to have to use Float, Double, or Decimial datatypes.  You may want to look up the differences between those three but .NET is mostly developed for Double and Decimal but Decimal is intended for currency (only two decimal places) so I could recommend using Double.  You should never round in algebra until the end.

Fourth, I would recommend focusing on the area and circumference first.  Tackle that problem first and deal with inches/feet later.  Conversion from feet to inches and inches to feet is fairly straight forward by comparison.

Fifth, I think I'll give you the basic area and circumference formulas to work off of:

```
' Make sure diameter is not equal to 0 before running this
Dim area As Double = Math.PI * Math.Pow(diameter / 2, 2)
Dim circumference As Double = Math.PI * diameter
```

Sixth, use Math.Round(variable, 2) to round to two places before converting the Doubles to text for displaying.


----------



## BiggieShady (Jul 12, 2013)

Killer_Rubber_Ducky said:


> Public Class frmHotTubCalc
> 
> Const diameter As Integer = (feet * 12)   inches
> Const radius As Integer = diameter \ 2
> ...



Declare your variables (not constants) here but do not calculate anything yet ... you don't have any input here yet, calculate it on click.



Killer_Rubber_Ducky said:


> Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
> Close()
> End Sub
> 
> ...



Calculate them here reading input from the text boxes on the form, and update the labels that should display the result.



Killer_Rubber_Ducky said:


> End Sub
> End Class


----------



## Killer_Rubber_Ducky (Jul 12, 2013)

Ok, I just ran through it again and can get it to calculate but the figures are WAY off. This is using a somewhat similar tutorial in the book. 



> Public Class frmHotTubCalc
> 'Program: Hot Tub Area / Circumference Calculator
> 'Author: Ian Comings
> 'Date: July 12th 2013
> ...


----------



## FordGT90Concept (Jul 12, 2013)

Can you zip up the project and attach it so we can run it?  It's a lot easier than creating a form to complement your code.


----------



## Killer_Rubber_Ducky (Jul 12, 2013)

fordgt90concept said:


> can you zip up the project and attach it so we can run it?  It's a lot easier than creating a form to complement your code.



View attachment HotTub.zip

This assignment is confusing as hell. I am assuming the hot tub is circular not rectangular. Why must I have the input dimensions in Feet and Inches? ugh I hate this book


----------



## FordGT90Concept (Jul 13, 2013)

Considering they aren't giving you the formulas for a rectangle, it certainly seems that it is circular.

Input in feet and inches is indeed messy.  Standard practice is usually to keep it in one or the other, not mix them.  Most likely it is to make it more difficult.


Anyway, on to the source...

In Visual Studio, you can step through programs using breakpoints to check the values at each stage.  It should help you isolate where the logical error(s) are at.

The first thing you need to do is convert the string in txtNumberofFeet.Text and txtNumberOfInches.Text to doubles.  There's not much sense in copying their values to a string variable so I would recommend changing strNumberofFeet and strNumberofInches to double.  The compiler will then tell you where you need to convert from string to double and you can do that via CDbl() or, the more universal route, Convert.ToDouble().

Second, on line 28 and 29, you are adding feet to inches without multiplying feet by 12 to get inches.  Instead of making all the formulas more complicated by converting feet to inches in them, I would recommend taking the two doubles in the step above and convert them into a unifying inch value.  Use that inch value in all your formulas.

Third, you are using the same math for radius and diameter on both formulas.  One requires a diameter and the other requires a radius.  You also need to specify on the user interface what the user is to input: a diameter or a radius.  I don't know if your instructions are explicit here but, anyway, it is a problem that needs to be resolved.

Fourth, again, avoid Decimal.  Decimal is intended to hold the amount of US public debt ($17 trillion), not small numbers with division and the like.  Use Double (unless your instructions are explicit).  You could get rounding problems with Decimal.

Fifth, I'm not sure why you are using 3.14 instead of Math.PI.  If the instructions aren't explicit, Math.PI is better.  Math.PI is a constant.

Sixth, it is a good idea to get in the habit of using Math.Pow instead of ^.  In most languages, ^ is a bitwise XOR operator.  It literally would make 1 to 0 and 0 to 1 (opposite from the binary perspective).

That should be enough feedback to get you on the right path.


----------



## Killer_Rubber_Ducky (Jul 13, 2013)

FordGT90Concept said:


> Considering they aren't giving you the formulas for a rectangle, it certainly seems that it is circular.
> 
> Input in feet and inches is indeed messy.  Standard practice is usually to keep it in one or the other, not mix them.  Most likely it is to make it more difficult.
> 
> ...


Well on the Math.Pi part AFAIK we have not covered the Math function.


----------



## Aquinus (Jul 13, 2013)

Killer_Rubber_Ducky said:


> Well on the Math.Pi part AFAIK we have not covered the Math function.



Learning how to program is about learning the language and how to solve a problem, not necessarily all the objects and abilities of the language that enable you to solve almost any problem. If you're not sure about a class, you should google it and typically use the first link. If you're going to program in the real world, you'll be expected to figure this stuff out on your own, so I recommend making more of an effort to do research on how to do things because the information you seek does exist. I don't know everything about the languages I use. I know enough to write it and I know enough to use Google when I'm unsure how to do something or if something exists or not. Just an FYI.

By the way, Math.PI is public class constant (Math is the class, PI is the public constant). All you do is access it.


----------



## Killer_Rubber_Ducky (Jul 13, 2013)

Aquinus said:


> Learning how to program is about learning the language and how to solve a problem, not necessarily all the objects and abilities of the language that enable you to solve almost any problem. If you're not sure about a class, you should google it and typically use the first link. If you're going to program in the real world, you'll be expected to figure this stuff out on your own, so I recommend making more of an effort to do research on how to do things because the information you seek does exist. I don't know everything about the languages I use. I know enough to write it and I know enough to use Google when I'm unsure how to do something or if something exists or not. Just an FYI.
> 
> By the way, Math.PI is public class constant (Math is the class, PI is the public constant). All you do is access it.



well, Im taking the class since my major requires at least 1 programming class. I wanted to take C but all the C classes this summer did not "make" so I was stuck with VB. I self taught myself QBasic back in the day and could have easily made this program in QBasic minus the pretty program interface. ^_^ All of these classes and objects confuse the hell out of me. I am not planning on being a programmer. I can write Linux Shell scripts and old QBasic programs but this "new fangled" Visual BASIC is confusing and not "basic".


----------



## Killer_Rubber_Ducky (Jul 13, 2013)

This is the final code I have decided to go with. Mainly since I am sick and tired of working on this program which seems like it should not have been assigned to INTRO to VB students in an Online class. I have gotten the calculations as close as possible. The Area seems right in Square inches while the Circumference is still way off it seems.



> Public Class frmHotTubCalc
> 'Program: Hot Tub Area / Circumference Calculator
> 'Author: Ian Comings
> 'Date: July 12th 2013
> ...



View attachment HotTub_Comings_Ian.zip


----------



## Aquinus (Jul 13, 2013)

Killer_Rubber_Ducky said:


> The Area seems right in Square inches while the Circumference is still way off it seems.



That's because you're using Integers for math when they should all be floating point (or double) values. You're also converting the text to a double then storing it in a String so you're negating what you're trying to do in the first place.

Use the correct data type for what is being stored.

Example of something bad:

```
Dim strRadiusNumberofFeet As String
Dim strRadiusNumberofInches As String
strRadiusNumberofFeet = Convert.ToDouble(txtRadiusNumberOfFeet.Text)
strRadiusNumberofInches = Convert.ToDouble(txtRadiusNumberOfInches.Text)
```
The two variables should be doubles.


```
Dim strDiameterFeet As String
Dim strDiameterInches As String
```
Should be doubles.


```
Dim intDiameterinches As Integer
```
Should be a double, but shouldn't make a difference if you type-cast it to a double or if it's preceded by a double value.


```
Dim AreaRadius As String
Dim CircumferenceDiameter As String
AreaRadius = Math.Round(Area, 2)
CircumferenceDiameter = Math.Round(Circumference, 2)
```
Once again, use the right type for the data. This should be double. This shouldn't be textual data until you're ready to display it, you also can't round text.

This isn't a hard problem, you've just used the language incorrectly in many ways which is why it's behaving strangely.

*Strings are text, do not treat them as numbers. If you're working with numbers, use numeric data types.*


----------



## FordGT90Concept (Jul 13, 2013)

What Aquinus said.  You should only be using doubles.  The only time you should be using string is when you take a value from the GUI and when you write a value to the GUI.  All the time in between, it should be double.

Also, I wouldn't submit what you have.  That issue really needs to be fixed or you'll get a bad grade on it.


----------



## MxPhenom 216 (Jul 13, 2013)

Aquinus said:


> Learning how to program is about learning the language and how to solve a problem, not necessarily all the objects and abilities of the language that enable you to solve almost any problem. If you're not sure about a class, you should google it and typically use the first link. If you're going to program in the real world, you'll be expected to figure this stuff out on your own, so I recommend making more of an effort to do research on how to do things because the information you seek does exist. I don't know everything about the languages I use. I know enough to write it and I know enough to use Google when I'm unsure how to do something or if something exists or not. Just an FYI.
> 
> By the way, Math.PI is public class constant (Math is the class, PI is the public constant). All you do is access it.



That's some useful information. Ill be taking a Computer Science class coming this fall for my Computer Engineering major.


----------



## Killer_Rubber_Ducky (Jul 14, 2013)

Please correct this section since it makes sense to me but will not compute correctly.



> Private Sub btnCalculate_Click(sender As System.Object, e As System.EventArgs) Handles btnCalculate.Click
> 'This event handler is executed when the user clicks the Calculate button.
> 'It calculates the Area and Circumference from the given Feet and Inches.
> 'Current step involves taking the content of the text boxes and
> ...


----------



## FordGT90Concept (Jul 14, 2013)

I'd suggest changing *Dim Circumference As Double = (2 * Math.PI * radius)* on line 31 (approximately) to *Dim Circumference As Double = Math.PI * Diameterinches*.  You already have the diameter so why not use it?

What about it doesn't compute correctly?  I put in 5'8" and got 3631.68 square inches area and 213.63 inch circumference.  Note that it is inches, not feet (the GUI I have from the ZIP says Feet).  If you're supposed to output feet, you might want to look into dividing by 12 or something.  Anyway, make sure the units on the GUI match what you are displaying.  If the value is square inches/feet, make sure to put _square_ inches/feet as well.  Units are very important.

Also, I don't know if you change the GUI but if you haven't, make sure to specify that the input expected is the diameter.

Going by your latest upload, you have fields for radius and diameter but it only uses diameter.  You should either delete the inputs for radius or synchronize them using the TextChanged event on the TextBoxes.  What I would do is make all four textboxes point to a single TextChanged event.  I'd then write a function that pulls feet out of inches (use divide by 12 to get the feet and modulus 12 to get the inches) that returns a Double(2) array or ByRef types to return the value.  Cast the Sender into TextBox and get the .Name property.  If .Name.StartsWith("txtRadius") convert the feet and inches into inches, multiply by two, and use the function described above to populate the diameter textboxes.  If the .Name.StartsWith("txtDiameter") convert the feet and inches into inches, divide by two, and use the function described above to populate the diameter textboxes.  Make sure all four fields are assumed to be 0 if blank or otherwise invalid.

Since that function is pretty complicated, I'll provide it for you:

```
' if value = 68
    Public Function [b]InchesToFeetInch[/b](ByVal value As Double) As Double()
        Dim output(1) As Double
        output(0) = Math.Floor(value / 12) ' Sets 5
        output(1) = value Mod 12 ' Sets 8
        Return output ' Return (0) = 5 and (1) = 8
    End Function
```
To use it:

```
Dim feetinch() As Double = [b]InchesToFeetInch[/b](inches) ' if inches = 68
        MsgBox(feetinch(0) & "'" & feetinch(1) & Chr(34)) ' prompt will say 5'8"
```
If you have any questions about the above code, ask.  It uses an array which you probably haven't been exposed to yet.  If you haven't, it is no doubt coming.


----------



## Killer_Rubber_Ducky (Jul 14, 2013)

FordGT90Concept said:


> I'd suggest changing *Dim Circumference As Double = (2 * Math.PI * radius)* on line 31 (approximately) to *Dim Circumference As Double = Math.PI * Diameterinches*.  You already have the diameter so why not use it?
> 
> What about it doesn't compute correctly?  I put in 5'8" and got 3631.68 square inches area and 213.63 inch circumference.  Note that it is inches, not feet (the GUI I have from the ZIP says Feet).  If you're supposed to output feet, you might want to look into dividing by 12 or something.  Anyway, make sure the units on the GUI match what you are displaying.  If the value is square inches/feet, make sure to put _square_ inches/feet as well.  Units are very important.
> 
> Also, I don't know if you change the GUI but if you haven't, make sure to specify that the input expected is the diameter.



hmm. you are right. I have been using a calculator to check the math and i must have been doing the math wrong on the calculator. I was not dividing the result in half for the radius!!


----------



## FordGT90Concept (Jul 14, 2013)

I made a pretty big edit to my previous post.  Check it.


----------



## Killer_Rubber_Ducky (Jul 14, 2013)

FordGT90Concept said:


> I'd suggest changing *Dim Circumference As Double = (2 * Math.PI * radius)* on line 31 (approximately) to *Dim Circumference As Double = Math.PI * Diameterinches*.  You already have the diameter so why not use it?
> 
> What about it doesn't compute correctly?  I put in 5'8" and got 3631.68 square inches area and 213.63 inch circumference.  Note that it is inches, not feet (the GUI I have from the ZIP says Feet).  If you're supposed to output feet, you might want to look into dividing by 12 or something.  Anyway, make sure the units on the GUI match what you are displaying.  If the value is square inches/feet, make sure to put _square_ inches/feet as well.  Units are very important.
> 
> ...



I had edited the form to remove all except the Diameter fields.

So your initial response was correct. 
i had made the mistake of not taking into account that the Diameter is double the radius so If I took the answer I got on the calculator and divided it in half I got my answer.\
Anyways. Thanks to all for the help. I have been stuck in QBasic mode.
I submitted it along with the Use Case document and Event planner documents.


----------



## Killer_Rubber_Ducky (Jul 15, 2013)

Just got the grade on the assignment.

Thanks to you guys' help, I scored 100% and the teacher said that it was a tough assignment and that the I did an excellent job on it even though it was complicated for beginners. I really wish there was a way to insert QBASIC code directly into a VB2010 program. I could have coded this thing in QB ( the math part anyway) and tied it to the form opr something.


----------



## Kreij (Jul 15, 2013)

techPowerUp! ... Helping people with their homework since 2004! lol

Good job, Ducky.


----------



## shovenose (Jul 15, 2013)

Nice, glad you got it worked out.


----------



## Killer_Rubber_Ducky (Jul 18, 2013)

I just wrote a program in QBasic 4.5 for the same project. It works flawlessly and took maybe 10 minutes to write. 

I have included the Standalone EXE I compiled as well as the .BAS file
The .BAS file is encoded only for QBasic compilers to read as Such I have posted the code below.

View attachment HotTubCalcbyIComings.zip


```
PRINT "Welcome to the Hot Tub Dimensions Calculator."
PRINT "With the Calculator, you will be able to find the Area"
PRINT "and Circumference of the Hot Tub so that you may make an"
PRINT "informed choice in your purchase. As they say: Measure twice"
PRINT "Cut once."
PRINT
PRINT
PRINT
SLEEP 5
25 PRINT "What are the dimensions in Feet and Inches of the Tub in question?"
DIM DiamFeet AS DOUBLE
DIM DiamInch AS DOUBLE
INPUT "Feet"; DiamFeet
INPUT "Inches"; DiamInch
DIM Diameter AS DOUBLE
LET Diameter = (DiamFeet * 12) + DiamInch
DIM Pi AS DOUBLE
LET Pi = 3.14159265359#
LET r = Diameter / 2
LET Area = Pi * r ^ 2
LET Circumference = Pi * Diameter
IF Diameter > 0 THEN
PRINT "The Area of the Tub is:"; Area; "in^2"
PRINT "The Circumference of the Tub is:"; Circumference; "inches"
SLEEP 10
ELSE PRINT "The diameter is invalid. It must be greater than 0."
SLEEP 2
GOTO 25
END IF
DIM CalcAns AS STRING
INPUT "Would you like to calculate for another tub"; CalcAns
IF CalcAns = "yes" THEN
GOTO 25
ELSE END
END IF
END
```


----------



## BiggieShady (Jul 18, 2013)

Killer_Rubber_Ducky said:


> I just wrote a program in QBasic 4.5 for the same project. It works flawlessly and took maybe 10 minutes to write.



Cool. This code with very minor modifications could run in VB2010 using console application project.


----------



## FordGT90Concept (Jul 18, 2013)

Yup...
PRINT = System.Console.WriteLine()
SLEEP = System.Threading.Sleep()
INPUT = System.Console.ReadLine()

VB should support goto statements but they aren't recommended.


----------



## Kreij (Jul 18, 2013)

GAH ! GOTO statements should have died a swift death 20 years ago.


----------



## Killer_Rubber_Ducky (Jul 18, 2013)

Kreij said:


> GAH ! GOTO statements should have died a swift death 20 years ago.



Well, one of the things I had attempted that I kind of forgot how to do was use Subs and Case statements.  I could call a subroutine but the variables from the Sub failed to work in the main program so I just used the GOTO command since it just works. Especially if you write a program with Line numbers like we used to do back in GWBASIC and BASICA days. If I wanted to make it even more complicated, I could have made a simple number based menu. If I wanted to add graphics, I could have done so as well. But since I was focusing on the Calculation side, I left it at that. ^_^ I have created GUIs in QBasic that used the mouse to navigate.


----------

