0% found this document useful (0 votes)
92 views

Array List

The document provides information and questions about using ArrayLists to store and manipulate sales representative, product, student, and player data. a) It asks to write a program to calculate phone bills for 50 sales representatives stored in an ArrayList based on unit rates, and display salaries deducted by the amount. b) Another question asks to write code to copy products from one ArrayList to another based on the producing company, and display the number of matching products. c) A third question involves updating student records between ArrayLists - changing statuses, removing students, and calculating percentages.

Uploaded by

Muh Ahmad
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

Array List

The document provides information and questions about using ArrayLists to store and manipulate sales representative, product, student, and player data. a) It asks to write a program to calculate phone bills for 50 sales representatives stored in an ArrayList based on unit rates, and display salaries deducted by the amount. b) Another question asks to write code to copy products from one ArrayList to another based on the producing company, and display the number of matching products. c) A third question involves updating student records between ArrayLists - changing statuses, removing students, and calculating percentages.

Uploaded by

Muh Ahmad
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

EXERCISE 1 ARRAYLIST

QUESTION 1
Public class salesRep { private String name; private String staffNo; private double salary; private int unit; public salesRep (String, String, double, int) {} public double calculateBill ( ) {} public String toString ( ) {} // storer methods // accessor methods } Public class ArrayList { ArrayList (int) // constructor with initial capacity void add (int, Object) // add element at the specified Boolean add (Object)// add element add the end of the list Object set (int, Object) // replace element at the specified position Object remove (int) // remove the object at the specified position Object get (int) // retrieve the object at the specified position }

The rate per unit is shown in the table below : Unit First 100 Next 101 150 Each extra unit after 150 Write program segments to do the following : a) Define menthod calculateBill ( ) to calculate the phone bill that has to be paid, based on the rate shown above: Input 50 sales representatives into an ArrayList object Display the sales representatives information with the amount that has to be paid by each of sales representative. Calculate and display the new salary for each sales representative if the company decides to automatically deduct the phone bill from their monthly salary. Rate per unit 15 cent 10 cent 5 cent

b) c)

d)

EXERCISE 1 ARRAYLIST
QUESTION 2
public class product { private String productld; private String productName; private double productPrice; private int productQuantity; private String factoryProduceName; public Item (String, String, double, int, String); // getter methods // setter methods } public class ArrayList { ArrayList (int); void add (int, Object); Object get (int); Object set (int, Object); Object set (int, Object); public int size(); }

a)

Write a Java program segment for each of the following questions:

i.

Declare TWO (2) array list objects named as productList1 and productList2.

ii. iii.

Insert TWENTY (20) products into productList1. Copy all products from productList1 produced by Success Sdn. Bhd. to productList2. company. Determine and display the number of products from this

b)

Update the record of product 181 in productList1 to a new price of RM 10.50.

EXERCISE 1 ARRAYLIST
QUESTION 3

Given the following Property and ArrayList ADTs:


public class Students { private String studId; private String studName; private int studPart; private String studStatus; private String gradStatus; public Student (String id, String name, int part, String sStat, String gStat) { .} public void setStudID (String id) { .} public void setStudName (String name) { .} public void setStudPart (int part) { .} public void setStudStatus (String sStat) { .} public void setGradStatus (String gStat) { .} public String getStudID ( ) { .} public String getStudName ( ) { .} public int getStudStatus ( ) { .} public String getGradStatus ( ) { .} } public class ArrayList { public ArrayList ( ) // default constructor public boolean add (Object item) // insert at back public Object get (int index) // return element from the specified location public Object remove (int index) // return element from the specified location public Object set (int index, Object item) // replace with specified element at specified location public int size ( ) // return the size of the list // definition of other methods }

Write Java program segments to solve the following problems. Assume that the data has been inserted into an array list called studListA. a) b) Create an array list named studListB. Update the student status from fulltime to part time and the graduation status as not graduated if the students part is greater than part 8. Remove all students who did not graduate after part 10 from studListA. Copy all part time students into studListB. Find the percentage for part time students over the whole student population.

c) d) e)

EXERCISE 1 ARRAYLIST
QUESTION 4 Given the following Player and ArrayList ADTs:
public class Player{ public String playerId; // player id public String playerName; // player name public String playerState; // player state public int contractYears; // year of contract service with the state public double ratePerMonth; // rate per month paid to the player public Player(); public Player(String a, String b, String c, int d, double e); public void setPlayerID(String playerId){} public void setPlayerName(String playerName){} public void setplayerState(String playerState){} public void setContractYears (int contractYears){} public void setRatePerMonth (double ratePerMonth){} public String getPlayerID(){} public String getPlayerName(){} public String getplayerState(){} public int getContractYears (){} public double getRatePerMonth (){} public String toString(); } public class ArrayList { public ArrayList() public void add(int index, Object obj) public boolean add(Object obj) public boolean remove (Object obj) public Object get (int index) public Object set(int index, Object obj) public int size() }

a)

Declare TWO (2) objects of ArrayList named playerArrList1 and playerArrList2. The size of both arrays is 100. Insert 100 records into playerArrList1. Search a player id and display detail information of the player. If the player id is not found, display an appropriate message. Then, set the rate per month of the player to RM 10000.00. Finally display back the detail information of the player after the modification of rate per month has been made. Copy all records to playerArrList2, where the player state is Selangor.

b) c)

d)

You might also like