+
CMSC 350 HOMEWORK 1

CMSC 350 HOMEWORK 1

Rating:
Rating
(0)
Author: Joyce Buda
Description:

http://theperfecthomework.com/cmsc-350-homework-1/


Create a class that can be used to test data structure - similar to the StudentA.java example foundshown below:StudentA.javaimport java.util.Scanner;import java.util.ArrayList;class StudentA implements Comparable <StudentA> {static java.util.Random rn = new java.util.Random ();static ArrayList <String> firstNames = new ArrayList <>();static ArrayList <String> lastNames = new ArrayList <>();static SORTBY sortBy = SORTBY.LAST;static int nextUID = 1;
String first, last;double gpa = 0;int credits = 0;int uid = 0;
static {try {java.util.Scanner scFirst = new java.util.Scanner (newjava.io.File("firstNames.txt"));java.util.Scanner scLast = new java.util.Scanner (newjava.io.File("lastNames.txt"));while (scFirst.hasNext()) firstNames.add (scFirst.next());while ( scLast.hasNext()) lastNames.add ( scLast.next());}catch (java.io.FileNotFoundException e) {System.out.println (e);} // end try catch} // end static intializer
enum SORTBY {LAST, FIRST, CREDITS, GPA}
public StudentA (String st) {this (new Scanner (st)) }
public StudentA (Scanner sc) {uid = nextUID++;first = sc.next();last = sc.next();credits = sc.nextInt();gpa = sc.nextDouble();} // end Scanner constructor
public StudentA () {uid = nextUID++;} // no parameter constructor
public int compareTo (StudentA x) {switch (sortBy) {case LAST : return last.compareTo (x.last);case FIRST : return first.compareTo (x.first);case CREDITS: return credits - x.credits;case GPA : return (gpa > x.gpa)? 1 : -1;} // end switchreturn 0;} // end compareTo for Comparable interface
public String toString () {return String.format ("%5d %15s, %15s: %5d %10.2f", uid, last, first,credits, gpa);} // end method toString
public static StudentA [] makeRandom (int m) {StudentA [] sa = new StudentA [m];for (int i = 0; i < sa.length; i++) {sa[i] = new StudentA ();sa[i].first = firstNames.get (rn.nextInt (firstNames.size()));sa[i].last = lastNames.get (rn.nextInt ( lastNames.size()));sa[i].credits = rn.nextInt (120);sa[i].gpa = rn.nextDouble () * 4.0;} // end for each student to instantiatereturn sa;} // end method makeRanom


(more)
See More
Try a College Course Free

Sophia’s self-paced online courses are a great way to save time and money as you earn credits eligible for transfer to over 2,000 colleges and universities.*

Begin Free Trial
No credit card required

25 Sophia partners guarantee credit transfer.

221 Institutions have accepted or given pre-approval for credit transfer.

* The American Council on Education's College Credit Recommendation Service (ACE Credit®) has evaluated and recommended college credit for 20 of Sophia’s online courses. More than 2,000 colleges and universities consider ACE CREDIT recommendations in determining the applicability to their course and degree programs.

Tutorial