Update (2-7-02)
Turnin deadline postponed to Feb 9 (a Saturday),
BUT NOTE: there are few proctors on duty Saturday and
proctor hours end early on Friday.
If you make it, Feb 9 (or earlier) is good for the early interview deal.
Update (2-4-02)
The total sum in the dialog was incorrect. It has been corrected to $16.25
turnin deadline postponed one day to Feb 8.
If you are trying for an early interview (one point deal)
remember Feb 8 is a friday and so proctor hours end early.
It would be good to aim for Feb 7 for an early interview, although
Feb 8 will count as an early interview.
the following policy applies to this assignment and all following assignments:
If you do your interview before the
turnin deadline, you get one free mistake! You still cannot get over 10
points, but if you would, for example, lose two points we will only take
of one point and you will get a 9 instead of an 8.
Due dates
turnin Feb 7
interview Feb 11
but, the MATERIAL IS COVERED ON THE MIDTERM!
CSE 11 Winter 2002
Home Work #4
Required reading: Savitch text Chapter 7
This exercise should be done using only the material from Chapters 1 through
7.
Due dates to be announced.
Note you can do this and take an interview without turning in the assignment,
as long as it is before the turnin deadline (and until the deadline is
announced it is definitely before the deadline.)
Programming: You may work in teams of two students, but once you form a team,
you cannot change the team for this assignment. You may work alone if you
prefer.
In this exercise you will define a class DiscountSale which is a derived class
of the class Sale and will use the class DiscountSale in a program. The class
Sale is given below. An object of the class Sale is a simple sale of one item
(in some store). The sale has a price and nothing else, no discount, no tax,
no nothing.
The class DiscountSale is a derived class of the class Sale. An object of the
class DiscountSale has a price (inherited from the class Sale) and a discount.
For example, if the cost is $10.00 and the discount is 10.0%, then the net
cost after discount is $9.00 ($10.00 minus a 10% discount). The method bill
for the class DiscountSale returns this net cost.
The class Dollars which is used in the definition of the class Sale (and which
you will use in the definition of the class DiscountSale) is from Figure 5.14
in Chapter 5 of the Savitch book. It is in the public directory and on the CD
that comes with the Savitch text.
Note that for the class DiscountSale you must add an instance variable and the
class inherits an instance variable so an object of the class DiscountSale has
a total of two instance variables, one for the price (inherited) plus one for
the discount.
The class DiscountSale will have a new accessor method named getDiscount and a
new mutator method called setDiscount. It will have at least two constructors,
a default constructor and a constructor that takes two arguments, one for the
price and one for the discount. It will also redefine the methods read,
readLine, write, writeLine, and bill.
Write a program, named SumSales, that uses the classes DiscountSale and Sale
to sum a list of bills. The program asks for a list of plain old sales and
then asks for a list of discount sales. For both types of sales it uses the
method bill to compute the "bill" and sums all these bill amounts. Be sure to
use the class methods for your I/O. When defining the I/O methods for the
class DiscountSale design the methods so the I/O is as in the following sample
dialogue. Note that we assume that no item has a price of zero so we can use
zero as a sentinel value.
Sample Dialogue (Note the format for I/O. Your program must have the same I/O
format.)
Enter a list of sales to total (maximum of 20):
First enter simple sales (enter a zero to end the list):
5.00
4.50
0
Now enter discount sales:
Format: price followed by discount:
(enter two zeros to end the list)
5.00 10
4.50 50
0 0
Sales are:
$5.00
$4.50
$5.00 with a 10% discount
$4.50 with a 50% discount
total sales = $16.25
Want to sum another total?(y/n): n
When reading the input, your program will read the input using a variable of
type Sale for the first part of the list and a variable of type DiscountSale
for the second part of the list, BUT you will store all these objects (whether
of type Sale or DiscountSale in an array with base type Sale). Use an array of
lengh 20. When you output this data, you will output it from the array. So
part of your program will implement the following pseudocode:
Sale[] a = new Sale[20];
int count = 0; //Contains the number of elements in the array a that are used.
Readin the input.
Compute the total sales using the array a.
System.out.println("Sales are:");
for (int i = 0; i < count; i++)
a[i].writeln();
Output total sales
Review Questions: Be prepared to answer the following questions (as well as
any others that pertain to this exercise):
1. Why can't you use a negative number as a sentinel value instead of zero?
2. If you assign an object of type DiscountSale to a variable of type Sale and
then invoke the method bill with the variable of type Sale, which definition
of the method bill will be used, the one defined in DiscountSale or the one
defined in Sale.
//This is the file Sale.java
//Objects of type Sale are a simple sale of one item with
//no discount or any other adjustments.
//Uses the classes SavitchIn and Dollars.
public class Sale
{
private double price;
public Sale( )
{
price = 0.0;
}
public Sale(double thePrice)
{
if (thePrice >= 0)
price = thePrice;
else
{
System.out.println("Error: Cannot have a negative price!");
System.exit(0);
}
}
public double getPrice( )
{
return price;
}
public void setPrice(double newPrice)
{
if (newPrice >= 0)
price = newPrice;
else
{
System.out.println("Error: Cannot have a negative price!");
System.exit(0);
}
}
public double bill( )
{
return price;
}
public void read()
{
price = SavitchIn.readDouble();
}
public void readLine()
{
price = SavitchIn.readLineDouble();
}
public void write()
{
Dollars.write(price);
}
public void writeLine()
{
Dollars.writeln(price);
}
}
What files to turn in:
When you are finished you will have 5 files:
SavitchIn.java, Dollars.java, Sale.java, DiscountSale.java, and SumSales.java.
The first three were given to you. You wrote the last two. You only turn in
the file DiscountSale.java. However, you must leave the file SumSales.java in
your course account and must not change it after the deadline day. (We will
check the dates on the files). If you want to play with these files, make
copies to play with.
If working in a team, you only turn in one copy of DiscountSale.java for both
of you. Be sure both partner’s names and login names are in a comment at the
head of the file. The file SumSales.java should be kept in both partners
accounts.
|