the high score (65816 th d different scores in it). t Score. After all the score
ID: 3920542 • Letter: T
Question
the high score (65816 th d different scores in it). t Score. After all the scores have been read, output 6 in this example, but it could be different if the file L, reads in each score as an integer, and 0.7 Graphics Supple the ach PROGRAMMING PROJECTS Programmir usually be solved many different ways. amming Projects requir e more problem-soluing than Practice Programs and can Write a program that checks a text file for several formating and punctua tion matters. The program asks for the names of both an input file and a output file. It then copies all the text from the input file to the output fl but with the following two changes: (1) Any string of two or more blan characters is replaced by a single blank; (2) all sentences start with an u percase letter. All sentences after the first one begin after either a perio a question mark, or an exclamation mark that is followed by one or m whitespace characters. inting 10.10 that can write anExplanation / Answer
Hi.. I have written java program for the above. Please check the below code.
FilesWork.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class FilesWork {
private static Scanner input;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
input = new Scanner(System.in);
System.out.println("Enter input file name:");
String fname = input.next();
System.out.println("Enter output file name:");
String>
File f1 = new File(fname);
File f2 = new File(oname);
BufferedReader br = new BufferedReader(new FileReader(f1));
String line = br.readLine();
StringBuilder newFileWriter = new StringBuilder();
while(line!=null){
//String line1 = line.replaceAll("^ +| +$|( )+", "$1");
String line1 = line.replaceAll("\s{2,}", " ").trim();
if(line1.trim()!=""){
int pos = 0;
boolean capitalize = true;
StringBuilder sb = new StringBuilder(line1);
while (pos < sb.length()) {
if (sb.charAt(pos) == '.') {
capitalize = true;
} else if (capitalize && !Character.isWhitespace(sb.charAt(pos))) {
sb.setCharAt(pos, Character.toUpperCase(sb.charAt(pos)));
capitalize = false;
}
pos++;
}
String s = sb.toString();
String[] ab = s.split(" ");
String line2 = "";
if(ab.length>1){
ab[0]+="!";
line2 = String.join(" ", ab);
}else{
line2 = s;
}
//System.out.println(sb.toString());}
newFileWriter.append(line2+" ");
}
//System.out.println(line1);
line = br.readLine();
}
br.close();
BufferedWriter bw = new BufferedWriter(new FileWriter(f2));
bw.write(newFileWriter.toString());
bw.close();
System.out.println("File writed");
}
}
Output:
Enter input file name:
C:Userssanthoshkumar.g01Desktopinput.txt
Enter output file name:
C:Userssanthoshkumar.g01Desktopoutput.txt
File writed
input.txt
PROBLEMS WITH ORPHAN PROCESSES:
1. Wastage of computer resources Orphans cause two undesirable problems.
First, they waste resources- the orphan process is active without knowing the parent process is no more there at the
requesting end. The resources held by the orphan processes are remain locked and even it may lead to the problem of
deadlock for a period of time as shown in the figures 2(a) & 2(b) below. Here no preemption happens until the orphan
computation completes or the orphan process is killed. Fig.2(a). Deadlock by no preemption of CPU In Fig.2(a)., CPU
is held by the orphan process P1 and the process P2 is waiting for the CPU. Unfortunately, the CPU can be allocated
to P2 only after execution of P1 or after deliberate killing of process P1. Fig.2(b). Deadlock situation by locked
Database Whereas in Fig.2 (b)., the database is locked by the orphan process P1 and the process P2 wants a lock on
the same. The lock cannot be granted for P2 because P1 is having a lock with database. So, a deadlock occurs till
completion of the execution of P1. Forced preemption can be possible only by killing the orphan process P1 and hence
all the resources held by P1 can be freed. The resources locked by the orphan computations not only the CPU but may
also be the memory, databases etc. More importantly the valuable CPU time to compute the unwanted computation of the
orphan process is wasted. So, the orphan processes slow down the entire performance of the distributed systems.
P1 p2 P1 P2
CPU Database
Deadlock situation for both CPU and database
2. Data inconsistency As it has been discussed above, the orphan process leaves data in an inconsistent state.
Data inconsistency exists when different and conflicting versions of the same data appear in different places of the
system. Data inconsistency creates unreliable information, because it will be difficult to determine which version of
the data is correct. Consider a situation where two data variables (Figure 3), A and B are accessed from the database
by two different computations, say X and Y, which are initiated by two different processes. After the initiation of
the request X, its parent process is crashed and the computation X becomes an orphan and continue to execute and
makes changes in the values of A and B. After some time, as shown in the fig. 3, the computation Y starts and it
reads the value of A and does its calculations. In the meanwhile the transaction Y is a successful one and it has
read the value of A which has jut updated by the computation X. But, being an orphan process, the transaction X
should be rolled back. Now the value of A becomes an inconsistent one. Again, the computation Y works with the old
value of A and subsequent transactions should produce unexpected and unwanted results. See, what a disastrous
situation made by a single orphan! Suppose a situation where the computation X makes another RPC to get a service
from any other server and the server itself again makes another request to any other server and hence a chain of
transactions or we say nested transactions. Then, after all these chain of requests the transaction X becomes an
orphan and the situation becomes more vulgar. It is concluded that the inconsistency made by an orphan process is
exponential!
The only solution is to kill the orphan immediately after their birth. Not make any wait to kill them!
Computation X
Read (A)
A: =A-100
Write (A) Read (B)
Computation Y
B: = B+ARead (A) time
Write (B) A: = A+100
Write (A)
Two parallel computations X and Y and X becomes
an orphan during its execution
Output.txt
PROBLEMS! WITH ORPHAN PROCESSES:
1.! Wastage of computer resources Orphans cause two undesirable problems.
First,! they waste resources- the orphan process is active without knowing the parent process is no more there at the
Requesting! end. The resources held by the orphan processes are remain locked and even it may lead to the problem of
Deadlock! for a period of time as shown in the figures 2(a) & 2(b) below. Here no preemption happens until the orphan
Computation! completes or the orphan process is killed. Fig.2(a). Deadlock by no preemption of CPU In Fig.2(a)., CPU
Is! held by the orphan process P1 and the process P2 is waiting for the CPU. Unfortunately, the CPU can be allocated
To! P2 only after execution of P1 or after deliberate killing of process P1. Fig.2(b). Deadlock situation by locked
Database! Whereas in Fig.2 (b)., the database is locked by the orphan process P1 and the process P2 wants a lock on
The! same. The lock cannot be granted for P2 because P1 is having a lock with database. So, a deadlock occurs till
Completion! of the execution of P1. Forced preemption can be possible only by killing the orphan process P1 and hence
All! the resources held by P1 can be freed. The resources locked by the orphan computations not only the CPU but may
Also! be the memory, databases etc. More importantly the valuable CPU time to compute the unwanted computation of the
Orphan! process is wasted. So, the orphan processes slow down the entire performance of the distributed systems.
P1! p2 P1 P2
CPU! Database
Deadlock! situation for both CPU and database
2.! Data inconsistency As it has been discussed above, the orphan process leaves data in an inconsistent state.
Data! inconsistency exists when different and conflicting versions of the same data appear in different places of the
System.! Data inconsistency creates unreliable information, because it will be difficult to determine which version of
The! data is correct. Consider a situation where two data variables (Figure 3), A and B are accessed from the database
By! two different computations, say X and Y, which are initiated by two different processes. After the initiation of
The! request X, its parent process is crashed and the computation X becomes an orphan and continue to execute and
Makes! changes in the values of A and B. After some time, as shown in the fig. 3, the computation Y starts and it
Reads! the value of A and does its calculations. In the meanwhile the transaction Y is a successful one and it has
Read! the value of A which has jut updated by the computation X. But, being an orphan process, the transaction X
Should! be rolled back. Now the value of A becomes an inconsistent one. Again, the computation Y works with the old
Value! of A and subsequent transactions should produce unexpected and unwanted results. See, what a disastrous
Situation! made by a single orphan! Suppose a situation where the computation X makes another RPC to get a service
From! any other server and the server itself again makes another request to any other server and hence a chain of
Transactions! or we say nested transactions. Then, after all these chain of requests the transaction X becomes an
Orphan! and the situation becomes more vulgar. It is concluded that the inconsistency made by an orphan process is
Exponential!
The! only solution is to kill the orphan immediately after their birth. Not make any wait to kill them!
Computation! X
Read! (A)
A:! =A-100
Write! (A) Read (B)
Computation! Y
B:! = B+ARead (A) time
Write! (B) A: = A+100
Write! (A)
Two! parallel computations X and Y and X becomes
An! orphan during its execution
Please check it and let me know any querries. Thank you. All the best.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.