*Java* 2. Suppose that you have the following vector objects: studentLastName =
ID: 3726532 • Letter: #
Question
*Java*
2. Suppose that you have the following vector objects:
studentLastName = ["Smith", "Shue", "Cox", "Jordan"]
studentFirstName = ["James", "Sherly", "Chris", "Eliot"]
a. Write a Java statement that outputs the first name of the third student
b. Write a Java statement that outputs the last name of the second student
c. Write a Java statement that outputs the first name followed by the last name separated by a single blank space of the first student
d. Write the necessary Java statements so that the first name and last name of Eliot Jordan are interchanged.
e. Write a Java statement that changes the names James Smith and Sherly Shue to Sherly Smith and James Shue
By changing the vector object studentLastName
By changing the vector studentFirstName
f. Write a for loop that prints all the students" names.
Explanation / Answer
a. Write a Java statement that outputs the first name of the third student
Answer:
System.out.println(studentFirstName[2]);
b. Write a Java statement that outputs the last name of the second student
Answer:
System.out.println(studentLastName[1]);
c. Write a Java statement that outputs the first name followed by the last name separated by a single blank space of the first student
Answer:
System.out.println(studentFirstName[0]+" "+studentLastName[0]);
d. Write the necessary Java statements so that the first name and last name of Eliot Jordan are interchanged.
Answer:
String temp = studentLastName[3];
studentLastName[3]= studentFirstName[3];
studentFirstName[3] = temp;
e. Write a Java statement that changes the names James Smith and Sherly Shue to Sherly Smith and James Shue
Answer:
By changing the vector object studentLastName
String temp = studentLastName[1];
studentLastName[1]= studentLastName[0];
studentLastName[0] = temp;
By changing the vector studentFirstName
String temp1 = studentFirstName[1];
studentFirstName[1]= studentFirstName[0];
studentFirstName[0] = temp1;
f. Write a for loop that prints all the students" names.
Answer:
for(int i =0;i<studentFirstName.length;i++) {
System.out.println(studentFirstName[i]+" "+studentLastName[i]);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.