Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.

Slides:



Advertisements
Similar presentations
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Web Application Development Slides Credit Umair Javed LUMS.
Using Classes to Store Data Computer Science 2 Gerb.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 2 Review Questions
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
Slides prepared by Rose Williams, Binghamton University Chapter 8 Polymorphism Part I.
CS102--Object Oriented Programming Lecture 10: – Abstract Classes Copyright © 2008 Xiaoyan Li.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 6 Objects and Classes. Using the RandomGenerator Class Before you start to write classes of your own, it helps to look more closely at how to.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
CSC 212 Object-Oriented Programming and Java Part 1.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CSC 142 Computer Science II Zhen Jiang West Chester University
C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.
CET203 SOFTWARE DEVELOPMENT Session 1A Revision of Classes.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CSC 212 Object-Oriented Programming and Java Part 2.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Objects and Classes Eric Roberts CS 106A January 25, 2010.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Class Fundamentals BCIS 3680 Enterprise Programming.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
Quiz: Design a Product Class Create a definition for a class called Product, which keeps track of the following information: –Name of the product –Weight.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
Five-Minute Review 1.What is a method? A static method? 2.What is the motivation for having methods? 3.What role do methods serve in expressions? 4.What.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
Andrew(amwallis) Classes!
Object-Oriented Concepts
The need for Programming Languages
A variable is a name for a value stored in memory.
CS100A, 10 Sept Lecture 4: Continue discussion of classes and introduces Class String, Printing output using System.out.print, System,out.println,
Five-Minute Review What is a method? A static method?
Five-Minute Review What is a method? A static method?
Creating Objects in a Few Simple Steps
More on Classes and Objects
© A+ Computer Science - OOP © A+ Computer Science -
OO Design with Inheritance
CS139 October 11, 2004.
Simple Classes in Java CSCI 392 Classes – Part 1.
JAVA CLASSES.
Classes and Methods 15-Aug-19.
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify life is to give it an object. —Jos é Martí, On Oscar Wilde, 1888

Representing Student Information Understanding the structure of a class is easiest in the context of a specific example. The next four slides walk through the definition of a class called Student, which is used to keep track of the following information about a student: –The name of the student –The student’s six-digit identification number –The number of credits the student has earned (which may include a decimal fraction to account for half- and quarter-credit courses) –A flag indicating whether the student has paid all university fees Each of these values is stored in an instance variable of the appropriate type.

Representing Student Information In keeping with the modern object-oriented convention, these instance variables are declared as private. private fields can only be accessed within the class they are defined in. All access to these values is therefore mediated by the get/set methods exported by the Student class.

public class Student { /* Private instance variables */ private String studentName; /* The student's name */ private int studentID; /* The student's ID number */ private double creditsEarned; /* The number of credits earned */ private boolean paidUp; /* Whether student is paid up */ /** * Creates a new Student object with the specified name and ID. name The student's name as a String id The student's ID number as an int */ public Student(String name, int id) { studentName = name; studentID = id; } The Student Class This comment describes the constructor. The constructor sets the instance variables. skip code page 1 of 4 These declarations define the instance variables that maintain the internal state of the object. All instance variables used in the text are private.

/** * The Student class keeps track of the following pieces of data * about a student: the student's name, ID number, the number of * credits the student has earned toward graduation, and whether * the student is paid up with respect to university bills. * All of this information is entirely private to the class. * Clients can obtain this information only by using the various * methods defined by the class. */ public class Student { /** * Creates a new Student object with the specified name and ID. name The student's name as a String id The student's ID number as an int */ public Student(String name, int id) { studentName = name; studentID = id; } /** * Gets the name of this student. The name of this student */ public String getName() { return studentName; } /** * Gets the ID number of this student. The ID number of this student */ public int getID() { return studentID; } /** * Sets the number of credits earned. credits The new number of credits earned */ public void setCredits(double credits) { creditsEarned = credits; } The Student Class These methods retrieve the value of an instance variable and are called getters. Because the student name and ID number are fixed, there are no corresponding setters. This method changes the value of an instance variable and is called a setter. skip code page 2 of 4

/** * Gets the name of this student. The name of this student */ public String getName() { return studentName; } /** * Gets the ID number of this student. The ID number of this student */ public int getID() { return studentID; } /** * Sets the number of credits earned. credits The new number of credits earned */ public void setCredits(double credits) { creditsEarned = credits; } /** * Gets the number of credits earned. The number of credits this student has earned */ public double getCredits() { return creditsEarned; } /** * Sets whether the student is paid up. flag The value true or false indicating paid-up status */ public void setPaidUp(boolean flag) { paidUp = flag; } /** * Returns whether the student is paid up. Whether the student is paid up */ public boolean isPaidUp() { return paidUp; } The Student Class Names for getter methods usually begin with the prefix get. The only exception is for getter methods that return a boolean, in which case the name typically begins with is. skip code page 3 of 4

/** * Gets the number of credits earned. The number of credits this student has earned */ public double getCredits() { return creditsEarned; } /** * Sets whether the student is paid up. flag The value true or false indicating paid-up status */ public void setPaidUp(boolean flag) { paidUp = flag; } /** * Returns whether the student is paid up. Whether the student is paid up */ public boolean isPaidUp() { return paidUp; } /** * Creates a string identifying this student. The string used to display this student */ public String toString() { return studentName + " (#" + studentID + ")"; } /* Public constants */ /** The number of credits required for graduation */ public static final double CREDITS_TO_GRADUATE = 32.0; } The Student Class The toString method tells Java how to display a value of this class. All of your classes should override toString. Classes often export named constants. skip code page 4 of 4

Using the Student Class Once you have defined the Student class, you can then use its constructor to create instances of that class. For example, you could use the following code to create two Student objects: Student chosenOne = new Student("Harry Potter", ); Student topStudent = new Student("Hermione Granger", ); You can then use the standard receiver syntax to call methods on these objects. For example, you could set Hermione’s number-of-credits field to 97 by writing or get Harry’s full name by calling topStudent.setCredits(97); chosenOne.getName();

Exercise: Design an Employee Class Create a definition for a class called Employee, which keeps track of the following information: –The name of the employee –A number indicating the order in which this employee was hired –A flag indicating whether the employee is still active –The salary (a number that may contain a decimal fraction) The name and employee number should be assigned as part of the constructor call, and it should not be possible to change them subsequently. By default, new employees should be marked as active. The salary field need not be initialized. The class should export appropriately named getters for all four fields and setters for the last two.

/** * The Employee class keeps track of the following pieces of * data about an employee: the name, employee number, whether * the employee is active, and the annual salary. */ public class Employee { /* Private instance variables */ private String employeeName; /* The employee's name */ private int employeeNumber; /* The employee number */ private boolean active; /* Whether the employee is active */ private double annualSalary; /* The annual salary */ /** * Creates a new Employee object with the specified name and * employee number. name The employee's name as a String id The employee number as an int */ public Employee(String name, int id) { employeeName = name; employeeNumber = id; active = true; } The Employee Class skip code page 1 of 4

/** * The Employee class keeps track of the following pieces of * data about an employee: the name, employee number, whether * the employee is active, and the annual salary. */ public class Employee { /** * Creates a new Employee object with the specified name and * employee number. name The employee's name as a String id The employee number as an int */ public Employee(String name, int id) { employeeName = name; employeeNumber = id; active = true; } /** * Gets the name of this employee. The name of this employee */ public String getName() { return employeeName; } /** * Gets the employee number of this employee. The employee number of this employee */ public int getEmployeeNumber() { return employeeNumber; } /** * Sets whether the employee is active. flag The value true or false indicating active status */ public void setActive(boolean flag) { active = flag; } The Employee Class skip code page 2 of 4

/** * Gets the name of this employee. The name of this employee */ public String getName() { return employeeName; } /** * Gets the employee number of this employee. The employee number of this employee */ public int getEmployeeNumber() { return employeeNumber; } /** * Sets whether the employee is active. flag The value true or false indicating active status */ public void setActive(boolean flag) { active = flag; } /** * Returns whether the employee is active. Whether the employee is active */ public boolean isActive() { return active; } /** * Sets the employee's salary. salary The new salary */ public void setSalary(double salary) { annualSalary = salary; } /** * Gets the annual salary for this employee. The annual salary for this employee works */ public double getSalary() { return annualSalary; } The Employee Class skip code page 3 of 4

/** * Returns whether the employee is active. Whether the employee is active */ public boolean isActive() { return active; } /** * Sets the employee's salary. salary The new salary */ public void setSalary(double salary) { annualSalary = salary; } /** * Gets the annual salary for this employee. The annual salary for this employee works */ public double getSalary() { return annualSalary; } /** * Creates a string identifying this employee. The string used to display this employee */ public String toString() { return employeeName + " (#" + employeeNumber + ")"; } The Employee Class skip code page 4 of 4

Exercise: Using the Employee Class Now that you have defined Employee, write declarations for three variables that contain the names of the following three employees: Ebenezer Scrooge (employee #1), Jacob Marley (employee #2), and Bob Cratchit (employee #3). Employee founder = new Employee("Ebenezer Scrooge", 1); Employee partner = new Employee("Jacob Marley", 2); Employee clerk = new Employee("Bob Cratchit", 3); Using these variables, write a Java statement that marks the Employee instance for Jacob Marley as inactive. partner.setActive(false); Write a Java statement that doubles Bob Cratchit’s salary. clerk.setSalary(2 * clerk.getSalary());

Quiz: Design a Product Class Create a definition for a class called Product, which keeps track of the following information: –Name of the product –Weight of the product –Price of the product –Currency of the product price The name and weight should be assigned as part of the constructor call, and it should not be possible to change them subsequently. By default, price of each product is 10 TL. The class should have appropriately getters for all four fields and setters for the last two.