Professional Documents
Culture Documents
Class Name
CoffeeReceipt
- String kind
Property
Data type - double pricePerPound or field
- double weight
Method
class CoffeeReceipt {
Field declarations. Each declaration
String kind; specifies the type of values that the field
double pricePerPound; name represents. Accordingly, kind
double weight; stands for a String and pricePerPound
and weight are doubles
CoffeeReceipt(String kind,
double pricePerPound, double weight) {
this.kind = kind;
this.pricePerPound = pricePerPound;
this.weight = weight;
}
} CONSTRUCTOR
of the class
This tests contain statement to create an object of the CoffeeReceipt class, you
apply the constructor to as many values as there are parameters:
new CoffeeReceipt("Hawaiian Kona", 15.95, 100)
Compound Data - Class 24
Date example
• Develop a program that helps you keep track of
daily. One date is described with three pieces of
information: a day, a month, and a year
• Class diagram
Date
- int day
- int month
- int year
import junit.framework.*;
public class DateTest extends TestCase {
public void testConstrutor() {
new Date(5, 6, 2003); // denotes June 5, 2003
new Date(6, 6, 2003); // denotes June 6, 2003
new Date(23, 6, 2000); // denotes June 23, 2000
}
}
Compound Data - Class 26
GPS Location example
• Develop a GPS navigation program for cars. The
physical GPS unit feeds the program with the
current location. Each such location consists of two
pieces of information: the latitude and the longitude.
• Class diagram
GPSLocation
- double latitude
- double longitude
import junit.framework.*;
public class GPSLocationTest extends TestCase {
public void testConstrutor() {
new GPSLocation (33.5, 86.8);
new GPSLocation (40.2, 72.4);
new GPSLocation (49.0, 110.3);
}
}
Compound Data - Class 28
Three steps in designing Classes
1. Read the problem statement.
Look for statements that mention or list the
attributes of the objects in your problem space.
Write down your findings as a class diagram
because they provide a quick overview of classes.
2. Translate the class diagram into a class definition,
adding a purpose statement to each class.
3. Obtain examples of information and represent
them with instances of the class.
Conversely, make up instances of the class and
interpret them as information.
Compound Data - Class 29
Excercice 1.1
• Develop a program that assists bookstore
employees. For each book, the program should
track the book’s title, its price, its year of
publication, and the author’s name…
• Develop an appropriate class diagram (by hand) and
implement it with a class. Create instances of the
class to represent these three books:
1. Daniel Defoe, Robinson Crusoe, $15.50, 1719;
2. Joseph Conrad, Heart of Darkness, $12.80, 1902;
3. Pat Conroy, Beach Music, $9.50, 1996.
Automobile
- String model
- int price [in dollars]
- double mileage [in miles per gallon]
- boolean used
• Example
(x != 0) && (x < 10) . . . determines whether a is not equal to
x (int or double) and x is less than 10
CoffeeReceipt
- String kind
- double pricePerPound
- double weight
double cost()
??? priceOver(???)
inside of Coffee
// to determine whether this coffee’s price is more
// than amount
boolean priceOver(double amount) { ... }
The only relevant pieces of data in the template are amt and
this.price:
// to determine whether this coffee’s price
// is more than amount
boolean priceOver(int amount) {
return this.pricePerPound > amount;
}
Posn
- int x
- int y
+ ??? distanceTo0(???)
+ ??? distanceTo(???)
import junit.framework.*;
+ ??? distanceToO(???)
+ ??? distanceTo(???)
• Examples
– Distance from A(5, 12) to O is 13
– Distance from B(6, 8) to O is 10
– Distance from A(3, 4) to O is 5
Posn(int x, int y) {
this.x = x;
this.y = y;
}
Posn(int x, int y) {
this.x = x;
this.y = y;
}
double distanceToO() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
}
Posn
- int x
- int y
+ double distanceToO()
+ ??? distanceTo(???)
• Example
– Distance from A(6, 8) to B(3, 4) is 5
Posn
- int x
- int y
+ double distanceToO()
+ double distanceTo(Posn that)
Employee
- String name
- int hours
+ ??? wage(???)
+ ??? tax(???)
+ ??? netpay(???)
+ ??? raisedWage(???)
+ ??? checkNotExceed100 (???)
import junit.framework.*;
public class EmployeeTest extends TestCase {
public void testContructor() {
new Employee("Nam", 40);
Employee aEmployee1 = new Employee("Mai", 30);
Employee aEmployee2 = new Employee("Minh", 102);
}
}
+ ??? wage(???)
+ ??? tax(???)
+ ??? netpay(???)
+ ??? raisedWage(???)
+ ??? checkNotExceed100(???)
• Examples
– Employee Nam works 40 and earns 480 $
– Employee Mai works 30 and earns 360 $
– Employee Minh works 100 and earns 1200 $
Compound Data - Class 67
wage method template
public class Employee {
String name;
int hours;
Employee(String name, int hours) {
this.name = name;
this.hours = hours;
}
+ ??? wage(???)
+ ??? tax(???)
+ ??? netpay(???)
+ ??? raisedWage(???)
+ ??? checkNotExceed100(???)
• Examples
– Employee Nam gets 480 $ and has to pay 72 $ for tax
– Employee Mai gets 360 $ and has to pay 54 $ for tax
– Employee Minh gets 1200 $ and has to pay 180 $ for tax
+ ??? wage(???)
+ ??? tax(???)
+ ??? netpay(???)
+ ??? raisedWage(???)
+ ??? checkNotExceed100(???)
• Examples
– With salary 480 $, Nam just receives 408 $ of netpay
– With salary 360 $, Mai just receives 306 $ of netpay
– With salary 1200 $, Minh just receives 1020 $ of netpay
+ ??? wage(???)
+ ??? tax(???)
+ ??? netpay(???)
+ ??? raisedWage(???)
+ ??? checkNotExceed100(???)
• Examples
– With basic salary 480$, after getting bonus, total income of Nam
is 494
– With basic salary 360$, after getting bonus, total salary of Mai is
374
– With basic salary 1200$, after getting bonus, total salary of Minh
is 1214
+ ??? wage(???)
+ ??? tax(???)
+ ??? netpay(???)
+ ??? raisedWage(???)
+ ??? checkNotExceed100(???)
• Examples
– It is true that Nam and Mai work 40 and 30 hours per week
– It is impossible for Minh to work 100 hours per week
Employee
- String name
- int hours
+ int wage()
+ double tax()
+ double netpay()
+ double raisedWage()
+ boolean checkNotExceed100()
Unit testing
public void testNetpayWithRate() {
assertEquals(new Employee("Nam", 10).netpayWithRate(),
120.0, 0.001);
Employee aEmployee1 = new Employee("Mai", 30);
assertEquals(aEmployee1.netpayWithRate(), 306.0, 0.001);
Employee aEmployee2 = new Employee("Minh", 100);
assertEquals(aEmployee2.netpayWithRate(), 864.0, 0.001);
}
Compound Data - Class 93