PROGRAMMING ASSIGNMENT #2 Type-Justification in Word Processing Generally speaki
ID: 3679612 • Letter: P
Question
PROGRAMMING ASSIGNMENT #2
Type-Justification in Word Processing
Generally speaking, the success of computing is attributable to the exciting growth in word processing. One important function in word processing systems is type-justification, i.e., the alignment of words to both the left and the right margins of the page. This generates professional looking documents. Type justification can be accomplished on computer systems by inserting one or more characters between each of the words in a line, so that the rightmost word aligns with the right margin.
Write a program that reads text from a text file and prints this text in a type-justified format. Your program should print between 40 characters per line and 100 characters per line.
As in the previous assignment, your program should start by asking the user for the name of the file in which the text is stored. Then, user should be asked to type in the number of characters per line in the justified (left and right aligned) printout. Your program should proceed by reading the text (read the whole file) and converting it in the internal representation. After the file is read, the program should immediately close the file, before any type justification processing starts. Please note that 25% of the grade will be taken off from programs that do not conform with the specification for reading the input file. The program MUST then ask for the line size. Please have in mind that the input file can contain any ASCII character, not just letters or numbers.
Since there are no limitations on the length of the text, the internal representation should be built using a dynamic data structure. Each token (word, number, punctuation mark, etc.) from the input file should be stored internally in a data structure, consisting of a string, a pointer to the next word from the text and, possibly, some additional information. For example, let us assume the input file para1.txt contains the following paragraph.
A knowledge and appreciation of interrelationship between places and events is essential if we are to make informed decisions about their place in the
world community. Therefore,
the extent of basic geographic and historical literacy may play crucial role in decision making in global international markets.
An abstraction of the internal representation of this paragraph is:
A knowledge and appreciation of interrelationship between places and events is essential if we are to make informed decisions about their place in the world community . Therefore , the extent of basic geographic and historical literacy may play crucial role in decision making in global international markets . EOF
Please note that this representation ignores all the white space characters, as they are not important for type-justification. Your program should create the left and right aligned printout of this (and any other) paragraph by placing exactly the required number of characters (including white spaces) per line. Words in the text must not be hyphenated (split across 2 lines). The white spaces included in a line of printout should be distributed throughout the line. Each line should contain the maximal possible number of words, i.e., do not start a new line unless the next word in the list cannot be fit in the current line. Make your own decisions regarding the policy of inserting white space characters (padding the line). Make sure that your program frees dynamically allocated memory space once it is not needed. One of the valid printouts of the paragraph from file para1.txt using a 65 characters long line is as follows:
A knowledge and appreciation of interrelationship between places and events is essential if we are to make informed decisions about their place in the world community. Therefore, the extent of basic geographic and historical literacy may play crucial role in decision making in global international markets.
However, if the user wants the same text right-aligned with 72 characters per line, the result is as follows:
A knowledge and appreciation of interrelationship between places and events is essential if we are to make informed decisions about their place in the world community. Therefore, the extent of basic geographic and historical literacy may lay crucial role in decision making in global international markets.
Once you solve the problem of type-justification of a paragraph, the next step is to allow input files that contain multiple paragraphs. The indication of a new paragraph in the input file will be the two or more “new line” characters in sequence. Your program may treat paragraphs independently and create different linked lists for each.
Submission
In order to be graded, you must submit the following:
Submit all the program source files through Moodle. It is your responsibility to submit the right files successfully.
Submit program documentation through Moodle. The documentation should contain a short description of program’s main data structures (what is the structure you use for representing the tokens, which fields it includes, is it a single or double linked list, etc.), description of your algorithm (how do you determine the position for line breaks, how do you treat punctuation characters, your approach to padding with white space characters...), and the algorithm imposed limitations (if any). If your program does not work properly or if it does not compile, state this clearly in the documentation.
Please make sure that your program is well documented, use in-line comments to explain what certain parts of your program do. The first few lines of the code file(s) MUST contain your name as a comment. Please submit a README.txt file with compilation instructions too.
Please note that Moodle keeps track of the submission time. You are advised to keep a copy of your submission in your own koding.com account (with the original time-stamp). To receive full credit, your program must be submitted by the deadline. Remember that late assignments will be penalized by 5% of the grade for each day of delay. No programs or their accompanying documentation will be accepted after midnight, Saturday, March 20th.
Programs that cannot be compiled will receive up to 30% of the assignment grade. Only well written, well documented, fully functional programs, will receive a full credit.
Explanation / Answer
skeleton code version 1: ( please see version 2 below as well)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package wordprocess1;
/*
The README.txt with compile and run instructions
1. open java net beans ide
2. Click main menu File new project
select java application
type this whole code in to that code window
click tha play button ( the right side facing triangle symbol on top menu bar in the net beand IDE
the program will run and show you the output
or simply press F6 - same effect as clicking the play button
*/
import java.util.Scanner; // for user input
/**
*
* @author Admin
*/
public class WordProcess1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
String fileNm = " "; // file name
System.out.println("Hey, You, Greetings of the day, please enter the file name having the text to be processed....., thanks....:");
fileNm = sc.next(fileNm); // 1. read a flat file or text file
// num of characters per line
System.out.println("please enter the number of characters per line.....:");
int numOfCharPerLn;
numOfCharPerLn = sc.nextInt();
// read the file
// close the file straight away
// start processing - real coding starts here actually!!!!!
/*
Algorithm or steps:
1. read a flat file or text file
2. print out 40 to 100 characters per line
3. get the file name from user
4. get the number of characters per line from user
a. (optional can validate that input to be between 40 to 100)
5. read the whole file and store it in an array ( like how Perl language would do – same can be done in java as well)
6. close the file now
7. start processing for indenting and
type justifications
8.
9.
10.
*/
}
}
code version 2: (meat filled in):
/*
changes or new code introduced since last version:
import java.io.*;
FileInputStream ip = null; // ip = input
ip = new FileInputStream(fileNm);
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package wordprocess1;
/*
The README.txt with compile and run instructions
1. open java net beans ide
2. Click main menu File new project
select java application
type this whole code in to that code window
click tha play button ( the right side facing triangle symbol on top menu bar in the net beand IDE
the program will run and show you the output
or simply press F6 - same effect as clicking the play button
*/
import java.util.Scanner; // for user input
import java.io.*; // for file read, buffered reader etc
/**
*
* @author Admin
*/
public class WordProcess1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
String fileNm = " "; // file name
System.out.println("Hey, You, Greetings of the day, please enter the file name having the text to be processed....., thanks....:");
fileNm = sc.next(fileNm); // 1. read a flat file or text file
// num of characters per line
System.out.println("please enter the number of characters per line.....:");
int numOfCharPerLn;
numOfCharPerLn = sc.nextInt();
FileInputStream ip = null; // ip = input
ip = new FileInputStream(fileNm);
String[] fileContents = new String[10000]; // 10,000 words must be long enough - can change on demand
BufferedReader bufRdr;
//bufRdr = new BufferedReader(new InputStreamReader(new FileInputStream(ip)));
String ln1 = null ; // ln 1 = line 1
int char1;
String str1; // str1 = string1 = a temperory string
char1 = ip.read();
/*
while(( ln1 = bufRdr.readLine()) != null ) { // while not end of file keep reading
// break and exit out of loop upon hitting end of file character
String [] words1 = ln1.split("\s+")
String word1 = words1[0];
String w2 = words1[1]; // etc for as many words as needed
} // end while
*/
}
}
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.