Question

Java coding. Create a class called Printer and a class called Job that will simulate a...

Java coding.

Create a class called Printer and a class called Job that will simulate a computer printer. The printer should store a queue of Job objects until they are printed. Each Job should consist of a title and the number of pages the job contains. The printer should also store the number of jobs it can print per minute. The class should include methods to add jobs and print jobs. The method that prints a job should return the amount of time it took to print the job. Create a driver program that processes an input file that contains one of two directions on each line.

The directions are: 1. Add Title # Pages– Add the specified Job to the printer

2. Print n – Print the specified number of jobs

Each instruction is on a separate line and each token of each instruction is separated by a single tab character. Each time a job is printed, determine how long it would take to print the job and keep track of how much time has been spent printing all together. When the whole file has been processed, print a summary that includes the total number of jobs and pages printed and the total amount of time spent printing.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Three classes are used in the solution.

  1. Printer class
  2. Driver class,
  3. PrintJobResponse - this class is used to send print details like job count, time and pages to driver for total

Code Details:

  1. Provide full path of the input file in variable fileName in Driver class with input formatted properly as mentioned in the problem
  2. Number of jobs Printer can print per min can be configured using jobsPerMin variable in Driver class.
  3. Total time is provided in min which can be a float value.

I am also adding a sample input file at the end.

How To Run:

  • Import the code in any IDE and then Run the Driver class

Below is the code:

1. Printer Class:

import java.util.LinkedList;
import java.util.List;

public class Printer {
    class Job {
        String title;
        int pages;
        Job(String title, int pages) {
            this.title = title;
            this.pages = pages;
        }
    }

    private List<Job> jobs;
    private int jobsPerMin;

    public Printer(int jobsPerMin) {
        jobs = new LinkedList<>();
        this.jobsPerMin = jobsPerMin;
    }

    public void addJobs(String title, int pages) {
        Job newJob = new Job(title, pages);
        jobs.add(newJob);
    }

    public PrintJobResponse printJobs(int jobCount) {
        if(jobCount > jobs.size())
            jobCount = jobs.size();

        if(jobs.size() == 0) {
            System.out.println("No jobs present.");
            return null;
        }
        int printPages = 0;

        for(int i=0; i<jobCount; i++) {
            Job job = jobs.get(0);
            printPages += job.pages;
            jobs.remove(0);
        }
        return new PrintJobResponse((float) jobCount/jobsPerMin, printPages, jobCount);
    }
}

2. Driver Class:

import java.io.File;
import java.util.Scanner;

public class Driver {
    private static final String ADD = "Add";
    private static final String PRINT = "Print";

    public static void main(String[] args) {
        String fileName = "<fullFilePath>", line, command;
        String[] commandLine;
        int jobsPerMin = 30;
        int totalJobs = 0, totalPages = 0;
        float totalTime = 0;

        try {
            File f = new File(fileName);
            Scanner sc = new Scanner(f);
            Printer printer = new Printer(jobsPerMin);
            PrintJobResponse jobResponse;

            while(sc.hasNextLine()) {
                line = sc.nextLine();
                commandLine = line.split(" ");
                command = commandLine[0];
                switch (command) {
                    case ADD:
                        String title = commandLine[1].trim();
                        int pages = Integer.parseInt(commandLine[3].trim());
                        printer.addJobs(title, pages);
                        break;
                    case PRINT:
                        int printJobs = Integer.parseInt(commandLine[1].trim());
                        jobResponse = printer.printJobs(printJobs);
                        totalJobs += jobResponse.getPrintJobs();
                        totalPages += jobResponse.getPrintPages();
                        totalTime += jobResponse.getPrintTime();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("Total Jobs: " + totalJobs);
        System.out.println("Total Pages Print: " + totalPages);
        System.out.println("Total Time in Min: " + totalTime);
    }
}

3. PrintJobResponse class:

public class PrintJobResponse {
    private float printTime;
    private int printPages;
    private int printJobs;

    public PrintJobResponse(float printTime, int printPages, int printJobs) {
        this.printTime = printTime;
        this.printPages = printPages;
        this.printJobs = printJobs;
    }

    public float getPrintTime() {
        return printTime;
    }

    public void setPrintTime(float printTime) {
        this.printTime = printTime;
    }

    public int getPrintPages() {
        return printPages;
    }

    public void setPrintPages(int printPages) {
        this.printPages = printPages;
    }

    public int getPrintJobs() {
        return printJobs;
    }

    public void setPrintJobs(int printJobs) {
        this.printJobs = printJobs;
    }
}

4. Sample Input File:

Add Title-1 # 5
Add Title-2 # 3
Add Title-3 # 5
Add Title-4 # 8
Print 3
Print 5
Print 3
Add a comment
Know the answer?
Add Answer to:
Java coding. Create a class called Printer and a class called Job that will simulate a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • On average, a printer takes 4 mins to print a file. You send a job to the printer at 10:00am, and...

    On average, a printer takes 4 mins to print a file. You send a job to the printer at 10:00am, and it appears to be the 3rd in line. Using this information answer the following (a) Suppose you have to leave for class by 10:15am. What is the probability that you wll be able to get the print out to class? (b) What is the probability that it will take between 7 and 13 mins for your file to finish...

  • 7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food...

    7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food to represent a Lunch food item. Fields include name, calories, carbs In a main method (of a different class), ask the user "How many things are you going to eat for lunch?". Loop through each food item and prompt the user to enter the name, calories, and grams of carbs for that food item. Create a Food object with this data, and store each...

  • in java Create a new project named, firstInitialLastNameCS185EOS. Create and use a class that prints a title.   Create a use a menu that contains the following items, and calls corresponding methods...

    in java Create a new project named, firstInitialLastNameCS185EOS. Create and use a class that prints a title.   Create a use a menu that contains the following items, and calls corresponding methods that do the tasks the menu describes. Use classes when they're appropriate. Keep the driver class as simple as possible. Give each class, method and property simple, descriptive names using Hungarian notation. Square a number Simply pass a decimal-type number to the object that does this task. Process its...

  • [Java] Create a class called FileExercises that contains the following method Method encrypt that does not...

    [Java] Create a class called FileExercises that contains the following method Method encrypt that does not return anything and takes a shift, input filename and output filename as arguments. It should read the input file as a text file and encrypt the content using a Caesar cypher with the shift provided. The resulting text should be placed in the output file specified. (If the output file already exists, replace the existing file with a new file that contains the required...

  • Problem 2. Jobs sent to a printer are held in a buffer until they can be printed. Jobs are printe...

    Problem 2. Jobs sent to a printer are held in a buffer until they can be printed. Jobs are printed sequentially on a first-come, first-served basis. Jobs arrive at the printer at the rate of four per minute. The average time to print a job is 10 seconds. Assume M/M/1 queuing and steady state. (a) Find λA and λS (b) Find the expected value and standard deviation of the number of jobs in this system at any time. (c) On...

  • This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This...

    This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This program will use the ArrayManager class. The final version of the program is described below, but initially use it to test your ArrayManager class as you write it. Complete the ArrayManager class as specified below: The class will have exactly two instance variables:  An array of integers  A count of the number of elements currently in the array The class will have...

  • Create a new Java class called: House public class House Remember that Java is case-sensitive. This...

    Create a new Java class called: House public class House Remember that Java is case-sensitive. This means that the class name Diamond should start with a capital letter, and then the rest of the letters should be all lower-case. It would work if you were to use any combination of upper and lower-case letters, but that idea does not follow the assignment instructions. Write a Java application program that prints, the following house drawing to the screen: The house is...

  • Please use JAVA only Write a Printer device, which also derives from generic devices, but this...

    Please use JAVA only Write a Printer device, which also derives from generic devices, but this time includes the notion of jobs to be printed. Like Disk, a Printer should extend from Device. Additionally, a Printer should have an intwhich represents the number of jobs currently in the printer's print queue. We can add or complete print jobs, or check the number currently in the queue, but for this assignment that is the extent of what we expect (we will...

  • Create a new class called DemoSortingPerformacne Create a private method called getRandomNumberArray. It returns an array...

    Create a new class called DemoSortingPerformacne Create a private method called getRandomNumberArray. It returns an array of Integer and has 2 parameters: arraySize of type int and numberOfDigits of type int. This method should create an array of the specified size that is filled with random numbers. Each random numbers should have the same number of digits as specified in the second parameter In your main method (or additional private methods) do the following: Execute selection sort on quick sort...

  • Java - In NetBeans IDE: • Create a class called Student which stores: - the name...

    Java - In NetBeans IDE: • Create a class called Student which stores: - the name of the student - the grade of the student • Write a main method that asks the user for the name of the input file and the name of the output file. Main should open the input file for reading . It should read in the first and last name of each student into the Student’s name field. It should read the grade into...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT