Question

In: Computer Science

I need this code translated from C++ to Java. Im personally still trying to learn Java,...

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;
}

Solutions

Expert Solution

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.


Related Solutions

im trying to write a java code that take a matrix of vector and fine it's...
im trying to write a java code that take a matrix of vector and fine it's inverse (the the inverse in linear algebra) then multiple this matrix with a vector to fine other vector (matrix)-1 × ( vector ) = (vector)
I get an error when im trying to run this java program, I would appreciate if...
I get an error when im trying to run this java program, I would appreciate if someone helped me asap, I will make sure to leave a good review. thank you in advance! java class Node public class Node { private char item; private Node next; Object getNext; public Node(){    item = ' '; next = null; } public Node(char newItem) { setItem(newItem); next = null; } public Node(char newItem, Node newNext){ setItem(newItem); setNext(newNext); } public void setItem(char newItem){...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef struct node { struct node *left; struct node *right; long data; long leftSize; } node; void btreeInsert(node *new, node **rootptr) { node *parent = NULL, *cursor; /* Find parent */ cursor = *rootptr; while (cursor != NULL) { parent = cursor; if (new->data < cursor->data) { cursor->leftSize += 1; cursor = cursor->left; } else { cursor = cursor->right; } } /* Insert node below...
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
PLESE CODE IN C# not java RECURSION Objectives • Learn the basics of recursion – Part...
PLESE CODE IN C# not java RECURSION Objectives • Learn the basics of recursion – Part II (last week was Part I) Submission Guidelines: You will turn in 2 program files (one for each lab problem) Tasks This lab has two parts: Write a driver program that calls this method from the Main program. • • Write a driver program that calls this method from the Main program. Note: Your program name should match the name of your java /...
Need this C++ code to be modified to work in C, still using 2d arrays... #include...
Need this C++ code to be modified to work in C, still using 2d arrays... #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; //Implementation of main function int main() { srand(time(NULL)); //Declaration of output,result,i,j,k ,figure as integer type and //assign flog with 0 int output[5][5], result[5], i, j, k, figure = 0; //Display statement cout << "The classic BINGO cards contains 25 squares arranged in five vertical" << endl; cout << "columns and five side to side rows. Each...
this is a python code that i need to covert to C++ code...is this possible? if...
this is a python code that i need to covert to C++ code...is this possible? if so, can you please convert this pythin code to C++? def main(): endProgram = 'no' print while endProgram == 'no': print # declare variables notGreenCost = [0] * 12 goneGreenCost = [0] * 12 savings = [0] * 12 months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] getNotGreen(notGreenCost, months) getGoneGreen(goneGreenCost, months) energySaved(notGreenCost, goneGreenCost, savings) displayInfo(notGreenCost, goneGreenCost, savings, months)...
Java homework problem: I need the code to be able to have a message if I...
Java homework problem: I need the code to be able to have a message if I type in a letter instead of a number. For example, " Please input only numbers". Then, I should be able to go back and type a number. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class LoginGui {    static JFrame frame = new JFrame("JFrame Example");    public static void main(String s[]) {        JPanel panel...
I need an idea of Java code that will convert an integer (1 to 3,999) into...
I need an idea of Java code that will convert an integer (1 to 3,999) into roman numerals using if statements; arrays and loops sadly aren't allowed and that's all I can come up with.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT