04_Writing_Classes_2_per_page

Þßà public boolean borrow double amount returns a

This preview shows page 19 - 24 out of 25 pages.

Þßà public boolean borrow( double amount) { § © } calculateTotalBalance: returns a double representing the loan amount with the interest added. Þßà public double calculateTotalBalance() { § © }
Image of page 19

Subscribe to view the full document.

20 COMP 1210 – Fundamentals of Computing I Slide 4 - 39 Building a Class • Now you have method stubs for each one of your methods, but your program will not compile. If a method does not have a void return type, then you must return something or it will not compile. For now, just include a placeholder return – any value of that type. Examples: Þßà public double getBalance() { ¹Ĺ¹ return 0; © } Þßà public boolean setInterest( double newInterest) { ¹Ĺ¹ return false ; © } COMP 1210 – Fundamentals of Computing I Slide 4 - 40 Building a Class • You can now compile your program with empty methods. See the Loan class in examples/method_stubs for an example. • This is the point where you would submit your project to the ungraded Web-CAT submission.
Image of page 20
21 COMP 1210 – Fundamentals of Computing I Slide 4 - 41 Building a Class: Variables • Add instance variables to the class. Take another look at the class description: Suppose you wanted to create a class called Loan representing a loan with a balance , interest , and maximum loan amount The balance starts at 0 • The interest is 0.05 unless set otherwise • There are two loan types: employees and customers • Employees can borrow up to $100,000 • Customers can borrow up to $10,000 private double balance = 0, interestRate, maxLoanAmount; COMP 1210 – Fundamentals of Computing I Slide 4 - 42 Building a Class: Constants • Look for values that could be represented as constants: The interest is 0.05 unless set otherwise There are two loan types: employees and customers Employees can borrow up to $100,000 Customers can borrow up to $10,000 The default interest rate and maximum loan amounts will be important for the class, but won’t be needed by client programs. Therefore, the constants are private. private static final double DEFAULT_INTEREST = 0.05; private static final double CUSTOMER_MAX = 10000, EMPLOYEE_MAX = 100000;
Image of page 21

Subscribe to view the full document.

22 COMP 1210 – Fundamentals of Computing I Slide 4 - 43 Building a Class: Constants • Look for values that could be represented as constants: The interest is 0.05 unless set otherwise There are two loan types: employees and customers Employees can borrow up to $100,000 Customers can borrow up to $10,000 The loan type (customer or employee) will be set in the constructor using an int value. It would therefore be useful to provide constants for the client program: public static final int EMPLOYEE_ACCOUNT = 0, CUSTOMER_ACCOUNT = 1; COMP 1210 – Fundamentals of Computing I Slide 4 - 44 Building a Class: Constructor • The constructor should set the interest rate to the default interest and then set the maximum loan amount based on the parameter. • To test your constructor, instantiate objects in the interactions pane and check the values of the instance variables in the workbench (next slide).
Image of page 22
23 COMP 1210 – Fundamentals of Computing I Slide 4 - 45 Building a Class: Constructor • The constructor should set the interest rate to the default interest and then set the maximum loan amount based on the parameter.
Image of page 23

Subscribe to view the full document.

Image of page 24
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern