complete the code segment below using the following instructions: 1. readPoints
ID: 3770227 • Letter: C
Question
complete the code segment below using the following instructions:
1. readPoints goes through a Scanner and reads int value.
2. Using the int value, it create an ArrayList of Point objects. Loop while thereare int values in the Scanner (use hasNextInt). If the loop reads in an int. then, check again in another int. iif so, read it inand object.
3. Add the point to your ArrayList.
4. After the loop, return the ArrayList.
5.If the Scanner is null, return null.
public ArrayList<Point> readPoints(Scanner scan){
}
Explanation / Answer
code:
public class Point{
public int first;
public int second;
}
public ArrayList<Point> readPoints(Scanner scan){
if(scan==null){
return null;
}
else{
Point p;
ArrayList<Point> arrl =new ArrayList<Point>();
int x,y;
while(scan.hasNext()){
x=scan.nextInt();
if(scan.hasNext()){
y=scan.nextInt();
}
p.first=x;
p.second=y;
arrl.add(p);
}
}
return arrl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.