Case Studies: in Oop and VB
Case Studies: in Oop and VB
Case Studies: in Oop and VB
Case Studies
In OOP and VB
by
{ cout << endl; cout << "Balance at start of period: $ " << start_balance << endl; cout << "Balance at end of period: $ " << current_balance << endl << endl; cout << "Number of deposits for this period: " << num_deps << endl; cout << "Number of checks for this period: " << num_checks << endl << endl; cout << "End of report." << endl; } // end print_report int CheckBalance::process_transactions() { int num, dep, wthdrwl; cout << "Function process_transactions entered." << endl; current_balance = start_balance; num_checks = 0; num_deps = 0; do { cout<<"Enter your number"<<endl; cout<<"1 : Deposit"<<endl<<"2 : withdrawal"<<endl<<"3 : Report"<<endl; cin>>num; switch(num) { case 1: cout<<"Enter the money you want to cin>>dep; current_balance += dep; num_deps += 1; cout<<"Your transaction has break; case 2: cout<<"Enter the money you want to cin>>wthdrwl; current_balance -= wthdrwl; num_checks += 1; cout<<"Your transaction has break; case 3: print_report(); break;
deposit : $ ";
been processed"<<endl;
withdrawal : $ ";
been processed"<<endl;
default: cout<<"invalid input"<<endl<<"Program exiting.."; } } while (num == 1 || num == 2); return 0; } // end process_transactions int main () { int dep; CheckBalance ch1; cout << setiosflags (ios::fixed | ios::showpoint) << setprecision (2); ch1.instruct_user(); ch1.initiate (); ch1.process_transactions(); _getch(); return 0; }
// basic dividend rate 4.5% // bonus dividend rate 0.5% // input: premium amount // input: number of claims // output: dividend amount
int main () { CompDiv d1; float prmum; int numbrClms; float dvdnd; // Display user instructions. d1.instruct_user(); // Enter premium and number of claims. cout << "Premium amount: $"; cin >> prmum; cout << "Number of claims: "; cin >> numbrClms; d1.set(prmum,numbrClms); // Compute the dividend. dvdnd = d1.compute_dividend (); // Print total dividend. cout << "Total dividend is $" << setprecision (2) << setiosflags(ios::showpoint | ios:: fixed) << dvdnd << endl; if (d1.get_numberClaims() == 0) cout << "This includes a bonus dividend for zero claims!" << endl; getch(); return 0; }
// Displays user instructions. void CompDiv::instruct_user() { cout << "This program displays an insurance policy dividend." << endl; cout << "The basic dividend is " << basic_rate; cout << " times the premium." << endl; cout << "A bonus dividend of " << bonus_rate; cout << " times the premium is paid" << endl; cout << "for policies with no claims against them." << endl << endl; return; } // end instruct_user
// COMPUTE DIVIDEND USING BONUS RATE WHEN EARNED. float CompDiv::compute_dividend() { dividend = premium * basic_rate; // basic dividend if (number_claims == 0) dividend = dividend + premium * bonus_rate; // plus bonus return dividend; } // end compute_dividend void CompDiv::set(float p, int nc) { premium = p; number_claims = nc; } int CompDiv::get_numberClaims() { return number_claims; }
class Payroll { private: float float float float public: void set(float,float); //set values for hours and rate void instructUser(); float computeGross(); float computeNet(); }; int main () { float hrs,rt,grss,nt; Payroll p1; // Display user instructions. p1.instructUser(); // Enter hours and rate. cout << "Hours worked: "; cin >> hrs; cout << "Hourly rate: "; cin >> rt; p1.set(hrs,rt); // Compute gross salary. grss = p1.computeGross(); // Compute net salary. nt = p1.computeNet(); // Print gross and net. cout << "Gross salary is " << grss << endl; cout << "Net salary is " << nt << endl; _getch(); return 0; } // Insert lower-level functions here. hours; rate; gross; net; // // // // input: hours worked input: hourly pay rate (dollars) output: gross pay (dollars) output: net pay (dollars)
// ... // Displays user instructions void Payroll::instructUser() { cout << "This program computes gross and net salary." << endl; cout << "A dues amount of " << dues << " is deducted for" << endl; cout << "an employee who earns more than " << MAX_NO_DUES << endl << endl; cout << "Overtime is paid at the rate of " << OVERTIME_RATE << endl; cout << "times the regular rate for hours worked over " << MAX_NO_OVERTIME << endl << endl; cout << "Enter hours worked and hourly rate" << endl; cout << "on separate lines after the prompts." << endl; cout << "Press <return> after typing each number." << endl << endl; } // end instructUser void Payroll::set(float h, float r) { hours = h; rate = r; } // FIND THE GROSS PAY float Payroll::computeGross() { float regularPay; // pay for first 40 hours float overtimePay; // pay for hours in excess of 40 // Compute gross pay. if (hours > MAX_NO_OVERTIME) { regularPay = MAX_NO_OVERTIME * rate; overtimePay = (hours - MAX_NO_OVERTIME) * OVERTIME_RATE * rate; gross = regularPay + overtimePay; } else gross = hours * rate; return gross; // end computeGross
// Find the net pay float Payroll::computeNet() { if (gross > MAX_NO_DUES) net = gross - dues; // deduct dues amount else net = gross; // no deductions return net; // end computeNet
// Compute area of circle. // Compute circumference of circle. // Display area and circumference.
} void Circle::set_radius(float r) { radius = r; } float Circle::get_radius() { return radius; } void Circle::Compute_Area() { area = pi * radius * radius; } void Circle::Compute_circumference() { circum = 2 * pi * radius; } void Circle::Display() { cout << "The area of the circle is " << area << endl; cout << "The circumference of the circle is " << circum ; }
// Read in the count of nickels and pennies. // Find total cents and value of dollars and change.
void Coins::Read_Pennies_Nickles_From_User() { cout << "Enter the number of nickels and press return: "; cin >> nickels; cout << "Enter the number of pennies and press return: "; cin >> pennies; } void Coins::Comppute_Cents_Dollars_Change() { total_cents = 5 * nickels + pennies; dollars = total_cents / 100; change = total_cents % 100; } void Coins::Display() { cout << "Your collection is worth " << dollars << " dollars and " << change << " cents." << endl; }
// output: alphabetically first letter // return alphabetically first letter among two letters
get_first(); set(char,char,char);
int main () { char c1,c2,c3; FirstLetter fl; // Read three letters. cout << "Enter any three letters: "; cin >> c1 >> c2 >> c3; fl.set(c1,c2,c3); // Save the alphabetically first of ch1,ch2 and ch3 in alpha_first.
// Display result. cout << endl << fl.get_first() << " is the first letter alphabetically." << endl; _getch(); return 0; }
// FINDS THE ALPHABETICALLY FIRST LETTER OF PAIR char FirstLetter::get_first() { if (ch1 < ch2 && ch1 < ch3) return ch1; // letter1 comes before letter2 and letter3 else if (ch2 < ch3 && ch2 < ch1) return ch2; // letter2 comes before letter3 and letter1 else if (ch3 < ch1 && ch3 < ch2) return ch3; // letter3 comes before letter1 and letter2 }
void FirstLetter::set(char c1, char c2, char c3) { ch1 = c1; ch2 = c2; ch3 = c3; }
End Class
= = = =
Public Function ComputeGross() As Double If hours <= max_hours Then Return hours * rate Else Return (max_hours * rate) + (hours - max_hours) * overtime_rate * rate End If End Function Public Function ComputeNet() As Double If (gross > maxNoDues) Then Return gross - dues Else Return gross End If End Function
End Class
End Class
tb.BorderStyle = BorderStyle.FixedSingle frmCheckBalanceVB1.tbName = "txtbxTrnsactnDscrptn" & CStr(frmCheckBalanceVB1.i) Dim tb2 As New TextBox tb2.Name = frmCheckBalanceVB1.tbName frmCheckBalanceVB1.Controls.Add(tb2) tb2.Location = New Point(179, frmCheckBalanceVB1.y) tb2.Size = New Size(350, 20) tb2.BorderStyle = BorderStyle.FixedSingle frmCheckBalanceVB1.tbName = "txtbxCtgry" & CStr(frmCheckBalanceVB1.i) Dim tb3 As New TextBox tb3.Name = frmCheckBalanceVB1.tbName frmCheckBalanceVB1.Controls.Add(tb3) tb3.Location = New Point(528, frmCheckBalanceVB1.y) tb3.Size = New Size(143, 20) tb3.BorderStyle = BorderStyle.FixedSingle frmCheckBalanceVB1.tbName = "txtbxDbt" & CStr(frmCheckBalanceVB1.i) Dim tb4 As New TextBox tb4.Name = frmCheckBalanceVB1.tbName frmCheckBalanceVB1.Controls.Add(tb4) tb4.Location = New Point(670, frmCheckBalanceVB1.y) tb4.Size = New Size(100, 20) tb4.BorderStyle = BorderStyle.FixedSingle frmCheckBalanceVB1.tbName = "txtbxCrdt" & CStr(frmCheckBalanceVB1.i) Dim tb5 As New TextBox tb5.Name = frmCheckBalanceVB1.tbName tb5.Location = New Point(769, frmCheckBalanceVB1.y) tb5.Size = New Size(100, 20) tb5.BorderStyle = BorderStyle.FixedSingle tb5.Text = txtbxDeposit.Text tb5.TextAlign = ToolBarTextAlign.Right frmCheckBalanceVB1.Controls.Add(tb5) frmCheckBalanceVB1.tbName = "txtbxBlnc" & CStr(frmCheckBalanceVB1.i) Dim tb6 As New TextBox tb6.Name = frmCheckBalanceVB1.tbName tb6.Location = New Point(868, frmCheckBalanceVB1.y) tb6.Size = New Size(100, 20) tb6.BorderStyle = BorderStyle.FixedSingle tb6.TextAlign = ToolBarTextAlign.Right frmCheckBalanceVB1.Current_Balance += tb5.Text tb6.Text = "$" & frmCheckBalanceVB1.Current_Balance frmCheckBalanceVB1.Controls.Add(tb6) End If Me.Close() frmCheckBalanceVB1.txtbxEndngBlnc.Text = "$" & frmCheckBalanceVB1.Current_Balance End Sub Private Sub frmAddRecord_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub
End Class
Dim cb As clsCheckBalanceVB1 Private Sub checkBalanceVB1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Enabled = False frmOpeningBalance.Show() End Sub Private Sub txtbxDbt0_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtbxDbt.TextChanged txtbxCrdt.Enabled = False 'd txtbxBlnc.Text = "$" & CType(txtbxBlnc.Text, Integer) - CType(txtbxDbt.Text, Integer) If Not (txtbxCrdt.Text = "") And Not (txtbxDbt.Text = 0) Then 'd MsgBox("You can either debit or credit") txtbxDbt.Text = 0 End If If txtbxDbt.Text = 0 Then txtbxCrdt.Enabled = True 'd txtbxCrdt.Focus() 'd End If End Sub Private Sub txtbxCrdt0_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtbxCrdt.TextChanged txtbxBlnc.Text += txtbxCrdt.Text End Sub
Public Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click frmAddRecord.Show() End Sub
Private Sub btnDltRcrd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDltRcrd.Click Dim c As Control For Each c In Me.Controls If c.Name = "dtp" & CStr(i) Then Me.Controls.Remove(c) End If Next For Each c In Me.Controls If c.Name = "txtbxChck" & CStr(i) Then Me.Controls.Remove(c) End If Next For Each c In Me.Controls If c.Name = "txtbxTrnsactnDscrptn" & CStr(i) Then Me.Controls.Remove(c) End If Next For Each c In Me.Controls If c.Name = "txtbxCtgry" & CStr(i) Then Me.Controls.Remove(c)
End If Next For Each c In Me.Controls If c.Name = "txtbxDbt" & CStr(i) Then Me.Controls.Remove(c) End If Next For Each c In Me.Controls If c.Name = "txtbxCrdt" & CStr(i) Then Me.Controls.Remove(c) End If Next For Each c In Me.Controls If c.Name = "txtbxBlnc" & CStr(i) Then Me.Controls.Remove(c) End If Next If i >= 0 Then i -= 1 End If If y > 28 Then y -= 19 End If End Sub
End Class
Public Property SetAndGet_start_balance() As Double Get Return start_balance End Get Set(ByVal value As Double) start_balance = value End Set End Property
End Class
Private Sub frmCoinCollection_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub txtbxNickels_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtbxNickels.TextChanged
If (txtbxNickels.Text.ToString = "") Then nickles = 0 End If If (txtbxNickels.Text.ToString <> "") Then nickles = CType(txtbxNickels.Text.ToString, Integer) End If If (txtbxPennies.Text.ToString <> "") Then pennies = CType(txtbxPennies.Text.ToString, Integer) End If c1.SetValues(pennies, nickles) If (c1.CalTotalCents() >= 100) Then dollar = c1.CalTotalCents() \ 100 txtbxDollars.Text = CType(dollar, Integer) End If txtbxCents.Text = c1.CalTotalCents Mod 100
End Sub Private Sub txtbxPennies_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtbxPennies.TextChanged If (txtbxPennies.Text.ToString = "") Then pennies = 0 End If If (txtbxPennies.Text.ToString <> "") Then pennies = CType(txtbxPennies.Text.ToString, Integer) End If If (txtbxNickels.Text.ToString <> "") Then nickles = CType(txtbxNickels.Text.ToString, Integer) End If c1.SetValues(pennies, nickles) If (c1.CalTotalCents() >= 100) Then dollar = c1.CalTotalCents() \ 100 txtbxDollars.Text = CType(dollar, Integer) End If txtbxCents.Text = c1.CalTotalCents Mod 100 End Sub
End Class
End Class