lec3 - COMP201 Java Programming Topic 3: Classes and...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
COMP201 Java Programming Topic 3: Classes and Objects Readings: Chapter 4 Especially “documentation and class design hints”
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP201 Topic 3 / Slide 2 Outline An Example: What does a Java class look like Ingredients of a class: How to write a class Instance fields Initialization and constructors Methods Class modifiers Packages: How classes fit together
Background image of page 2
COMP201 Topic 3 / Slide 3 An Example class Employee { // constructor public Employee( String n, double s, int year, int month, int day) { name = n; salary = s; GregorianCalendar calendar = new GregorianCalendar (year, month - 1, day); // GregorianCalendar uses 0 for January hireDay = calendar.getTime(); } // methods public String getName() { return name; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP201 Topic 3 / Slide 4 An Example public double getSalary() { return salary; } public Date getHireDay() { return hireDay; } public void raiseSalary( double byPercent) { double raise = salary * byPercent / 100; salary += raise; } private String name; private double salary; private Date hireDay; }
Background image of page 4
COMP201 Topic 3 / Slide 5 An Example import java.util.*; public class EmployeeTest { public static void main( String [] args) { // fill the staff array with three Employee objects Employee[] staff = new Employee[3]; staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15); staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1); staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP201 Topic 3 / Slide 6 An Example // raise everyone's salary by 5% for ( int i = 0; i < staff.length; i++) staff[i].raiseSalary(5); // print out information about all Employee objects for ( int i = 0; i < staff.length; i++) { Employee e = staff[i]; System .out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" + e.getHireDay()); } } } //EmployeeTest.java
Background image of page 6
COMP201 Topic 3 / Slide 7 Date and GregorianCalendar Date : a class belong to the standard Java library. An instance of the Date class has a state, means a particular point in time , represented by the number of milliseconds from a fixed point. GregorianCalendar : expresses dates in the familiar calendar notation. new GregorianCalendar() //midnight of Chinese New Year eve GregorianCalendar now= new GregorianCalendar(2002,2, 11) int month = now.get(Calendar.MONTH); int day = now.get(Calendar.DAY_OF_WEEK); Converting between Date and GregorianCalendar Date time = now.getTime(); now.setTime(time);
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP201 Topic 3 / Slide 8 Output of the Example name=Carl Cracker,salary=78750.0,hireDay=Tue Dec 15 00:00:00 GMT+08:00 1987 name=Harry Hacker,salary=52500.0,hireDay=Sun Oct 01 00:00:00 GMT+08:00 1989 name=Tony Tester,salary=42000.0,hireDay=Thu Mar 15 00:00:00 GMT+08:00 1990
Background image of page 8
Ingredients of a Class A class is a template or blueprint from which objects are created. class
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 47

lec3 - COMP201 Java Programming Topic 3: Classes and...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online