Posted by: iamunknown November 30, 2011
help with java assignment
Login in to Rate this Post:     0       ?        
 
 
import java.util.*;
import java.lang.StrictMath;
 
public class Generator
{
Random generator = new Random();
 
//Roster Components
public Roster getRoster()
{
Roster roster = new Roster();
 
ArrayList<Student> students = new ArrayList<Student>();
ArrayList<Course> courses = new ArrayList<Course>();
 
//Generate data
for(int i = 0; i < 100; i++)
students.add(getStudent());
for(int i = 0; i < 20; i++)
courses.add(getCourse());
 
roster.setCourses(courses); //Add in the courses
 
for(int i = 0; i < roster.getCourseCount(); i++) //Loop through the courses
{
int base = roster.getCourse(i).getEnrollmentMin();
int ceil = roster.getCourse(i).getEnrollmentMax()-base;
 
//Create an array between the min and max enrollment sizes
Student[] temp = new Student[generator.nextInt(ceil)+base];
 
for(int k = 0; k < temp.length;)
{
Student student = students.get(generator.nextInt(students.size())); //Grab a random student
if(roster.getCourse(i).isValidStudent(student)) //See if the student can be added
temp[k++] = student; //Add the student
}
 
roster.getCourse(i).setStudents(new ArrayList<Student>(Arrays.asList(temp)));
}
 
return roster;
}
 
//Student Components
public Student getStudent()
{
Student student = new Student();
student.setID(getStudentID(4));
student.setName(getStudentName());
student.setAddress(getStudentAddress());
student.setGraduate(getStudentGraduate());
return student;
}
 
public int getStudentID(int length)
{
//10^length gives me a number starting with one with 'n'-1 remaining 0's
//To create a base amount, I add this number to whatever the random number would be
//To stay within the length, I create a number with an extra 0 at the end, and subtract 1, giving me 'n' number of 9's
//I then subtract the base amount from this number to create my random range of (10000...n to 99999....n)
 
int base = (int)Math.pow(10,length);
int ceiling = (int)Math.pow(10,length+1)-1;
 
return generator.nextInt(ceiling-base)+base;
}
 
public String getStudentName()
{
return getStudentFirstName() + " " + getStudentMiddleName() + " " + getStudentLastName();
}
 
public String getStudentFirstName()
{
String[] first = {"Mohammed", "Ahmed", "Ali", "Ibrahim", "Abdallah", "Khaled", "Sarah", "Mona", "Omar", "Yousef", //10
"Adam", "David", "Eric", "Arthur", "Tobias", "Alexander", "Simon", "Jonas", "Sebastian", "Felix", //20
"Julian", "Ivan", "Nathan", "Lucas", "Thomas", "Louis", "Noah", "Hugo", "Tom", "Gabriel", //30
"Daniel", "Antonio", "Dominik", "Leon", "Oscar","Peter", "Michael", "Jack", "Harry", "Charlie", //40
"William", "Joshua", "George", "James", "Leo", "Robin", "Mathis", "Ethan", "Raphael", "Paul", //50
"Felix", "Ben", "Francesco", "Leonardo", "Nicola", "Gustav", "Robert", "Ryan", "Marian", "Sergey", //60
"Dimitry", "Ivan", "Lewis", "Logan", "Aaron", "Oliver", "Jamie", "Cameron", "Patrick", "Diego", //70
"Mario", "Alejandro", "Pablo", "Mary", "Eva", "Anna", "Hannah", "Sophie", "Julia", "Laura", //80
"Marie", "Alexandra", "Louise", "Clara", "Emma", "Ella", "Elise", "Camille", "Yasmine", "Sara", //90
"Gabriela", "Isabella", "Rebecca", "Lily", "Emily", "Amelia", "Jessica", "Ruby", "Chloe", "Grace", //100
"Daisy", "Alice", "Olivia", "Greta", "Viola", "Lisa", "Venessa", "Lea", "Lucy", "Katie", //110
"Katrina", "Diana", "Victoria", "Pedro", "Mateo", "Luiz", "Benjamin", "Carter", "Jacob", "Mathew", //120
"Alexis", "Justin", "Christopher", "Anthony", "Ashley", "Margaret", "Dorothy", "Elizabeth", "Linda", "Mary", //130
"Madison", "Patricia", "Barbara", "Susan", "Dorothy", "Kayla", "Vivian", "Nicole", "Destiny", "Sophia", //140
"Cooper", "Jackson", "Riley", "Ethan", "Max", "Tyler", "Austin", "Chad", "Christian", "Lloyd", //150
"Summer", "Charlotte", "Angelica", "Angela", "Jasmine", "Kimberly", "Angel", "Jared", "Jonathon", "Nikki"}; //160
 
return first[generator.nextInt(first.length)];
}
 
public String getStudentMiddleName()
{
String[] middle = {"Mohammed", "Ahmed", "Ali", "Ibrahim", "Abdallah", "Khaled", "Sarah", "Mona", "Omar", "Yousef", //10
"Adam", "David", "Eric", "Arthur", "Tobias", "Alexander", "Simon", "Jonas", "Sebastian", "Felix", //20
"Julian", "Ivan", "Nathan", "Lucas", "Thomas", "Louis", "Noah", "Hugo", "Tom", "Gabriel", //30
"Daniel", "Antonio", "Dominik", "Leon", "Oscar","Peter", "Michael", "Jack", "Harry", "Charlie", //40
"William", "Joshua", "George", "James", "Leo", "Robin", "Mathis", "Ethan", "Raphael", "Paul", //50
"Felix", "Ben", "Francesco", "Leonardo", "Nicola", "Gustav", "Robert", "Ryan", "Marian", "Sergey", //60
"Dimitry", "Ivan", "Lewis", "Logan", "Aaron", "Oliver", "Jamie", "Cameron", "Patrick", "Diego", //70
"Mario", "Alejandro", "Pablo", "Mary", "Eva", "Anna", "Hannah", "Sophie", "Julia", "Laura", //80
"Marie", "Alexandra", "Louise", "Clara", "Emma", "Ella", "Elise", "Camille", "Yasmine", "Sara", //90
"Gabriela", "Isabella", "Rebecca", "Lily", "Emily", "Amelia", "Jessica", "Ruby", "Chloe", "Grace", //100
"Daisy", "Alice", "Olivia", "Greta", "Viola", "Lisa", "Venessa", "Lea", "Lucy", "Katie", //110
"Katrina", "Diana", "Victoria", "Pedro", "Mateo", "Luiz", "Benjamin", "Carter", "Jacob", "Mathew", //120
"Alexis", "Justin", "Christopher", "Anthony", "Ashley", "Margaret", "Dorothy", "Elizabeth", "Linda", "Mary", //130
"Madison", "Patricia", "Barbara", "Susan", "Dorothy", "Kayla", "Vivian", "Nicole", "Destiny", "Sophia", //140
"Cooper", "Jackson", "Riley", "Ethan", "Max", "Tyler", "Austin", "Chad", "Christian", "Lloyd", //150
"Summer", "Charlotte", "Angelica", "Angela", "Jasmine", "Kimberly", "Angel", "Jared", "Jonathon", "Nikki"}; //160
 
return middle[generator.nextInt(middle.length)];
}
 
public String getStudentLastName()
{
String[] last = {"Smith", "Johnson", "Williams", "Brown", "Jones", "Miller", "Davis", "Garcia", "Rodriguez", "Wilson", //10
"Martinez", "Anderson", "Taylor", "Thomas", "Hernandez", "Moore", "Martin", "Jackson", "Thompson", "White", //20
"Lopez", "Lee", "Gonzalez", "Harris", "Clark", "Lewis", "Robinson", "Walker", "Perez", "Hall", //30
"Young", "Allen", "Sanchez", "Wright", "King", "Scott", "Green", "Baker", "Adams", "Nelson", //40
"Hill", "Ramirez", "Campbell", "Mitchell", "Roberts", "Carter", "Phillips", "Evans", "Turner", "Parker", //50
"Collins", "Edwards", "Stewart", "Flores", "Morris", "Nguyen", "Murphy", "Rivera", "Cook", "Rogers", //60
"Morgan", "Peterson", "Cooper", "Reed", "Bailey", "Bell", "Gomez", "Kelly", "Howard", "Ward", //70
"Cox", "Diaz", "Richardson", "Wood", "Watson", "Brooks", "Bennett", "Gray", "James", "Reyes", //80
"Cruz", "Hughes", "Price", "Myers", "Long", "Foster", "Sanders", "Ross", "Morales", "Powell", //90
"Sullivan", "Russel" ,"Ortiz", "Jenkins", "Gutierrez", "Perry", "Butler", "Barnes", "Fisher", "Henderson", //100
"Coleman", "Simmons", "Patterson", "Jordan", "Reynolds", "Hamilton", "Graham", "Kim", "Gonzales", "Alexander", //110
"Ramos", "Wallace", "Griffin", "West", "Cole", "Hayes", "Chaves", "Gibson", "Bryant", "Ellis", //120
"Stevens", "Murray", "Ford", "Marshall", "Owens", "McDonald", "Harrison", "Ruiz", "Kennedy", "Wells", //130
"Alvarez", "Woods", "Mendoza", "Castillo", "Olson", "Webb", "Washington", "Tucker", "Freeman", "Burns", //140
"Henry", "Vasquez", "Snyder", "Simpson", "Crawford", "Jimenez", "Porter", "Mason", "Shaw", "Gordon", //150
"Wagner", "Hunter", "Romero", "Hicks", "Dixon", "Hunt", "Palmer", "Robertson", "Black", "Holmes"}; //160
 
return last[generator.nextInt(last.length)];
}
 
public String getStudentAddress()
{
String[] direction = {"", " North", "" , " East", "", " South", "", " West"}; //8 (5)
 
String[] street = {"Main", "Church", "High", "Elm", "Chestnut", "Maple", "Center", "Broad", "Washington", "Walnut", //10
"1st", "2nd", "3rd", "4th", "5th", "Pine", "Water", "Park", "School", "Union", //20
"Oak", "River", "Prospect", "Spring", "Market", "Court", "Front", "Highland", "Cedar", "Spruce", //30
"Central", "Cherry", "State", "Bridge", "Franklin", "Ridge", "Lincoln", "Locust", "Pleasant", "Adams", //40
"Dogwood", "Mill", "Liberty", "Green", "Grove", "Hill", "Hillside", "Broadway", "Elizabeth", "Jefferson", //50
"Academy", "Hickory", "Holly", "Jackson", "Meadow", "Pearl", "Vine", "Arch", "New", "Valley"}; //60
 
String[] annex = {"Road", "Street", "Boulevard", "Lane", "Avenue", "Parkway", "Court", "Drive"}; //8
 
return getStudentID(3) + " " + direction[generator.nextInt(direction.length)] + " " + street[generator.nextInt(street.length)] + " " + annex[generator.nextInt(annex.length)];
}
 
public boolean getStudentGraduate()
{
return generator.nextBoolean();
}
 
//Course Components
public Course getCourse()
{
Course course = new Course();
course.setID(getCourseID());
course.setName(getCourseName());
course.setCreditHours(getCourseCreditHours(course));
course.setEnrollmentMin(getCourseEnrollmentMin());
course.setEnrollmentMax(getCourseEnrollmentMax());
course.setGraduateLevel(getCourseGraduateLevel(course));
 
return course;
}
 
public int getCourseID()
{
return (generator.nextInt(7)+1)*1000 + (generator.nextInt(5)+1)*100 + generator.nextInt(3)*10 + generator.nextInt(3);
}
 
public String getCourseName()
{
String[] name = {"AP", "ARTS", "ATEC", "BA", "BIOL", "BIS", "BMEN", "CE", "CGS", "CHEM", //10
"CRIM", "CRWT", "CS", "DANC", "DMTH", "DRAM", "ECON", "ECS", "ECSC", "ED", //20
"EE", "EMTH", "ENGR", "FILM", "FIN", "GEOG", "GEOS", "GOVT", "HIST", "MATH", //30
"MECH", "MILS", "MUSI", "NANO", "NSC", "PHIL", "PHYS", "PSCI" ,"PSY", "RHET", //40
"SCE", "SCI", "SE", "SOC", "STAT", "SYSM", "TE", "UNIV"}; //48
 
return name[generator.nextInt(name.length)];
}
 
public int getCourseCreditHours(Course course)
{
try
{
return Integer.parseInt((course.getID()+"").charAt(1)+"");
}
catch(Exception e)
{
return -1;
}
}
 
public int getCourseEnrollmentMin()
{
int[] min = {5,7,10}; //3
return min[generator.nextInt(min.length)];
}
 
public int getCourseEnrollmentMax()
{
int[] max = {15, 20, 25, 30, 35, 40, 45, 50, 55, 60}; //11
return max[generator.nextInt(max.length)];
}
 
public boolean getCourseGraduateLevel(Course course)
{
return (Integer.parseInt((course.getID()+"").charAt(0)+"")) > 4;
}
}
Read Full Discussion Thread for this article