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

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
Chapter 4 Defining Classes I Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 41 Defining Classes and Methods Chapter 4.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 4 Defining Classes I Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Information Hiding and Encapsulation
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Defining Classes and Methods Chapter 4. Objectives become familiar with the concept of –a class –an object that instantiates the class learn how to –define.
Classes, Objects, and Methods
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Class Example - Rationals Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Two Parts of Every ADT An abstract data type (ADT)  is a type for encapsulating related data  is abstract in the sense that it hides distracting implementation.
10-Nov-15 Java Object Oriented Programming What is it?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Chapter 7 Functions. Types of Functions Value returning Functions that return a value through the use of a return statement They allow statements such.
Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Chapter 41 Defining Classes and Methods Chapter 4.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Unit 2 Review (Part 1) Arrays, Inheritance, and the Ethical Use of Computers.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Catie Welsh February 23,  Lab 4 due on Friday  Lab 5 will be assigned on Friday 2.
Class and Method Definitions. UML Class Diagram Automobile - fuel: double - speed: double - license: String + increaseSpeed(double howHardPress): void.
Interfaces and Inner Classes
Chapter 4 Defining Classes I Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
CMSC 202 Lesson 5 Functions I. Warmup Use setw() to print the following (in tabular format) Fred Flintstone Barney Rubble Bugs Bunny Daffy Duck.
1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
COMP Information Hiding and Encapsulation Yi Hong June 03, 2015.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Defining Classes and Methods
User-Written Functions
Introduction to Classes and Objects
Software Development Java Classes and Methods
Interfaces.
Computer Science II Exam 1 Review.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
COMP 110 Information hiding and encapsulation
Classes, Objects, and Methods
Defining Classes and Methods
An Introduction to Java – Part II
Announcements Program 2 is due tomorrow by noon Lab 4 was due today
Defining Classes and Methods
COP 3330 Object-oriented Programming in C++
Chapter 4 Defining Classes I
Object Oriented Programming in java
Defining Classes and Methods
Defining Classes and Methods
Classes, Objects and Methods
Information Hiding and Encapsulation Section 4.2
Presentation transcript:

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

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”);

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

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)

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.

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

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.

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

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) { … }

Exercise: Write an encapsulated class that categorizes students based on their age and academic average as follows: AgeGrade

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.

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.

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

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.