Transcript Chapter 4

Chapter 4 : Defining Your Own Classes Part 1
- Objectives
After you have read and studied this chapter, you should
be able to
•
•
•
•
•
•
Define a class with multiple methods and data members
Differentiate the local and instance variables
Define and use value-returning methods
Distinguish private and public methods
Distinguish private and public data members
Pass both primitive data and objects to a method
Programmer-Defined Classes
• Classes we define ourselves are called programmer-defined classes.
• Java cannot provide everything for us. For example, If we have to
design a “Student” class?
• Student.java – In class demonstration (server class)
• TestStudent.java – In class demo (application or driver class)
• Creating multiple objects
• Both Student.java and TestStudent.java need to be in the same
directory
• Program diagrams and class diagram
©The McGraw-Hill Companies, Inc.
Permission required for reproduction or
display.
4th Ed Chapter 2 - 2
Template for Class Definition
Import Statements
Class Comment
class
{
Class Name
Data Members
Methods
(incl. Constructor)
}
©The McGraw-Hill Companies, Inc.
Permission required for reproduction or
display.
4th Ed Chapter 2 - 3
Declarations
•
•
•
•
Data member declarations
Method declarations
Constructor
An example driver program using two different classes
• Object oriented advantages so far
– Encapsulation
– Information hiding
– Re using classes and objects
©The McGraw-Hill Companies, Inc.
Permission required for reproduction or
display.
4th Ed Chapter 2 - 4
Arguments and Parameters
• An argument is a value we pass to a method.
• A parameter is a placeholder in the called method to
hold the value of the passed argument.
• Matching arguments and parameters
• Passing objects to a method
• Sharing an object
class Sample {
class Account {
public static void
main(String[] arg) {
}
. . .
Account acct = new Account();
. . .
public void add(double amt) {
acct.add(400);
. . .
}
. . .
}
parameter
balance = balance + amt;
. . .
}
argument
©The McGraw-Hill Companies, Inc.
Permission required for reproduction or
display.
4th Ed Chapter 2 - 5
Variables , Constants and Method calls
•
•
•
•
•
•
Instance variables
Class variables
Constants
Local variables
Scope rules for local variables
Calling methods from the same class
©The McGraw-Hill Companies, Inc.
Permission required for reproduction or
display.
4th Ed Chapter 2 - 6
Changing Any Class to a Main Class
• Any class can be set to be a main class.
• All you have to do is to include the main method.
class Bicycle {
//definition of the class as shown before comes here
//The main method that shows a sample
//use of the Bicycle class
public static void main(String[] args) {
Bicycle myBike;
myBike = new Bicycle( );
myBike.setOwnerName("Jon Java");
System.out.println(myBike.getOwnerName() + "owns a bicycle");
}
}
©The McGraw-Hill Companies, Inc.
Permission required for reproduction or
display.
4th Ed Chapter 2 - 7
Problem Statement
•
•
Write a program that accepts the unit weight of a bag of coffee in
pounds and the number of bags sold and displays the total price of the
sale, computed as
subTotal = bagWeight * numberOfBags * pricePerPound;
totalPrice = subTotal + subTotal * taxRate;
Display the result in the following manner:
•
/ * Sample Run 1
•
Number of bags sold: 32
Weight per bag: 5
Price per pound: $5.99
Sales tax: 7.25%
Total price: $ 1027.88
•
End of Sample Run 1
•
Create two files , CoffeeBag.java (server application) and
CoffeeShipper.java (client application or driver program)
©The McGraw-Hill Companies, Inc.
Permission required for reproduction or
display.
*/
4th Ed Chapter 2 - 8