[go: up one dir, main page]

0% found this document useful (0 votes)
28 views6 pages

Oop Lab4 2024

Uploaded by

terecencelau2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views6 pages

Oop Lab4 2024

Uploaded by

terecencelau2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

OOP

Object-Oriented Programming (OOP, CCIT-4023)


HKU SPACE Community College, 2024-2025

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) {
}
}

b) In the main() method body:


o Declare local variables to store the information of the player, e.g.
int pID = 20182018;
String pName = "CHAN Tai Man";
String pType = "Tennis";
double pWeight = 68.5;
o Display the information of the player, e.g.
System.out.println("\nPlayer Info: ID, Name, Type, Weight:\n"
+ pID+", "+pName+", "+pType+", "+pWeight);

1 / 6
OOP

2. Model a Player, with an array of String


a) String Object Declaration, Creation (Instantiation of a Class) and Assignment
Inside the main() method:
o Declare a local variable, pInfo of String array, representing a player (information).
o Create and assign/initialize an instance (object) of String array to variables above:
o {"20182020", "CHAN Siu Man", "Badminton", "62"}
* Suppose these Strings represent a simple player record, about the id, name, and sport-type

b) Accessing Object String


o Modify the source code, to display Player Information (reference code below):
o Compile, Run and debug the Java program, until the problem is solved.
o If finished successfully, similar display should be output on console.

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]);

3. Model a Player, with a given class Player

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

ID Name Sport Type Weight


20002000 LAU Andy Badminton 65
20012001 CHOW Betty Tennis 50

Given the Class Player:


a) Put the given bytecode file Player.class to the same directory as our program (our source
code). Modify our current Java file properly to finish the task.

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.

4. Reference Code (* May contain bugs):


// 3)
Player andy = new Player(20002000, "LAU Andy");
andy.setType("Badminton");
andy.setWeight(65);
andy.dispPInfo();

Player betty = new Player(20012001, "CHOW Betty");


andy.setType("Tennis");
andy.setWeight(50);
andy.dispPInfo();

3 / 6
OOP

Lab 4.2: Handling Player Information, with User Input


Task: Create a Java program, to
o Handle student player information with user input, and display on console

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.

2. Inside the main() method:


o Declare a local variable, p2Info of String array.
o Let User input player information as String from standard input console
o Display the player information on standard output console
o Also get and display the date of generating info., as sample display (may check lecture notes)

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):

import java.util.*; // import statement needed for Scanner, Calendar


public class L42 {
public static void main (String[] args) {
System.out.println("Input a Player Info. in a form \"ID,Name,Type,Weight\":");
Scanner scanner = new Scanner(System.in); // Scanner scans from std input
String inStr = scanner.nextLine(); // scan the next Line as String
String[] p2Info = inStr.split(","); // Splits string to array, with ","
System.out.println("\nPlayer Info: ID, Name, Type, Weight:\n"+
p2Info[0]+", "+p2Info[1]+", "+p2Info[2]+", "+p2Info[3]);
Calendar c = Calendar.getInstance(); //get current time as Calendar
int year = c.get(Calendar.YEAR);
int month = 1 + c.get(Calendar.MONTH); // * RANGE: 0~11, for Jan~Dec
int day = c.get(Calendar.DAY_OF_MONTH); // 1~31
System.out.println("Date of Generation: "+year+"-"+month+"-"+day);
}
}

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.

2. Object Declaration, Creation (Instantiation of a Class) and Assignment


o Declare a local variable, p3Info of String array.
o Let User input player information as String from JOptionPane.showInputDialog()
o Display the player information on JOptionPane.showMessageDialog()
* Suppose these strings represent a simple player record: id, name, sport-type and weight
o You should consider the string format of player information input. The following shows user input
of id, name, type and weight.

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

You might also like