Part A

A.1 Please complete the "where the members of the Alpha class are visible" table by filling in Y or N.

Figure: see image.

Modifier Alpha Beta AlphaSub Gamma
public
protected N
no modifier N N
private N N N

If a member of the class Beta is protected, is this member visible to Gamma? If yes, please explain why. If not, please explain what we should do make it possible.

A.2 What are the 2 ways to use the keyword "super" in a class? - Give code examples to demonstrate your answers.

A.3 When using a linked list for stack implementation, we use the first Node as the top entry of stack. Explain, in this linked list implementation, how we add and how we remove an entry from a stack. Use linked nodes diagrams to save your time.

Explain one major difference between the behaviors of the LinkedBag and the Stack which we implemented.

A.4 What are stored in each activation record and Why? Which method is pushed in the Program Stack first? Which method is popped out of the Program Stack last?

A.5 What is the ouput (what are in the Bag) when the 2 below lines are executed? Please show the steps.

String[] items = {"Z", "Y", "Y", "X", "S", "C", "A", "E", "M"};
testAdd(aBag, items);

What is the output (what are in the Bag) when the 3 below lines are executed? Please show the steps.

String[] testString = { "X", "Y", "Z" };
aBag.removeAllOccurences(testString);
displayBag(aBag);

Part B

B.1 Which of the statement(s) are erroneous and why?

MidtermExam midtermExam = new Exam(); // A
SFSUStaff person = new Person(); // B
StackInterface< String > s = new ArrayStack< >(); // C

B.2 What is the output if any?

abstract class Person {
private final String name;
protected Person(String name) {
this.name = name;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}
}

class Student extends Person {
public Student(String name) {
super(name);
}

public static void main(String[] args) {
Student stu = new Student("Mickey");
System.out.println(stu.getName());
stu.setName("Super Mouse");
System.out.println(stu.getName());
}
}

Attention to the keyword "final"

B.3 Anything wrong with the code? If yes, how to fix?

class CSC220 {
private CSC220(){}
private CSC220(int x){}
private CSC220(int x, int y){}
}

And is it possible to create a sub class to this class? Why?

B.4 What is the output if any?

public static void main(String[] args) {
int x = 12;
System.out.println(sumOf(x) - 42);
}

public static int sumOf(int n) {
int sum = 15;
if (n == 0) {
System.out.println("Base case: n is " + n);
sum += 5 + n % 2;
} else {
sum = sumOf(n - 3) + n;
}
System.out.println(sum);
return sum;
}

B.5 This program outputs 10 lines. What are they?

Stack< String > resume = new Stack< >();
resume.push("JavaScript");
System.out.println("Is empty: \t" + resume.isEmpty());
resume.push("Scala");
resume.push("C++");
resume.push("Dart");
resume.push("Go");
resume.pop();
System.out.println("Stack : \t" + resume);
resume.push("Python");
System.out.println("search() : \t" + resume.search("Scala"));
System.out.println("pop() : \t" + resume.pop());
System.out.println("pop() : \t" + resume.pop());
System.out.println("search() : \t" + resume.search("Dart"));
System.out.println("After pop() : \t" + resume);
System.out.println("pop() : \t" + resume.pop());
System.out.println("Is empty : \t" + resume.isEmpty());
System.out.println("Stack: \t" + resume);

B.6 Implement findTheThird method in linked list that searches the bag for a given entry. If found,

  • removes the first occurrence
  • leave the second occurrence intact
  • then replace third occurrence with the string "Found3rd"
  • remove the rest of the occurrences

Return false if no replacement happened. Otherwise, true.

public boolean findTheThird (T entry)

Note: You may assume that firstNode is a private data in list which references to first node.

B.7 Assume that you have the following Bag object, myBag, with n String data:

BagInterface < String > myBag = new ArrayBag< >();

Write Java statements that create a newBag object which contains non-duplicate data in myBag and marks the duplicate data.

Example:

if myBag contains data:

"A", "A", "A", "B", "B", "C", "D", " "

newBag object should contain:

"A", "DUP.1.A", "DUP.2.A", "B", "DUP.3.B", "C", "D", " "

Hint: You can use the Bag's methods:

int getCurrentSize ();
boolean isFull ();
boolean isEmpty ();
boolean add (T newEntry);
T remove ();
boolean remove (T anEntry);
void clear ();
int getFrequencyOf (T anEntry);
boolean contains (T anEntry);
T [] toArray ();
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.