Question
What output is produced by the following code? Explain how it works.
1.1 1 ...2..... 5 package taesik; import java.util.; /* Your name: Date: Answer: How it works: */ class student String name;
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Output:

Emily 0
Emily 1
Emily 2
Emily 3
Emily 4
Emily 5

Emily 1
Emily 3
Emily 5

Explanation:

  • ArrayList<student>myArray=new ArrayList<>();
           for(int i=0;i<6;i++)
           {
               student s = new student();
               s.name="Emily";
               s.age=i;
               myArray.add(s);
           }

This part of code will create an ArrayList names myArray which stores objects of type student. And the loop will create 6 student objects. In those objects, in all objects the name will be "Emily" and age will be 0,1,2,3,4,5 respectively. In the last part of this code, the created objects will be added into ArrayList myArray which is created before.

  • for(int i=0;i<myArray.size();i++)
           {
               System.out.format("%s %d %n",myArray.get(i).name,myArray.get(i).age);
           }

This Part of code will display names and ages in the objects which are stored in myArray.

i.e.,

Emily 0

Emily 1

Emily 2

Emily 3

Emily 4

  Emily 5

  • for(int i=0;i<myArray.size();i++)

       {
           myArray.remove(i);
           System.out.format("%s %d %n",myArray.get(i).name,myArray.get(i).age);
}

In this loop, first, it is removing the object that is stored at index respective to the value of i. And then it;s displaying the name and age that are stored in the ith object in ArrayList.

If we remove student object at 0th index, the objects after index 0 will be shifted left. That means object at index 1 will move to index 0. That's why we are only getting

Emily 1

Emily 3

Emily 5

Add a comment
Know the answer?
Add Answer to:
What output is produced by the following code? Explain how it works. 1.1 1 ...2..... 5...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello, How can I make the program print out "Invalid data!!" and nothing else if the...

    Hello, How can I make the program print out "Invalid data!!" and nothing else if the file has a grade that is not an A, B, C, D, E, or F or a percentage below zero? I need this done by tomorrow if possible please. The program should sort a file containing data about students like this for five columns: one for last name, one for first name, one for student ID, one for student grade percentage, one for student...

  • Please explain if the following code is actually correct. If the following code correct, please explain...

    Please explain if the following code is actually correct. If the following code correct, please explain why the code works and is also correct. Don’t use * Java’s Integer .toBinaryString(int) in this program./* According to the textbook and the instructor, I am not supposed to use arrays such as int binary[] = new int[25]; I guess this is the reason why this problem is starting to look kind of hard.   Chapter 5 Exercise 37: Java Programming * * (Decimal to...

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • How to build Java test class? I am supposed to create both a recipe class, and...

    How to build Java test class? I am supposed to create both a recipe class, and then a class tester to test the recipe class. Below is what I have for the recipe class, but I have no idea what/or how I am supposed to go about creating the test class. Am I supposed to somehow call the recipe class within the test class? if so, how? Thanks in advance! This is my recipe class: package steppingstone5_recipe; /** * *...

  • Provide comments for this code explaining what each line of code does import java.util.*; public class...

    Provide comments for this code explaining what each line of code does import java.util.*; public class Chpt8_Project {    public static void main(String[] args)       {        Scanner sc=new Scanner(System.in);        int [][]courses=new int [10][2];        for(int i=0;i<10;i++)        {            while(true)            {                System.out.print("Enter classes and graduation year for student "+(i+1)+":");                courses[i][0]=sc.nextInt();                courses[i][1]=sc.nextInt();                if(courses[i][0]>=1...

  • Can someone help me with my code.. I cant get an output. It says I do...

    Can someone help me with my code.. I cant get an output. It says I do not have a main method. HELP PLEASE. The instruction are in bold on the bottom of the code. package SteppingStones; //Denisse.Carbo import java.util.Scanner; import java.util.ArrayList; import java.util.List; public class SteppingStone5_Recipe { private String recipeName; private int servings; private List<String> recipeIngredients; private double totalRecipeCalories; public String getRecipeName() { return recipeName; } public void setRecipeName (string recipeName){ this.recipeName = recipeName; } public int getServings() { return...

  • I need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

  • I need to add a method high school student, I couldn't connect high school student to...

    I need to add a method high school student, I couldn't connect high school student to student please help!!! package chapter.pkg9; import java.util.ArrayList; import java.util.Scanner; public class Main { public static Student student; public static ArrayList<Student> students; public static HighSchoolStudent highStudent; public static void main(String[] args) { int choice; Scanner scanner = new Scanner(System.in); students = new ArrayList<>(); while (true) { displayMenu(); choice = scanner.nextInt(); switch (choice) { case 1: addCollegeStudent(); break; case 2: addHighSchoolStudent(); case 3: deleteStudent(); break; case...

  • I asked this a little bit ago but didn't clarify exactly what I needed, so I'm...

    I asked this a little bit ago but didn't clarify exactly what I needed, so I'm asking again...I've attached my code below the question. Thanks! Do not change the original contract of the Course class. Instead, ONLY change the implementation of property students, from an array to and ArrayList, and update any methods in class Course that access students to reflect ArrayList. The methods are overridden. That means that you do not need to change the header for methods, only...

  • So this is all I have done right now. Next steps: 1. Create a method called "printArray" which wo...

    So this is all I have done right now. Next steps: 1. Create a method called "printArray" which would put the code : for(int i = 0; i < myArray.length; i++) System.out.println(myArray[i]); at the bottom of that screen and put it into a method and then call it. 2. Create another method thats going to count all the numbers that are evenly divisible by 10 and call the method whatever you like. * Just make sure to return, pass at...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT