12.
CSC508
TEST 1
ANSWER SCHEME
PART A [10 marks]
The following is a JAVA application using the ArrayList ADTs.
public static void main ( String[] args) {
ArrayList <Integer> list = new
ArrayList(); int data = 2;
for ( int x=O; x<3; x++)
{ data = data * 3;
if ( data % 2 == O)
{ list.add(dat
a); data++;
}
else
list.add(x,data);
System.out.print(data + " ");
}
a) Show the output of the program. (3 marks)
7 21 63
b) Draw a diagram of the sequential list after the loop is (1 marks)
executed.
Index 0 1 23m
Data 21 6 6
This study source was downloaded by 100000894106403 from CourseHero.com on 11-20-2024 08:56:00 GMT -06:00
https://www.coursehero.com/file/127276088/CSC508-TEST-1-ANSWER-SCHEMEdocx/
c) Write JAVA codes segment to display data at the last index.
System.out.print(list[2]);
(2 mark)
d) Write JAVA codes to multiply all data in the list with 2.
for(int x=0;x<list.size();x++)
list[x]=list[x] * 2;
(4 marks)
PART B [45 marks]
QUESTION 1 (10 marks)
Given the following ADTs.
public class RoadTax{
private String carPlateNum; // EX : AAA123
private String dueDate ; // EX : dd/mm/yyyy
private double amount ; EX: 125.00
private String serialNum;
public RoadTax(){}
public RoadTax(String,String,double,String){…}
public void setRoadTax(String,String,double,String){…}
public String getPlateNum(){..}
public String getDueDate(){..}
public double getAmount(){…}
public String getSerialNum(){…}
public String toString(){..}
public ArrayList(int size){…}
public void add(Object elem){..}
public Object get(int index){..}
public Object remove(int index){…}
public int size(){..}
}
Write a program to
a) create a list rdTxList and add 100 RoadTax objects.
(3 mark)
ArrayList rdTxList = new ArrayList(100);….0.5
// request user input for each attribute 100 times of loop…………..1
//create object RoadTax , parameters arrangement follow constructor…1
//insert rdTxList.add(obj);…..0.5
2
This study source was downloaded by 100000894106403 from CourseHero.com on 11-20-2024 08:56:00 GMT -06:00
https://www.coursehero.com/file/127276088/CSC508-TEST-1-ANSWER-SCHEMEdocx/
b) display car plate number and amount due for all RoadTax objects from Perak (plate
starts with A) and due in February 2015
(7marks)
LinnkedList l=new LinkedList();…..0.5
for(i=0;i<RdTxList.size(),i++)……………1
{
Object obj = RdTxList.get(i);……….0.5
RoadTax o=(RoadTax)obj;…………….0.5
String s=(o.getDueDate()).substring(3, 9);………….2
char c =(o.getPlateNum()).charAt(0));……..0.5
If ( c=’A’) && (s.equalsIgnoreCase(“02/2015”)…………..1
{
System.out.print(o.toString())……………0.5
l.insertAtBack( c);….0.5
}
QUESTION 2 (14 marks)
Given the following ADTs
public class Magazines {
private String title ; // reader’s digest , solusi etc
private int volumeNo;
private double price;
private String monthYear; // ex : january 2015
private String publisher ; // ex :KarangKraf, Telaga Biru etc
public Magazines() { }
public Magazines (String,int,double,String){…}
public void set Magazines (String,int,double,String){…}
public String getTitle(){..}
public int getVolumeNo(){..}
public double getPrice(){…}
public String getMonthYear(){..}
public String getPublisher(){…}
public String toString(){..}
public LinkedList(){…}
public void insertAtFront(Object elem){..}
public Object getFirst(){..}
public Object getNext(){..}
public Object delete(Object elem){…}
public int size(){..}
}
3
This study source was downloaded by 100000894106403 from CourseHero.com on 11-20-2024 08:56:00 GMT -06:00
https://www.coursehero.com/file/127276088/CSC508-TEST-1-ANSWER-SCHEMEdocx/
Assumed that 100 magazine objects have been inserted into a LinkedList object named
magList.
Write a program segment to do the following:
a) display all Magazines objects issued in Febuary 2015
(4 marks)
Object obj=getFirst();…………………0.5
while(obj !=null)……………….0.5
{
Magazines m=(Magazines)obj;……………0.5
If(m.getMonthYear().equalsIgnoreCase(“Febuary 2015”)……………1.5
m.toString();…………………0.5
obj=getNext();……………………….0.5
}
b) count and display the number of Magazines objects published by Telaga Biru
(5 marks)
Object obj=getFirst();…………………0.5
int c=0;
while(obj !=null)……………….0.5
{
Magazines m=(Magazines)obj;……………0.5
if(m.getPublisher ().equalsIgnoreCase(“Telaga Biru”)……………1.5
m.toString();…………………0.5
obj=getNext();……………………….0.5
}
c) Assumed that an ArrayList named MagArrayList has been created. Find all
magazines with title IMPIANA and insert the magazines into the arraylist.
(5 marks)
Object obj=getFirst();………………..……0.5
while(obj !=null)……………….0.5
{
Magazines m=(Magazines)obj;……………0.5
if(m.getTitle ().equalsIgnoreCase(“IMPIANA”)……………1.5
MagArrayList.add(m)………1.5
obj=getNext();……………………….0.5
}
4
This study source was downloaded by 100000894106403 from CourseHero.com on 11-20-2024 08:56:00 GMT -06:00
https://www.coursehero.com/file/127276088/CSC508-TEST-1-ANSWER-SCHEMEdocx/
QUESTION 3 (21 marks)
a) Convert the infix expression into postfix and show the contain of the stack for the
process.
Symbol Stack Expression
Scanned
1 ( (
2 ( ((
3 A (( A
4 * ((* A
5 ( ((*( A
6 B ((*( AB
7 + ((*(+ AB
8 D ((*(+ ABD
9 ) ((* ABD+
10 / ((*/ ABD+
11 E ((*/ ABD+E
12 ) ( ABD+E/*
13 - (- ABD+E/*
14 ( (-( ABD+E/*
15 F (-( ABD+E/*F
16 * (-(* ABD+E/*F
17 ( (-(*( ABD+E/*F
18 G (-(*( ABD+E/*FG
5
This study source was downloaded by 100000894106403 from CourseHero.com on 11-20-2024 08:56:00 GMT -06:00
https://www.coursehero.com/file/127276088/CSC508-TEST-1-ANSWER-SCHEMEdocx/
19 + (-(*(+ ABD+E/*FG
20 H (-(*(+ ABD+E/*FGH
21 / (-(*(+/ ABD+E/*FGH
22 K (-(*(+/ ABD+E/*FGHK
23 ) (-(* ABD+E/*FGHK/+
24 ) (- ABD+E/*FGHK/+*
25 ) ABD+E/*FGHK/+*-
Steps = 2 marks
Correct answer = 2marks
b) Evaluate the following postfix expression and show the contain of the stack for the
process.
4*((2+6)/3)-2^2^3
(4 marks)—abaikan silap soalan…
c) Define ADTs for stack and queue data structures to store information about an entity
related to hospital applications such as patient, doctor, medical, etc (Choose one entity).
Based on your ADTs, write JAVA program segment for the following:
I. To insert some items to the stack. The number of items depends on the user input.
II. To display the last item in the stack. Remain the original contents of the stack.
III. To move some items from stack into a queue based on a criteria.
(13 marks)
ANSWER
class queue extend LinkedList{
public queue(){..}
public enqueue (Object obj){..}
public Object dequeue(){..}
}…………………1
class stack extend LinkedList{
public stack(){..}
6
This study source was downloaded by 100000894106403 from CourseHero.com on 11-20-2024 08:56:00 GMT -06:00
https://www.coursehero.com/file/127276088/CSC508-TEST-1-ANSWER-SCHEMEdocx/
public push (Object obj){..}
public Object pop(){..}
}…………………….1
public class doctor {
private String name ;
private int drID;
private String majoring;
public Magazines (String,int,String){…}
//accessor
//mutator
}…………..1
i)3.5m
Stack s=new Stack();………0.5
for(i=0;i<300;i++)……1
{
//ask user to enter doctor name
//ask user to enter di ID
//ask user to enter majoring……………….1
doctor d= new doctor(name, id, majoring);….0.5
s.push(d);…..0.5
ii)3.5n2m2m21m
Stack stemp = new Stack();…………..0.5
doctor d= s.pop();….1
stemp.push(d);………..1
d.toString();…………….0.5
Object ss= stemp.pop();….0.5
s.push(ss);………..0.5
iii) 2.5m
Queue q=new Queue();……………….0.5
while(!s.isEmpty()) {……..0.5
doctor d= s.pop();….0.5
if (d.getmajoring().equalsIgnoreCase(“pediatricians”)…………….0.5
q.enqueue(d);………..0.5
7
This study source was downloaded by 100000894106403 from CourseHero.com on 11-20-2024 08:56:00 GMT -06:00
https://www.coursehero.com/file/127276088/CSC508-TEST-1-ANSWER-SCHEMEdocx/
Powered by TCPDF (www.tcpdf.org)