Module 4 Advance Java Programming
Exercise 1: Reflection, Annotation and Software Testing
Read carefully each item below. Each item below requires all programming concepts mentioned in the previous
discussions and demos. Follow Java Coding Standard seriously and follow all the needed requirements. All outputs must be
readable, presentable and clear. Not following instructions will result to series of deductions.
Note: Project name must be <LastName>Ex6 and the official package for SRC is org.alibata.training.codes and
TEST is org.alibata.training.codes.test.
Duration: 3 hours
A. Reflection
Create an ScrutinizeScan class that examines the methods and fields of your Scanner object.
Create a method viewSupplyMethods() to invoke methods on the said object. You should
properly display the parameter types, returned values or exceptions thrown.
Create a method viewSupplyConstructors that invoke constructors of an arbitrary class,
displaying any exceptions.
Create a method viewSupplyFields that invoke all type of fields in the said object.
B. Test Temperature Code
1. Refer to Module 2 Seatwork on Temperature. Use your code to do the following using Junit 4
Framework. (Use Junit 3.x)
a. We know that 0 degrees Celsius is 32 degrees Fahrenheit. Write a test that verifies that your
method computes the expected value.
b. We know that 100 degrees Celsius is 212 degrees Fahrenheit. Write a test that verifies that your
method computes the expected value.
c. If your tests reveal errors, correct the code. Then run the tests again.
d. Write any other tests you think are relevant. If the tests reveal errors, correct the code and then
run the tests again.
e. Add three more test cases using three more data.
C. Test Others Code
1. Based from your Module 1 Exercise time conversion, th following Time class is created by a team in
DOST ICT team. Copy this class in your own Eclipse project. (Use Junit 4.x)
import java.util.Calendar;
public class Time {
// Class constants (shared by all Time objects)
private static final int SECONDS_PER_MINUTE = 60;
private static final int MINUTES_PER_HOUR = 60;
private static final int SECONDS_PER_HOUR = 3600;
private static final int HOURS_PER_DAY = 24;
// Instance variables
private int hours;
private int minutes;
private int seconds;
public Time() {
Calendar now = Calendar.getInstance();
hours = now.get(Calendar.HOUR_OF_DAY);
minutes = now.get(Calendar.MINUTE);
seconds = now.get(Calendar.SECOND);
}
public Time(int h, int m, int s) {
Alibata Business and Technology Solutions Inc. Page 1
set(h, m, s);
}
public Time(String timeString) {
String[] parts = timeString.split(":");
if (parts.length < 2 || parts.length > 3) {
throw new IllegalArgumentException("ill-formed time string");
}
int h = Integer.parseInt(parts[0]);
int m = Integer.parseInt(parts[1]);
int s = Integer.parseInt(parts[2]);
set(h, m, s);
}
/**
* @return Hours component of this time
*/
public int getHours() {
return hours;
}
/**
* @return Minutes component of this time
*/
public int getMinutes() {
return minutes;
}
/**
* @return Seconds component of this time
*/
public int getSeconds() {
return seconds;
}
@Override public String toString() {
return String.format(
"%02d:%02d:%2d", getHours(), getMinutes(), getSeconds());
}
@Override public boolean equals(Object other) {
if (other == this) {
// 'other' is same object as this one!
return true;
}
else if (! (other instanceof Time)) {
// 'other' is not a Time object
return false;
}
else {
// Compare fields
Time otherTime = (Time) other;
return getHours() == otherTime.getHours()
&& getMinutes() == otherTime.getMinutes()
&& getSeconds() == otherTime.getSeconds();
}
}
@Override public int hashCode() {
// Limits on the values of hours, minutes and seconds allow us
// to use the inSeconds method as a perfect hash; see Josh Bloch's
// "Effective Java" for a more general hashCode recipe
return this.inSeconds();
Alibata Business and Technology Solutions Inc. Page 2
}
public int inSeconds() {
int hourSec = SECONDS_PER_HOUR * getHours();
int minSec = SECONDS_PER_MINUTE * getMinutes();
return hourSec + minSec + getSeconds();
}
public Time plus(int sec) {
if (sec < 0) {
throw new IllegalArgumentException("invalid number of seconds");
}
else {
int future = this.inSeconds() + sec;
int h = (future / SECONDS_PER_HOUR) % HOURS_PER_DAY;
int remainder = future % SECONDS_PER_HOUR;
int m = remainder / SECONDS_PER_MINUTE;
int s = remainder % SECONDS_PER_MINUTE;
return new Time(h, m, s);
}
}
private void set(int h, int m, int s) {
if (h < 0 || h > HOURS_PER_DAY) {
throw new IllegalArgumentException("hours out of range");
}
else if (m < 0 || m >= MINUTES_PER_HOUR) {
throw new IllegalArgumentException("minutes out of range");
}
else if (s < 0 || s >= SECONDS_PER_MINUTE) {
throw new IllegalArgumentException("seconds out of range");
}
else {
hours = h;
minutes = m;
seconds = s;
}
}
}
Do the following transactions:
Create a Test class TestTime that does the following:
a. Fixtures that implements morning, noon, afternoon and midnight Time
b. Test all the time objects in (a) if it really exhibits morning, noon, afternoon and midnight
respectively using its getters and Assert utilities.
c. Test what happens if time creation with invalid data results to Exception. Instantiate
another set of Time objects. Apply Exception testing.
d. Check the return value of each object by applying a separate test case for them. Use the
objects created in (a).
e. Create 5 more test cases but this time exhibiting the rainy day scenarios.
2. Below is a Java transaction that converts an ASCII string to the sum of its characters represented as a
Binary string i.e. adds up the values (ASCII) of strings, character by character, and show the sum as a
binary digit. (Use Junit 4.x)
Create a JUnit 4 test class that contains the following test scenarios:
evaluate total() method using 5 data sets in sunny day scenario
evaluate total() method using 5 data sets in rainy day scenario
evaluate binaries() method using 5 data sets in sunny day scenario
evaluate binaries() method using 5 data sets in rainy day scenario
create exception test case for a scenario that will result to exceptions
public class ChekcStr {
Alibata Business and Technology Solutions Inc. Page 3
public ChekcStr() {}
public String convert(String str) {
return binarise(total(str));
}
public int total(String str) {
if(str=="") return 0;
if(str.length()==1) return ((int)(str.charAt(0)));
return ((int)(str.charAt(0)))+total(str.substring(1));
}
public String binarise(int givenstrvalue) {
if(givenstrvalue==0) return "";
if(givenstrvalue %2==1) return "1"+binarise(givenstrvalue/2);
return "0"+binarise(givenstrvalue/2);
}
}
C. Test Others Code
Create a Junit 4.x TestSuite for C.
Alibata Business and Technology Solutions Inc. Page 4