Lecture 6

Selected Project Ideas
Project Ideas
Physics 101 - A bouncing ball in a picture box with controls to adjust the kinematics.
Features:
• User can Start/Stop/Reset the simulation
• User can set the level of gravity
• User can "nudge" the ball Up/Down/Left/Right
Loan Calculator - A calculator that permits user to compute the unknown quantity for a simple
interest loan.
Features:
• User can choose to calculate Monthly Payment/Principal/
Interest Rate given the other two quantities
• User can set the duration of the loan
• Calculator will show amortization table for loan repayment
• Outputs amortization table as a *.csv format file
Weather Station - Displays weather maps and other weather data for the US or region.
Features:
• User chooses regional or whole US
• Information is taken from online sources
• Maps/data are updated periodically
Maze Crawler - Timed maze solver with threats and rewards.
Features:
•
•
•
•
New mazes can be generated/saved replayed
Players high scores are saved/displayed
In play mode is local view only
Once solved sky view is unlocked and shows players path
Roll Your Own - A two player dice game in which each player tries to accumulate as many
points as possible without rolling their first number again.
Features:
• The pair of dice are "animated" with a slowing sequence
of pips accompanied by a sound effect
• State of Play is maintained - current player - initial roll
and accumulated value are displayed
• First player to reach 120 wins. During a turn a player can
roll as many times as they want, however if a player rolls
their first number again before passing the dice their
score resets to zero (0)
Software Development Lifecycle
DOCUMENT
Define
Design
Encode
Debug
Verify
Validate
Physics 101 - Module Level Test
public void move()
{
x += vx + 0.5F*ax;
y += vy + 0.5F*ay;
vx += ax;
vy += ay;
}
public void bounce(double w, double h)
{
if (x < size / 2.0F)
{
vx *= -1.0F;
x = size / 2.0F;
}
if (x > w - size / 2.0F)
{
vx *= -1.0F;
x = w - size / 2.0F;
}
if (y < size / 2.0F)
{
vy *= -1.0F;
y = size / 2.0F;
}
if (y > h - size / 2.0F)
{
vy *= -1.0F;
y = h - size / 2.0F;
}
}
private Bitmap aBitmap;
private void drawBall()
{
resetFrameBuffer();
Graphics g = Graphics.FromImage(aBitmap);
System.Drawing.SolidBrush aBrush = new System.Drawing.SolidBrush(Color.Black);
aBrush.Color = aball.Color;
g.FillEllipse(aBrush, (float)(X - Size / 2.0), (float)(Y - Size / 2.0), (float)Size, (float)Size);
picBox.Image = aBitmap;
}
private void resetFrameBuffer()
{
Graphics g = CreateGraphics();
if (aBitmap != null)
aBitmap.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
aBitmap = new Bitmap(picBox.Width, picBox.Height, g);
}
The Loan Calculator
The generation of a loan amortization schedule is an important business calculation. Using
simple interest (annual interest rate divided by the number of periodic payments per year)
we can determine the required periodic payment to retire (amortize) a loan of a specified
amount distributed over a specified number of payments.
𝑟 1+𝑟 𝑛
𝑀=𝑃
1+𝑟 𝑛−1
M = monthly payment
P = principal (amount of loan)
r = monthly interest rate = annual interest rate/12
n = number of payments
We will build a C# program to compute a loan amortization schedule with the features:
•
User can choose to calculate Monthly Payment/Principal/Interest Rate
given the other two quantities
•
User can set the duration of the loan
•
Calculator will show amortization table for loan repayment
•
Outputs amortization table as a *.csv format file
The GUI Design
It is important to understand that as part of the GUI design process the programmer must have some plan for how the functions
of the program are going to be implemented. For example, deciding to use radio buttons to determine which parameter will be
calculated affects the way those calculations will be performed.
Placing the radio buttons inside a GroupBox links
them so that only one will be permitted to be checked
at any given time.
private void btnCalc_Click(object sender, EventArgs e)
{
if (radioPayment.Checked)
calc_payment();
if (radioPrincipal.Checked)
calc_principal();
if (radioAnnualInterest.Checked)
calc_interest();
if (radioNumPayments.Checked)
calc_num_payments();
}
Calculating Monthly Payment and/or Amount of Loan Amount
public double payment_calc(double p, double r, double n)
{
return p * (r * Math.Pow(r + 1.0, n) / (Math.Pow(r + 1.0, n) - 1.0));
}
public double principal_calc(double m, double r, double n)
{
return m * ((Math.Pow(r + 1.0, n) - 1.0) / (r * Math.Pow(r + 1.0, n)));
}
Getting Ready to Call the payment_calc( ) Method
In a Windows Forms application the values placed in textboxes are type string, so we need to convert them to their proper
numeric types before passing them to the payment_calc( ) method. If the user forgets to enter values in the textboxes for
the know quantities then we don't want the program to fail...Rather we would like to give the user a helpful message...
private void calc_payment()
{
try
{
principal = Convert.ToDouble(tbxPrincipal.Text);
monthly_interest = Convert.ToDouble(tbxAnnualInterest.Text) / 1200.0;
num_payments = Convert.ToDouble(tbxNumPayments.Text);
monthly_payment = payment_calc(principal, monthly_interest, num_payments);
tbxPayment.Text = String.Format("{0:C}", monthly_payment);
}
catch
{
MessageBox.Show("Be sure to provide values for known quantities");
}
}
Calculating an Interest Rate
Directly calculating the interest rate given the desired monthly payment, loan amount (principal) and number of payments
is not feasible due to the difficulty (or impossibility) of solving the amortization formula for interest when the number of
payments is large (>5). Instead we can use the principal_calc( ) method and iterate until we find an interest rate that
most closely matches the specified parameters.
private void calc_interest()
{
double curint = 0.0;
double curprin;
try
{
principal = Convert.ToDouble(tbxPrincipal.Text);
monthly_payment = Convert.ToDouble(tbxPayment.Text);
num_payments = Convert.ToDouble(tbxNumPayments.Text);
why is this an important test?
try
{
if (monthly_payment * num_payments < principal)
MessageBox.Show("Sorry but the total of all
payments can't be less than the
amount you are borrowing...");
else
{
do
slowly increases interest
{
curint += 0.00001;
rate until current principal
curprin = principal_calc(monthly_payment, curint, num_payments);
matches the specified value
} while (curprin > principal);
tbxAnnualInterest.Text = String.Format("{0:0.00}",curint * 1200.0);
}
}
catch
{
MessageBox.Show("Calculation went squirrely");
}
}
catch
{
MessageBox.Show("Be sure to provide values for the known quantities");
}
}
Displaying the Amortization Schedule in a Rich Text Box
This code segment DOES NOT create the amortization schedule. It is a sample of code to show how to align table values
into columns.
private void btnSchedule_Click(object sender, EventArgs e)
{
double balance = principal;
double int_payment;
double princ_payment;
int_payment = balance * monthly_interest;
princ_payment = monthly_payment - int_payment;
rtbSchedule.Text = "
Balance \t Payment \t\t Interest \t\t Principal \r\n";
rtbSchedule.Text += String.Format("{0:C}\t{1:C}\t\t{2:C}\t\t{3:C}\r\n", balance,
monthly_payment, int_payment, princ_payment);
}
The balance column will show the remaining balance after each scheduled payment.
The interest payment is the monthly interest rate x the current balance.
The principal payment is the monthly payment - interest payment.
The remaining balance is the previous balance - the principal payment.
These calculations are repeated for the number of payments specified.
Note: The last payment may be slightly different (less) than the regular monthly payment.