Solved

Visual Basic 6.0 knowledge of decision making and math operations

Posted on 2007-11-16
18
776 Views
Last Modified: 2012-06-21
I'm taking  intro to programming in school using Visual Basic 6.0 I don't understand on how to do the problem below. If someone can assist me on how to solve the problem.

PROBLEM:
There are three common temperature scales: Fahrenheit, Celsius, and Kelvin. Converting from Fahrenheit to Celsius is done with this formula:

    C = (F - 32) * 5 / 9
To convert from Celsius to Kelvin, you just add 273.16.
The form has one box, into which a floating-point value of degrees Fahrenheit will be entered. When the button is clicked, the program should print out the temperature in Fahrenheit, Celsius, and Kelvin. It should also report on the state (using one of the words solid, liquid, or gas) of Water, Aluminum, and Copper. The melting point and boiling point of these materials is as follows:

    Material         Melting Point (C)       Boiling Point (C)
   -----------------------------------------------------------
   Aluminum               660.3                   2519
   Copper                1085                     2562
   Water                    0                      100
If "1500" is enter, then the picture box should print something like:

Fahrenheit: 1500.0
Celsius:  815.6
Kelvin: 1088.7
Aluminum: Liquid
Copper: Solid
Water: Gas

0
Comment
Question by:apr2505
  • 8
  • 7
  • 2
  • +1
18 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 20300240
Just so you know, EE rules state that we can guide you in accomplishing your homework, but not actually do it for you.  EE is not a homework site.

So with that in mind, tell us how you think you should start.
0
 

Author Comment

by:apr2505
ID: 20300363
Ok I have this for a code so far

Private Sub cmdCompute_Click()
Dim celsius As Single
   'Convert Celsius to Fahrenheit to Kelvin
    picResults.Print "Celsius:"
    picResults.Print "Fahrenheit:"
    picResults.Print "Kelvin:"
End Sub
Private Function Fahrenheit(celsius As Single) As Single
'Convert Celsius to Fahrenheit
Fahrenheit = (9 / 5) * celsius + 32

Private Function Kelvin(celsius As Single) As Single
 'Convert Celsius to Kelvin
celsius = (Fahrenheit - 32) * 5 / 9 + 273.16
End Function

I'm not sure  how to get the other information to work. in the picture box it just displays Celsius, Fahrenheit, and Kelvin.


 
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 20300533
Is it part of your assignment to write the results of these calculations into a picture box?  Under normal circumstances text boxes, or maybe labels, would be used.

>IF "1500" is entered
Likely entered into a text box, I'll call it txtCelsius for this example

Create a button, name it cmbCalculate (for lack of a more original name), create two textboxes txtFarenheit and txtKelvin, and have the button's click event do the following;

Private Sub cmdCalculate_Click()

'Convert the value in Celsius to Farenheit and Kelvin.

'Hey, looky here, code comments.  Create lots of these so you can understand your own code better.

txtFarenheit = Farenheit(Me.txtCelsius.Text)
txtKelvin = Kelvin(txtCelsius.Text)

End Sub
0
 

Author Comment

by:apr2505
ID: 20300759
For this problem its a picture box,  understand what you are saying however my professor gave us  a form  to enter the code in and that has one text box, one picture box, and one command button. So I'm unable to create anything else to the form.
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 20300913
So, it's assumed that the text box always has celcius degrees in it?

if so, then it shouldn't be too hard.

you do the print like you've got but you need to append your formula results to each string

 picResults.Print "Celsius:" + your_celsius_string

same thing with your Farenheit and Kelvin lines,


Double check your Kelvin calculation.  What you said above is correct about how to convert from C to K.  But your code doesn't reflect that.





0
 
LVL 73

Expert Comment

by:sdstuber
ID: 20300952
ooops, also check your requirements.  Your code looks like you're trying to convert from Celsius, however, your instructions say you're converting from Farenheit
0
 

Author Comment

by:apr2505
ID: 20301628
How would I  have the calculation reflected in the code? thats what I'm stuck on I dont know i'm lost.
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 20302091
what I mean is you have this as a formula...

celsius = (Fahrenheit - 32) * 5 / 9 + 273.16

that's an accurate formula.

Youf need something like

Private Function Celsius(farenheit as Single) As Single
   Celsius =  '  put your F tp C formula here
End Function

Private Function Kelvin(farenheit as Single) As Single
   Kelvin=  '  put your F to K formula here
End Function

You've got the code, you've got the formulas,  you even have code executing formulas.
You just don't have them put together correctly.  Try again.  You're very close

0
 

Author Comment

by:apr2505
ID: 20302602
ok made changes and added some but my numbers arent popping up right
Here is what I have now. I'm not sure on how to get solid, liquid, and gas in the code. Everytime I hit the command button with the number 1500. 1500 is displayed for everything.

Private Sub cmdCompute_Click()
Dim Celsius As Single
Dim fahrenheit As Single
Dim Kelvin As Single
Dim Aluminum As Single
Dim Copper As Single
Dim water As Single
Dim solid, liquid, gas As String


   Celsius = Val(txtDegrees.Text)
   fahrenheit = Val(txtDegrees.Text)
   Kelvin = Val(txtDegrees.Text)
   
    picResults.Print "Celsius:"; Val(txtDegrees.Text)
    picResults.Print "Fahrenheit:"; Val(txtDegrees.Text);
    picResults.Print "Kelvin:"; Val(txtDegrees.Text)
    picResults.Print "Aluminum:"; Val(txtDegrees.Text); Tab(20)
    picResults.Print "Copper:"; Val(txtDegrees.Text); Tab(20)
    picResults.Print "Water:"; Val(txtDegrees.Text); Tab(20)
End Sub

Private Function Celsius(fahrenheit As Single) As Single
   Celsius = (9 / 5) * Celsius + 32
End Function

Private Function Kelvin(fahrenheit As Single) As Single
   Kelvin = (fahrenheit - 32) * 5 / 9 + 273.16
End Function

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 73

Expert Comment

by:sdstuber
ID: 20302997
you're printing Val(txtDegrees.Text) for every line,  so of course you're printing whatever value you put into your text box.  You need to use your functions for the Celsius and Kelvin lines.


Review your Celsius formula,  it''s not referencing your parameter.


As for your aluminum, copper and water lines, you need to have functions for them as well.

I suggest one function for each substance.  it takes a a celsius parameter and returns the state.

since you already have a function to convert farenheit to celsius, simply call that function when you invoke the state functions.
0
 

Author Comment

by:apr2505
ID: 20303048
ok I'll try again I did have txtcelsius.Text etc but it didn't work  I'll keep tryin though thanks
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 20303078
you don't want a separate txtbox for each output of the functions.

you have a farenheit text box so print the text from that on your farenheit line.
on your celsius line print the result of your celsius function
on your kelvin line print the result of your kelvin function
on your aluminum line print the result of your aluminum function
and so on
0
 
LVL 18

Expert Comment

by:JR2003
ID: 20305931
You need to call your functions:

    picResults.Print "Celsius:"; Celsius(Val(txtDegrees.Text))
    picResults.Print "Fahrenheit:"; Val(txtDegrees.Text);
    picResults.Print "Kelvin:"; Kelvin(Val(txtDegrees.Text))

    picResults.Print "Aluminum:"; Val(txtDegrees.Text); Tab(20)
    picResults.Print "Copper:"; Val(txtDegrees.Text); Tab(20)
    picResults.Print "Water:"; Val(txtDegrees.Text); Tab(20)
0
 

Author Comment

by:apr2505
ID: 20306317
Ok I've those changes and when the program is ran it highlights Celsius and Kelvin stating a Compile Error; Expected Array. is there any missing or something made in error? Here is my Code  I'm also trying to match Aluminum, Copper, and water with Liquid, Solid, and Gas. As stated above. This is really making pull my hair out.

Private Sub cmdCompute_Click()
Dim Celsius As Single
Dim fahrenheit As Single
Dim Kelvin As Single
Dim Aluminum As Single
Dim Copper As Single
Dim water As Single
Dim solid, liquid, gas As Single


   Celsius = Val(txtDegrees.Text)
   fahrenheit = Val(txtDegrees.Text)
   Kelvin = Val(txtDegrees.Text)
 


    picResults.Print "Celsius:"; Celsius(Val(txtDegrees.Text))
    picResults.Print "Fahrenheit:"; Val(txtDegrees.Text);
    picResults.Print "Kelvin:"; Kelvin(Val(txtDegrees.Text))
    picResults.Print "Aluminum:"; Celsius(Val(txtDegrees.Text); Tab(20))
    picResults.Print "Copper:"; Celsius(Val(txtDegrees.Text); Tab(20))
    picResults.Print "Water:"; Celsius(Val(txtDegrees.Text); Tab(20))
End Sub

Private Function Celsius(fahrenheit As Single) As Single
   Celsius = (9 / 5) * Celsius + 32
End Function

Private Function Kelvin(fahrenheit As Single) As Single
   Kelvin = (fahrenheit - 32) * 5 / 9 + 273.16
End Function
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 20307539
I think you're trying to rush and guess at the answer.
look at your code, piece by piece.

   Celsius = Val(txtDegrees.Text)
   fahrenheit = Val(txtDegrees.Text)
   Kelvin = Val(txtDegrees.Text)
 
you have 3 variables all set to the same value. That doesn't make sense.
plus, look through the rest of the code, even if those were correct, you never
use them!  Either don't declare and set the variables at all, or set them to the
values they need to be: celsius to celsius, farenheit to farenheit and kelving to kelvin.


    picResults.Print "Celsius:"; Celsius(Val(txtDegrees.Text))
    picResults.Print "Fahrenheit:"; Val(txtDegrees.Text);
    picResults.Print "Kelvin:"; Kelvin(Val(txtDegrees.Text))

These are correct! If you decide to use the variables then change these lines
but otherwise these should work just fine.

    picResults.Print "Aluminum:"; Celsius(Val(txtDegrees.Text); Tab(20))
    picResults.Print "Copper:"; Celsius(Val(txtDegrees.Text); Tab(20))
    picResults.Print "Water:"; Celsius(Val(txtDegrees.Text); Tab(20))

You're trying to get the words "solid", "liquid" or "gas"
but you're calling a function that returns a number.   So, that should be obvious to
you that these aren't correct.  However, it is a step in the right direction, because you're evaluation of the state of each is based on the celsius temperature you have above.


Private Function Celsius(fahrenheit As Single) As Single
   Celsius = (9 / 5) * Celsius + 32
End Function

This doesn't make sense.  you don't use the farenheit parameter
and your self reference celsius to.  But not as a function.  This is where your syntax error is coming from


Private Function Kelvin(fahrenheit As Single) As Single
   Kelvin = (fahrenheit - 32) * 5 / 9 + 273.16
End Function

This is correct!

Once you fix the above, you need something like the following for each substance
to determine the state.

Private Function AluminumState(Celsius As Single) As String
   AluminumState  = ' figure this part out.
End Function





0
 

Author Comment

by:apr2505
ID: 20308732
o no I'm not rushing this I'm just trying to see where I'm going wrong I'm not used to VB its making me frustrated. I've made more changes this is what I have below. When I hit the command button to see if the calculations is correct. The answers a completely wrong. However when I do the calculations on a calculator its write. o boy

Private Sub cmdCompute_Click()

   Celsius = (Fahrenheit - 32) * 5 / 9
   Fahrenheit = (Celsius + 32) * 9 / 5
   Kelvin = (Fahrenheit - 32) * (5 / 9) + 273.15

    picResults.Print "Celsius:"; (Celsius)
    picResults.Print "Fahrenheit:"; (Fahrenheit); Tab(20)
    picResults.Print "Kelvin:"; (Kelvin)

    picResults.Print "Celsius:"; (Celsius)
    picResults.Print "Fahrenheit:"; (Fahrenheit); Tab(20)
    picResults.Print "Kelvin:"; (Kelvin)
 
End Sub

0
 

Author Closing Comment

by:apr2505
ID: 31409604
I finally see I what needed to do. Thanks
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 20310016
Celsius = (Fahrenheit - 32) * 5 / 9

You haven't set Farenheit yet, so that's why this doesn't work.


   Fahrenheit = (Celsius + 32) * 9 / 5

You don't need to convert Farenheit, you have that from your text box.  Set this first
then setl Celsius and Kelvin

   Kelvin = (Fahrenheit - 32) * (5 / 9) + 273.15

The reason it seems like you're rushing is these problems  have had nothing to do with Visual Basic.  It's just a matter of examining each step.  What are you trying to do?  Does this step do that?

Programming is an art, but it's a methodical art.  Visual Basic doesn't change that.  Good luck in the rest of your studies.  Thanks for the points.  I'm glad I could help.

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A short article about problems I had with the new location API and permissions in Marshmallow
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now