You are on page 1of 7

Toturial Week 11

Name : MUHAMMAD SYAFIQ BIN MOHD NAZRI

Matric No. : 2022891926

Class : RCDCS1103C
QUESTION 1

a) Input some data of wood produced into LinkedList WoodList.


LinkedList <Wood> WoodList = new LinkedList();

Wood wood1 = new Wood('c', 13.4, 2.9, 4.2);


WoodList.add(wood1);

Wood wood2 = new Wood('c', 15.7, 11.0, 12.1);


WoodList.add(wood2);

Wood wood3 = new Wood('m', 9.2, 13.1, 9.3);


WoodList.add(wood3);

Wood wood4 = new Wood('c', 13.8, 8.5, 11.5);


WoodList.add(wood4);

Wood wood5 = new Wood('m', 14.9, 15.0, 4.0);


WoodList.add(wood5);

Wood wood6 = new Wood('c', 13.2, 13.5, 9.0);


WoodList.add(wood6);

Wood wood7 = new Wood('c', 9.2, 13.5, 9.5);


WoodList.add(wood7);

Wood wood8 = new Wood('m', 7.0, 4.5, 3.1);


WoodList.add(wood8);

Wood wood9 = new Wood('m', 8.1, 3.0, 1.3);


WoodList.add(wood9);

Wood wood10 = new Wood('c', 7.1, 4.0, 8.3);


WoodList.add(wood10);

b) Extract plywood from WoodList into MerantiStack and CengalStack accordingly


Stack <Wood> MerantiStack = new Stack();
Stack <Wood> CengalStack = new Stack();
Stack <Wood> TempStack = new Stack();

Wood temp;

for(int i = 0; i < WoodList.size(); i++)


{
temp = WoodList.get(i);

if(temp.getWoodType() == 'm')
{
MerantiStack.push(temp);
}

else if(temp.getWoodType() == 'c')


{
CengalStack.push(temp);
}
}
c) Display the information regarding Jengka Sawmill plywood in the format below:
int countBigM = 0;
int countBigC = 0;
int countSmallM = 0;
int countSmallC = 0;
int totalBig = 0;
int totalSmall = 0;
int countTotalM = 0;
int countTotalC = 0;
int totalBoth = 0;

while(!MerantiStack.isEmpty()) // Meranti
{
temp = MerantiStack.pop();

countTotalM++;

if(temp.getWidth() >= 6 && temp.getHeight() >= 8) // Big


{
countBigM++;
}

else // Small
{
countSmallM++;
}

TempStack.push(temp);
}

while(!TempStack.isEmpty())
{
MerantiStack.push(TempStack.pop());
}

while(!CengalStack.isEmpty()) // Cengal
{
temp = CengalStack.pop();

countTotalC++;

if(temp.getWidth() >= 6 && temp.getHeight() >= 8) // Big


{
countBigC++;
}

else // Small
{
countSmallC++;
}

TempStack.push(temp);
}

while(!TempStack.isEmpty())
{
CengalStack.push(TempStack.pop());
}

// Calculation for total


totalBig = countBigM + countBigC;
totalSmall = countSmallM + countSmallC;
totalBoth = totalBig + totalSmall;

// Display
System.out.println(" _________________________________________________________ ");
System.out.printf("|%-13s|%-15s|%-15s|%-11s|\n", "TYPE", "BIG SIZE", "SMALL SIZE", "TOTAL");
System.out.println("|_____________|_______________|_______________|___________|");
System.out.printf("|%-13s|%-15s|%-15s|%-11s|\n", "Meranti", countBigM, countSmallM, countTotalM);
System.out.printf("|%-13s|%-15s|%-15s|%-11s|\n", "Cengal", countBigC, countSmallC, countTotalC);
System.out.printf("|%-13s|%-15s|%-15s|%-11s|\n", "TOTAL", totalBig, totalSmall, totalBoth);
System.out.println("|_____________|_______________|_______________|___________|");
System.out.println("Big size: Width >= 6, Heigth >= 8\nSmall size: Other size");
Output:
--------------------Configuration: <Default>--------------------
_________________________________________________________
|TYPE |BIG SIZE |SMALL SIZE |TOTAL |
|_____________|_______________|_______________|___________|
|Meranti |1 |3 |4 |
|Cengal |4 |2 |6 |
|TOTAL |5 |5 |10 |
|_____________|_______________|_______________|___________|
Big size: Width >= 6, Heigth >= 8
Small size: Other size

Process completed.

QUESTION 2

a) Convert the following infix expressions to postfix expressions.

i) ( A * B ) + ( C * D ) + ( E / F )

(AB*)+(CD*)+(EF/)
(AB*)(CD*)++(EF/)
(AB*)(CD*)+(EF/)+
AB*CD*+EF/+

ii) M * N $ O – X * ( Y * Z )

M*N$O–X*(YZ*)
(MN*)$O–X*(YZ*)
(MN*)O$-X*(YZ*)
(MN*)O$-X(YZ*)*
(MN*)O$X(YZ*)*-
MN*O$XYZ**-

iii) 1 + 2 * 3 – 4 / 4 - 1 2 / 6 * 6

1+(23*)–(44/)-1(26/)*6
1+(23*)–(44/)-1(26/)6*
1(23*)+(44/)-1(26/)-6*
123*+44/-126/-6*
b) Evaluate the following arithmethic expression using stack. Show all your works.

86*84+/5+

Step 1 : 8 6 * 8 4 + / 5 +

Step 2 :

4
6 8 8 12 5
8 8 48 48 48 48 4 4 9
8 6 * 8 4 + / 5 +

Answer : 9

Question 3

b) Answer the following questions.

i) Convert the following expression into prefix notation.

• A*B/C+D

(*AB)/C+D
/(*AB)C+D
+/(*AB)CD
+/*ABCD

• E–(F+G)/H

E–(+FG)/H
E-/(+FG)H
-E/(+FG)H
-E/+FGH

ii) Convert the following expression into postfix notation.

• (P+Q–R)/S

(PQ+-R)/S
(PQ+R-)/S
(PQ+R-)S/
PQ+R-S/

• T–U$V *W

T–(UV$)*W
T–(UV$)W*
T(UV$)W*-
TUV$W*-
c) Given the following infix expression:

X=J/K+(L–M*N)

i) Convert the above infix into postfix notation using stack. Show all the steps.

Infix operatorStack Postfix

J empty J

/ / J

K / JK

+ + JK/

( +( JK/

L +( JK/L

- +(- JK/L

M +(- JK/LM

* +(-* JK/LM

N +(-* JK/LMN
) +(-*) JK/LMN*-
+
empty JK/LMN*-+

ii) Evaluate the above postfix notation using stack configuration if,

J = 140, K = 7, L = 55, M = 9, N = 5

Show all the steps and the results.

Step 1 : J K / L M N * - +
Step 2 :

5
9 9 45
7 55 55 55 55 10
140 140 20 20 20 20 20 20 30
J K / L M N * - +

Answer : 30

You might also like