Question: in Java Please Read The Input One Line at A Time, and For Each Line, Output The Minimum of All Li..
Question: in Java Please Read The Input One Line at A Time, and For Each Line, Output The Minimum of All Li..
home / study / engineering / computer science / computer science questions and answers...
Find solutions for your homework
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;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
/**
* @throws IOException
*/
/**
* and System.out or from filenames specified on the command line, then call doIt.
* @param args
*/
try {
BufferedReader r;
PrintWriter w;
if (args.length == 0) {
w = new PrintWriter(System.out);
} else if (args.length == 1) {
w = new PrintWriter(System.out);
} else {
doIt(r, w);
w.flush();
} 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;
/**
* @param r
* @param w
* @throws IOException
*/
throws IOException {
PriorityQueue<String> q = new
PriorityQueue<String>();
if (line.startsWith("#")) {
if (!q.isEmpty()) {
q.remove();
} else {
if (line.startsWith("c")) {
q.add(line);
// removing it
if (!q.isEmpty()) {
System.out.println(q.peek());
} else {
// else printing *
System.out.println("*");
/**
* @param args
*/
try {
BufferedReader r;
PrintWriter w;
if (args.length == 0) {
w = new PrintWriter(System.out);
} else if (args.length == 1) {
w = new PrintWriter(System.out);
} else {
doIt(r, w);
w.flush();
} catch (IOException e) {
System.err.println(e);
System.exit(-1);
/*
*INPUT*/
berry
apple
cheer
car
car
###
###
add
/*OUTPUT*/
cheer
car
car
car
cheer
cheer
PLEASE UPVOTE 🙏
0 Comment
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