W04Assignment 11-2014

13
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); } 1

description

week4assignment

Transcript of W04Assignment 11-2014

Page 1: W04Assignment 11-2014

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);

}

1

Page 2: W04Assignment 11-2014

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;

}

2

Page 3: W04Assignment 11-2014

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 Date(int inMonth, int inDay, int inFullYear)public int getMonth()public int getDay()public 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.defaultValuereturn total;

3

Page 4: W04Assignment 11-2014

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());

4

Page 5: W04Assignment 11-2014

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());

5

Page 6: W04Assignment 11-2014

c. public String formatTime() Format would be hh:mm:ss

public void formatTime (int inHour, int inMinute, int inSecond){

this.hour = inHour;this.minute = inMinute;this.second = 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 changeTime (int inHH, int inMM, int inSS){this.hour = inHH;this.minute = inMM;this.second = 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());

6

Page 7: W04Assignment 11-2014

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 areaw) }

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

Page 8: W04Assignment 11-2014

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” (doesn’t exist).

Variable Name

Instance or Local

tmpBox setHeight setLength computedArea

Value Local Local Instance Instancea De ud ud udb De ud ud Udc De ud ud Udd De ud ud Ude De ud ud Udf De ud ud Udg De Udh De Udi De Udj De Udk Del Dem Den Deo Dep Deq Der Des Det Deu Dev Dew De

I just dont get this box - hopefully you can shed some light when you send me this back.

Problem 6 (5 points total)

Part 1: Test Class(2 points)

8

Page 9: W04Assignment 11-2014

Write the TestHomeOwner class to test the following method for the HomeOwner class.

HomeOnwer() empty constructoro 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 namename1.setName("Bill Gates Jr");assertEquals(Bill Gates Jr, owner1.getName);

}}

Part 2: Class(2 points)

9

Page 10: W04Assignment 11-2014

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 nameprivate 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)

{this.addr = inAddr;

}public String getAddr()

{return this.addr;

} }

10

Page 11: W04Assignment 11-2014

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 meHomeOwner owner2 = new HomeOwner();owner2.setName("Matt Ginsberg");// Printing new informationowner2.setAddr("7707 Hunt Drive");System.out.println(owner2.getName());System.out.println(owner2.getAddr());

}

}

11