Question: Can I get this C++ program translated to Java? I'm having trouble getting my head to stop being null when outside of the scope. Below
Can I get this C++ program translated to Java? I'm having trouble getting my head to stop being null when outside of the scope. Below the C++ code is what I have so far. I have various print lines for testing purposes in case you were wondering. Please help meeeee!
//************PROGRAM STARTS IN C++************
#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; } //****************C++ code ends****************
//****************My shot at the translation****************
package Package;
public class Pizza
{
public String ingredients, address;
public Pizza next;
public Pizza()
{
ingredients = null;
address = null;
next = null;
}
public Pizza(String setIngredients, String setAddress)
{
ingredients = setIngredients;
address = setAddress;
next = null;
}
static void enqueue(Pizza head, Pizza tail, Pizza thispizza)
{
if (head == null)
{
head = thispizza;
}
else
{
tail.next = thispizza;
System.out.println("NOT NULL----------------------");
}
if (head != null)
System.out.println(" + + + " + head.address);
tail = thispizza;
return;
}
static Pizza dequeue(Pizza head, Pizza tail)
{
Pizza pizzatodeliver = null;
if (head != null)
{
pizzatodeliver = head;
head = head.next;
System.out.println(" WEFW " + head);
}
if (head == null)
{
tail = null;
System.out.println("DING DING MOFO");
}
return pizzatodeliver;
}
static void deliver(Pizza head, Pizza tail)
{
System.out.println( " - + - " + head);
Pizza thisPizza = dequeue(head, tail);
System.out.println( " - - - " + head);
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;
enqueue(first, last, new Pizza("cheese", "1234 Bobcat Trail"));
enqueue(first, last, new Pizza("Sausage", "2345 University Drive"));
if (first == null)
System.out.println(" (((((( ");
if (last == null)
System.out.println(" )))))) ");
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);
}
}
//************PROGRAM ENDS IN JAVA************
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
