156k views
2 votes
Convert the attached C++ code to working Java code. Be judicious in the change that you make. This assignment is not about re-writing or improving this code, but rather about recognizing the differences between C++ and Java, and making the necessary coding changes to accommodate how Java does things. PLEASE DO NOT use a built-in Java QUEUE (or any other) container. Additional resources for assignment:

#include

#include

using namespace std;

class pizza

{

public:

string ingrediants, address;

pizza *next;

pizza(string ingrediants, string address)

{

this->address = address;

this->ingrediants = ingrediants;

next = NULL;

}

};

void enqueue(pizza **head, pizza **tail, pizza *thispizza)

{

if (*head == NULL) *head = thispizza;

else (*tail)->next = thispizza;

*tail = thispizza;

return;

}

pizza* dequeue(pizza **head, pizza **tail)

{

pizza* pizzatodeliver = NULL;


if (*head != NULL)

{

pizzatodeliver = *head;

*head = (*head)->next;

}

if (*head == NULL) *tail = NULL;

return pizzatodeliver;

}

void deliver(pizza **head, pizza`** tail)

{

pizza *thispizza = dequeue(head, tail);

if (thispizza == NULL)

{

cout << "No deliveries pending" << endl; return;

}

cout << "Deliver a pizza with " << thispizza->ingrediants

<< " to " << thispizza->address << endl;

}

int main()

{

pizza *first = NULL, *last = NULL;

enqueue(&first, &last, new pizza("pepperoni", "1234 Bobcat Trail"));

enqueue(&first, &last, new pizza("sausage", "2345 University Drive"));

deliver(&first, &last);

enqueue(&first, &last, new pizza("extra cheese", "3456 Rickster Road"));

enqueue(&first, &last, new pizza("everything", "4567 King Court"));

enqueue(&first, &last, new pizza("coffee beans", "5678 Java Circle"));

deliver(&first, &last);

deliver(&first, &last);

deliver(&first, &last);

deliver(&first, &last);

deliver(&first, &last);

cin.get();

return 0;

}

User Csjpeter
by
7.8k points

1 Answer

6 votes

Answer:

class Pizza {

String ingredients;

String address;

Pizza next;

Pizza(String ingredients, String address) {

this.address = address;

this.ingredients = ingredients;

next = null;

}

}

public class PizzaDelivery {

static Pizza enqueue(Pizza head, Pizza tail, Pizza thisPizza) {

if (head == null) {

head = thisPizza;

} else {

tail.next = thisPizza;

}

tail = thisPizza;

return tail;

}

static Pizza dequeue(Pizza head, Pizza[] tail) {

Pizza pizzaToDeliver = null;

if (head != null) {

pizzaToDeliver = head;

head = head.next;

}

if (head == null) {

tail[0] = null;

}

return pizzaToDeliver;

}

static void deliver(Pizza[] head, Pizza[] tail) {

Pizza thisPizza = dequeue(head[0], tail);

if (thisPizza == null) {

System.out.println("No deliveries pending");

return;

}

System.out.println("Deliver a pizza with " + thisPizza.ingredients

+ " to " + thisPizza.address);

}

public static void main(String[] args) {

Pizza first = null;

Pizza[] last = { null };

last[0] = enqueue(first, last[0], new Pizza("pepperoni", "1234 Bobcat Trail"));

last[0] = enqueue(first, last[0], new Pizza("sausage", "2345 University Drive"));

deliver(first, last);

last[0] = enqueue(first, last[0], new Pizza("extra cheese", "3456 Rickster Road"));

last[0] = enqueue(first, last[0], new Pizza("everything", "4567 King Court"));

last[0] = enqueue(first, last[0], new Pizza("coffee beans", "5678 Java Circle"));

deliver(first, last);

deliver(first, last);

deliver(first, last);

deliver(first, last);

deliver(first, last);

}

}

Step-by-step explanation:

User Deathemperor
by
8.1k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.