Oop Lab4 2024
Oop Lab4 2024
Lab 4
(Practicing Lab, NOT for submission)
* Given Reference codes are for reference only, and may contain bugs. Students are advised to attempt
their own coding first, before referencing these given codes.
* If students want more programming exercises, they are also recommended to try code examples given in
the related lecture notes and materials to consolidate their OOP concepts and skills.
Lab 4.1: Handling Player Information: Manipulating String Class and Objects
Task: Create a Java program, to
o Handle two student player information (with different data types), and display on console
o Generate and Display Information (of Player), already stored in an array of String
o Suppose we are modelling sports player, with information 1) id, 2) name, 3) sport type, 4) weight
◼ We have already got a player: 20182018, "CHAN Tai Man", "Tennis", 68.5
Player ID Player Name Sport Type Weight
20182018 CHAN Tai Man Tennis 68.5
< Yourself > < Yourself > < Yourself > < Yourself >
1. Create a Java class L41 in a file L41.java, and complete the tasks in main() method.
a) Create a basic skeleton of declaring a Java class named L41 in the file, as below.
// L41.java
public class L41 {
public static void main (String[] args) {
}
}
1 / 6
OOP
c) Add code to model & display another Player, with your own information, similar to last steps
* [Optional] You may extend this lab to handle more than one player, with 2D array of String
Reference Code (* May contain bugs):
String[] pInfo = {"20182020", "CHAN Siu Man", "Badminton", "62"};
System.out.println("\nPlayer Info: ID, Name, Type, Weight:\n"
+ pInfo[0]+", "+pInfo[1]+", "+pInfo[2]+", "+pInfo[3]);
Given a class Student Player (bytecode file Player.class only, without completed source file), its
UML class diagram, and the reference code below, create two objects of this class Player and display
their information by calling its methods.
public class Player { // Class Player Player
////////// Field(s) pID: int
int pID; pName: String
String pName; pType: String
String pType; pWeight: double
double pWeight; + Player(id: int, name: String)
////////// Constructor(s) + setType(type: String)
public Player (int id, String name){ + setWeight(weight: double)
// ... + getPInfo(): String
} + dispPInfo()
////////// Method(s)
public void setType(String type) { // ...
public void setWeight(double weight) { // ...
public String getPInfo(){ // ...
public void dispPInfo(){ // ...
2 / 6
OOP
b) Create the object to represent the first player LAU Andy. Add codes to end of main() method:
o Declare, Create (& assign) a local variable, andy, as object of Player, representing a player.
o Parameter for pID is 20002000, and for pName is "LAU Andy"
o Player andy = new Player(20002000, "LAU Andy");
o Access: Set of sport type and weight of this player, by calling the proper method:
o andy.setType("Badminton");
o //... try similar code to set the weight
o Access: Display the information of this player, by calling the proper method:
o andy.dispPInfo();
o Compile, Debug and Run the updated program and check if this works as expected.
c) Repeat the steps, to create object for the second player CHOW Betty with a local variable betty
d) [Optional] Add code to model & display another Player, with your own information, similar to
last steps.
3 / 6
OOP
User Input
Generate and Display Player Information, with User I/O on standard output console (e.g. PowerShell
or Command Prompt), stored in array of String.
* Reminder: student may use the given class Player.class to represent the data.
1. Create a Java class L42 in a file L42.java, and complete the tasks in main() method.
* You may reuse (modify, copy-n-paste) the last lab.
You should consider the string format of player information input. The following shows an approach
sample. One line of string including id, name, sport-type and weight, separated with a comma.
Reference Code (* May contain bugs):
4 / 6
OOP
3. Compile, Run and debug the Java program, until the problem is solved.
* E.g. for User Input: 20202020,LAU Andy,Badminton,65
User Input
* [Optional] Again, may extend this lab to handle more than one player, with 2D array of String.
o E.g. in a new case, we have 3 players below:
ID Name Sport Type Weight
20002000 LAU Andy Badminton 65
20012001 CHOW Betty Tennis 50
20022002 AU Candy Tennis 60.5
5 / 6
OOP
[Optional] Lab 4.3: Interactively Manipulating String Class and Objects, with GUI
Task: Create a Java program, to
o Handle student player information with user input, via GUI - JOptionPane
>
Generate and Display Player Information, with simple JOptionPane GUI User I/O
* Reminder: student may use the given class Player.class to represent the data.
1. Create a Java class L43 in a file L43.java, and complete the tasks in main() method.
You may reuse (modify, copy-n-paste) the last lab.
Reference Codes:
String [] p3Info = new String[4];
p3Info[0] = JOptionPane.showInputDialog (null, "What is your ID?");
p3Info[1] = JOptionPane.showInputDialog (null, "What is your Name?");
String[] optVal = { "Badminton", "Tennis", "Soccer" }; //option values
p3Info[2] = (String) JOptionPane.showInputDialog(null,
"Choose your sport-type", "Sport Type",
JOptionPane.INFORMATION_MESSAGE,
null, optVal, optVal[0]);
p3Info[3] = JOptionPane.showInputDialog (null, "What is your Weight?");
JOptionPane.showMessageDialog(null,
"\nPlayer Info: ID, Name, Type, Weight:\n"+
p3Info[0]+", "+ p3Info[1]+", "+ p3Info[2]+", "+ p3Info[3]);
3. Compile, Run and debug the Java program, until the problem is solved.
* [Optional] Again, may extend this lab to handle more than one player, with 2D array of String
~ END ~
6 / 6