ArrayList names = new ArrayList(); names.add(\"Ahmed\"); names.add(\"Khalid\");
ID: 3583304 • Letter: A
Question
ArrayList names = new ArrayList(); names.add("Ahmed"); names.add("Khalid"); names.add("Faris"); names.set(1, "Muhammad"); Consider the above code snippet. What is the output of each of the following: System.out.println(names.get(1)); System.out.println(names.size()); System.out.println(names.get(names.size()-1)); System.out.println(names.get(3)); Write a program that reads students information from the user. Your program must have 2 classes: Class named (Students) that have: Two instance variables: Student ID and Student name and (Choose a good type for each of them). Methods for creating a new student (constructor) >>return information from existing instance (getter methods). Runner class named as (StudentsRunner) that gets student information from the user and stores it. The program must give the user the ability to stop inserting new values. After the user finish inserting, the program prints the entire list (using for each loop). The following is a typical run of the program: Enter Student IDs123456 Enter Student nameAhmad The student info has been stored insert Q to quit t Enter Student IDs98765 Enter Student nameAbdulrahman The student info has been stored insert Q to quitq student #1 the student ID: s123456 the student name: Ahmad student #2 the student ID: s98765 the student name: AbdulrahmanExplanation / Answer
Q1) initial the arraylist names will be empty
Initial size of names will be '0'
names.add("Ahmed") implies the first element of names will be :Ahmed
names.add("Khalid")
names.add("Faris")
After these three statements the array list will contain the following elements
Elements index
Ahmed 0
Khalid 1
Faris 2
4th instruction names.set(1,"Muhammad") this will update the 2nd element i.e the element at the index 1
after executing this statement the names arraylist will be updated as
Elements index
Ahmed 0
Muhammad 1
Faris 2
Answers:
i)System.out.println(names.get(1)) get(int index) method will retrieve the value present at the specified index which is provided in the get method
output :Muhammad the value present at index 1 is muhammad
ii)System.out.println(names.size()) // size() method will return the number of elements present in the arraylist
output : 3
iii)System.out.println(names.get(names.size()-1))
names.get(names.size()-1) we have names.size() =3
names.get(3-1) = names.get(2) // which will return the value present at the index '2'
output:Faris
iv)System.out.println(names.get(3))
output:java.lang.IndexOutOfBoundsException since we have idex till 2 so we are trying to access the element which is not defined and no index of 3.
Question 2)
1) public class Students {
private int studentId; // variable for student id
private String studentName; // variable for student name
Students(int id, String name){ //constructor with 2 parameters id and name
student name and student id cant be null in case if we get any null values exception will be raised
this.studentId=id;
this.studentName=name;
}// end of constructor
}// end of class Students.
2) public class StudentsRunner{
public static void main(String[] args){
int id;
String name;
char forquit;
List<Students> students=new ArrayList<Students>();
while(forquit!= 'q' or forquit!='Q'){
System.out.println("Enter Student Id");
id=sc.nextInt();
sc.nextLine();
System.out.println("Enter Student Name");
name=sc.nextLine();
Students s= new Students(id,name);
s.getdata();
students.add(s);
System.out.println("The student info has been stored");
System.out.println("insert Q to quit);
forquit=sc.nextLine();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.