Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

QUESTION 1. Why does this code not work? How do you fix it? ---------------- ``` public class RemoveBigNumbers { public List removeBigNumbers(List data) { for

QUESTION 1. Why does this code not work? How do you fix it?

----------------

```

public class RemoveBigNumbers {

public List removeBigNumbers(List data) {

for (Integer i : data) {

if (i > 10) {

data.remove(i);

}

}

return data;

}

}

Write a class that implements the following interface, assuming that all methods

are used with approximately the same frequency.

```

interface ItemStore {

interface Item {

String getID();

String getTag();

}

/**

* Adds an {@link Item} to the store, replacing any existing item with the

* same {@link Item#id} value.

*/

public void put(Item item);

/**

* Retrieves the {@link Item} with the given {@link Item#id} value, or

* null if no such {@link Item} exists in the store.

*/

public Item get(String id);

/**

* Delete all {@link Item}s with the specified tag.

*/

public void dropAllByTag(String tag);

/**

* Returns the number of Items in the store

*/

public int size();

}

QUESTION 3: Memory Management

----------------

The `SmallMemoryMessageTest` class below passes on our development machines,

but the client reports that it fails on their 64MB VM.

1. Run on a 64M VM and copy the failing stack trace.

2. Modify `main()` to work on a 64MB VM.

3. Ensure there are no more than one performance warnings.

To set 64Mb VM, run using `java -Xmx64M SmallMemoryMessageTest`.

```

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

import java.util.function.Predicate;

/**

* The following mock program passes on our development machines,

* but the client reports that it fails on their 64MB VM.

*

* 1. Run on a 64M VM and copy the failing stack trace

* 2. Modify main() to work on a 64MB VM.

* 3. Ensure at most one performance warning.

*

* (To set 64Mb VM) run using java -Xmx64M SmallMemoryMessageTest

*/

/** A given interface to process messages (DO NOT CHANGE) **/

interface MessageProcessor {

void processMessage(Message msg);

}

/** A given interface to archive select messages (DO NOT CHANGE) **/

interface MessageArchiver {

void archiveMessages(List messages, Predicate filter);

}

/** A given class to represent a message (DO NOT CHANGE) **/

class Message {

private String subject;

private String body;

public Message(String subject, String body) {

this.subject = subject;

this.body = body;

}

public String getSubject() {

return subject;

}

public String getBody() {

return body;

}

@Override

public String toString() {

return "Message{" +

"subject: " + Util.abbreviate(subject, 20) +

", body: " + Util.abbreviate(body, 40) + "}";

}

}

/**

* Test class that works on developer boxes but fails on small VMs.

*

* Fix this class to work on 64Mb VM

*/

public class SmallMemoryMessageTest {

public static void main(String []args) {

MessageProcessor processor = Util.createMessageProcessor();

MessageArchiver archiver = Util.createMessageArchiver();

List messages = new ArrayList<>();

for (int i = 0; i < Util.EXPECTED_TOTAL; i++) {

Message msg = Util.random();

processor.processMessage(msg);

messages.add(msg);

}

archiver.archiveMessages(messages, m -> m.getSubject().startsWith("A"));

/*

* DO NOT CHANGE ANYTHING BELOW THIS LINE.

* PROGRAM MUST EXIT SUCCESSFULLY

*/

Util.validate();

}

}

/** A given utility class (DO NOT CHANGE) **/

class Util {

static final int EXPECTED_TOTAL = 98765;

static final int EXPECTED_ARCHIVED = 3799;

static Message random() {

String subject = randomSubject();

String body = randomBody();

Message m = new Message(subject, body);

return m;

}

static int count = 0;

static String randomSubject() {

StringBuilder sb = new StringBuilder(128);

sb.append((char) ((int) 'A' + (count++ % 26)));

Random r = new Random();

while (sb.length() < 128) {

char c = (char) r.nextInt(127);

if (Character.isLetterOrDigit(c)) {

sb.append(c);

}

}

return sb.toString();

}

static String randomBody() {

StringBuilder sb = new StringBuilder(4096);

Random r = new Random();

while (sb.length() < 4096) {

char c = (char) r.nextInt(127);

if (!Character.isISOControl(c)) {

sb.append(c);

}

}

return sb.toString();

}

static String abbreviate(String s, int n) {

return s.length() > n + 3 ? s.substring(0, n - 3) + "..." : s;

}

static MessageProcessor createMessageProcessor() {

return new TestProcessor();

}

static MessageArchiver createMessageArchiver() {

return new TestArchiver();

}

static void validate() {

if (TestArchiver.count != EXPECTED_TOTAL ||

TestArchiver.archived != EXPECTED_ARCHIVED) {

throw new IllegalStateException("Failed to archive all messages!");

}

System.out.println("SUCCESS. PROCESSED: " + TestArchiver.count + " ARCHIVED: " + TestArchiver.archived);

}

}

/** Noop implementation of a message processor (DO NOT CHANGE) **/

class TestProcessor implements MessageProcessor {

long count = 0;

@Override

public void processMessage(Message msg) {

if(++count%1000 == 0) {

System.out.println("Processed: " + (count) + " Latest: " + msg);

System.out.flush();

}

}

}

/** Noop implementation of a message archiver (DO NOT CHANGE) **/

class TestArchiver implements MessageArchiver {

static int count = 0;

long bytes = 0;

static long archived = 0;

@Override

public void archiveMessages(List messages, Predicate filter) {

if(messages.size() < 1000) {

System.err.println("WARNING: message list too short, this will drastically reduce performance!");

}

messages.stream().filter(filter).forEach(this::archiveOne);

System.out.println("Archived: " + bytes + " bytes.");

count += messages.size();

bytes = 0;

}

private void archiveOne(Message msg) {

bytes += msg.getSubject().getBytes().length + 1;

bytes += msg.getBody().getBytes().length;

archived++;

}

}

QUESTION 4: Debugging

----------------

```

package com.moesol.hr.bugs;

public class Bug1 {

private Integer rating;

public int rating() {

return rating;

}

public static void main(String[] args) {

System.out.println("rating:"

+ new Bug1().rating());

}

}

```

The program above throws a `NullPointerException` with this stack trace:

```

Exception in thread "main" java.lang.NullPointerException

at com.moesol.hr.bugs.Bug1.rating(Bug1.java:7)

at com.moesol.hr.bugs.Bug1.main(Bug1.java:12)

```

What is happening? How can it be fixed?

QUESTION 5: Wrong Result

----------------

The following program produces inconsistent results. It should always output

this:

```

counter is 20000

```

Please correct the program.

```

public class WrongAnswer {

private int counter = 0;

public static void main(String[] args) {

new WrongAnswer().run();

}

private void run() {

try {

Thread t1 = new Thread(this::incrementToOnHundred);

Thread t2 = new Thread(this::incrementToOnHundred);

t1.start();

t2.start();

t1.join();

t2.join();

System.out.println("counter is " + counter);

} catch (InterruptedException e) {

System.err.println("fatal error, unexpected interrupt exception");

System.exit(2);

}

}

private void incrementToOnHundred() {

for (int i = 0; i < 10_000; i++) {

doSomeFakeWork();

}

}

private void doSomeFakeWork() {

counter++;

}

}

```

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke

4th Edition

0136086535, 9780136086536

More Books

Students also viewed these Databases questions

Question

What are the Foreign Exchange markets contributions to society?

Answered: 1 week ago

Question

What is the competition?

Answered: 1 week ago

Question

What is the relative priority among the viable goals?

Answered: 1 week ago