Do these steps on paper first and then enter into a text file to submit 1. Write
ID: 3926529 • Letter: D
Question
Do these steps on paper first and then enter into a text file to submit
1. Write a declaration for an array which can contain 5 integers.
2. Write a statement that puts the value 100 into the 3rd cell.
3. Write a statement that both declares an array of single characters and initializes the cells to these values: ‘a’, ‘b’, ‘c’.
4. Write a code segment that declares an array of 5 Strings, and then uses a loop to insert 5 String values into the array. Now you will start writing code. Create a project called Lab7 in Eclipse.
5. Create a class called StringListFun. In your main method, declare and initialize an array that holds String values, and initialize it with "apple", "avocado", "eggplant", "carrot", "chard", "tomato". Write a loop that prints out the contents of the array.
6. Add a loop that prints out the contents of the array in reverse.
7. Add a loop that prints out the values at even positions in the array.
8. Declare a second array of String named list2. Write a loop that copies the values in your array into list2. Then write another loop that prints out list2.
Explanation / Answer
Hi, Please find my code.Please let me know in case of any issue:
1.
int[] arr = new int[5];
2.
arr[2] = 100; (array index start from 0)
3.
char[] charArr = {'a', 'b', 'c'};
4 ########## Lab7.java ##########
import java.util.Scanner;
public class Lab7 {
public static void main(String[] args) {
// Scanner Object to take user input
Scanner sc =new Scanner(System.in);
// declaring a String array of size 5
String[] strArr = new String[5];
// for loop to take user input
for(int i=0; i<5; i++){
System.out.print("Enter a string: ");
strArr[i] = sc.next(); // taking user input and storing in strArr at ith index
}
}
}
/*
Sample output:
Enter a string: apple
Enter a string: orabge
Enter a string: cat
Enter a string: dog
Enter a string: rule
*/
5, 6,7,8: StringListFun.java
public class StringListFun {
public static void main(String[] args) {
// declaring String array and initiazing
String[] strArr1 = {"apple", "avocado", "eggplant", "carrot", "chard", "tomato"};
// getting length of strArr1
int length = strArr1.length;
// printing content of strArr1
for(int i=0; i < length; i++){
System.out.print(strArr1[i]+" ");
}
System.out.println();
// printing content in reverse order
for(int i=length-1; i>=0; i--){
System.out.print(strArr1[i]+" ");
}
System.out.println();
// printing content at even position
for(int i=1; i < length; i=i+2){ // array index starts from 0, so elements at index 1,actually
// it is 2nd element of array, so start printing from 1 and increment by 2
System.out.print(strArr1[i]+" ");
}
System.out.println();
// declaring other array list1
String[] list2 = new String[length];
// copying content of strArr1 inot list1
for(int i=0; i<length; i++){
list2[i] = strArr1[i];
}
// printing list1
for(int i=0; i<length; i++){
System.out.print(list2[i]+" ");
}
System.out.println();
}
}
/*
Sample Output:
apple avocado eggplant carrot chard tomato
tomato chard carrot eggplant avocado apple
avocado carrot tomato
apple avocado eggplant carrot chard tomato
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.