Question

In: Computer Science

Determine the Output: Package questions; import java.util.*; public class Quiz1 {       public static void main(String[] args)...

Determine the Output:

Package questions;

import java.util.*;

public class Quiz1 {

      public static void main(String[] args) {

            // TODO Auto-generated method stub

            System.out.println("*** 1 ***");

            ArrayList<Integer>list1=new ArrayList<Integer>();

            for(int i=0;i<30;i++)

            {

                  list1.add(new Integer(i));

            }

            System.out.print("[");

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

            {

                  System.out.print(list1.get(i)+" ");

            }

            System.out.println("]");

             

            System.out.println("*** 2 ***");

            ArrayList<Integer>list2=new ArrayList<Integer>();

            for(int i=30;i<60;i++)

            {

                  list2.add(i); //Auto Boxing an Integer not need to use new Integer

            }

            System.out.println(list2); //toString for an ArrayList --created by the Java Programmers

             

            System.out.println("*** 3 ***");

            ArrayList<Integer>list3=new ArrayList<Integer>();

            list3.add(list1.remove(22)); //when an ArrayList removes an object it returns the object

            list3.add(list1.remove(0));

            list3.add(list1.remove(13));

            list3.add(list1.remove(7));

            list3.add(list1.remove(7));

            list3.add(list1.remove(13)); //remember as an ArrayList removes the size is reduced

            list3.add(list1.remove(11));

            list3.add(list1.remove(list1.size()-1));

            System.out.println(list3);

             

            System.out.println("*** 4 ***");

            System.out.println(list1);

             

            System.out.println("*** 5 ***");

            Collections.sort(list3);

            System.out.println(list3);

             

            System.out.println("*** 6 ***");

            System.out.print("{ ");

            for(int item:list3) //This is called unwrapping an Integer into an int

            {

                  if(item<10)

                        System.out.print("0");

                  System.out.print(item+" ");

            }

            System.out.println("}");

       

                         

      }//end main

}//end class

/*

OUTPUT

*** 1 ***

*** 2 ***

*** 3 ***

*** 4 ***

*** 5 ***

*** 6 ***

*/

Solutions

Expert Solution

Package questions;
import java.util.*;

public class Quiz1{

      public static void main(String[] args) {

            // TODO Auto-generated method stub

            System.out.println("*** 1 ***");

            //to create an ArrayList that will hold the integer  data
            ArrayList<Integer>list1=new ArrayList<Integer>();

            for(int i=0;i<30;i++)

            {

                  list1.add(new Integer(i));//Here we are adding data from 0 to 29 in list1

            }

            System.out.print("[");
            //loop over the list1 
            for(int i=0;i<list1.size();i++)

            {

                  System.out.print(list1.get(i)+" ");//Here we are printing the data of list1

            }

            System.out.println("]");

             

            System.out.println("*** 2 ***");

            ArrayList<Integer>list2=new ArrayList<Integer>();

            for(int i=30;i<60;i++)

            {

                  //Here we are adding data from 30 to 59 in list2
                  list2.add(i); //Auto Boxing an Integer not need to use new Integer

            }
            //here we are printing the data of list2,internally it will call toString() method to print the output in [data1,data2,data3....] format
            System.out.println(list2); //toString for an ArrayList --created by the Java Programmers

             

            System.out.println("*** 3 ***");

            ArrayList<Integer>list3=new ArrayList<Integer>();

            //here when we remove data at given index of list1,so after removing data it will shift the indexes to the left
            list3.add(list1.remove(22)); //when an ArrayList removes an object it returns the object

            list3.add(list1.remove(0));

            list3.add(list1.remove(13));

            list3.add(list1.remove(7));

            list3.add(list1.remove(7));

            list3.add(list1.remove(13)); //remember as an ArrayList removes the size is reduced

            list3.add(list1.remove(11));

            list3.add(list1.remove(list1.size()-1));
            
            //here we are printing list3 data
            System.out.println(list3);

             

            System.out.println("*** 4 ***");
            
            //here we are printing the list1 after performing various removing operations
            System.out.println(list1);

             

            System.out.println("*** 5 ***");

            //here we are sorting the data of list3 using Collections.sort() method
            Collections.sort(list3);
            
            //here we are printing list3 data
            System.out.println(list3);


            System.out.println("*** 6 ***");

            System.out.print("{ ");

            //here we are traversing each data of list3 using for-each loop
            for(int item:list3) //This is called unwrapping an Integer into an int

            {

                  if(item<10)

                        System.out.print("0");

                  System.out.print(item+" ");

            }

            System.out.println("}");
      }//end main
}//end class

The Output of this code along with step by step explainations:

Output:

*** 1 ***                                                                                                                                                                  

[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ]                                                                                         

*** 2 ***                                                                                                                                                                  

[30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59]                                                   

*** 3 ***                                                                                                                                                                  

[22, 0, 14, 8, 9, 17, 15, 29]                                                                                                                                              

*** 4 ***                                                                                                                                                                  

[1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 16, 18, 19, 20, 21, 23, 24, 25, 26, 27, 28]                                                                                          

*** 5 ***                                                                                                                                                                  

[0, 8, 9, 14, 15, 17, 22, 29]                                                                                                                                              

*** 6 ***                                                                                                                                                                  

{ 00 08 09 14 15 17 22 29 }  

Step by Step Explainations:

*** 1 ***        

Here we are just printing the data of list1 by using a for loop,here we have formatted output as "[" and then print the data seperated by space and at the end we have add "]"

So the output will be :

[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ]

*** 2 ***       

Here we are just printing the data of list2 without using a for loop, here we just write System.out.println(list2), so internally it will call toString() method and the output will be printed as :    [30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59]

*** 3 ***      

This is a bit tricky part here we are removing various data from list1 and adding appropriate data in list3 as,

list3.add(list1.remove(22));

list3.add(list1.remove(0));

list3.add(list1.remove(13));

list3.add(list1.remove(7));

list3.add(list1.remove(7));

list3.add(list1.remove(13));

list3.add(list1.remove(11));

list3.add(list1.remove(list1.size()-1));

Here the output will be

[22, 0, 14, 8, 9, 17, 15, 29]  

Note: Whenever we remove data from a particular valid index in ArrayList then the indexes will be shifted in left, for example when we remove 22 from list1, first it will remove 22 from list1 and then shift the indexes to the left by 1 and finally it will return the data that has been removed that is 22 and we have added 22 in list3.

In this way,the list3 [22, 0, 14, 8, 9, 17, 15, 29] is formed.

*** 4 ***  

Here we have printed the updated list1(after performing various removal operations on it) as:

[1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 16, 18, 19, 20, 21, 23, 24, 25, 26, 27, 28]

Note:we can clearly see that all the data which is present in list3 that is [22, 0, 14, 8, 9, 17, 15, 29], will not be present in list1 that is [1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 16, 18, 19, 20, 21, 23, 24, 25, 26, 27, 28] as we have removed those data from list1

*** 5 ***         

Here simply we have sorted the list3 [22, 0, 14, 8, 9, 17, 15, 29] by applying Collections.sort() on list3 and printed as:

[0, 8, 9, 14, 15, 17, 22, 29]

*** 6 ***            

Here we have printing the data of list3 (after sorting it) in some condition that if the data is less then 10 then we just add a 0 in beginning and if it is greater then 10 we simply print it as:

{ 00 08 09 14 15 17 22 29 }  

So in this way all the output of given program will be printed,If you are having any doubts regarding any thing in code or anything else that is not being understood,you please feel free to ask me in comment section,I will be more then happy to guide.

Screeshot of code:

  


Related Solutions

// problem2.java import java.util.*; public class problem_a { public static void main(String[] args) { // test...
// problem2.java import java.util.*; public class problem_a { public static void main(String[] args) { // test the smallest method System.out.print("smallest(1, 0, 2) -> "); System.out.println( smallest(1, 0, 2) ); // test the average method System.out.print("average(95, 85, 90) -> "); System.out.println( average(95, 84, 90) ); } // end main /* * smallest(double, double, double) -> double * * method is given 3 numbers, produces the smallest of the three * * examples: * smallest(1, 0, 2) -> 0.0 */ public static...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        } System.out.println(""+getAvg(new_stack));    }     public static int getAvg(Stack s) {        //TODO: Find the average of the elements in the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int result = 0; System.out.print("Enter the first number: "); int x = input.nextInt(); System.out.print("Enter the second number: "); int y = input.nextInt(); System.out.println("operation type for + = 0"); System.out.println("operation type for - = 1"); System.out.println("operation type for * = 2"); System.out.print("Enter the operation type: "); int z = input.nextInt(); if(z==0){ result = x + y; System.out.println("The result is " + result); }else...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) {...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,1,2}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: " + WordsCharsLetters[1]); System.out.println("Letters: " + WordsCharsLetters[2]); } static int[] calculateAndPrintChars(String sentence) { int[] WCL = new int[3]; String[] sentenceArray=sentence.split(" "); WCL[0] = sentenceArray.length; int letterCount=0; for(int i=0;i<sentence.length();i++) { if(Character.isLetter(sentence.charAt(i))) letterCount++; } WCL[1]...
package datastructure; public class UseQueue { public static void main(String[] args) { /* * Demonstrate how...
package datastructure; public class UseQueue { public static void main(String[] args) { /* * Demonstrate how to use Queue that includes add,peek,remove,pool elements. * Use For Each loop and while loop with Iterator to retrieve data. * */ } }
package datastructure; public class UseMap { public static void main(String[] args) { /* * Demonstrate how...
package datastructure; public class UseMap { public static void main(String[] args) { /* * Demonstrate how to use Map that includes storing and retrieving elements. * Add List<String> into a Map. Like, Map<String, List<string>> list = new HashMap<String, List<String>>(); * Use For Each loop and while loop with Iterator to retrieve data. * * Use any databases[MongoDB, Oracle, MySql] to store data and retrieve data. */ } }
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {   ...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {    Scanner console = new Scanner(System.in);    double radius; double height; System.out.println("This program can calculate "+ "the area of a rectangle, the area "+ "of a circle, or volume of a cylinder."); System.out.println("To run the program enter: "); System.out.println("1: To find the area of rectangle."); System.out.println("2: To find the area of a circle."); System.out.println("3: To find the volume of a cylinder."); System.out.println("-1: To terminate the...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) {...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) { int N; Scanner keybd = new Scanner(System.in); int[] counts = new int[12];    System.out.print("Enter the number of trials: "); N = keybd.nextInt();    Random die1 = new Random(); Random die2 = new Random(); int value1, value2, sum; for(int i = 1; i <= N; i++) { value1 = die1.nextInt(6) + 1; value2 = die2.nextInt(6) + 1; sum = value1 + value2; counts[sum-1]++; }   ...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT