----------문제---------
Instructions
1. You are required to demonstrate your problem solving skills using code. You may use your favourite IDE anddevelopment tool to test your code.
2. You may code in Java, Groovy, C#, JavaScript, C++ or C. Your code should only use the API from the standardlibrary and should not rely on third-party libraries to function.
3. Your code should be written inside the templates below. You should not write any code that prompts for userinput.
4. If you are submitting your answers online, please save them in a text file and name it submission.txt.
Programming Questions
1. Write a method to add up all even numbers in a range of numbers. The result should include the lower boundand upper bound numbers.
Use the following template:
public int addEven(int lower, int upper) {
}
2. Write a method to return the average of an array of integers.
Use the following template:
public double average(int[] numbers) {
}
3. Write a method to calculate the difference between 2 date values in month. E.g. the difference between28/02/2011 and 01/03/2011 is 1 month; the difference between 28/02/2011 and 01/03/2012 is 13 months; thedifference between 01/05/2011 and 31/05/2011 is 0 month.
Use the following template:
public int differenceInMonth(Date fromDate, Date toDate) {
}
4. Write a method that generates a collection of random integers between 1 to 12 inclusive. The number of items toreturn is specified in the argument of the method. Use java.util.Randomto help you generate the randomnumbers. Use the following template:
public Collection<Integer> generateRandomNumbersBetween1And12(int count) {
}
------- 답 ---------
1. Write a method to add up all even numbers in a range of numbers. The result should include the lower boundand upper bound numbers.
Use the following template:
public int addEven(int lower, int upper) {
int result = 0;
int counter;
for ( counter = lower; counter <=upper; counter++){
if(counter %2 ==0)
result+=counter;
}
return result;
}
2. Write a method to return the average of an array of integers.
Use the following template:
public double average(int[] numbers) {
int counter;
int sum = 0;
double result;
for(counter =0;counter< numbers.length; counter++){
sum+= numbers[counter];
}
result = (double) sum / 2;
return result;
}
3. Write a method to calculate the difference between 2 date values in month. E.g. the difference between28/02/2011 and 01/03/2011 is 1 month; the difference between 28/02/2011 and 01/03/2012 is 13 months; thedifference between 01/05/2011 and 31/05/2011 is 0 month.
Use the following template:
public int differenceInMonth(Date fromDate, Date toDate) {
Calendar date1 = Calendar.getInstance();
date1.setTime(fromDate);
Calendar date2 = Calendar.getInstance();
date2.setTime(toDate);
int result =0;
//I have assumed that fromDate and toDate can be swapped.
//If they are guaranteed to be correctly ordered, only the first if is necessary
//else is not needed due to no difference means 0 difference and its already set at top.
if(date1.compareTo(date2)<0){
result += ( date2.get(Calendar.YEAR) - date1.get(Calendar.YEAR) ) * 12;
result += ( date2.get(Calendar.MONTH) - date1.get(Calendar.MONTH) );
}
else if(date1.compareTo(date2)>0){
result += ( date1.get(Calendar.YEAR) - date2.get(Calendar.YEAR) ) * 12;
result += ( date1.get(Calendar.MONTH) - date2.get(Calendar.MONTH) );
}
return result;
}
4. Write a method that generates a collection of random integers between 1 to 12 inclusive. The number of items toreturn is specified in the argument of the method. Use java.util.Randomto help you generate the randomnumbers. Use the following template:
public Collection<Integer> generateRandomNumbersBetween1And12(int count) {
ArrayList<Integer> result = new ArrayList<Integer>();
Random random = new Random();
int counter;
for (counter=0; counter < count; counter++){
result.add(random.nextInt(12)+1);
}
return result;
}
---------------------
으앙 설마 이대로 진짜화 되가는건가 ... ㅠㅠ..
만약 첫월급 타면 후원좀 쏠게요...므엉
만약 첫월급 타면 후원좀 쏠게요...므엉
댓글 12개 ▼