Homework
Homework
Chapter 7
Name: ___SOLUTION_____
Ex 7.1 [14] Which of the following are valid declarations? Which instantiate an array object? Explain your answers.
int primes = {2, 3, 4, 5, 7, 11}; //Not valid array declaration
float elapsedTimes[] = {11.47, 12.04, 11.72, 13.88}; //Not valid array declaration; loss of precision, array is float and literals are by default double. To fix use the explicit float modifier on the literals, i.e 11.47 should be 11.47f b/c 11.47 is double by default.
int[ ] scores = int[30]; //Not valid, does not instantiate the int via new
int [ ] primes = new {2,3,5,7,11}; //Not valid, mix of implicit and explicit array declaration
int[ ] scores = new int[30]; //Valid
char grades[ ] = {‘a’,’b’,’c’,’d’,’f’}; //Valid
char[ ] grades new char[ ]; //Not valid, does not specify the size of the array in new
Ex 7.4 [16]Write an array declaration and any necessary supporting classes to represent the following statements:
a. students’ names for a class of 25 students
String names[] = new String[25];
b. students’ test grades for a class of 40 students
double grades[] = new double [40];
c. credit-card transactions that contain a transaction number, a merchant name, and a charge
public class CreditCardTrans
{
//data members
long transNumber;
String merchantName;
float charge;
//Constructor
public CreditCardTrans(long tn, String mn, float ch){ … }
…
}
CreditCardTrans cc[] = {new CreditCardTrans(11234,”StoreXY”,34.45), new CreditCardTrans(3423341,”StoreABC”, 123.43)};
d. students’ names for a class and homework grades for each student
final int NUMSTUDENTS = 10; //number of students
final int NUMGRADES = 8;//each student will have 8 grades
String name = new String[10]; //10 students names
float grades[][] = new float[NUMSTUDENTS][NUMGRADES];
//first index refers to the student; it can be cross-referenced with name via the index, e.g. student 2 (index 3) name[3] her grades can be accessed by grades[3][i]
e. for each employee of the L&L International Corp: the employee number, hire date, and the amount of the last five raises
//Class Employee contains the employee number, hire date, and the amount of raises as data members.
Employee emp[MAX] = new Employee[MAX] // this will call default constructor so all data that will be held in array must be explicitly added
Ex 7.6 [10] Write code that prints the values stored in an array called names backwards.
public void backwards( String names[] )
{
for( int i= names.length, i>0, name--) //decrement over names array
System.out.println(names[i]);
}
Ex 7.8 [10] Write a method called sumArray that accepts an array of floating point values and returns the sum of the values stored in the array.
public double sumArray( double a[] )
{
double sum;
for( int i=0, i ................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- 3rd grade homework sheets printable
- homework why is it important
- answers to homework questions free
- english homework answers
- is homework helpful or harmful
- homework sheets 6th grade
- benefits of homework statistics
- printable 1st grade homework packets
- first grade summer homework packet
- kindergarten homework packet printable free
- preschool homework packets printables
- homework for 2 year old