97%(230)222 out of 230 people found this document helpful
This preview shows page 24 - 27 out of 31 pages.
// must use month - 1 to access correct dayelseif da < 1 thenday = 1elseday = daendifyear = yrreturnpublic void showDate()output "Date: ", month, “/”, day, “/”, yearreturnendClassstartDeclarationsDate birthday, anniversary, graduation, partybirthday.setDate(6, 24, 1982)// must use public setDate() method// cannot use private class members hereanniversary.setDate(10, 15, 2009)graduation.setDate(14, 19, 2016)party.setDate(7, 35, 2015)output "Birthday "birthday.showDate()output "Anniversary "anniversary.showDate()output "Graduation "graduation.showDate()output "Party "party.showDate()stopDebug10-02// The Invoice class contains invoice number, customer name,// balance due, and tax owed fields,// and methods to set and display the values.// The invoice number must be between 1000 and 9999 inclusive// or else it is set to 0// Tax is 7 % of the balance due// The demonstration program instantiates three Invoices and// purposely assigns invalid values to some of the arguments;
Programming Logic and Design, 8eSolutions 10-25// the class methods will correct the invalid values.class InvoiceDeclarationsprivate num invoiceNumberprivate string customerprivate num balanceDueprivate num taxpublic void setInvoiceNumber(num number)// setInvoiceNumber() method should be publicDeclarationsnum LOW_NUM = 1000num HIGH_NUM = 9999if number > HIGH_NUM theninvoiceNumber = 0elseif number < LOW_NUM theninvoiceNumber = 0elseinvoiceNumber = numberendifendifreturnpublic void setCustomer(string cust)// setCustomer() should be publiccustomer = custreturnpublic void setBalanceDue(num balance)balanceDue = balancesetTax()returnprivate void setTax()// setTax() does not need any parametersDeclarationsnum TAX_RATE = 0.07tax = balanceDue * TAX_RATEreturn public void displayInvoice()output "Invoice #", invoiceNumberoutput "Customer: ", customeroutput "Due: ", balanceDueoutput "Tax: ", taxoutput "Total ", balance + taxreturnendClassstartDeclarationsInvoice inv1Invoice inv2Invoice inv3inv1.setInvoiceNumber(1244)// setInvoiceNUmber() was spelled incorrectlyinv1.setCustomer("Brown")inv1.setBalanceDue(1000.00)inv1.displayInvoice()inv2.setInvoiceNumber(77777)inv2.setCustomer("Jenkins")
Programming Logic and Design, 8eSolutions 10-26inv2.setBalanceDue(2000.00)inv2.displayInvoice()inv3.setInvoiceNumber(888)inv3.setCustomer("Russell")inv3.setBalanceDue(3000.00)inv3.displayInvoice()stopDebug10-03// The Mortgage class contains fields for mortgage// number, customer name, price of house, down// payment, mortgage amount, and mortgage rate,// and methods to set and get the values.// The mortgage number must be betwen 10000 and 59999 inclusive// or else it is set to 0// The down payemnt cannot be more than the price of the house// If it is, an error message is displayed// and the down payment is reduced// The mortage amount is the difference between the house price// and the down payment// The mortgage rate differs for loans of different sizes// Loans over $200,000 get a preferred rate// The demonstration program instantiates three Mortgages and// purposely assigns invalid values to some of the arguments;// the class methods will correct the invalid values.