Presentation is loading. Please wait.

Presentation is loading. Please wait.

Day 8 4.1 Class Definitions and Methods Local & global variables, parameters & arguments,

Similar presentations


Presentation on theme: "Day 8 4.1 Class Definitions and Methods Local & global variables, parameters & arguments,"— Presentation transcript:

1 Day 8 4.1 Class Definitions and Methods Local & global variables, parameters & arguments,

2 Correspondence between formal parameters and arguments Formal parameters are given in parentheses after the method name at the beginning of a method definition i.e., public void setName(String newName) In a method invocation, arguments are given in parentheses after the method name i.e., student.setName(“Billy Bob”);

3 4.2 Information Hiding & Encapsulation Encapsulation: designing a method so that it can be used without any need to understand the fine detail of the code Also known as abstraction

4 Pre- and Post-conditions Example (in addition to our past coverage on this): /** Precondition: years is a nonnegative number Postcondition: Returns the projected population of the calling object after the specified number of year. */ public int projectPopulation(int years)

5 Assertion Checks An assertion check consists of the keyword assert followed by a Boolean expression and a semicolon. You can insert it anywhere in your code. If it is turned on and it evaluates to false, your program will end and output a suitable error message.

6 Assertion Checks Example: int x = 10; assert x == 10; while (x < 15) { x++; }

7 API API: application programming interface is essentially the same thing as the user interface for the class. You will see it again when reading the documentation for class libraries.

8 ADT Abstract Data Type: a data type that is written using good information hiding techniques In Java, an ADT is basically the same thing as a well-encapsulated class definition

9 Multiple Parameters Your method heading can be followed by multiple parameters of different types. i.e., public void isStudentCheck(String name, int age, int ID) { … }

10 Exercise: Write an encapsulated class that categorizes students based on their age and academic average as follows: AgeGrade 13-149 15-1610 17-1811 19-2012

11 Exceptional Circumstances: If a student has an academic average greater than or equal to 95%, then are transferred into the next grade. If the average was lower than or equal to 59%, then they are transferred into the previous grade.

12 Other Info: The student’s specific information (only the age or only the name) should be accessible (get methods). The specific information can also be modified (set) through separate methods.

13 Sample Output (show main program) > run studentInfoAPP Student Name: Henry Zimmerman Student Age: 16 Student Average: 61.5 Student Grade: 10 Student Name: Billy Bob Student Age: 18 Student Average: 88.0 Student Grade: 11 Student Name: Billy Bob Student Age: 18 Student Average: 97.5 Student Grade: 12

14 UML Class Diagram studentInfodescription -String name -int age -int grade -double average + studentData(String newName, int newAge, double newAverage) + public void recheckGrade() + public void writeOutput() + getName() + getAge() + getAverage() + getGrade() + setName(String newName) + setAge(int newAge) + setGrade(int newGrade) + setAverage(double newGrade) First method refers to second method inside it. Second method revises grade if student is >= 95 or <= 59 Third method simply outputs all information neatly.


Download ppt "Day 8 4.1 Class Definitions and Methods Local & global variables, parameters & arguments,"

Similar presentations


Ads by Google