Linked Questions

Popular Questions

so im writing an assignment for class. I have gotten stuck in my attempts to write a function to select a random element. The array that I am trying to access is stored in an interface file. I have one main file that implements the interface, and then multiple files extending the main file.

I am to write and test out how to select a random element from that interface file. The file contains many different arrays but for testing purposes, I will be selecting the lastName[]. How the function is written below is throwing an out of bounds error, but I don't know how to go about correcting it.

This is the test file:

import java.util.Random;

public class MemberTest {

  public static void main(String[] args) {

    Random rnd = new Random();
    int rndNum = rnd.nextInt();

    Member m = new Member();
    Student s = new Student();
    Staff ss = new Staff();
    Faculty f = new Faculty();
    Employee e = new Employee();
    Names n = new Member();


    System.out.println(n.lastName[rndNum]);

    System.out.println(m.toString(true));
  }
}

This is the file that implements Names.java

public class Member implements Comparable<Member>, Names{

public Member() {}

// randomly fill in all data member.
public void generate() {}

public String toString() {
    return toString(false);
}

public String toString(boolean ok) {
    return String.format("%s%3d-%2d-%4d %15s, %-15s", ok? "Mem " : "", ID / 1000000, ID / 10000
            % 100, ID % 10000, lastName, firstName );
}

//public int compareTo(Member) {}

public int compareTo(Member m) { 
    return ID - m.ID; 
}

public String htmlRow() {
    return String.format("<TR>%s</TR>", htmlColumns());
}

public String htmlColumns() {
    return String.format("<TD>%3d-%2d-%4d</TD> <TD>%15s,</TD> <TD>%-15s</TD>", ID / 1000000, ID / 10000
            % 100, ID % 10000, lastName, firstName );
} 

protected String firstName[], lastName[]; 
protected int ID;

}

This is the interface file:

public interface Names {

// Fields in interface are always and implicitly public, static and final.
// All methods in interface are implicitly public and abstract.

public static final String lastName [] = {
    "Abdalla", "Andres", "Anzai", "Armstrong", "Arvig", "Ash", "Baca", "Badine",
    "Baehr", "Bair", "Baligad", "Barlow", "Barrett", "Becker", "Bell",
    "Benbow", "Biggs", "Blevins", "Blood", "Bohan", "Bond", "Bonner",
    "Bosch", "Bryson", "Buechele", "Bullis", "Burk", "Burns", "Byron",
    "Calderon", "Careuthers", "Carr", "Carson", "Carter", "Castle", "Chan",
    "Charney", "Christenson", "Cisneros", "Cliton", "Coldewey", "Coodey",
    "Cook", "Cooper", "Coy", "Currie", "Curtis", "Dirkse", "Dirscoll",
    "Dodrill", "Ehlert", "Elias", "Elliott", "Enterline", "Ericsson",
    "Fernandez", "Fisher", "Flory", "Freese", "Fruzza", "Fu", "Fuhrman", "Gage",
    "Garcia", "Garmon", "Giffith", "Gill", "Gillen", "Glascock", "Gomez",
    "Goraya", "Greer", "Hansen", "Hartley", "Hawkins", "Hemme", "Hensley",
    "Hentges", "Herron", "Hightower", "Hines", "Holloway", "Holmes",
    "Issac", "Jackson", "Jagodin", "Jiang", "Johnson", "Jordan", "Jouda", "Joven",
    "Kalpesh", "Katz", "Kaur", "Kegley", "Kimsey", "King", "Kohn", "Kone",
    "LaCasse", "Lackey", "Lathrop", "Le", "Levy", "Lynos", "Maas", "Mackall",
    "Madsen", "Magee", "Maldonado", "McDaneld", "McMillian", "McNeill",
    "Meadors", "Medina", "Metz", "Millikin", "Minner", "Mondragon", "Monge", "Moore",
    "Moreno", "Murkland", "Musick", "Myers", "Neal", "Ngo", "Nolan", "Nunez",
    "O'Neil", "Oropeza", "Owen", "Patel", "Pfister", "Philips", "Picato",
    "Porras", "Porter", "Pratt", "Pryor", "Ragle", "Rai", "Raja", "Rill",
    "Roberts", "Roddy", "Rose", "Roux", "Rubio", "Ruiz", "Saito", "Samidin",
    "Sampson", "Sanchez", "Scanlan", "Schmidt", "Schoberg", "Sheppard",
    "Simmons", "Sison", "Smith", "Snow", "Soto", "Sozinho", "Stevens",
    "Stidham", "Strange", "Sy", "Tamayo", "Taylor", "Thomas", "Tran", "Vincent",
    "Walker", "Wandke", "Ward", "Warner", "Waugh", "Webb", "Wells", "White",
    "Wilson", "Winther", "Wuertz", "Yong" };

Related Questions