- Pleas complete the following survey
http://www.itd.depaul.edu/quickdata2/viewwebform.asp?id=4560
- Edit, Compile and Run the following piece of code.
class ArrayEx2 {
public static void main(String [] args) {
// using initializer list to create
// and initialize the array.
int [] data = {5,7,1,9,20};
/*
* data[0] contains 5
* data[1] contains 7
* data[2] contains 1
* data[3] contains 9
* data[4] contains 20
*
*/
printArray(data);
}
public static void printArray(int [] data){
for(int i = 0; i < data.length; i++)
System.out.print(data[i] + " ");
System.out.println();
}
}
- The following piece of code will search the array for the value 9 and print out wether 9 is in the array or not. Add it to the above
main
method.
boolean isNine = false;
for(int j = 0; j < data.length; j++){
if( data[j] == 9 ){
isNine = true;
}
}
if (isNine)
System.out.println("9 is in the list ");
else
System.out.println("9 is not in the list");
- Write the code that will replace all occurrences of the value 1 with 10. Do this in the
main
method
- Print out the array. Just call the function.
- Study the print function. Write a method that will take in an array of integers and return the first element in the array.
- Add the following methods to ArrayTool.java. Get ArrayTool.java from the course homepage under class 8.
- A method called smallestIndex, that takes as its parameter the array and returns the index of the (first occurrence of the)
smallest element in the array. Also test your method
- Write a method that inputs an integer array and swaps the first and last element of the array.
- Write a method that inputs an integer array and an integer. The method will return the number of times the integer appears in the array.
- Write a method called
isVowel
that will input a character and return true or false whether or not that character is a vowel.
- Write a program that prompts the user to input a sequence of characters and outputs the number of vowels. Use the method from question 1.
- Consider the following program segment.
class Ch7Ex3
{
public static void main(String [] args)
{
int num;
double dec;
}
public static int one(int x, int y)
{
}
public static double two(int x, double a)
{
int first;
double z;
}
}
- Write the definition of method one so that it returns the sum of x and y if x is greater than y; otherwise, it should return x minus 2 times y.
- Write the definition of method two as follows:
- Read a number and store it in z.
- Update the value of z by adding the value of parameter a to its previous value.
- Assign the variable first the value returned by method one with the parameters 6 and 8.
- Update the value of first by adding the value of x to its previous value.
- If the value of z is more than twice the value of first, return z; otherwise, return 2 times first minus z.
- Write a program that tests your methods