c#作业doc1.docx
- 文档编号:5426576
- 上传时间:2022-12-16
- 格式:DOCX
- 页数:6
- 大小:15.57KB
c#作业doc1.docx
《c#作业doc1.docx》由会员分享,可在线阅读,更多相关《c#作业doc1.docx(6页珍藏版)》请在冰豆网上搜索。
c#作业doc1
第一题
usingSystem;
publicclassGradeBook
{
//auto-implementedpropertyCourseNameimplicitlycreatesan
//instancevariableforthisGradeBook'scoursename
publicstringCourseName{get;set;}
publicstringTeacherName{get;set;}
//constructorinitializesauto-implementedproperty
//CourseNamewithstringsuppliedasargument
publicGradeBook(stringcname,stringtname)
{
CourseName=cname;
TeacherName=tname;//setCourseNametoname
}//endconstructor
//displayawelcomemessagetotheGradeBookuser
publicvoidDisplayMessage()
{
//useauto-implementedpropertyCourseNametogetthe
//nameofthecoursethatthisGradeBookrepresents
Console.WriteLine("Welcometothegradebookfor\n{0}!
",
CourseName);
Console.WriteLine("Thiscourseispresentedby:
\n{0}",TeacherName);
}//endmethodDisplayMessage
}//endclassGradeBook
/**************************************************************************
*(C)Copyright1992-2011byDeitel&Associates,Inc.and*
*PearsonEducation,Inc.AllRightsReserved.*
**
*DISCLAIMER:
Theauthorsandpublisherofthisbookhaveusedtheir*
*besteffortsinpreparingthebook.Theseeffortsincludethe*
*development,research,andtestingofthetheoriesandprograms*
*todeterminetheireffectiveness.Theauthorsandpublishermake*
*nowarrantyofanykind,expressedorimplied,withregardtothese*
*programsortothedocumentationcontainedinthesebooks.Theauthors*
*andpublishershallnotbeliableinanyeventforincidentalor*
*consequentialdamagesinconnectionwith,orarisingoutof,the*
*furnishing,performance,oruseoftheseprograms.*
*************************************************************************/
usingSystem;
publicclassGradeBookTest
{
//Mainmethodbeginsprogramexecution
publicstaticvoidMain(string[]args)
{
//createGradeBookobject
GradeBookgradeBook1=newGradeBook(//invokesconstructor
"CS101IntroductiontoC#Programming","蔡广基");
GradeBookgradeBook2=newGradeBook(//invokesconstructor
"CS102DataStructuresinC#","邹海涛");
//displayinitialvalueofcourseNameforeachGradeBook
Console.WriteLine("gradeBook1coursenameis:
{0}",
gradeBook1.CourseName);
Console.WriteLine("gradeBook2coursenameis:
{0}",
gradeBook2.CourseName);
gradeBook1.DisplayMessage();
gradeBook2.DisplayMessage();
}//endMain
}//endclassGradeBookTest
/**************************************************************************
*(C)Copyright1992-2011byDeitel&Associates,Inc.and*
*PearsonEducation,Inc.AllRightsReserved.*
**
*DISCLAIMER:
Theauthorsandpublisherofthisbookhaveusedtheir*
*besteffortsinpreparingthebook.Theseeffortsincludethe*
*development,research,andtestingofthetheoriesandprograms*
*todeterminetheireffectiveness.Theauthorsandpublishermake*
*nowarrantyofanykind,expressedorimplied,withregardtothese*
*programsortothedocumentationcontainedinthesebooks.Theauthors*
*andpublishershallnotbeliableinanyeventforincidentalor*
*consequentialdamagesinconnectionwith,orarisingoutof,the*
*furnishing,performance,oruseoftheseprograms.*
*************************************************************************/
第二题
usingSystem;
publicclassAccount{
privatedecimalbalance;//instancevariablethatstoresthebalance
//constructor
publicAccount(decimalinitialBalance)
{
Balance=initialBalance;//setbalanceusingproperty
}//endAccountconstructor
//credit(add)anamounttotheaccount
publicvoidDebit(decimalamount)
{
Balance=Balance-amount;//addamounttobalance
}//endmethodCredit
//apropertytogetandsettheaccountbalance
publicdecimalBalance
{
get
{
returnbalance;
}//endget
set
{
//validatethatvalueisgreaterthanorequalto0;
//ifitisnot,balanceisleftunchanged
if(value>=0)
balance=value;
else
Console.WriteLine("Debitamountexceededaccountbalance");
}//endset
}//endpropertyBalance
}//endclassAccount
/**************************************************************************
*(C)Copyright1992-2011byDeitel&Associates,Inc.and*
*PearsonEducation,Inc.AllRightsReserved.*
**
*DISCLAIMER:
Theauthorsandpublisherofthisbookhaveusedtheir*
*besteffortsinpreparingthebook.Theseeffortsincludethe*
*development,research,andtestingofthetheoriesandprograms*
*todeterminetheireffectiveness.Theauthorsandpublishermake*
*nowarrantyofanykind,expressedorimplied,withregardtothese*
*programsortothedocumentationcontainedinthesebooks.Theauthors*
*andpublishershallnotbeliableinanyeventforincidentalor*
*consequentialdamagesinconnectionwith,orarisingoutof,the*
*furnishing,performance,oruseoftheseprograms.*
*************************************************************************/
usingSystem;
publicclassDebitTest
{
//MainmethodbeginsexecutionofC#application
publicstaticvoidMain(string[]args)
{
Accountaccount1=newAccount(50.00M);//createAccountobject
//displayinitialbalanceofeachusingaproperty
Console.WriteLine("account1balance:
{0:
C}",
account1.Balance);//displayBalanceproperty
decimalwithdrawAmount;//depositamountreadfromuser
//promptandobtainuserinput
Console.Write("withdrawingamountfromaccount1:
");
withdrawAmount=Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("withdrawing{0:
C}fromaccount1balance\n",
withdrawAmount);
account1.Debit(withdrawAmount);//addtoaccount1balance
//displaybalances
//promptandobtainuserinput
//displaybalances
Console.WriteLine("account1balance:
{0:
C}",account1.Balance);
}//endMain
}//endclassAccountTest
/**************************************************************************
*(C)Copyright1992-2011byDeitel&Associates,Inc.and*
*PearsonEducation,Inc.AllRightsReserved.*
**
*DISCLAIMER:
Theauthorsandpublisherofthisbookhaveusedtheir*
*besteffortsinpreparingthebook.Theseeffortsincludethe*
*development,research,andtestingofthetheoriesandprograms*
*todeterminetheireffectiveness.Theauthorsandpublishermake*
*nowarrantyofanykind,expressedorimplied,withregardtothese*
*programsortothedocumentationcontainedinthesebooks.Theauthors*
*andpublishershallnotbeliableinanyeventforincidentalor*
*consequentialdamagesinconnectionwith,orarisingoutof,the*
*furnishing,performance,oruseoftheseprograms.*
******************************************************************
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c# 作业 doc1