Você está na página 1de 12

Week 4 Homework

Problem 1 (2 points)
Given the following constructor definitions, write the
statement to create an object using each constructor.
a. public Customer(String inCustName, String inCustPhone)
{
}

Customer newCust = new Customer ("Matt", "555-555-5555");

b. public Date(int inMonth, int inDay, int inFullYear)


{

Date birthdayDate = new Date (9, 29, 1983);

c. public Account()
{

Account billingAccount = new Account ();

d. public Product(String inBarCode, double inCost)


{

Product newProduct = new Product ("987654321", 99.99);

Problem 2 (2 points)
Given the following method definitions for the Date class,
write the statement to call each method, assuming the object
name is newDate. Be sure to pass in appropriate explicit
parameters and to put any returned values into an
appropriate variable.
a. public void setMonth(int newMonth)
private int month;
public void setMonth (int newMonth)
{
this.month = newMonth;
}
monthNew.setMonth(8);
int returnMonth = monthNew.getMonth();

b. public int getYear()


private int year;
public int getyear()
{
return this.year;
}

c. public String formatDateYYYYMMDD()


String displayDate = registerDate.fotmatDateYYYYMMDD();

d. public void changeDate(int inMM, int inDD, int inYYYY)


private int MM;
private int DD;
private int YYYY;
public void changeDate (int inMM, int inDD, int inYYYY)
{
this.MM = inMM;
this.DD = inDD;
this.YYYY = inYYYY;
}

Problem 3 (2 points)
Given the following constructor and method definitions,
write the unit test cases for each constructor. Remember
that before testing you must first create the object. Be
sure to include tests for the following:
object has been created (assertNotNull)
instance variables have been correctly set to a value
a. public Customer(String inCustName, String inCustPhone)
public String getCustName()
public String getCustPhone()
Customer tmpCust = new Customer ("Matt", 5555555555);
assertNotNull(tmpCust);
assertEquals("Matt", tmpCust.getCustName());
assertEquals(5555555555, tmpCust.getPhone());

b. public
public
public
public

Date(int inMonth, int inDay, int inFullYear)


int getMonth()
int getDay()
int getFullYear()

Date tmpDate = new Date (01, 01, 2015)


assertNotNull(tmpDate);
assertEquals(01, tmpCust.getMonth());
assertEquals(01, tmpCust.getDay());
assertEquals(2015, tmpCust.getFullYear());

c. public Account()
public double getBalance() default value = 0.0

double total = this.balance - this.defaultValue


return total;

d. public Product(String inBarCode, double inCost)


public String getBarCode()
public double getCost()
Product tmpBC = new Product (5555555555, 19.99)
assertNotNull(tmpBC);
assertEquals(5555555555, tmpBC.getBarCode());
assertEquals(19.99, tmpBC.getCost());

Problem 4 (2 points)
Given the following method definitions for the Date class,
write the unit test statements to test each method. Assume
that the following object has been created and use this in
your testing:
public Time(int inHour, int inMinute, int inSecond)
Time tmpTime = new Time(04, 14, 43);
When writing your test cases, it may be necessary to first
call the method and then to call a get or accessor method to
verify the expected action has occurred.
a. public void setHour(int newHour)
Corresponding get method is:
i. public int getHour()
public void setHour (int newHour)
{
this.hour = newHour;
}
public int getHour()
{
return this.hour;
}
public Time (int inHour, int inMinute, int inSecond)
Time tmpHour = new Time(04, 14, 43);
assertEquals(04, tmpHour.getHour());

b. public int getSecond()


public void setHour (int newSecond)
{
this.second = newSecond;
}
public int getSecond()
{
return this.second;
}
public Time (int inHour, int inMinute, int inSecond)
Time tmpSecond = new Time(04, 14, 43);
assertEquals(43, tmpSecond.getSecond());

c. public String formatTime()


Format would be hh:mm:ss
public void formatTime
{
this.hour =
this.minute
this.second
}

(int inHour, int inMinute, int inSecond)


inHour;
= inMinute;
= inSecond;

return this.hour + " : " + this.minute " : " + this.second;

d. public void changeTime(int inHH, int inMM, int inSS)


Corresponding get methods are:
i. public int getHour()
ii. public int getMinute()
iii. public int getSecond()
public void
{
this.hour =
this.minute
this.second
}

changeTime (int inHH, int inMM, int inSS)


inHH;
= inMM;
= inSS;

public changeTime (int inHH, int inMM, int inSS)


changeTime tmpTime = new Time (01, 14, 43);
assertEquals(01, tmpTime.getHH());
assertEquals(14, tmpTime.getMM());
assertEquals(43, tmpTime.getSS());

Problem 5 (2 points)
For the class code:
a) public Class Box()
b) {
c)
private int height;
d)
private int length;
e) }
f) public Box()
g) {
h)
this.height = 0;
i)
this.length = 0;
j) }
k) public void setHeight(int inHeight)
l) {
m)
this.height = inHeight;
n) }
o) public void setLenght(int inLength)
p) {
q)
this.height = inLength;
r) }
s) public int computeArea()
t) {
u)
int area = this.height * this.length;
v)
return area
w) }

Assuming the following code statements were written for the


class above.
Box tmpBox = new Box();
tmpBox.setHeight(4);
tmpBox.setLength(8);
int computedArea = tmpBox.computeArea();

Identify all of the variables in the class


7

Indicate if the variable is an instance variable or a


local variable
Determine the value of the variable after the execution
of each line of code, if the variable has not been
assigned a value enter UD (undefined). If the
variable does not exist enter DE (doesnt exist).
setHeigh
setLengt
computedAre
Variable tmpBox
t
h
a
Name
Instanc
e or
Local
Value
Local
Local
Instance
a
De
ud
ud
b
De
ud
ud
c
De
ud
ud
d
De
ud
ud
e
De
ud
ud
f
De
ud
ud
g
De
Ud
h
De
Ud
i
De
Ud
j
De
Ud
k
De
l
De
m
De
n
De
o
De
p
De
q
De
r
De
s
De
t
De
u
De
v
De
w
De
I just dont get this box - hopefully you can
when you send me this back.

Instance
ud
Ud
Ud
Ud
Ud
Ud

shed some light

Problem 6 (5 points total)


Part 1: Test Class(2 points)
Write the TestHomeOwner class to test the following method for the
HomeOwner class.
HomeOnwer() empty constructor
o should initialze the instance variables to the following
values:
name = NO NAME
addr = NO ADDRESS
void setName(String inName)
o This method will change the name instance variable to be
equal to the explicit parameter passed to the method.
String getName()
o This method will return the value of the name.
void setAddr(String inAddr)
o This method will change the addr instance variable to be
equal to the explicit parameter passed to the method.
String getAddr()
o This method will return the value of the address.
public class TestHomeOwner
{
public void testHomeOwner()
{
HomeOwner owner1 = new HomeOwner("Bill Gates", "777
Lucky Dr");
assertNotNull(name1);
assertEquals(Bill Gates, owner1.getName);
assertEquals(777 Lucky Dr, owner1.getAddr);

//Test setting new name


name1.setName("Bill Gates Jr");
assertEquals(Bill Gates Jr, owner1.getName);

Part 2: Class(2 points)


Write the HomeOwner class with the following methods:
HomeOnwer() empty constructor
o should initialze the instance variables to the following
values:
name = NO NAME
addr = NO ADDRESS
void setName(String inName)
o This method will change the name instance variable to be
equal to the explicit parameter passed to the method.
String getName()
o This method will return the value of the name.
void setAddr(String inAddr)
o This method will change the addr instance variable to be
equal to the explicit parameter passed to the method.
String getAddr()
o This method will return the value of the address.

public class HomeOwner


{
private String name
private String addr
public HomeOwner()
{
this.name = "NO NAME";
this.addr = "NO ADDRESS";
}
public void setName(String inName)
{
this.name = inName;
}
public String getName()
{
return this.name;
}
public void setAddr (String inAddr)
{

10

this.addr = inAddr;
}
public String getAddr()
{
return this.addr;
}
}

11

Part 3: Driver (1 pt)


Write the HomeOwnerDriver class to do the following.
Create a main method
Use HomeOwner class written in Part 2
Create a HomeOwner object.
Change the value of the name to your name using the set
method.
Change the value of the address to your address using the set
method
Print the value of the name using the get method.
Print the value of the address using the get method.
public class HomeOwner
{
public static void main()
{
HomeOwner owner1 = new HomeOwner("Bill Gates", "777
Lucky Dr");
System.out.println(owner1);
// Changing name to me
HomeOwner owner2 = new HomeOwner();
owner2.setName("Matt Ginsberg");
// Printing new information
owner2.setAddr("7707 Hunt Drive");
System.out.println(owner2.getName());
System.out.println(owner2.getAddr());
}
}

12