04_Writing_Classes_2_per_page

# Þßà public boolean borrow double amount returns a

• Notes
• 25

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() { § © }

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.
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;

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).
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.

Subscribe to view the full document.

You've reached the end of this preview.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern