In: Computer Science
I need this code translated from C++ to Java. Im personally still trying to learn Java, so if you can include screenshots of your IDE/output that would be helpful. Much appreciated!
#include <iostream>
#include <string>
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;
}
public class PizzaQueue { static class Pizza { String ingrediants, address; Pizza next; public Pizza(String ingrediants, String address) { this.address = address; this.ingrediants = ingrediants; next = null; } } private Pizza head; void enqueue( Pizza toAdd) { if(head == null) { head = toAdd; } else { Pizza p = head; while(p.next != null) { p = p.next; } p.next = toAdd; } } Pizza dequeue() { if(head == null) { return null; } else { Pizza p = head; head = head.next; return p; } } void deliver() { Pizza d = dequeue(); if(d == null) { System.out.println("No deliveries pending"); } else { System.out.println("Deliver a pizza with " + d.ingrediants + " to " + d.address); } } public static void main(String[] args) { PizzaQueue queue = new PizzaQueue(); queue.enqueue(new Pizza("pepperoni", "1234 Bobcat Trail")); queue.enqueue(new Pizza("sausage", "2345 University Drive")); queue.deliver(); queue.enqueue(new Pizza("extra cheese", "3456 Rickster Road")); queue.enqueue(new Pizza("everything", "4567 King Court")); queue.enqueue(new Pizza("coffee beans", "5678 Java Circle")); queue.deliver(); queue.deliver(); queue.deliver(); queue.deliver(); queue.deliver(); } }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.