Coder Social home page Coder Social logo

java-threads-lab-2's Introduction

Threads Lab 2

Instruction

You’re given a Pizza class that has methods for each step of cooking the pizza. Each of the methods except for the cookPizza method extends the Thread class and overrides the run method.

The cookPizza method is not giving the expected output. Your task is to fix it so that the output looks like the following:

cook base
cover base with sauce
slice tomatoes 3
slice tomatoes 2
slice tomatoes 1
put tomatoes on pizza
grate cheese on pizza
to bake...4 min
to bake...3 min
to bake...2 min
to bake...1 min
Your pizza is ready!

Starter Code

Main.java

class Main {
    public static void main(String[] args) throws InterruptedException {
        Pizza.cookPizza();
    }
}

Pizza.java

public class Pizza {
    //Fix this method
    public static void cookPizza() throws InterruptedException {

        Base base = new Base();
        Sauce sauce = new Sauce();
        Tomatoes tomatoes = new Tomatoes();
        Cheese cheese = new Cheese();
        Bake bake = new Bake();

        java.util.List<Thread> cookingSteps = new java.util.ArrayList<>();
        cookingSteps.add(base);
        cookingSteps.add(sauce);
        cookingSteps.add(tomatoes);
        cookingSteps.add(cheese);
        cookingSteps.add(bake);

        for (Thread step : cookingSteps) {
            step.start();
        }
    }

    // DO NOT change any of the methods below
    static class Base extends Thread {
        @Override
        public void run() {
            System.out.println("cook base");
        }
    }

    static class Sauce extends Thread {
        @Override
        public void run() {
            System.out.println("cover base with sauce");
        }
    }

    static class Tomatoes extends Thread {
        @Override
        public void run() {
            for (int i = 3; i >= 1; i--) {
                System.out.println("slice tomatoes " + i);
            }
            System.out.println("put tomatoes on pizza");
        }
    }

    static class Cheese extends Thread {
        @Override
        public void run() {
            System.out.println("grate cheese on pizza");
        }
    }

    static class Bake extends Thread {
        @Override
        public void run() {
            for (int i = 4; i >= 0; i--) {
                if (i == 0) {
                    System.out.println("Your pizza is ready!");
                    break;
                }
                System.out.println("to bake..." + i + " min");

            }
        }
    }
}

java-threads-lab-2's People

Contributors

alveem avatar leeyoh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.