Question

In: Computer Science

Write a for-each loop that prints all of the Student objects in an ArrayList<student> object called...

Write a for-each loop that prints all of the Student objects in an ArrayList<student> object called roster. Write this piece in Java.

Write the correct code to execute.

Solutions

Expert Solution

Code in java:

//for-each loop that prints all of the Student objects in an
//ArrayList<student> object called roster.

import java.util.*;

//defining class student
class Student{
String name;
Student(String name){
this.name=name;
}
  
@Override
public String toString() {
return ("Student object Names:"+this.name); //prints student names
}
  
}

class Main{
public static void main(String args[]){

//Creating user-defined class objects
Student s1=new Student("charith");
Student s2=new Student("srikar");
Student s3=new Student("sai");
  
//creating arraylist
ArrayList<Student> roaster =new ArrayList<Student>();
roaster.add(s1);//adding Student class object
roaster.add(s2);
roaster.add(s3);
  
//for each loop :it iterates through each student object in roaster
//and calls respective toString method
roaster.forEach((n) ->System.out.println(n));
}
}

Screenshot code:

Output:


Related Solutions

You will generate a People class of object and load an ArrayList with person objects, then...
You will generate a People class of object and load an ArrayList with person objects, then report the contents of that ArrayList. To do so, you must perform the following: (30 pts.) A) (15/30 pts. (line break, 11 pt) ) - Create a class file “People.java” (which will generate People.class upon compile). People.java will have eight(8) methods to 1) read a .txt file ‘people.txt’ 2) generate: ▪ List of all students AND teachers ▪ List of all students OR teachers...
***Given a class called Student and a class called Course that contains an ArrayList of Student....
***Given a class called Student and a class called Course that contains an ArrayList of Student. Write a method called dropStudent() as described below. Refer to Student.java below to learn what methods are available.*** Course.java import java.util.*; import java.io.*; /****************************************************** * A list of students in a course *****************************************************/ public class Course{ /** collection of Students */ private ArrayList<Student> roster; /***************************************************** Constructor for objects of class Course *****************************************************/ public Course(){ roster = new ArrayList<Student>(); } /***************************************************** Remove student with the...
Checkpoint 8.1 Assume the variable name references a string. Write a for loop that prints each...
Checkpoint 8.1 Assume the variable name references a string. Write a for loop that prints each character letter in the string. The actual print statement is provided. # What goes here? print(letter) Checkpoint 8.11 Write an if statement using the in operator that determines whether 'd' is in my_string. A print statement that might be placed after the if statement is included. # What goes here? print('Yes, it is there.') Checkpoint 8.13 Write the first if statement to complete the...
Write a Python loop that goes through the list and prints each string where the string...
Write a Python loop that goes through the list and prints each string where the string length is three or more and the first and last characters of the strings are the same. Test your code on the following three versions of the list examples: examples = ['abab', 'xyz', 'aa', 'x', 'bcb'] examples = ['', 'x', 'xy', 'xyx', 'xx'] examples = ['aaa', 'be', 'abc', 'hello'].
Write a python code which prints triangle of stars using a loop ( for loop )...
Write a python code which prints triangle of stars using a loop ( for loop ) Remember what 5 * "*" does The number of lines of output should be determined by the user. For example, if the user enters 3, your output should be: * ** *** If the user enters 6, the output should be: * ** *** **** ***** ****** You do NOT need to check for valid input in this program. You may assume the user...
Objectives: Sort data objects Compare the CPU efficiency between the object array and ArrayList used in...
Objectives: Sort data objects Compare the CPU efficiency between the object array and ArrayList used in the sorting operation. Input file: Data.csv (file is to big;I cannot update, you can use any data file) there are less than 20,000 records in Data.csv Your program will skip/reject dirty data lines. For example, if a line contains 2 fields, it is considered as a dirty data line and will be rejected/skipped by your program. You are required to develop a Java program...
We have created an ArrayList of Person class. write a method called push that pushes all...
We have created an ArrayList of Person class. write a method called push that pushes all the people with the even length last name to the end of the ArrayList Content of the ArrayList before push [alex Bus, Mary Phillips, Nik Lambard, Rose Rodd, Esa khan, Jose Martinex, Nik Patte] content of the ArrayList after the push method [alex Bus, Nik Lambard, Nik Patte, Mary Phillips, Rose Rodd, Esa khan, Jose Martinex] import java.util.*; class Person { private String name;...
java please Write a program that creates an ArrayList and adds 5 circle objects to the...
java please Write a program that creates an ArrayList and adds 5 circle objects to the list , and display all elements in the list by invoking the object’s toString() method.
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance...
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance Variables Declare an ArrayList of BabyName objects. Constructor public BabyNamesDatabase () - instantiate the ArrayList of BabyName objects. You will not insert the items yet. This method will be one line of code. Mutator Methods public void readBabyNameData(String filename) - open the provided filename given as input parameter, use a loop to read all the data in the file, create a BabyName object for...
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date...
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date object, a ClockWithAudio object, a BMI object, a Day object, and a FigurePane object. Then display all elements in the list. Assume that all classes (i.e. Date, Account, etc.) have their own no-argument constructor.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT