Docsity

exercise for everyone, Exercises of Mathematics

Toggle sidebar
bg1
Questions 1:
In this question you should complete some methods in MyList.java file.
The class Watermelon with 3 data members: source, price and type is given and you do not need
to edit it. The MyList class is a linked list of Watermelon objects. The following methods should be
completed:
0) void addLast(String xSource, int xPrice, int xType). Add new node with source=xSource,
price=xPrice, type=xType to the end of the list. (price and type can get arbitrary, even
negative values).
1) void f1() โ€“ Do not edit this method. Your task is to fix the addLast() method, check if
xSource.charAt(0) == 'D' then do nothing, otherwise add new node with source=xSource,
price=xPrice, type=xType to the end of the list. Output in the file f1.txt must be the
following:
(A,8,7) (B,4,2) (C,5,4) (E,6,8) (F,3,6)
2) void f2() โ€“There are 2 given Watermelon objects g, h in this function. Suppose the list
contains at least 5 elements. Write statements to insert g and h to the list so that h will be
the 2nd, g will be the 3rd node. Output in the file f2.txt must be the following:
(A,6,5) (B,4,7) (C,9,4) (E,8,1) (F,2,3)
(A,6,5) (H,5,6) (G,2,3) (B,4,7) (C,9,4) (E,8,1) (F,2,3)
3) void f3() โ€“ Suppose the list contains at least 7 elements. Delete the third and the fourth
elements of the original list. Output in the file f3.txt must be the following:
(A,7,3) (B,8,1) (C,5,6) (E,8,3) (F,4,7) (G,3,4) (H,2,8)
(A,7,3) (B,8,1) (F,4,7) (G,3,4) (H,2,8)
4) void f4() โ€“ Sort the first 4 elements ascendingly by type. The content of the output file
f4.txt must be the following:
(A,5,8) (E,8,2) (B,6,4) (C,3,5) (G,6,2) (H,7,9) (K,1,5)
(E,8,2) (B,6,4) (C,3,5) (A,5,8) (G,6,2) (H,7,9) (K,1,5)
5) void f5() โ€“ Delete the last node of the list. The content of the output file f5.txt must be the
following:
(A,5,8) (E,8,2) (B,6,4) (C,3,5) (G,6,2) (H,7,9) (K,1,5)
(E,8,2) (B,6,4) (C,3,5) (A,5,8) (G,6,2) (H,7,9)
6) void f6() โ€“ Delete the first node of the list. The content of the output file f6.txt must be the
following:
(A,5,8) (E,8,2) (B,6,4) (C,3,5) (G,6,2) (H,7,9) (K,1,5)
(B,6,4) (C,3,5) (A,5,8) (G,6,2) (H,7,9) (K,1,5)
1

Partial preview of the text

Download exercise for everyone and more Exercises Mathematics in PDF only on Docsity!

Questions 1: In this question you should complete some methods in MyList.java file. The class Watermelon with 3 data members: source , price and type is given and you do not need to edit it. The MyList class is a linked list of Watermelon objects. The following methods should be completed:

  1. void addLast(String xSource, int xPrice, int xType). Add new node with source=xSource, price=xPrice, type=xType to the end of the list. (price and type can get arbitrary, even negative values).
  2. void f1() โ€“ Do not edit this method. Your task is to fix the addLast() method, check if xSource.charAt(0) == ' D ' then do nothing, otherwise add new node with source=xSource, price=xPrice, type=xType to the end of the list. Output in the file f1.txt must be the following: (A,8,7) (B,4,2) (C,5,4) (E,6,8) (F,3,6)
  3. void f2() โ€“There are 2 given Watermelon objects g, h in this function. Suppose the list contains at least 5 elements. Write statements to insert g and h to the list so that h will be the 2nd, g will be the 3rd^ node. Output in the file f2.txt must be the following: (A,6,5) (B,4,7) (C,9,4) (E,8,1) (F,2,3) (A,6,5) (H,5,6) (G,2,3) (B,4,7) (C,9,4) (E,8,1) (F,2,3)
  4. void f3() โ€“ Suppose the list contains at least 7 elements. Delete the third and the fourth elements of the original list. Output in the file f3.txt must be the following: (A,7,3) (B,8,1) (C,5,6) (E,8,3) (F,4,7) (G,3,4) (H,2,8) (A,7,3) (B,8,1) (F,4,7) (G,3,4) (H,2,8)
  5. void f4() โ€“ Sort the first 4 elements ascendingly by type. The content of the output file f4.txt must be the following: (A,5,8) (E,8,2) (B,6,4) (C,3,5) (G,6,2) (H,7,9) (K,1,5) (E,8,2) (B,6,4) (C,3,5) (A,5,8) (G,6,2) (H,7,9) (K,1,5)
  6. void f5() โ€“ Delete the last node of the list. The content of the output file f5.txt must be the following: (A,5,8) (E,8,2) (B,6,4) (C,3,5) (G,6,2) (H,7,9) (K,1,5) (E,8,2) (B,6,4) (C,3,5) (A,5,8) (G,6,2) (H,7,9)
  7. void f6() โ€“ Delete the first node of the list. The content of the output file f6.txt must be the following: (A,5,8) (E,8,2) (B,6,4) (C,3,5) (G,6,2) (H,7,9) (K,1,5) (B,6,4) (C,3,5) (A,5,8) (G,6,2) (H,7,9) (K,1,5) 1