简体   繁体   中英

Java vectors without generics

I have this question and I'm unsure on what exactly it is asking, i've posted the whole question but i dont expect the answer, just someone to point me in the right direction please!

Also the question says not to use generics.

Thanks in advance

Question:

In the following program, the code in the method CountHighs is missing. Write the code for this method, which takes as argument the vector m and returns the number of objects in the vector that are instances of HighRights

The method should also check that the elements extracted from the vector are indeed instances of the classes HighRights or LowRights. If an element is not an instance of such classes, then the method should return -1.

handle the NullPointerException in case the vector is null. Use the following code when catching the occurring exception:

System.out.println("Error");

Hint:

1 use m.size() to get the number of elements in the vector m

2 use the keyword instanceof to check if an object is an instance of a class)

For example, if the vector m contains only two HighRights objects and one LowRights objects then CountHighs(m) will return 2 if the vector m contains only two HighRights objects and one String objects then CountHighs(m) will return -1 if the vector m contains only five LowRights objects no HighRights objects then CountHighs(m) will return 0

import java.util.*;
@SuppressWarnings( "unchecked" ) public class Ex6 {

    public static void main(String[] a)
    {
       int i=Integer.parseInt(a[0]);
       if (i==0) {
           Vector ma = new Vector();
          ma.add(new HighRights("Jimmy"));
          ma.add(new HighRights("Jim"));
          ma.add(new HighRights("Mark"));
          ma.add(new HighRights("John"));
          ma.add(new LowRights("Lisa") );
           System.out.println(new Ex6().CountHighs(ma));
        }
         if (i==1) {
           Vector ma = new Vector();
          ma.add(new HighRights("Jimmy"));
          ma.add("I'm not Jimmy" );
           System.out.println(new Ex6().CountHighs(ma));
        }
        if (i==2)  System.out.println(new Ex6().CountHighs(null));
    }
    public static int CountHighs (Vector m) {

    }
}

The question is asking you to count the number of values of a specific type in a vector. you need to implement CountHighs to have the following behavior

  • if the provided list is NULL, then do System.out.println("Error");
  • if the list has any value other than HighRights or LowRights, return -1
  • otherwise return the amount of HighRights in the list

In your function, implement in this order. If null, do the printout, and return, you are done.

Next iterate through the vector. It suggests you use m.size() to define when to break out of your for or while loop.

hold on to a local int variable and increment it by 1 each time the value you are iterating on is of type 'HighRights' (you determine this using the instanceof keyword)

if during iteration you encounter an invalid value, return -1 immediately.

That should be pointing you in the right direction without writing you code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM