In: Computer Science
Question 1.
// In method main
// In order to make the process of populating this array easier, we have the following local arrays defined:
String names[] = { "Smith", "Johnson", "Hill"};
String months[] = { "May", "August", "June"};
int days[] = { 12, 4, 28};
// SET2. You do this one! Declare an array of type Event_Type (that is defined in class SummerEvent) and initialize
// it so that the first element is constant SOCIAL, the second is the constant ATHLETIC and the third is the
// constant SOCIAL. Call this array types.
Question 2.
// In method main
// SET3. At this point in our program, we are ready to instantiate SummerEvent objects in our array events.
// Complete the following loop so that the first object in the array, is initialized by the first elements of the
// above locally declared arrays.
// For example we want the first Summer_Event object to be instantiated by the Event_Type of SOCIAL, the name of Smith,
// the month of May, and the day of 12; and the pattern continues for the other two remaining objects in the array.
for ( int i = 0; i < 3; i++ )
{
// Your object instantiation code goes here:
}
Question 3.
// In method main
// We are calling a method called displayEventInfo here. You will be asked to write the complete defintion of this
// method after main in this class.
// Use this method call as a guide in defining this method in full after main.
displayEventInfo(events);
} // end of main
// SET7. Provide the complete definition of method displayEventInfo here. You have seen this method called above in
// main and DIRECTLY.
// This method is a void method that receives an array of SummerEvent type, and it will have the code to invoke
// the toString method on each object it stores. The goal is to display the String representation of the objects on
// separate lines.
} // end of class SummerEventTest
Question 4.
Which describes the following statement BEST:
"In Java, we can use a variable to specify array dimensions, even the user can be prompted to enter the size of an array."
1. |
Using variables instead of literal values always helps program readability and debugging. |
|
2. |
This capability is only possible because in Java, arrays are objects, and objects are created at execution time. |
|
3. |
Variables ultimately represent values, so they are practically the same. |
|
4. |
None of the above. |
The array is a group of contiguous or related data items that share a common name.for instance ,we can an array nam name salary to represnt a set of salaries of a group of employees.A particular value is writing a number called index number or subscript in bracket after the array name.for example,
salary[10]
The complete set of values is referred to as an array individual values are called elements can be of any variable type.The ability to use a single name to represnt a collection of items and to refer to an item by specifying the number enables use to develop concise and efficient programs.for example, a loop with the control variable can be used to read th entire array.
1)One-dimensional arrays:
The items can be given one variable name using only one subscript and such a variable is called a subscripted variable or a one-dimensional array.
creating an array: Like any other variabes,arrays must be declared and created in the computer memory before theyare used,creation of an array involves three steps:
a) Declaring the array.
b)Creating memory locations.
c)Putting values into the memory locations.
Declaration of arrays:
Arrays in java may be declared in two forms:
form1:
type arrayname[ ];
form2:
type [ ] arrayname;
examples:
int number[ ];
float average[ ];
int[ ] countr;
float[ ] marks;
Initialization of arrays:
The finalstep is to put values into the array created.This process is known as initialization.This is done using the array subscripts as shown below.
arrayname[subscript]=value;
We can also initialize arrays automtically in the same way as the ordinary variables when they are declared,as shown below:
type arrayname[ ]={list of values};
example:
int number[ ]= { 23,53,78,90,36};
2)after declaring an array,we need to create it in the memory.java allows us to create arrays using new operator only,as shown below:
arrayname=new type[size];
examples:
number=new int[5];
average= new float[10];
These lines create necessary memory locations for the arrays number and average and designate them as int and float respectively.Now, the variable number refers to an array of 5 integers and average refers to an array of 10 floating point values.
In java ,all arrays store the allocated size in the variable named length .we can obtained length of the array a using a.length.
int aSIZE = a.length;
Loop may be used to initialize large size arrays,for example:
...............................
...............................
for(int i=0; i<100;i++)
{
if(i<50)
sum[i]=0.0;
else
sum[i]1.0;
}
.................
..................
the first fifty elements of the array sum are initialized to zero while the remaining are initialized to 1.0.
3) String manipulation is the most common part of many java prgrams.String represent a sentence of characters.The easiest way to represent a sequence of characters in java is by using a characterarray.
example:
char chararray[ ]=new char[4];
chararray[0] ='j';
chararray[1] ='a';
chararray[2] ='v';
chararray[3] ='a';
Stringa may be declared and created as follows:
String stringName;
stringName = new string ("string");
string arrays:
We can also create and use arrays that contain strings.the statement
string itemarray[ ]=new string[];
4)Java treats mutidimensional array as " arrays of arrays".it is possible to declare a two dimensional array as follows.
int x[ ][ ]=newint[3][ ];
x[0]=new int[2];
x[1]=new int[4];
x[]= new int[3];
For creating two dimensional arrays ,we must follows the same steps as that o simple arrays.we are create a two dimensional array like this:
int myarray[ ] [ ];
my array=new int[3][4];