Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 2 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

3 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 3 Template for Class Definition class { } Import Statements Class Comment Class Name Data Members Methods (incl. Constructor) Methods (incl. Constructor)

4 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 4 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

5 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 5 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 Account {... public void add(double amt) { balance = balance + amt; }... } class Sample { public static void main(String[] arg) { Account acct = new Account();... acct.add(400);... }... } argument parameter

6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 6 Variables, Constants and Method calls Instance variables Class variables Constants Local variables Scope rules for local variables Calling methods from the same class

7 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 7 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"); }

8 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 8 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)


Download ppt "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."

Similar presentations


Ads by Google