ftoader
11-14-2005, 09:47 PM
Hi!
I have a couple of small pieces of codes that I wrote for my homework, and don't know how to double check them. Can you please take a look and let me know I am in the right track?
Thanks!
1. A method named arraySum that will receive the reference of an array of double values and return the sum of its elements as a double value:
public static double arraySum(double numbers) {
double total = 0;
for (int i = 0; i < numbers.size(); i++) {
total += numbers(i);
}
return total;
}
2. Assuming that the arraySum method is in a different class named MyClass and qtrlySales references an array of double values, fill-in the blank to make this statement display the sum of the elements in the qtrlySales array:
System.out.println("Yearly sales: " + MyClass.arraySum(qtrlySales.doubleValue));
3. Code the definition of two public static overloaded methods named grossPay. One method must receive an int value representing hours worked and a double value representing hourly rate. It is to return the product of the two arguments as a double. The other method must receive a double parameter representing yearly salary. It is to return the value of the argument divided by 26 as a double. Do NOT be concerned with checking the arguments for validity (negative numbers, etc.):
public static double grossPay(int hoursWorked, double hoursRate) {
return hoursWorked * hoursRate;
}
public static double grossPay(double yearSalary) {
return yearSalary / 26;
}
4. Assume that sb references a StringBuffer object. Declare a String reference named s and assign it the string of sb:
String s = sb.toString();
5. Code a single statement to display the square root of π (pi):
System.out.println(" Square root: " + Math.sqrt(Math.PI));
6. Assume that the addEmUp method will receive an ArrayList containing only Double objects. The purpose of the method is to calculate the sum of the values "wrapped" by those Double objects:
import java.util.*;
public class SomeClass {
public static double addEmUp(ArrayList list) {
double total = 0;
for (int i = 0; i < list.size(); i++) {
total += ((Double)list.get(i)).doubleValue();
}
return total;
}
}
I have a couple of small pieces of codes that I wrote for my homework, and don't know how to double check them. Can you please take a look and let me know I am in the right track?
Thanks!
1. A method named arraySum that will receive the reference of an array of double values and return the sum of its elements as a double value:
public static double arraySum(double numbers) {
double total = 0;
for (int i = 0; i < numbers.size(); i++) {
total += numbers(i);
}
return total;
}
2. Assuming that the arraySum method is in a different class named MyClass and qtrlySales references an array of double values, fill-in the blank to make this statement display the sum of the elements in the qtrlySales array:
System.out.println("Yearly sales: " + MyClass.arraySum(qtrlySales.doubleValue));
3. Code the definition of two public static overloaded methods named grossPay. One method must receive an int value representing hours worked and a double value representing hourly rate. It is to return the product of the two arguments as a double. The other method must receive a double parameter representing yearly salary. It is to return the value of the argument divided by 26 as a double. Do NOT be concerned with checking the arguments for validity (negative numbers, etc.):
public static double grossPay(int hoursWorked, double hoursRate) {
return hoursWorked * hoursRate;
}
public static double grossPay(double yearSalary) {
return yearSalary / 26;
}
4. Assume that sb references a StringBuffer object. Declare a String reference named s and assign it the string of sb:
String s = sb.toString();
5. Code a single statement to display the square root of π (pi):
System.out.println(" Square root: " + Math.sqrt(Math.PI));
6. Assume that the addEmUp method will receive an ArrayList containing only Double objects. The purpose of the method is to calculate the sum of the values "wrapped" by those Double objects:
import java.util.*;
public class SomeClass {
public static double addEmUp(ArrayList list) {
double total = 0;
for (int i = 0; i < list.size(); i++) {
total += ((Double)list.get(i)).doubleValue();
}
return total;
}
}