diff --git a/tps/gabaritos/tp02/TP02Q01 - Classe em Java/Player.java b/tps/gabaritos/tp02/TP02Q01 - Classe em Java/Player.java new file mode 100644 index 0000000..4b638ae --- /dev/null +++ b/tps/gabaritos/tp02/TP02Q01 - Classe em Java/Player.java @@ -0,0 +1,209 @@ +/** + * @path TP02Q01 - Classe em Java/Player.java + * @description Java class of a player from the NBA database + * @author Pedro Lopes - github.com/httpspedroh + * @version 1.0 + * @update 2023-09-27 + */ + +// ---------------------------------------------------------------------------------------------------- // + +// Imports +import java.util.Scanner; +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; + +// ---------------------------------------------------------------------------------------------------- // + +public class Player { + + // Global variables + public static final String FILE_PATH = "/tmp/players.csv"; + public static ArrayList allPlayers = new ArrayList(); + + // -------------------------- // + + // Attributes + private int id; + private String name; + private int height; + private int weight; + private String college; + private int yearOfBirth; + private String birthCity; + private String birthState; + + // Empty constructor + public Player() { + + this.id = 0; + this.name = ""; + this.height = 0; + this.weight = 0; + this.college = ""; + this.yearOfBirth = 0; + this.birthCity = ""; + this.birthState = ""; + } + + // Constructor + public Player(int id, String name, int height, int weight, String college, int yearOfBirth, String birthCity, String birthState) { + + this.id = id; + this.name = name; + this.height = height; + this.weight = weight; + this.college = college; + this.yearOfBirth = yearOfBirth; + this.birthCity = birthCity; + this.birthState = birthState; + } + + // Gets + public int getId() { return this.id; } + public String getName() { return this.name; } + public int getHeight() { return this.height; } + public int getWeight() { return this.weight; } + public String getCollege() { return this.college; } + public int getYearOfBirth() { return this.yearOfBirth; } + public String getBirthCity() { return this.birthCity; } + public String getBirthState() { return this.birthState; } + + // Sets + public void setId(int id) { this.id = id; } + public void setName(String name) { this.name = name; } + public void setHeight(int height) { this.height = height; } + public void setWeight(int weight) { this.weight = weight; } + public void setCollege(String college) { this.college = college; } + public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; } + public void setBirthCity(String birthCity) { this.birthCity = birthCity; } + public void setBirthState(String birthState) { this.birthState = birthState; } + + // Clone + public Player clone() { return new Player(this.id, this.name, this.height, this.weight, this.college, this.yearOfBirth, this.birthCity, this.birthState); } + + // Print + public void print() { + + System.out.printf("[%d ## %s ## %d ## %d ## %d ## %s ## %s ## %s]\n", + this.id, this.name, this.height, this.weight, this.yearOfBirth, this.college, this.birthCity, this.birthState); + } + + // Read + public void read(String line) { + + // Split line by "," + String[] splitted = line.split(",", -1); + + // Fill empty attributes + for(int i = 0; i < splitted.length; i++) { + + if(splitted[i].equals("")) splitted[i] = "nao informado"; + } + + // Set attributes + this.id = Integer.parseInt(splitted[0]); + this.name = splitted[1]; + this.height = Integer.parseInt(splitted[2]); + this.weight = Integer.parseInt(splitted[3]); + this.college = splitted[4]; + this.yearOfBirth = Integer.parseInt(splitted[5]); + this.birthCity = splitted[6]; + this.birthState = splitted[7]; + } + + // ---------------------------------------------------------------------------------------------------- // + + // Read all players function + public static void startPlayers() { + + // Initialize variables + try { + + FileInputStream fstream = new FileInputStream(FILE_PATH); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + + // ---------------------- // + + // Explode CSV file + String line = br.readLine(); + + while((line = br.readLine()) != null) { + + // Initialize player + Player player = new Player(); + + // Read line + player.read(line); + + // Add player to array + allPlayers.add(player); + } + + // Close CSV file + fstream.close(); + } + catch(IOException e) { e.printStackTrace(); } + } + + // ---------------------------------------------------------------------------------------------------- // + + // Search by id function + public static Player searchById(int id, ArrayList players) { + + // Search for player + for(int i = 0; i < players.size(); i++) { + + if(players.get(i).getId() == id) return players.get(i); + } + return null; + } + + // ---------------------------------------------------------------------------------------------------- // + + public static void main(String[] args) { + + // ----------------------------------------------------------------- // + + // #1 - Start - Read all players in CSV file + startPlayers(); + + // ----------------------------------------------------------------- // + + // #2 - Read input and print players from pub.in id entries + + // Initialize scanner + Scanner inScanner = new Scanner(System.in); + + // Initialize player + Player player = new Player(); + + // Read first line + String line = inScanner.nextLine(); + + // While line is not "FIM" + while(!line.equals("FIM")) { + + // Get id + int id = Integer.parseInt(line); + + // Search for player + player = searchById(id, allPlayers); + + // Print player + if(player != null) player.print(); + else System.out.println("x Player not found!"); + + // Read line + line = inScanner.nextLine(); + } + + // Close scanner + inScanner.close(); + } +} + +// ---------------------------------------------------------------------------------------------------- // \ No newline at end of file diff --git a/tps/gabaritos/tp02/TP02Q01 - Classe em Java/pedro.out b/tps/gabaritos/tp02/TP02Q01 - Classe em Java/pedro.out new file mode 100644 index 0000000..3b0b6fc --- /dev/null +++ b/tps/gabaritos/tp02/TP02Q01 - Classe em Java/pedro.out @@ -0,0 +1,40 @@ +[106 ## Walt Lautenbach ## 188 ## 83 ## 1922 ## University of Wisconsin ## nao informado ## nao informado] +[1228 ## Willie Wise ## 196 ## 95 ## 1947 ## Drake University ## San Francisco ## California] +[1307 ## James Hardy ## 203 ## 99 ## 1956 ## University of San Francisco ## Knoxville ## Alabama] +[1397 ## Joe Barry ## 193 ## 88 ## 1969 ## Georgia Institute of Technology ## Oakland ## California] +[1412 ## Darrell Griffith ## 193 ## 86 ## 1958 ## University of Louisville ## Louisville ## Kentucky] +[1554 ## Clark Kellogg ## 201 ## 102 ## 1961 ## Ohio State University ## Cleveland ## Ohio] +[1585 ## Trent Tucker ## 196 ## 87 ## 1959 ## University of Minnesota ## Tarboro ## North Carolina] +[1669 ## Stuart Gray ## 213 ## 106 ## 1963 ## University of California - Los Angeles ## Panama Canal Zone ## Panama] +[1750 ## Terry Porter ## 190 ## 88 ## 1963 ## University of Wisconsin-Stevens Point ## Milwaukee ## Wisconsin] +[1928 ## Andrew Lang ## 211 ## 111 ## 1966 ## University of Arkansas ## Pine Bluff ## Arkansas] +[1961 ## Anthony Taylor ## 193 ## 79 ## 1965 ## University of Oregon ## Los Angeles ## California] +[2107 ## Tony Smith ## 201 ## 95 ## 1968 ## nao informado ## nao informado ## nao informado] +[2108 ## Felton Spencer ## 213 ## 120 ## 1968 ## University of Louisville ## Louisville ## Kentucky] +[2127 ## Terrell Brandon ## 180 ## 81 ## 1970 ## University of Oregon ## Portland ## Oregon] +[2159 ## Dikembe Mutombo* ## 218 ## 111 ## 1966 ## Georgetown University ## Kinshasa ## Democratic Republic of the Congo] +[2317 ## Derrick Alston ## 211 ## 102 ## 1972 ## Duquesne University ## Bronx ## New York] +[2453 ## Bruce Bowen ## 201 ## 83 ## 1971 ## California State University - Fullerton ## Merced ## California] +[2462 ## Tony Delk ## 185 ## 85 ## 1974 ## University of Kentucky ## Covington ## Tennessee] +[2681 ## Jumaine Jones ## 203 ## 98 ## 1979 ## University of Georgia ## Cocoa ## Florida] +[2735 ## Mark Madsen ## 206 ## 108 ## 1976 ## Stanford University ## Walnut Creek ## California] +[2806 ## Antonis Fotsis ## 208 ## 99 ## 1981 ## nao informado ## Athens ## Greece] +[2861 ## Gordan Giricek ## 198 ## 95 ## 1977 ## nao informado ## Zagreb ## Croatia] +[2996 ## Matt Freije ## 208 ## 108 ## 1981 ## Vanderbilt University ## Overland Park ## Kansas] +[3059 ## Noel Felix ## 206 ## 102 ## 1981 ## California State University - Fresno ## Los Angeles ## California] +[3117 ## Hakim Warrick ## 206 ## 99 ## 1982 ## Syracuse University ## Philadelphia ## Pennsylvania] +[3150 ## Randy Foye ## 193 ## 96 ## 1983 ## Villanova University ## Newark ## New Jersey] +[3154 ## Daniel Gibson ## 188 ## 86 ## 1986 ## University of Texas at Austin ## Houston ## Texas] +[3182 ## Chris Quinn ## 188 ## 83 ## 1983 ## University of Notre Dame ## New Orleans ## Louisiana] +[3229 ## Spencer Hawes ## 216 ## 111 ## 1988 ## University of Washington ## Seattle ## Washington] +[3292 ## Steven Hill ## 213 ## 112 ## 1985 ## University of Arkansas ## Chanute ## Kansas] +[3529 ## Jordan Williams ## 188 ## 83 ## 1951 ## New Mexico State University ## New Haven ## Connecticut] +[3564 ## Kris Joseph ## 201 ## 95 ## 1988 ## Syracuse University ## Montreal ## Canada] +[3565 ## Michael Kidd-Gilchrist ## 201 ## 105 ## 1993 ## University of Kentucky ## Philadelphia ## Pennsylvania] +[3670 ## Peyton Siva ## 183 ## 83 ## 1990 ## University of Louisville ## Seattle ## Washington] +[3830 ## Christian Wood ## 211 ## 99 ## 1995 ## University of Nevada - Las Vegas ## Long Beach ## California] +[3869 ## Brandon Ingram ## 206 ## 86 ## 1997 ## Duke University ## Kinston ## North Carolina] +[3879 ## Caris LeVert ## 201 ## 92 ## 1994 ## University of Michigan ## Columbus ## Ohio] +[70 ## Robert Hahn ## 208 ## 108 ## 1925 ## North Carolina State University ## nao informado ## nao informado] +[707 ## Jim Caldwell ## 208 ## 108 ## 1943 ## Georgia Institute of Technology ## Durham ## North Carolina] +[723 ## Dave Lattin ## 198 ## 102 ## 1943 ## University of Texas at El Paso ## Houston ## Texas] diff --git a/tps/gabaritos/tp02/TP02Q01 - Classe em Java/pub.in b/tps/gabaritos/tp02/TP02Q01 - Classe em Java/pub.in new file mode 100644 index 0000000..5030a68 --- /dev/null +++ b/tps/gabaritos/tp02/TP02Q01 - Classe em Java/pub.in @@ -0,0 +1,41 @@ +106 +1228 +1307 +1397 +1412 +1554 +1585 +1669 +1750 +1928 +1961 +2107 +2108 +2127 +2159 +2317 +2453 +2462 +2681 +2735 +2806 +2861 +2996 +3059 +3117 +3150 +3154 +3182 +3229 +3292 +3529 +3564 +3565 +3670 +3830 +3869 +3879 +70 +707 +723 +FIM diff --git a/tps/gabaritos/tp02/TP02Q01 - Classe em Java/pub.out b/tps/gabaritos/tp02/TP02Q01 - Classe em Java/pub.out new file mode 100644 index 0000000..3b0b6fc --- /dev/null +++ b/tps/gabaritos/tp02/TP02Q01 - Classe em Java/pub.out @@ -0,0 +1,40 @@ +[106 ## Walt Lautenbach ## 188 ## 83 ## 1922 ## University of Wisconsin ## nao informado ## nao informado] +[1228 ## Willie Wise ## 196 ## 95 ## 1947 ## Drake University ## San Francisco ## California] +[1307 ## James Hardy ## 203 ## 99 ## 1956 ## University of San Francisco ## Knoxville ## Alabama] +[1397 ## Joe Barry ## 193 ## 88 ## 1969 ## Georgia Institute of Technology ## Oakland ## California] +[1412 ## Darrell Griffith ## 193 ## 86 ## 1958 ## University of Louisville ## Louisville ## Kentucky] +[1554 ## Clark Kellogg ## 201 ## 102 ## 1961 ## Ohio State University ## Cleveland ## Ohio] +[1585 ## Trent Tucker ## 196 ## 87 ## 1959 ## University of Minnesota ## Tarboro ## North Carolina] +[1669 ## Stuart Gray ## 213 ## 106 ## 1963 ## University of California - Los Angeles ## Panama Canal Zone ## Panama] +[1750 ## Terry Porter ## 190 ## 88 ## 1963 ## University of Wisconsin-Stevens Point ## Milwaukee ## Wisconsin] +[1928 ## Andrew Lang ## 211 ## 111 ## 1966 ## University of Arkansas ## Pine Bluff ## Arkansas] +[1961 ## Anthony Taylor ## 193 ## 79 ## 1965 ## University of Oregon ## Los Angeles ## California] +[2107 ## Tony Smith ## 201 ## 95 ## 1968 ## nao informado ## nao informado ## nao informado] +[2108 ## Felton Spencer ## 213 ## 120 ## 1968 ## University of Louisville ## Louisville ## Kentucky] +[2127 ## Terrell Brandon ## 180 ## 81 ## 1970 ## University of Oregon ## Portland ## Oregon] +[2159 ## Dikembe Mutombo* ## 218 ## 111 ## 1966 ## Georgetown University ## Kinshasa ## Democratic Republic of the Congo] +[2317 ## Derrick Alston ## 211 ## 102 ## 1972 ## Duquesne University ## Bronx ## New York] +[2453 ## Bruce Bowen ## 201 ## 83 ## 1971 ## California State University - Fullerton ## Merced ## California] +[2462 ## Tony Delk ## 185 ## 85 ## 1974 ## University of Kentucky ## Covington ## Tennessee] +[2681 ## Jumaine Jones ## 203 ## 98 ## 1979 ## University of Georgia ## Cocoa ## Florida] +[2735 ## Mark Madsen ## 206 ## 108 ## 1976 ## Stanford University ## Walnut Creek ## California] +[2806 ## Antonis Fotsis ## 208 ## 99 ## 1981 ## nao informado ## Athens ## Greece] +[2861 ## Gordan Giricek ## 198 ## 95 ## 1977 ## nao informado ## Zagreb ## Croatia] +[2996 ## Matt Freije ## 208 ## 108 ## 1981 ## Vanderbilt University ## Overland Park ## Kansas] +[3059 ## Noel Felix ## 206 ## 102 ## 1981 ## California State University - Fresno ## Los Angeles ## California] +[3117 ## Hakim Warrick ## 206 ## 99 ## 1982 ## Syracuse University ## Philadelphia ## Pennsylvania] +[3150 ## Randy Foye ## 193 ## 96 ## 1983 ## Villanova University ## Newark ## New Jersey] +[3154 ## Daniel Gibson ## 188 ## 86 ## 1986 ## University of Texas at Austin ## Houston ## Texas] +[3182 ## Chris Quinn ## 188 ## 83 ## 1983 ## University of Notre Dame ## New Orleans ## Louisiana] +[3229 ## Spencer Hawes ## 216 ## 111 ## 1988 ## University of Washington ## Seattle ## Washington] +[3292 ## Steven Hill ## 213 ## 112 ## 1985 ## University of Arkansas ## Chanute ## Kansas] +[3529 ## Jordan Williams ## 188 ## 83 ## 1951 ## New Mexico State University ## New Haven ## Connecticut] +[3564 ## Kris Joseph ## 201 ## 95 ## 1988 ## Syracuse University ## Montreal ## Canada] +[3565 ## Michael Kidd-Gilchrist ## 201 ## 105 ## 1993 ## University of Kentucky ## Philadelphia ## Pennsylvania] +[3670 ## Peyton Siva ## 183 ## 83 ## 1990 ## University of Louisville ## Seattle ## Washington] +[3830 ## Christian Wood ## 211 ## 99 ## 1995 ## University of Nevada - Las Vegas ## Long Beach ## California] +[3869 ## Brandon Ingram ## 206 ## 86 ## 1997 ## Duke University ## Kinston ## North Carolina] +[3879 ## Caris LeVert ## 201 ## 92 ## 1994 ## University of Michigan ## Columbus ## Ohio] +[70 ## Robert Hahn ## 208 ## 108 ## 1925 ## North Carolina State University ## nao informado ## nao informado] +[707 ## Jim Caldwell ## 208 ## 108 ## 1943 ## Georgia Institute of Technology ## Durham ## North Carolina] +[723 ## Dave Lattin ## 198 ## 102 ## 1943 ## University of Texas at El Paso ## Houston ## Texas]