import java.util.ArrayList; import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.net.URL; import java.net.MalformedURLException; import java.io.InputStream; import java.net.Socket; class Student { private void checkValidity( ) { if( pantherID.length( ) != 7 ) throw new IllegalArgumentException( "PantherID is 7 digits!" ); if( gpa < 0 ) throw new IllegalArgumentException( "GPA out of bounds" ); gpa = Math.min( gpa, 4.0 ); checkMajor( major ); } public Student( String p, String ln, String fn, String m, double g ) { pantherID = p; lastName = ln; firstName = fn; major = m; gpa = g; checkValidity( ); } /** * * @param studentInfo: in format ID, LN, FN, MAJOR, GPA */ public Student( String studentInfo ) { Scanner sc = new Scanner( studentInfo ); pantherID = sc.next( ); lastName = sc.next( ); firstName = sc.next( ); major = sc.next( ); gpa = sc.nextDouble( ); checkValidity( ); } public String getName( ) { return firstName + " " + lastName; } public String getPantherID( ) { return pantherID; } public double getGPA( ) { return gpa; } public String getMajor( ) { return major; } public void changeMajor( String newMajor ) { checkMajor( newMajor ); major = newMajor; } public boolean equals( Object other ) { if( other instanceof Student == false ) return false; Student rhs = (Student) other; return pantherID.equals( rhs.pantherID ); } public String toString( ) { return getName( ) + " " + "(" + pantherID + ")" + " majoring in " + major + " with " + gpa + " gpa"; } private String pantherID; private String lastName; private String firstName; private String major; private double gpa; private void checkMajor( String proposedMajor ) { for( String m : validMajors ) if( m.equals( proposedMajor ) ) return; // found a major throw new IllegalArgumentException( "Invalid major " + proposedMajor ); } public static final String [ ] validMajors = { "CS", "IT", "MIS" }; } class Day04 { public static void main( String [ ] args ) { /* * THIS PART WAS TESTING STUDENT WITH A FEW DIRECTLY CONSTRUCTED */ /* Student s1 = new Student( "1234567", "Doe", "Jane", "IT", 4.65 ); Student s2 = new Student( "1234556", "Doe", "John", "CS", 3.41 ); Student s3 = new Student( "1234556", "Doe", "John", "CS", 3.41 ); System.out.println( "s1: " + s1 ); System.out.println( "s2: " + s2 ); System.out.println( "s3: " + s3 ); System.out.println( s3.equals( s2 ) ); s3.changeMajor( "BIO" ); ArrayList arr = new ArrayList( ); arr.add( s1 ); arr.add( s2 ); System.out.println( arr.contains( s3 ) ); * */ /** * This part was reading from a file looking for * CS majors */ /* String fileName = "day04.txt"; File f = new File( fileName ); try { Scanner sc = new Scanner( f ); ArrayList arr = new ArrayList( ); while( sc.hasNextLine( ) ) arr.add( new Student( sc.nextLine( ) ) ); System.out.println( arr ); System.out.println( "CS Majors only: " ); for( Student s : arr ) if( s.getMajor( ).equals( "CS" ) ) System.out.println( s ); } catch( IOException e ) { System.out.println( "I/O error in file " + fileName ); } */ try { /* URL u = new URL( "http://www.cnn.com/" ); InputStream is = u.openStream( ); Scanner sc = new Scanner( is ); while( sc.hasNextLine( ) ) System.out.println( sc.nextLine( ) ); * */ Socket s = new Socket( "hpdrc.cs.fiu.edu", 13 ); InputStream is = s.getInputStream( ); Scanner sc = new Scanner( is ); while( sc.hasNextLine( ) ) System.out.println( sc.nextLine( ) ); } catch( IOException e ) { System.out.println( "Bad URL" ); } } }