04_Writing_Classes_2_per_page

Constructor which makes it a regular method that

This preview shows page 16 - 20 out of 25 pages.

constructor, which makes it a “regular” method that happens to have the same name as the class If the programmer does not define a constructor, then the class has a default constructor that accepts no parameters Q COMP 1210 – Fundamentals of Computing I Slide 4 - 32 Constant Fields Recall that a class constant can be declared as follows: private static final int MAX_AMOUNT = 5; Uses the static and final reserved words (static will be covered in detail later in the course) The name is in all caps with words separated by _ In general, you do not want “magic numbers” (literal values) in your code. Replace this code: if (number > 5) . . . With this code: if (number > MAX_AMOUNT) . . .
Image of page 16

Subscribe to view the full document.

17 COMP 1210 – Fundamentals of Computing I Slide 4 - 33 Constant Fields Unlike variables, constants can be public without violating encapsulation. For example, the PI constant in the Math class. double circumference = Math.PI * diameter; Referenced by using the name of the class, the dot operator, and the name of the constant. When you define a constant inside your own class, you can reference it inside the class without using the class name. if (number > MAX_AMOUNT). . . COMP 1210 – Fundamentals of Computing I Slide 4 - 34 Constant Fields Suppose the Student class had two types of students: undergrad and graduate. public static final int GRADUATE = 0, UNDERGRAD = 1; Now client programs can set student type using constants (studentObj is an instance of Student) : Replace this code: studentObj.setStudentType( 0 ); With this code: studentObj.setStudentType( Student.GRADUATE );
Image of page 17
18 COMP 1210 – Fundamentals of Computing I Slide 4 - 35 Building a Class 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 Create the empty class: ÕÖ× public class Loan { § © } COMP 1210 – Fundamentals of Computing I Slide 4 - 36 Building a Class The constructor for the bank class should take an int parameter representing the type of loan (employee or customer) Create an empty constructor; you will fill in the code later: ÕÖ× public class Loan { § § Þßà public Loan( int accountType) { § § § © } § © }
Image of page 18

Subscribe to view the full document.

19 COMP 1210 – Fundamentals of Computing I Slide 4 - 37 Building a Class • Create an empty method for each of the following methods: A ‘getter’ method for the balance (a double): Þßà public double getBalance() { § © } A ‘setter’ method for the interest rate (a double) that returns true and sets the interest only if the interest is from 0 to 1: Þßà public boolean setInterest( double newInterest) { § © } COMP 1210 – Fundamentals of Computing I Slide 4 - 38 Building a Class • Create an empty method for each of the following additional methods: borrow: adds an additional amount to the loan and returns true only if the loan stays below the maximum amount.
Image of page 19
Image of page 20
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