0% found this document useful (0 votes)
28 views2 pages

Question: in Java Please Read The Input One Line at A Time, and For Each Line, Output The Minimum of All Li..

Uploaded by

Mr Perfect
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views2 pages

Question: in Java Please Read The Input One Line at A Time, and For Each Line, Output The Minimum of All Li..

Uploaded by

Mr Perfect
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

  Home Study tools  My courses  My books Career Life 

home / study / engineering / computer science / computer science questions and answers...
Find solutions for your homework

Question: In java please


Read the input one line at a time, and for each line, output the
minimum of all li...

in java please

Read the input one line at a time, and for each line, output the
minimum of all lines so far that start with ‘c’.
If you ever read
in the line ‘###’ remove the current minimum line that starts with
‘c’ before printing the
(new) minimum, and continue. As with
question 1, if no minimum exists, print a ‘*’ on its own line. When
removing the minimum line, if no such minimum exists, just
continue. For example, input lines “berry”,
“apple”, “cheer”,
“car”, “car”, “###”, “###” “add” would result in the output “*”,
“*”, “cheer”, “car”, “car”, “car”,
“cheer”, “cheer”

package comp2402a4;

// Thanks to Pat Morin for this file!

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Part2 {

/**

* Your code goes here - see Part0 for an example

* @param r the reader to read from

* @param w the writer to write to

* @throws IOException

*/

public static void doIt(BufferedReader r, PrintWriter w) throws IOException {

//TODO: Your solution goes here.

/**

* The driver. Open a BufferedReader and a PrintWriter, either from System.in

* and System.out or from filenames specified on the command line, then call doIt.

* @param args

*/

public static void main(String[] args) {

try {

BufferedReader r;

PrintWriter w;

if (args.length == 0) {

r = new BufferedReader(new InputStreamReader(System.in));

w = new PrintWriter(System.out);

} else if (args.length == 1) {

r = new BufferedReader(new FileReader(args[0]));

w = new PrintWriter(System.out);

} else {

r = new BufferedReader(new FileReader(args[0]));

w = new PrintWriter(new FileWriter(args[1]));

long start = System.nanoTime();

doIt(r, w);

w.flush();

long stop = System.nanoTime();

System.out.println("Execution time: " + 1e-9 * (stop-start));

} catch (IOException e) {

System.err.println(e);

System.exit(-1);

Expert Answer

Anonymous
answered this

package comp2402a4;

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.util.PriorityQueue;

public class Part2 {

/**

* Your code goes here - see Part0 for an example

* @param r

* the reader to read from

* @param w

* the writer to write to

* @throws IOException

*/

public static void doIt(BufferedReader r, PrintWriter w)

throws IOException {

// defining a priority queue to store strings starting with 'c'


in

// sorted order (min to max)

PriorityQueue<String> q = new
PriorityQueue<String>();

// looping through each line

for (String line = r.readLine(); line != null; line =


r.readLine()) {

// checking if line starts with #

if (line.startsWith("#")) {

// removing minimum value from q if q is not empty

if (!q.isEmpty()) {

q.remove();

} else {

// otherwise if line starts with 'c' (remember: case


sensitive),

// simply adding to the q

if (line.startsWith("c")) {

q.add(line);

// if q is not empty, printing next element of queue without

// removing it

if (!q.isEmpty()) {

System.out.println(q.peek());

} else {

// else printing *

System.out.println("*");

/**

* The driver. Open a BufferedReader and a PrintWriter, either


from

* System.in and System.out or from filenames specified on the


command line,

* then call doIt.

* @param args

*/

public static void main(String[] args) {

try {

BufferedReader r;

PrintWriter w;

if (args.length == 0) {

r = new BufferedReader(new InputStreamReader(System.in));

w = new PrintWriter(System.out);

} else if (args.length == 1) {

r = new BufferedReader(new FileReader(args[0]));

w = new PrintWriter(System.out);

} else {

r = new BufferedReader(new FileReader(args[0]));

w = new PrintWriter(new FileWriter(args[1]));

long start = System.nanoTime();

doIt(r, w);

w.flush();

long stop = System.nanoTime();

System.out.println("Execution time: " + 1e-9 * (stop -


start));

} catch (IOException e) {

System.err.println(e);

System.exit(-1);

/*

*INPUT*/

berry

apple

cheer

car

car

###

###

add

/*OUTPUT*/

cheer

car

car

car

cheer
cheer

Execution time: 0.054466800000004

PLEASE UPVOTE 🙏
0 Comment

Was this answer helpful? 0

COMPANY LEGAL & POLICIES CHEGG PRODUCTS AND SERVICES CHEGG NETWORK CUSTOMER SERVICE
About Chegg Advertising Choices Cheap Textbooks Mobile Apps EasyBib Customer Service
Chegg For Good Cookie Notice Chegg Coupon Sell Textbooks Internships.com Give Us Feedback
College Marketing General Policies Chegg Play Solutions Manual Thinkful Help with eTextbooks
Corporate Development Intellectual Property Rights Chegg Study Help Study 101 Help to use EasyBib Plus
Investor Relations Terms of Use College Textbooks Textbook Rental Manage Chegg Study
Jobs Global Privacy Policy eTextbooks Used Textbooks Subscription
Join Our Affiliate Program DO NOT SELL MY INFO Flashcards Digital Access Codes Return Your Books
Media Center Honor Code Learn Chegg Money Textbook Return Policy
Site Map Honor Shield Chegg Math Solver

© 2003-2021 Chegg Inc. All rights reserved.

You might also like