SlideShare ist ein Scribd-Unternehmen logo
1 von 4
Downloaden Sie, um offline zu lesen
You Do lt In this section, you will create a working cxample of inheritance. You will create this
example in four parts: 1. You will create a general Loan class that holds data pertaining to a bank
loan-a loan number, a customer name, and the amount borrowed. 2. After you create the general
Loan class, you will write a program to instantiate and use a Loan object. 3. You will create a
more specific Cartoan derived class that inherits the attributes of the Loan class but adds
information about the automobile that serves as collateral for the loan. 4. You will modify the
Loan demonstration program to add a Carloan object and demonstrate its use. To create the Loan
class: 1. Open a new file in your text editor, then enter the following first few lines for a Loan
class. The class will contain three auto-implemented properties for the loan number the last name
of the customer, and the valuc of the loan. class Loan f public int LoanNumber {oet: set: public
string LastNano {got; sot; } public double LoanAnount {get; set; r 2. At the top of the file,
enter the following code to add a Demot oan class that contains a Main() method. The class
declares a Loan object and shows how to set each field and display the results.
using Systen; public class Demoloan { public static void MainO t Loan aLoan = new LoanO;
aLoan. LoanNumber =2239; aLoan. LastNane = "Mitche11"; a Loan . LoanAmount =1000.00
Console.WriteLine("Loan s{0} for {1} is for {2} ". aloan. LoanNunber, aloan. Las tName,
aloan. Loandmount. Tostring("(2")): f3 3. Save the file as Demoloan.cs, then compile and
execute the program. The output looks like Figure 10-40. There is nothing unusual about this
class or how it operates; it is similar to many you sew in the last chapter before you learned
about inheritanoe. Figure 10-40 Output of the Denotoan program Extending a Class Next, you
will create a class named Carloan. A CarLoan "is a" type of Loan. As such, it has all the
attributes of a Loan, but it also has the year and make of the car that the customer is using as
collateral for the loan. Therefore, Cartoan is a subxiass of Loan.
Extending a Class Next, you will crate a class numed cari inan A carions "is a" mre of toan. As
auch, it has all the attribites of i Loan toat it who has the yar and male of the car that the cuitomer
is using as celateral fse the kan. Therefore, Cartoen is a subclau of toan. Te create the Cartoan
class that eatends the Lean clese: 1. Save the Deroolinanos file as Densokarleanos. Olaner the
Destoan clas name to Deoscartoen Begin the definition in the Cartaan clas after the cosing curly
tract for the toan clas Cartoon exiende Loan and eontains two properties that fild the year and
make of the car. cless Carloan & toen i I public int year foet: aetif 1) public string Make foet;
set:} 2. Within the kainO method of the DemCarioan clas inat afier the declaration of the ioan
object, declare a Carteon as follown. Cartoan aCarioan = new CartoenO: 3. Afier the three
property askienments for the Loas object, insert five a esienment watements for the cartoan
object. acartoen. toantuber = 335a aCartoan-tasthabe - "Jansen": acartoan, toandevent =20000.00
: acarloan. Wake - "Ford"? acarloan. Year =2005 : 4. Fullowing the writet ine 0 statment that
diapups the isan pbject data. insert two kritel ine O stalements that diyplar the Cartoan obpetis
data. Console. Writeline ("ioun ${0} for (1) is for (2). acarloun, Leentluber, efarloun. Lastiase,
aCarloan. Loentwavent. Tostrieg("CZ")): Console. Writeline(" Loen (f0) is for a (1) (2)".
icarloan .Loentwaber, Narleen- Year, artaen Make); 5. Seve the proyerm, thea compile and
curcute it. The outpet boks liae higure 10-41. The Cartoan obyect coerectly wass its own fidde
and properrues ai well as thase ef she perent toen cluas.
You Do It In the previous sections, you created Loan and CarLoan classes and objects. Suppose
the bank adopts new rules as follows: - No regular loan will be made for less than $5000. - No
car loan will be made for any car older than model year 2006. - Although Loans might have
larger loan numbers, CarLoans will have loan numbers that are no more than three digits. If a
larger loan number is provided, the program will use only the last three digits for the loan
number. To implement the new CarLoan rules: 1. Open the DemoCarLoan.cs file and
immediately save it as DemoCarLoan2.cs. Also change the class name from DemoCarloan to
DenoCarLoan2. 2. Within the Loan class, add a new constant that represents the minimum loan
value: public const double MINIMUM LOAN = 5000: 3. Add a field for the loanAmount
because the LoanAmount property will need to use it: protected double loanAmount: The field is
protected so that carLoan objects will be able to access it as well as Loan objects. 4. Replace the
auto-implemented property for LoanAmount in the Loan class with standard get and set
accessors as follows. This change ensures that no loan is made for less than the minimum
allowed value.
5. Within the CarLoan class, add two new constants to hold the earliest year for which car loans
will be given and the lowest. allowed loan number: private const int EARLIEST_YEAR =2006;
private const int LOWEST_INVALID NUM = 1000; 6. Also within the CarLoan class, add a
fidd for the year of the car and replace the existing auto-implemented Year property with one
that contains coded get and set accessors. The Year property set accessor not only sets the year
field, it sets ToanAnount to 0 when a car's year is less than 2006. private int year; public int Yoar
{ set { if (value < EARLIEST_YEAR) I year = value; loanAnount =0 f else year = value; ] get f
return year; 3 b
If loanAmount was private in the parent Loan class, you would not be able to set its value in the
child CarLoan class, as you do here. You could use the public property Loandmount to set the
value, but the parent class sot accessor would force the value to 5000 . 7. Suppose there are
unique rules for issuing loan numbers for cars. Within the Carloan class, just before the closing
curly brace. change the inherited LoanNumber property to accommodate the new rules. If a car
loan number is three digits or fewer, pass it on to the base class property. If not, obtain the last
three digits by calculating the remainder when the loan number is divided by 1000, and pass the
new number to the base class property. Add the following property after the definition of the
Make property. public new int LoanNunber { get { return base. LoanNuaber; } set { if (value <
LOWEST INVALID NUM) base. LoanNumber = value: else 3 base.LoanNumber = value %
LOWEST INVALID NUM; 3 If you did not use the keyword base to access the LoanNumber
property within the CarLoan class, you would be telling this version of the LoanNumber
property to call itself. Although the program would compile, it would run continuously in an
infinite loop until it ran out of memory and issued an error message.
8. Save the file. Compile it and correct any errors. When you execute the program, the output
looks like Figure 10-42. Compare the output to Figure 1041. Notice that the $1000 bank loan has
been forced to $5000. Also notice that the car loan number has been shortened to three digits and
the value of the loan is $0 because of the age of the car. Figure 10-42 Output of the
DemoCarLoan2 program 9. Change the assigned values within the DemoCarLoan2 class to
combinations of early and late years and valid and invalid loan numbers. After each change, save
the program, compile and execute it, and confirm that the program operates as expected.
You Do lt Adding Constructors to Base and Derived Classes When a base class contains only
constructors that require parameters, then any derived classes must provide for the base class
constructor. In the next steps, you will add constructors to the Loan and Carloan classes and
demonstrate that they work as expected. To add constructors to the classes: 1. Open the
DemoCarLoan2 program and change the class name to Democarloan3. Save the file as
DemoCarloan3.cs. 2. In the Loan class, just after the declaration of the loanmmount field, add a
constructor that requires values for all the Loans properties: public Loan(int nun, string nane,
double amount) {oanNumber = num; LastName = nane; LoanAnount = anount; 3. In the
Carloan cass, just after the declaration of the year field, add a constructor that takes five
parameters. It passes three of the parameters to the base class constructor and uses the other two
to assign values to the properties that are unique to the child class. public Carloan(int nua, string
nase, double anount. { int year. string make) : base(nue, nose. anount) Year = year: Make -
make; f 4. In the Main O method of the DesocarLoan3 class, remove the existing declarations for
a oan and a Carloan and replace them with two declarations that use the arguments passed to the
constructors. Loen aloan - new Loan (333, "Hanson", 7000,00); Carloan aCarloan = new Carloan
(444, "Carlisle", 30000.00,2011, "Bexr");
5. Remove the eight statements that assigned values to Loan and Carloan, but retain the Console.
Writeline() statements that display the values. 6. Save the program, then compile and execute it.
The output looks like Figure 10-43. Both constructors work as expected. The CarLoan
constructor has called its parent's constructor to set the necessary fields before executing its own
unique statements. Figure 10-43 Output of the DemoCarLoan3 program

Weitere ähnliche Inhalte

Ähnlich wie You Do lt In this section, you wi.pdf

Please be advised that there are four (4) programs just like this on.docx
Please be advised that there are four (4) programs just like this on.docxPlease be advised that there are four (4) programs just like this on.docx
Please be advised that there are four (4) programs just like this on.docxlorindajamieson
 
Copying number ranges in SAP FICO ECC
Copying number ranges  in SAP FICO ECCCopying number ranges  in SAP FICO ECC
Copying number ranges in SAP FICO ECCSrinivas Rao
 
CSC139 Chapter 9 Lab Assignments (1) Classes and Obj.docx
CSC139 Chapter 9 Lab Assignments (1) Classes and Obj.docxCSC139 Chapter 9 Lab Assignments (1) Classes and Obj.docx
CSC139 Chapter 9 Lab Assignments (1) Classes and Obj.docxruthannemcmullen
 
Learn Concept of Class and Object in C# Part 3
Learn Concept of Class and Object in C#  Part 3Learn Concept of Class and Object in C#  Part 3
Learn Concept of Class and Object in C# Part 3C# Learning Classes
 
Object Oriented Programming (Advanced )
Object Oriented Programming   (Advanced )Object Oriented Programming   (Advanced )
Object Oriented Programming (Advanced )ayesha420248
 
C++ beginner's guide ch08
C++ beginner's guide ch08C++ beginner's guide ch08
C++ beginner's guide ch08Jotham Gadot
 
Java căn bản- Chapter1
Java  căn bản- Chapter1Java  căn bản- Chapter1
Java căn bản- Chapter1Vince Vo
 
Chapter1 - Introduction to Object-Oriented Programming and Software Development
Chapter1 - Introduction to Object-Oriented Programming and Software DevelopmentChapter1 - Introduction to Object-Oriented Programming and Software Development
Chapter1 - Introduction to Object-Oriented Programming and Software DevelopmentEduardo Bergavera
 
COIT20245, 2016 Term One - Page 1 of 9 Assessment detail.docx
COIT20245, 2016 Term One - Page 1 of 9 Assessment detail.docxCOIT20245, 2016 Term One - Page 1 of 9 Assessment detail.docx
COIT20245, 2016 Term One - Page 1 of 9 Assessment detail.docxclarebernice
 
Workbooks_Testings_Designs_&_Process.pptx
Workbooks_Testings_Designs_&_Process.pptxWorkbooks_Testings_Designs_&_Process.pptx
Workbooks_Testings_Designs_&_Process.pptxRGibesh
 

Ähnlich wie You Do lt In this section, you wi.pdf (13)

Please be advised that there are four (4) programs just like this on.docx
Please be advised that there are four (4) programs just like this on.docxPlease be advised that there are four (4) programs just like this on.docx
Please be advised that there are four (4) programs just like this on.docx
 
OOP Assignment 03.pdf
OOP Assignment 03.pdfOOP Assignment 03.pdf
OOP Assignment 03.pdf
 
L9
L9L9
L9
 
Copying number ranges in SAP FICO ECC
Copying number ranges  in SAP FICO ECCCopying number ranges  in SAP FICO ECC
Copying number ranges in SAP FICO ECC
 
CSC139 Chapter 9 Lab Assignments (1) Classes and Obj.docx
CSC139 Chapter 9 Lab Assignments (1) Classes and Obj.docxCSC139 Chapter 9 Lab Assignments (1) Classes and Obj.docx
CSC139 Chapter 9 Lab Assignments (1) Classes and Obj.docx
 
Learn Concept of Class and Object in C# Part 3
Learn Concept of Class and Object in C#  Part 3Learn Concept of Class and Object in C#  Part 3
Learn Concept of Class and Object in C# Part 3
 
Object Oriented Programming (Advanced )
Object Oriented Programming   (Advanced )Object Oriented Programming   (Advanced )
Object Oriented Programming (Advanced )
 
exa_cer_g23
exa_cer_g23exa_cer_g23
exa_cer_g23
 
C++ beginner's guide ch08
C++ beginner's guide ch08C++ beginner's guide ch08
C++ beginner's guide ch08
 
Java căn bản- Chapter1
Java  căn bản- Chapter1Java  căn bản- Chapter1
Java căn bản- Chapter1
 
Chapter1 - Introduction to Object-Oriented Programming and Software Development
Chapter1 - Introduction to Object-Oriented Programming and Software DevelopmentChapter1 - Introduction to Object-Oriented Programming and Software Development
Chapter1 - Introduction to Object-Oriented Programming and Software Development
 
COIT20245, 2016 Term One - Page 1 of 9 Assessment detail.docx
COIT20245, 2016 Term One - Page 1 of 9 Assessment detail.docxCOIT20245, 2016 Term One - Page 1 of 9 Assessment detail.docx
COIT20245, 2016 Term One - Page 1 of 9 Assessment detail.docx
 
Workbooks_Testings_Designs_&_Process.pptx
Workbooks_Testings_Designs_&_Process.pptxWorkbooks_Testings_Designs_&_Process.pptx
Workbooks_Testings_Designs_&_Process.pptx
 

Mehr von santosha5446

Which of the following is TRUE about diversification Through diversi.pdf
 Which of the following is TRUE about diversification Through diversi.pdf Which of the following is TRUE about diversification Through diversi.pdf
Which of the following is TRUE about diversification Through diversi.pdfsantosha5446
 
Which of the following is true about corporations. Select all correct.pdf
 Which of the following is true about corporations. Select all correct.pdf Which of the following is true about corporations. Select all correct.pdf
Which of the following is true about corporations. Select all correct.pdfsantosha5446
 
Which one of these statements is NOT true a. Clinical data are colle.pdf
 Which one of these statements is NOT true a. Clinical data are colle.pdf Which one of these statements is NOT true a. Clinical data are colle.pdf
Which one of these statements is NOT true a. Clinical data are colle.pdfsantosha5446
 
Which of the following statements is consistent with the assertion th.pdf
 Which of the following statements is consistent with the assertion th.pdf Which of the following statements is consistent with the assertion th.pdf
Which of the following statements is consistent with the assertion th.pdfsantosha5446
 
Which of the following statements about cloud computing is incorrect.pdf
 Which of the following statements about cloud computing is incorrect.pdf Which of the following statements about cloud computing is incorrect.pdf
Which of the following statements about cloud computing is incorrect.pdfsantosha5446
 
Which one of the following statements is incorrect A. There are tw.pdf
 Which one of the following statements is incorrect A. There are tw.pdf Which one of the following statements is incorrect A. There are tw.pdf
Which one of the following statements is incorrect A. There are tw.pdfsantosha5446
 
Which of the following kinds of speech is given the most protection b.pdf
 Which of the following kinds of speech is given the most protection b.pdf Which of the following kinds of speech is given the most protection b.pdf
Which of the following kinds of speech is given the most protection b.pdfsantosha5446
 
Wildhorse Corporation provides the following information about its de.pdf
 Wildhorse Corporation provides the following information about its de.pdf Wildhorse Corporation provides the following information about its de.pdf
Wildhorse Corporation provides the following information about its de.pdfsantosha5446
 
Which of the following circumstances may lead to concern when analyzi.pdf
 Which of the following circumstances may lead to concern when analyzi.pdf Which of the following circumstances may lead to concern when analyzi.pdf
Which of the following circumstances may lead to concern when analyzi.pdfsantosha5446
 
Which of the following is a motvation for visiting Culture Heritage V.pdf
 Which of the following is a motvation for visiting Culture Heritage V.pdf Which of the following is a motvation for visiting Culture Heritage V.pdf
Which of the following is a motvation for visiting Culture Heritage V.pdfsantosha5446
 
Your Block class should exhibit the following features. Public child .pdf
 Your Block class should exhibit the following features. Public child .pdf Your Block class should exhibit the following features. Public child .pdf
Your Block class should exhibit the following features. Public child .pdfsantosha5446
 
Your are assigned to dentify the self-actualization need in UDEMY w.pdf
 Your are assigned to dentify the self-actualization need in UDEMY w.pdf Your are assigned to dentify the self-actualization need in UDEMY w.pdf
Your are assigned to dentify the self-actualization need in UDEMY w.pdfsantosha5446
 
You are to address the questions in each of the following problems. 1.pdf
 You are to address the questions in each of the following problems. 1.pdf You are to address the questions in each of the following problems. 1.pdf
You are to address the questions in each of the following problems. 1.pdfsantosha5446
 
Which of the following is FALSE about RBC contain a dimer of spectri.pdf
 Which of the following is FALSE about RBC contain a dimer of spectri.pdf Which of the following is FALSE about RBC contain a dimer of spectri.pdf
Which of the following is FALSE about RBC contain a dimer of spectri.pdfsantosha5446
 
Yoo hove learned obout the various straceyies thint companies underta.pdf
 Yoo hove learned obout the various straceyies thint companies underta.pdf Yoo hove learned obout the various straceyies thint companies underta.pdf
Yoo hove learned obout the various straceyies thint companies underta.pdfsantosha5446
 
Xavier and his wife Maria have total W-2 income of $96,002. They will.pdf
 Xavier and his wife Maria have total W-2 income of $96,002. They will.pdf Xavier and his wife Maria have total W-2 income of $96,002. They will.pdf
Xavier and his wife Maria have total W-2 income of $96,002. They will.pdfsantosha5446
 
Which of the following are potential medical consequences that can re.pdf
 Which of the following are potential medical consequences that can re.pdf Which of the following are potential medical consequences that can re.pdf
Which of the following are potential medical consequences that can re.pdfsantosha5446
 
Write a short response explaining in detail steps to compute for endi.pdf
 Write a short response explaining in detail steps to compute for endi.pdf Write a short response explaining in detail steps to compute for endi.pdf
Write a short response explaining in detail steps to compute for endi.pdfsantosha5446
 
Write a Matlab function f which takes a quadratic matrix A and a scal.pdf
 Write a Matlab function f which takes a quadratic matrix A and a scal.pdf Write a Matlab function f which takes a quadratic matrix A and a scal.pdf
Write a Matlab function f which takes a quadratic matrix A and a scal.pdfsantosha5446
 
Write a program that counts the number of characters and lines in the.pdf
 Write a program that counts the number of characters and lines in the.pdf Write a program that counts the number of characters and lines in the.pdf
Write a program that counts the number of characters and lines in the.pdfsantosha5446
 

Mehr von santosha5446 (20)

Which of the following is TRUE about diversification Through diversi.pdf
 Which of the following is TRUE about diversification Through diversi.pdf Which of the following is TRUE about diversification Through diversi.pdf
Which of the following is TRUE about diversification Through diversi.pdf
 
Which of the following is true about corporations. Select all correct.pdf
 Which of the following is true about corporations. Select all correct.pdf Which of the following is true about corporations. Select all correct.pdf
Which of the following is true about corporations. Select all correct.pdf
 
Which one of these statements is NOT true a. Clinical data are colle.pdf
 Which one of these statements is NOT true a. Clinical data are colle.pdf Which one of these statements is NOT true a. Clinical data are colle.pdf
Which one of these statements is NOT true a. Clinical data are colle.pdf
 
Which of the following statements is consistent with the assertion th.pdf
 Which of the following statements is consistent with the assertion th.pdf Which of the following statements is consistent with the assertion th.pdf
Which of the following statements is consistent with the assertion th.pdf
 
Which of the following statements about cloud computing is incorrect.pdf
 Which of the following statements about cloud computing is incorrect.pdf Which of the following statements about cloud computing is incorrect.pdf
Which of the following statements about cloud computing is incorrect.pdf
 
Which one of the following statements is incorrect A. There are tw.pdf
 Which one of the following statements is incorrect A. There are tw.pdf Which one of the following statements is incorrect A. There are tw.pdf
Which one of the following statements is incorrect A. There are tw.pdf
 
Which of the following kinds of speech is given the most protection b.pdf
 Which of the following kinds of speech is given the most protection b.pdf Which of the following kinds of speech is given the most protection b.pdf
Which of the following kinds of speech is given the most protection b.pdf
 
Wildhorse Corporation provides the following information about its de.pdf
 Wildhorse Corporation provides the following information about its de.pdf Wildhorse Corporation provides the following information about its de.pdf
Wildhorse Corporation provides the following information about its de.pdf
 
Which of the following circumstances may lead to concern when analyzi.pdf
 Which of the following circumstances may lead to concern when analyzi.pdf Which of the following circumstances may lead to concern when analyzi.pdf
Which of the following circumstances may lead to concern when analyzi.pdf
 
Which of the following is a motvation for visiting Culture Heritage V.pdf
 Which of the following is a motvation for visiting Culture Heritage V.pdf Which of the following is a motvation for visiting Culture Heritage V.pdf
Which of the following is a motvation for visiting Culture Heritage V.pdf
 
Your Block class should exhibit the following features. Public child .pdf
 Your Block class should exhibit the following features. Public child .pdf Your Block class should exhibit the following features. Public child .pdf
Your Block class should exhibit the following features. Public child .pdf
 
Your are assigned to dentify the self-actualization need in UDEMY w.pdf
 Your are assigned to dentify the self-actualization need in UDEMY w.pdf Your are assigned to dentify the self-actualization need in UDEMY w.pdf
Your are assigned to dentify the self-actualization need in UDEMY w.pdf
 
You are to address the questions in each of the following problems. 1.pdf
 You are to address the questions in each of the following problems. 1.pdf You are to address the questions in each of the following problems. 1.pdf
You are to address the questions in each of the following problems. 1.pdf
 
Which of the following is FALSE about RBC contain a dimer of spectri.pdf
 Which of the following is FALSE about RBC contain a dimer of spectri.pdf Which of the following is FALSE about RBC contain a dimer of spectri.pdf
Which of the following is FALSE about RBC contain a dimer of spectri.pdf
 
Yoo hove learned obout the various straceyies thint companies underta.pdf
 Yoo hove learned obout the various straceyies thint companies underta.pdf Yoo hove learned obout the various straceyies thint companies underta.pdf
Yoo hove learned obout the various straceyies thint companies underta.pdf
 
Xavier and his wife Maria have total W-2 income of $96,002. They will.pdf
 Xavier and his wife Maria have total W-2 income of $96,002. They will.pdf Xavier and his wife Maria have total W-2 income of $96,002. They will.pdf
Xavier and his wife Maria have total W-2 income of $96,002. They will.pdf
 
Which of the following are potential medical consequences that can re.pdf
 Which of the following are potential medical consequences that can re.pdf Which of the following are potential medical consequences that can re.pdf
Which of the following are potential medical consequences that can re.pdf
 
Write a short response explaining in detail steps to compute for endi.pdf
 Write a short response explaining in detail steps to compute for endi.pdf Write a short response explaining in detail steps to compute for endi.pdf
Write a short response explaining in detail steps to compute for endi.pdf
 
Write a Matlab function f which takes a quadratic matrix A and a scal.pdf
 Write a Matlab function f which takes a quadratic matrix A and a scal.pdf Write a Matlab function f which takes a quadratic matrix A and a scal.pdf
Write a Matlab function f which takes a quadratic matrix A and a scal.pdf
 
Write a program that counts the number of characters and lines in the.pdf
 Write a program that counts the number of characters and lines in the.pdf Write a program that counts the number of characters and lines in the.pdf
Write a program that counts the number of characters and lines in the.pdf
 

Kürzlich hochgeladen

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 

Kürzlich hochgeladen (20)

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 

You Do lt In this section, you wi.pdf

  • 1. You Do lt In this section, you will create a working cxample of inheritance. You will create this example in four parts: 1. You will create a general Loan class that holds data pertaining to a bank loan-a loan number, a customer name, and the amount borrowed. 2. After you create the general Loan class, you will write a program to instantiate and use a Loan object. 3. You will create a more specific Cartoan derived class that inherits the attributes of the Loan class but adds information about the automobile that serves as collateral for the loan. 4. You will modify the Loan demonstration program to add a Carloan object and demonstrate its use. To create the Loan class: 1. Open a new file in your text editor, then enter the following first few lines for a Loan class. The class will contain three auto-implemented properties for the loan number the last name of the customer, and the valuc of the loan. class Loan f public int LoanNumber {oet: set: public string LastNano {got; sot; } public double LoanAnount {get; set; r 2. At the top of the file, enter the following code to add a Demot oan class that contains a Main() method. The class declares a Loan object and shows how to set each field and display the results. using Systen; public class Demoloan { public static void MainO t Loan aLoan = new LoanO; aLoan. LoanNumber =2239; aLoan. LastNane = "Mitche11"; a Loan . LoanAmount =1000.00 Console.WriteLine("Loan s{0} for {1} is for {2} ". aloan. LoanNunber, aloan. Las tName, aloan. Loandmount. Tostring("(2")): f3 3. Save the file as Demoloan.cs, then compile and
  • 2. execute the program. The output looks like Figure 10-40. There is nothing unusual about this class or how it operates; it is similar to many you sew in the last chapter before you learned about inheritanoe. Figure 10-40 Output of the Denotoan program Extending a Class Next, you will create a class named Carloan. A CarLoan "is a" type of Loan. As such, it has all the attributes of a Loan, but it also has the year and make of the car that the customer is using as collateral for the loan. Therefore, Cartoan is a subxiass of Loan. Extending a Class Next, you will crate a class numed cari inan A carions "is a" mre of toan. As auch, it has all the attribites of i Loan toat it who has the yar and male of the car that the cuitomer is using as celateral fse the kan. Therefore, Cartoen is a subclau of toan. Te create the Cartoan class that eatends the Lean clese: 1. Save the Deroolinanos file as Densokarleanos. Olaner the Destoan clas name to Deoscartoen Begin the definition in the Cartaan clas after the cosing curly tract for the toan clas Cartoon exiende Loan and eontains two properties that fild the year and make of the car. cless Carloan & toen i I public int year foet: aetif 1) public string Make foet; set:} 2. Within the kainO method of the DemCarioan clas inat afier the declaration of the ioan object, declare a Carteon as follown. Cartoan aCarioan = new CartoenO: 3. Afier the three property askienments for the Loas object, insert five a esienment watements for the cartoan object. acartoen. toantuber = 335a aCartoan-tasthabe - "Jansen": acartoan, toandevent =20000.00 : acarloan. Wake - "Ford"? acarloan. Year =2005 : 4. Fullowing the writet ine 0 statment that diapups the isan pbject data. insert two kritel ine O stalements that diyplar the Cartoan obpetis data. Console. Writeline ("ioun ${0} for (1) is for (2). acarloun, Leentluber, efarloun. Lastiase, aCarloan. Loentwavent. Tostrieg("CZ")): Console. Writeline(" Loen (f0) is for a (1) (2)". icarloan .Loentwaber, Narleen- Year, artaen Make); 5. Seve the proyerm, thea compile and curcute it. The outpet boks liae higure 10-41. The Cartoan obyect coerectly wass its own fidde and properrues ai well as thase ef she perent toen cluas. You Do It In the previous sections, you created Loan and CarLoan classes and objects. Suppose the bank adopts new rules as follows: - No regular loan will be made for less than $5000. - No car loan will be made for any car older than model year 2006. - Although Loans might have larger loan numbers, CarLoans will have loan numbers that are no more than three digits. If a larger loan number is provided, the program will use only the last three digits for the loan number. To implement the new CarLoan rules: 1. Open the DemoCarLoan.cs file and immediately save it as DemoCarLoan2.cs. Also change the class name from DemoCarloan to DenoCarLoan2. 2. Within the Loan class, add a new constant that represents the minimum loan value: public const double MINIMUM LOAN = 5000: 3. Add a field for the loanAmount because the LoanAmount property will need to use it: protected double loanAmount: The field is
  • 3. protected so that carLoan objects will be able to access it as well as Loan objects. 4. Replace the auto-implemented property for LoanAmount in the Loan class with standard get and set accessors as follows. This change ensures that no loan is made for less than the minimum allowed value. 5. Within the CarLoan class, add two new constants to hold the earliest year for which car loans will be given and the lowest. allowed loan number: private const int EARLIEST_YEAR =2006; private const int LOWEST_INVALID NUM = 1000; 6. Also within the CarLoan class, add a fidd for the year of the car and replace the existing auto-implemented Year property with one that contains coded get and set accessors. The Year property set accessor not only sets the year field, it sets ToanAnount to 0 when a car's year is less than 2006. private int year; public int Yoar { set { if (value < EARLIEST_YEAR) I year = value; loanAnount =0 f else year = value; ] get f return year; 3 b If loanAmount was private in the parent Loan class, you would not be able to set its value in the child CarLoan class, as you do here. You could use the public property Loandmount to set the value, but the parent class sot accessor would force the value to 5000 . 7. Suppose there are unique rules for issuing loan numbers for cars. Within the Carloan class, just before the closing curly brace. change the inherited LoanNumber property to accommodate the new rules. If a car loan number is three digits or fewer, pass it on to the base class property. If not, obtain the last three digits by calculating the remainder when the loan number is divided by 1000, and pass the new number to the base class property. Add the following property after the definition of the Make property. public new int LoanNunber { get { return base. LoanNuaber; } set { if (value < LOWEST INVALID NUM) base. LoanNumber = value: else 3 base.LoanNumber = value % LOWEST INVALID NUM; 3 If you did not use the keyword base to access the LoanNumber property within the CarLoan class, you would be telling this version of the LoanNumber property to call itself. Although the program would compile, it would run continuously in an infinite loop until it ran out of memory and issued an error message. 8. Save the file. Compile it and correct any errors. When you execute the program, the output looks like Figure 10-42. Compare the output to Figure 1041. Notice that the $1000 bank loan has been forced to $5000. Also notice that the car loan number has been shortened to three digits and the value of the loan is $0 because of the age of the car. Figure 10-42 Output of the DemoCarLoan2 program 9. Change the assigned values within the DemoCarLoan2 class to combinations of early and late years and valid and invalid loan numbers. After each change, save the program, compile and execute it, and confirm that the program operates as expected.
  • 4. You Do lt Adding Constructors to Base and Derived Classes When a base class contains only constructors that require parameters, then any derived classes must provide for the base class constructor. In the next steps, you will add constructors to the Loan and Carloan classes and demonstrate that they work as expected. To add constructors to the classes: 1. Open the DemoCarLoan2 program and change the class name to Democarloan3. Save the file as DemoCarloan3.cs. 2. In the Loan class, just after the declaration of the loanmmount field, add a constructor that requires values for all the Loans properties: public Loan(int nun, string nane, double amount) {oanNumber = num; LastName = nane; LoanAnount = anount; 3. In the Carloan cass, just after the declaration of the year field, add a constructor that takes five parameters. It passes three of the parameters to the base class constructor and uses the other two to assign values to the properties that are unique to the child class. public Carloan(int nua, string nase, double anount. { int year. string make) : base(nue, nose. anount) Year = year: Make - make; f 4. In the Main O method of the DesocarLoan3 class, remove the existing declarations for a oan and a Carloan and replace them with two declarations that use the arguments passed to the constructors. Loen aloan - new Loan (333, "Hanson", 7000,00); Carloan aCarloan = new Carloan (444, "Carlisle", 30000.00,2011, "Bexr"); 5. Remove the eight statements that assigned values to Loan and Carloan, but retain the Console. Writeline() statements that display the values. 6. Save the program, then compile and execute it. The output looks like Figure 10-43. Both constructors work as expected. The CarLoan constructor has called its parent's constructor to set the necessary fields before executing its own unique statements. Figure 10-43 Output of the DemoCarLoan3 program