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

I need this Java code translated into C Code. Thank you. //Logical is the main public...
I need this Java code translated into C Code. Thank you. //Logical is the main public class public class Logical { public static void main(String args[]) { char [][] table= {{'F','F','F'},{'F','F','T'},{'F','T','F'},{'F','T','T'},{'T','F','F'},{'T','F','T'},{'T','T','F'},{'T','T','T'}}; // table contains total combinations of p,q,& r int totalTrue, totalFalse, proposition;    //proposition 1: proposition=1; start(proposition); totalTrue=0; totalFalse=0; for(int i=0;i<8;i++) {    { char o= conjuctive(implecation(negation(table[i][0]),table[i][1]),implecation(table[i][2],table[i][0])); System.out.println(" "+table[i][0]+" "+table[i][1]+" "+table[i][2]+" "+o); if(o=='T') totalTrue++; else totalFalse++;    } } finalOutput(totalTrue,totalFalse,proposition); System.out.println(" "); System.out.println(" ");    //proposition 2: proposition=2; start(proposition);...
I need this code translated to C code. Thank you. public class FourColorTheorem { public static...
I need this code translated to C code. Thank you. public class FourColorTheorem { public static boolean isPrime(int num) { // Corner case if (num <= 1) return false; // Check from 2 to n-1 for (int i = 2; i < num; i++) if (num % i == 0) return false; return true; } public static void main(String[] args) { int squares[] = new int[100]; for (int i = 1; i < squares.length; i++) squares[i-1] = i * i;...
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 convert this java code to C language. There is no string can be used...
I need convert this java code to C language. There is no string can be used in C. Thank you! import java.util.Scanner; public class Nthword { public static void main( String args[] ) { String line; int word; Scanner stdin = new Scanner(System.in); while ( stdin.hasNextLine() ) { line = stdin.nextLine(); word = stdin.nextInt(); stdin.nextLine(); // get rid of the newline after the int System.out.println( "Read line: \"" + line + "\", extracting word [" + word + "]" );...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public static void main( String args[] ) { Scanner input = new Scanner( System.in ); // create array to hold user input int nums[] = new int[10]; int i = 0, truthCount = 0; char result = 'F', result2 = 'F'; // ask user to enter integers System.out.print("Please Enter 10 Different integers: "); // gather input into array for ( i = 0; i <...
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...
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...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT