aFrom this coding import javautilArrayList public class Town

a)From this coding:

import java.util.ArrayList;

public class Town{

   public static void main (String[] args) {

       ArrayList<String> townList = new ArrayList<String>();
       townList.add(\"Kangar\");
       townList.add(\"Penang\");
       townList.add(\"Kuala Lumpur\");
       townList.add(\"Johor Bahru\");

       townList.size();

       System.out.print(\"Town List array : | \");

       for(int i=0; i < townList.size(); i++) {
           System.out.print(townList.get(i) + \" | \");
       }

       boolean retval = townList.contains(\"Shah Alam\");
       System.out.println();

           if (retval == true) {
              System.out.println(\"Element Shah Alam is contained in the list\");
          }

          else {
              System.out.println(\"Element Shah Alam is not contained in the list\");
          }

       townList.add(3, \"Shah Alam\");
       townList.remove(\"Kuala Lumpur\");
       townList.remove(0);

       int retval2 = townList.indexOf(\"Shah Alam\");
       System.out.println(\"The element Shah Alam is at index \" + retval2);

       townList.set(1, \"Muar\");

       boolean retval3 = townList.isEmpty();
          if (retval3 == true) {
              System.out.println(\"List is empty\");
          }

          else {
              System.out.println(\"List is not empty\");
            }
    }
}

1. Save the program as Town.java, compile and run the program. Give reason why youmay get the following warning. How to eliminate this warning?

Town.java uses unchecked or unsafe operation.
Recompile with -Xlint: unchecked for details.

2. Determine the output of the program.

3. Write a complete Java program that will do the same task as above, but this time use Vector class

Solution

As far as i am compiling program there was no such warning

(b) It gives following output

Town List array : | Kangar | Penang | Kuala Lumpur | Johor Bahru |
Element Shah Alam is not contained in the list
The element Shah Alam is at index 1
List is not empty

(c)

import java.util.Vector;

public class Town{

    public static void main (String[] args) {

        Vector<String> townList=new Vector<String>();
        townList.add(\"Kangar\");
        townList.add(\"Penang\");
        townList.add(\"Kuala Lumpur\");
        townList.add(\"Johor Bahru\");

        townList.size();

        System.out.print(\"Town List array : | \");

        for(int i=0; i < townList.size(); i++) {
            System.out.print(townList.get(i) + \" | \");
        }

        boolean retval = townList.contains(\"Shah Alam\");
        System.out.println();

            if (retval == true) {
                   System.out.println(\"Element Shah Alam is contained in the list\");
               }

               else {
                   System.out.println(\"Element Shah Alam is not contained in the list\");
               }

        townList.add(3, \"Shah Alam\");
        townList.remove(\"Kuala Lumpur\");
        townList.remove(0);

        int retval2 = townList.indexOf(\"Shah Alam\");
        System.out.println(\"The element Shah Alam is at index \" + retval2);

        townList.set(1, \"Muar\");

        boolean retval3 = townList.isEmpty();
               if (retval3 == true) {
                   System.out.println(\"List is empty\");
               }

               else {
                   System.out.println(\"List is not empty\");
               }
       }
}

a)From this coding: import java.util.ArrayList; public class Town{ public static void main (String[] args) { ArrayList<String> townList = new ArrayList<
a)From this coding: import java.util.ArrayList; public class Town{ public static void main (String[] args) { ArrayList<String> townList = new ArrayList<

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site