Visual X++: - Dr Bernie Domanski's CSI/CUNY Website

Download Report

Transcript Visual X++: - Dr Bernie Domanski's CSI/CUNY Website

Visual X++
A Tutorial About Several Popular
Programming Languages
Dr. Bernie Domanski
The City University of New York
The College of Staten Island
[email protected]
http://domanski.cs.csi.cuny.edu/
Outline
Languages We’ll Look at (time permitting):
• Visual Basic
• C++ and Visual C++
• Java and Visual J++
• JavaScript and a little HTML
Disclaimer:
• This talk is NOT ABOUT PERFORMANCE OR CAPACITY
• We are looking only at Microsoft implementations of VB, VC++
and VJ++
• There are other versions that may, in fact, be better
• I am not an expert – just a language freak!
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
2
Common Program
• We’ll try to implement a simple loan
program in each language –
• Inputs –
– Principle (amount being borrowed)
– Interest Rate (annual – expressed as 8.5 )
– No. of Years (term of the loan)
• Output
– Monthly Payment
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
3
Some Explanation
• The formula:
Payment = Principle * (InterestRate/1200.) /
(1- { [ 1 / (1+InterestRate/1200.) ] ^ [NoOfYears*12] } );
• pmt = prin*(irate/1200.) / (1 - ((1 / (1+ irate/1200.)) ^ (NumYears*12)))
• a = p * ( i/1200.)/ (1-(pow( (1/(1+i/1200.)),(y*12) ) ));
• What’s Going On?
– Dividing the InterestRate by 1200 yields the interest rate
per month as a fraction – eg. .085
– Multiplying the NoOfYears by 12 yields the number of
months of the loan’s term
– ^ is the symbol for exponentiation - “to the power”
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
4
The Development Environment
• Very similar in all the Microsoft products
• How to develop an application –
– Draw the user interface … like a paint
program would
– Command buttons, text boxes, and other
objects will automatically recognize events
– You write code that responds to each event
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
5
Events and Objects
• Events
– Mouse click or double click
– Key pressed
• Objects
– Command button, Radio button, Check box
– Text box, Scroll Bar, etc
• Event-driven programming
– Tiny interrupt handlers – receive Windows
messages
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
6
Objects and Methods
• Properties
– Features that you can change –
– eg. Color, font, alignment
– Use the Properties Window to set feature
values at design time
• Methods
– Functions that come packaged with the
object that manipulate the object
– eg. Add items to a combo box
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
7
Visual Basic
• Lets build the loan program –
– Draw the GUI
– Write some code
– Try it out …
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
8
Try IT !!
Start VB
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
9
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
10
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
11
Common Objects & Samples
•
•
•
•
•
•
•
Command buttons
Edit box, labels
Scroll bars
Timers
Combo boxes
Common Dialog Control
Graphics
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
12
Where to Get Stuff
• All of the samples shown in this
presentation can be accessed and
downloaded from
http://domanski.cs.csi.cuny.edu/
• Look for the Languages Tutorial .zip file
within the area called ‘Stuff to
Download’
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
13
Memory
• Run the Game
• See the code
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
14
Visual C++
• Remember the movie The Exorcist ?
Remember the twisting of the little girl’s
head? That what happens to YOUR
head when diving into C++ !!
• Lets build the loan program …
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
15
Run the Loan Program
Then We’ll Build It !!
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
16
There’s LOTS to C++
•
•
•
•
•
•
•
Classes yield objects
Templates
Function Overloading
Operator Overloading
Constructors / Destructors
Inheritance
More (Virtual Functions, Containers, …)
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
17
C++ Code Samples
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
18
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
19
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
20
Visual J++
Microsoft’s Java Implementation
• Java is considered by many to be a
subset of C++
• Designed and first developed by SUN
• Java programs can run as non-Web
entities … but …
• Java applets (programs that execute on
Web servers) are very popular
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
21
Java Applets
• There’s got to be a web page defined
(.HTML code) where the Java code
resides.
• So we have to define a web page, and
• … the Java applet
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
22
The Web Page
Run the web page & applet …
• And the java source …
• And the java applet …
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
23
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
24
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
25
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
26
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
27
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
28
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
29
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
30
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
31
C:\JavaScript\Java\examples\mortgage-applet\mortgage.htm
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
32
JavaScript
• Fast and fun …
• Simplistic programming language …
similar to a macro language
• Great JavaScript Tutorial website:
http://www.netkontoret.dk/javascript.htm
• The Dr Bernie Java & JavaScript site:
http://domanski.cs.csi.cuny.edu/420stuff/420frame.htm
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
33
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
34
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
35
The Loan Program in JavaScript
• HTML defines the web page
• JavaScript code is embedded in the
HTML source
• We’ll also use some graphics and …
• … a scrolling message
• Run it first …
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
36
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
37
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
38
Source Code
<HTML>
<HEAD>
<TITLE>Home Finders Nationwide Realty</TITLE>
<!---------------------------------------------->
<SCRIPT language="javascript">
<!--Hide from old browsers
//
// Scrolling message has 4 components -
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
39
JS Code – page 2
// 1- display object- defines where the scrolling object displays
// 2- the message - text string assigned to a variable
// 3- the position - the starting location in which the message first displays
in the display object
// 4- the delay - the length of time between when a message ends and
when it starts to appear again
//
var scrollMsg = "Mortgage rates are at their LOWEST !!"
var msgSpace = "--- ---"
var beginPos = 0
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
40
JS Code – page 3
function scrollingMsg(){
document.msgForm.textBox.value =
scrollMsg.substring(beginPos,scrollMsg.length)+
msgSpace+scrollMsg.substring(0,beginPos)
beginPos = beginPos + 1
if (beginPos > scrollMsg.length) {
beginPos = 0
}
//
// recursive function call using setTimeout ...
window.setTimeout("scrollingMsg()",200)
}
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
41
JS Code – page 4
function doMort() {
//
// first, statements to clear the text boxes ...
//
document.MortCalc.Amount.value=" "
document.MortCalc.Rate.value=" "
document.MortCalc.Years.value=" "
document.MortCalc.Payment.value=" "
document.MortCalc.Amount.focus()
}
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
42
JS Code – page 5
function Calc(myform) {
//
// converts & strores the text box values as integer or floating point var's
// this function passes the value of the text box to a variable,
// uses the partseInt() function to convert the variable to a number, and
// then uses the isNaN() function to verify the value is a number.
//
// To pass the text box values to the function, you must
//
pass the form (in this case, myform) to the Calc function
//
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
43
JS Code – page 6
var mortAmount = document.MortCalc.Amount.value
var mortAmount = parseInt(mortAmount,10)
if (isNaN(mortAmount)) {
alert("The loan amount is not a number")
document.MortCalc.Amount.value=" "
document.MortCalc.Amount.focus()
}
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
44
JS Code – page 7
else {
//
// check the other fields ...
//
var mortRate = document.MortCalc.Rate.value
var mortRate = parseFloat(mortRate)
if (isNaN(mortRate)) {
alert("The interest rate is not a number")
document.MortCalc.Rate.value=" "
document.MortCalc.Rate.focus()
}
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
45
JS Code – page 8
//
else {
var mortYears = document.MortCalc.Years.value
var mortYears = parseInt(mortYears,10)
if (isNaN(mortYears)) {
alert("Number of years is not a number")
document.MortCalc.Years.value = " "
document.MortCalc.Years.focus()
}
}
}
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
46
JS Code – page 9
//
// so if everything is valid, call on the monthly() function
// to compute the monthly payment
//
document.MortCalc.Payment.value = monthly(mortAmount, mortRate,
mortYears)
}
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
47
JS Code – page 10
//
// the monthly() function - following the formula
function monthly(mortAmount, mortRate, mortYears) {
var Irate = mortRate / 1200
var Pmts = mortYears * 12
var Loan = mortAmount
return Loan * (Irate / (1 - (1 / Math.pow(1+Irate,Pmts))))
}
//-->
</script>
<!---------------------------------------------->
</HEAD>
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
48
JS Code – page 11
<BODY onload="scrollingMsg()">
<P ALIGN="CENTER"><IMG SRC="HOMELOGO.JPG">
</P>
<DIV ALIGN="CENTER"><CENTER>
<TABLE BORDER="0" WIDTH="75%">
<TR>
<TD></TD>
<TD><P ALIGN="CENTER"><IMG SRC="calculator.jpg" WIDTH="100"
HEIGHT="106"
ALT="Check out Rates"></TD>
<TD></TD>
</TR>
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
49
JS Code – page 12
<TR>
<TD><P ALIGN="CENTER">
<IMG SRC="house1.gif" WIDTH="180" HEIGHT="144">
</TD>
<TD>
<P ALIGN="CENTER">
<A HREF="#LoanCalc" onclick="doMort()">Estimate Mortgage Payment</a>
</p>
<!---------------------------------------------->
<FORM Name="msgForm">
<INPUT Type="text" Name="textBox" Size="33">
</FORM>
<!---------------------------------------------->
</TD>
<TD><P ALIGN="CENTER">
<IMG SRC="house2.gif" WIDTH="180" HEIGHT="144"></TD>
</TR>
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
50
JS Code – page 13
<TR>
<TD></TD>
<TD><IMG SRC="house3.gif" WIDTH="180" HEIGHT="144"></TD>
<TD></TD>
</TR>
</TABLE>
</CENTER></DIV>
<P ALIGN="CENTER">
<BR>
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
51
JS Code – page 14
<A NAME="LoanCalc"></P>
<H3 ALIGN="CENTER">Estimate Mortgage Payments</H3>
</A>
<CENTER>
<FORM Name="MortCalc">
<TABLE>
<TR>
<TD>Amount of Mortgage:</TD>
<TD><INPUT Type="text" Name="Amount" value=" " Size="9"></TD>
</TR>
<TR>
<TD>Interest Rate as % (e.g. 7.9):</TD>
<TD><INPUT Type="text" Name="Rate" value=" " Size="9"></TD>
</TR>
<TR>
<TD>Number of Years:</TD>
<TD><INPUT Type="text" Name="Years" value=" " Size="9"></TD>
</TR>
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
52
JS Code – page 15
<TR>
<TD>Monthly Payment:</TD>
<TD><INPUT Type="text" Name="Payment" value=" " Size="12"></TD>
</TR>
<TR>
<TD><INPUT Type="Button" value="Calculate" onclick="Calc(MortCalc)">
<INPUT Type="Reset"></TD>
</TR>
</TABLE>
</FORM>
</CENTER>
<HR>
<BR>
<BR>
</BODY>
</HTML>
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
53
Summary
• Tried to just scratch the surface on
these languages.
• A lot to absorb!
• Hope your interest has been
stimulated!
• Any questions??
• Enjoy UKCMG2K !!
• [email protected]
• http://domanski.cs.csi.cuny.edu/
(c) Dr. Bernie Domanski, 1999-2000. All Rights Reserved.
54