Introductionto Classes and Objects: Objectives
Introductionto Classes and Objects: Objectives
You will see something new. Two things. And I call them Thing One and Thing Two.
Dr. Theodor Seuss Geisel
What classes, objects, methods and instance variables are. How to declare a class and use it to create an object. How to declare methods in a class to implement the classs behaviors. How to declare instance variables in a class to implement the classs attributes. How to call an objects methods to make those methods perform their tasks. The differences between instance variables of a class and local variables of a method. How to use a constructor to ensure that an objects data is initialized when the object is created. The differences between primitive and reference types.
Knowing how to answer one who speaks, To reply to one who sends a message.
Amenemope
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
of the class must maintain information separately from all other objects of the class. For example, a class called Account that represents a bank account provides an instance variable to represent the balance of the account. Each Account object maintains its own balance, but does not know the balances of the banks other accounts.
3.9
java.util.
Explain how a program could use class Scanner without importing the class from package
ANS: If every use of a classs name in a program is fully qualified, there is no need to import
the class. A classs fully qualified name consists of the classs package followed by the class name. For example, a program could use class Scanner without importing it if every use of Scanner in the program is specified as java.util.Scanner.
3.13 Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0.0. Write a test application named InvoiceTest that demonstrates class Invoices capabilities.
ANS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Exercises 3.13 Solution: Invoice.java // Invoice class. public class Invoice { private String partNumber; private String partDescription; private int quantity; private double pricePerItem; // four-argument constructor public Invoice( String part, String description, int count, double price ) { partNumber = part; partDescription = description; if ( count > 0 ) // determine whether count is positive quantity = count; // valid count assigned to quantity if ( price > 0.0 ) // determine whether price is positive
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
if ( price <= 0.0 ) // determine whether price is zero or negative pricePerItem = 0.0; // invalid price; pricePerItem set to 0.0 } // end method setPricePerItem // get price per item public double getPricePerItem()
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
4
77 78 79 80 81 82 83 84 85 86 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
return pricePerItem; } // end method getPricePerItem // calculates and returns the invoice amount public double getInvoiceAmount() { return getQuantity() * getPricePerItem(); // calculate total cost } // end method getPaymentAmount } // end class Invoice
// Exercises 3.13 Solution: InvoiceTest.java // Application to test class Invoice. public class InvoiceTest { public static void main( String args[] ) { Invoice invoice1 = new Invoice( "1234", "Hammer", 2, 14.95 ); // display invoice1 System.out.println( "Original invoice information" ); System.out.printf( "Part number: %s\n", invoice1.getPartNumber() ); System.out.printf( "Description: %s\n", invoice1.getPartDescription() ); System.out.printf( "Quantity: %d\n", invoice1.getQuantity() ); System.out.printf( "Price: %.2f\n", invoice1.getPricePerItem() ); System.out.printf( "Invoice amount: %.2f\n", invoice1.getInvoiceAmount() ); // change invoice1's data invoice1.setPartNumber( "001234" ); invoice1.setPartDescription( "Yellow Hammer" ); invoice1.setQuantity( 3 ); invoice1.setPricePerItem( 19.49 ); // display invoice1 with new data System.out.println( "\nUpdated invoice information" ); System.out.printf( "Part number: %s\n", invoice1.getPartNumber() ); System.out.printf( "Description: %s\n", invoice1.getPartDescription() ); System.out.printf( "Quantity: %d\n", invoice1.getQuantity() ); System.out.printf( "Price: %.2f\n", invoice1.getPricePerItem() ); System.out.printf( "Invoice amount: %.2f\n", invoice1.getInvoiceAmount() ); Invoice invoice2 = new Invoice( "5678", "Paint Brush", -5, -9.99 ); // display invoice2 System.out.println( "\nOriginal invoice information" ); System.out.printf( "Part number: %s\n", invoice2.getPartNumber() ); System.out.printf( "Description: %s\n", invoice2.getPartDescription() ); System.out.printf( "Quantity: %d\n", invoice2.getQuantity() );
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
System.out.printf( "Price: %.2f\n", invoice2.getPricePerItem() ); System.out.printf( "Invoice amount: %.2f\n", invoice2.getInvoiceAmount() ); // change invoice2's data invoice2.setQuantity( 3 ); invoice2.setPricePerItem( 9.49 ); // display invoice2 with new data System.out.println( "\nUpdated invoice information" ); System.out.printf( "Part number: %s\n", invoice2.getPartNumber() ); System.out.printf( "Description: %s\n", invoice2.getPartDescription() ); System.out.printf( "Quantity: %d\n", invoice2.getQuantity() ); System.out.printf( "Price: %.2f\n", invoice2.getPricePerItem() ); System.out.printf( "Invoice amount: %.2f\n", invoice2.getInvoiceAmount() ); } // end main } // end class InvoiceTest
Original invoice information Part number: 1234 Description: Hammer Quantity: 2 Price: 14.95 Invoice amount: 29.90 Updated invoice information Part number: 001234 Description: Yellow Hammer Quantity: 3 Price: 19.49 Invoice amount: 58.47 Original invoice information Part number: 5678 Description: Paint Brush Quantity: 0 Price: 0.00 Invoice amount: 0.00 Updated invoice information Part number: 5678 Description: Paint Brush Quantity: 3 Price: 9.49 Invoice amount: 28.47
3.15 Create a class called Date that includes three pieces of information as instance variablesa month (type int), a day (type int) and a year (type int). Your class should have a constructor that initializes the three instance variables and assumes that the values provided are correct. Provide a set
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
and a get method for each instance variable. Provide a method displayDate that displays the month, day and year separated by forward slashes (/). Write a test application named DateTest that demonstrates class Dates capabilities.
ANS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
// Exercise 3.15 Solution: Date.java // Date class with instance variables for the month, day and year. public class Date { private int month; private int day; private int year; // constructor public Date( int monthValue, int dayValue, int yearValue ) { month = monthValue; day = dayValue; year = yearValue; } // end three-argument constructor // set the month public void setMonth( int monthValue ) { month = monthValue; } // end method setMonth // return the month public int getMonth() { return month; } // return month // set the day public void setDay( int dayValue ) { day = dayValue; } // end method setDay // return the day public int getDay() { return day; } // return day // set the year public void setYear( int yearValue ) { year = yearValue; } // end method setYear // return the year public int getYear()
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
return year; } // return year // display the date public void displayDate() { System.out.printf( "%d/%d/%d", getMonth(), getDay(), getYear() ); } // end method displayDate } // end class Date
// Exercise 3.15 Solution: DateTest.java // Application to test class Date. public class DateTest { public static void main( String args[] ) { Date date1 = new Date( 7, 4, 2004 ); System.out.print( "The initial date is: " ); date1.displayDate(); // change date values date1.setMonth( 11 ); date1.setDay( 1 ); date1.setYear( 2003 ); System.out.print( "\nDate with new values is: " ); date1.displayDate(); System.out.println(); // output a newline } // end main } // end class DateTest
The initial date is: 7/4/2004 Date with new values is: 11/1/2003
2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.