473,396 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

A program for printing a paragraph in MLA style

12
Hi all,

I have an assignment and I need some help with it. This is the assignment
//
Research reports are often required to conform to a given standard such as APA or MLA. These standards specify things like the size of the margins and formats for titles, paragraphs, references, page numbers, etc. Suppose that a program is to produce a report conforming to a standard. It would be desirable to provide utility class to automatically conform to these standards. That way the application need only be concerned about supplying the content. The utility class would provide methods such as printParagraph(String test), printTitle(String title), printReference(String author, String title, int year, String publisher), printLine(String) and breakPage().

Develop a utility class to provide the above listed methods. Define a constructor for the class that takes number of columns in a page, number of lines in a page, and the author’s name. The class is to perform the following formatting:

1- 5 spaces for left and right margin
2- first line of every page after the first page is to print left justified the authors name and page number.
3- the second and third lines (at the top), and the last 5 lines of each page are to be blank. The first line of the first page is also blank.
4- the print title is to center the title across the page. Note the tile may contain embedded new line characters, if so center and print each line. A blank line is to follow the title.
5- the printParagraph is to perform word wrap. The first line of the paragraph is to be indented 5 spaces.
6- the printReference is to format a reference paragraph. That is the paragraph is to have a comma between each item, perform word wrap if needed with a hanging indent of 5 spaces on lines past the first. A blank line is printed after a reference.
7- the breakPage() method will print enough blank lines to complete the current page.

Have your printLine method throw a RuntimeException in the event that the String passed exceeds the available number of columns (i.e. number of columns in a page less the margins.

Include getter methods to get the instance variables (e.g. include a getAuthor() so that the class can to printTitle(getAuthor()) to add the author centered as a title.

Write a test program to thoroughly test your class.

Bonus 3 points: Allow and additional left and right margin to be specified. Thus if a user wanted to format a paragraph that had an extra indent on both the left and right, their program could call the methods to set the indent before print paragraph then reset the extra margin to 0 afterwards. If you do this task make sure you have a test case that demonstrates it is working correctly.

This is the class that I did:

Expand|Select|Wrap|Line Numbers
  1. public class Utility {
  2.  
  3.     private int columns;
  4.     private int lines;
  5.     private String author;
  6.  
  7.     Utility (int col, int lin, String aut){
  8.     columns = col;
  9.     lines = lin;
  10.     author = aut;
  11.     }
  12.     public void printParagraph(String test){
  13.     }
  14.     public void printTitle(String title){
  15.     }
  16.     public void printReference(String author, String title,  int year, String publisher){
  17.     }
  18.     public void printLine(String line){
  19.     }
  20.     public void breakPage(){
  21.  
  22.     }
  23.  
  24. }
  25.  
.
Sep 23 '07 #1
14 4256
JosAH
11,448 Expert 8TB
Pardon me for saying so but I smell a bit of TeX and LaTeX here ...

kind regards,

Jos
Sep 23 '07 #2
wshaer
12
Pardon me for saying so but I smell a bit of TeX and LaTeX here ...

kind regards,

Jos
Are these methouds or classes and are they in the API doc?
Sep 23 '07 #3
Nepomuk
3,112 Expert 2GB
Are these methouds or classes and are they in the API doc?
No, TeX and LaTeX aren't Part of Java. According to Wikipedia LaTeX is a document markup language and document preparation system for the TeX typesetting program (which is a pretty good description). LaTeX is often used in scientific work (loads of Books in natural sciences are written with LaTeX) as TeX was originally designed, to make texts easy to create without the author having to think about the layout (and LaTeX is an extension to that by Leslie Lamport).

For more information, check Wikipedia.

There might be LaTeX libraries for Java available somewhere, but I think what JosAH meant was, that you're supposed to invent the wheel a second time.

Greetings,
Nepomuk
Sep 24 '07 #4
wshaer
12
No, TeX and LaTeX aren't Part of Java. According to Wikipedia LaTeX is a document markup language and document preparation system for the TeX typesetting program (which is a pretty good description). LaTeX is often used in scientific work (loads of Books in natural sciences are written with LaTeX) as TeX was originally designed, to make texts easy to create without the author having to think about the layout (and LaTeX is an extension to that by Leslie Lamport).

For more information, check Wikipedia.

There might be LaTeX libraries for Java available somewhere, but I think what JosAH meant was, that you're supposed to invent the wheel a second time.

Greetings,
Nepomuk
Thanks for your explanation. But I don't think that's what my instructor wants because we are in a 1st level class and we are just begninrs with java.

I found some text methods in API but my problem is how to set up the number of lines?
Sep 24 '07 #5
wshaer
12
Is it possibal to do it with the format out put print out?
Sep 24 '07 #6
Nepomuk
3,112 Expert 2GB
Is it possibal to do it with the format out put print out?
I think, you'll have to do a lot of that manually. For example, you could have a Collection of something, that represents one page. Then you fill this with the contents with various methods like writeParagraph or newLine.

You may also want to have a look at System.out.printf() and format specifiers.

Otherwise, you should just try getting started and ask, when you get stuck.

Greetings,
Nepomuk
Sep 24 '07 #7
wshaer
12
This is what I came up with:

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class Utility {    
  3.     private int numberColumns;
  4.     private int numberLines;
  5.     private String author;
  6.     private int pageNumber;
  7.     private final int lMargin = 5;
  8.     private final int rMargin = 5;
  9.     private final int tMargin = 3;
  10.     private final int bMargin = 5;
  11.     private int lengthForCenter;
  12.     private int lineNumber;
  13.  
  14.  
  15.     public Utility(int numberColumns, int numberLines, String author){
  16.  
  17.         this.author = author;
  18.         this.numberColumns = numberColumns;
  19.         this.numberLines = numberLines;
  20.         this.pageNumber = 1;
  21.  
  22.  
  23.     }
  24.  
  25.     public void printParagraph(String testParagraph){
  26.         int index= 0;
  27.         if (testParagraph.charAt(index) == ' ') {
  28.  
  29.             testParagraph.substring(10);    
  30.         }
  31.         else{
  32.             index++;
  33.         }
  34.  
  35.  
  36.     }
  37.     public void printTitle(String title){
  38.         String line = "";
  39.         for (int x = 0; x<this.tMargin; x++){
  40.             line += " ";
  41.  
  42.         }
  43.         lengthForCenter = (numberColumns - title.length())/2;
  44.  
  45.         for(int y =0; y < lengthForCenter; y++){
  46.             line += " ";
  47.         }
  48.  
  49.         line += title;
  50.         printLine(line);
  51.         printLine("");
  52.  
  53.  
  54.          }
  55.  
  56.  
  57.     private void printLine(String line) {
  58.         if(lineNumber == numberLines - bMargin){
  59.             System.out.println();
  60.             if(lineNumber == 5){
  61.                 pageNumber++;
  62.             }
  63.  
  64.             lineNumber = 0;
  65.         }
  66.         System.out.println(line);
  67.  
  68.     }
  69.  
  70.     public void printReference(String author, String title, int year, String publisher){
  71.  
  72.     }
  73.     public String getAuthor() {
  74.         return author;
  75.     }
  76.     public int getNumberColumns() {
  77.         return numberColumns;
  78.     }
  79.     public int getNumberLines() {
  80.         return numberLines;
  81.     }
  82.  
  83.  
  84. }
  85.  
  86.  
This is the driving class

Expand|Select|Wrap|Line Numbers
  1. public class TestUtility {
  2.    public static void main(String[] args) {
  3.        Utility util = new Utility(50, 25, "Sun News");
  4.        util.printTitle("Sun Soloaris Express Developer Edition Gets Usability MakeOver");
  5.        util.printParagraph("Sun Mircrosystems, Inc., anoynced new support subscriptions " +
  6.                "and enhanced graphical user interfaces to make Solaris Express Developer Edition " +
  7.                "software easier for developers to install and use. The new support offerings and usabitity "+
  8.                "enhancements in Solaris Express Developer Edition 9/07 software are designed to provide a faster route " +
  9.                "to profuctivity developing for the Solaris Operating System (OS), Java platform and Web 2.0 applications. ");
  10.    }
  11. }
  12.  
  13.  
Sep 25 '07 #8
wshaer
12
The problem it only prints the titel but not in the way I wanted.
Sep 26 '07 #9
Nepomuk
3,112 Expert 2GB
The problem it only prints the titel but not in the way I wanted.
How does it print the title?

Greetings,
Nepomuk
Sep 26 '07 #10
wshaer
12
How does it print the title?

Greetings,
Nepomuk
like this

Expand|Select|Wrap|Line Numbers
  1. Sun Soloaris Express Developer Edition Gets Usability MakeOver
Sep 26 '07 #11
Nepomuk
3,112 Expert 2GB
like this

Expand|Select|Wrap|Line Numbers
  1. Sun Soloaris Express Developer Edition Gets Usability MakeOver
And what should it be?
Sep 26 '07 #12
wshaer
12
As in the assignment the titel has to be centered in a 50 column page so it the out put should look some think like this

Expand|Select|Wrap|Line Numbers
  1. 12345678901234567890123456789012345678901234567890
  2.     Sun Soloaris Express Developer Edition Gets 
  3.                         Usability MakeOver
  4.  
Sep 26 '07 #13
Nepomuk
3,112 Expert 2GB
As in the assignment the titel has to be centered in a 50 column page so it the out put should look some think like this

Expand|Select|Wrap|Line Numbers
  1. 12345678901234567890123456789012345678901234567890
  2.     Sun Soloaris Express Developer Edition Gets 
  3.                         Usability MakeOver
  4.  
In that case, you'll have to tell Java, that you want a new line at some point.
Here's what your code looks like now:
Expand|Select|Wrap|Line Numbers
  1. public void printTitle(String title)
  2. {
  3.         String line = "";
  4.         for (int x = 0; x<this.tMargin; x++){
  5.             line += " ";
  6.         }
  7.         lengthForCenter = (numberColumns - title.length())/2;
  8.  
  9.         for(int y =0; y < lengthForCenter; y++){
  10.             line += " ";
  11.         }
  12.  
  13.         line += title;
  14.         printLine(line);
  15.         printLine("");
  16. }
  17.  
Now, I can't see you separating your title there.

First of all, you should calculate, how many characters you can print in the first line, then center it (Do you know how to do that? The method length from the String object will be useful...), then print the first line and do the same with the rest.

Greetings,
Nepomuk
Sep 26 '07 #14
wshaer
12
Thanks

I'll try it and see if I understode your point.
Sep 26 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Mark Preston | last post by:
Admission first - I don't actually have a problem here but have noticed that a lot of people have been asking similar questions and getting very varied answers. What I've done is to sort of...
6
by: AFN | last post by:
I want to click a checkbox and have a 4 line (approx) paragraph appear beneath the checkbox, pushing the rest of the page down. And when unchecking the checkbox, the paragraph should disappear...
10
by: Jeff B. | last post by:
Has anyone come across a decent algorithm for implementing word wrap features in .net printing? I have a small component that uses basic printing techniques (i.e. e.Graphics.DrawString in a...
7
by: Amirallia | last post by:
Hello! I have a wecontrol table in a page, this table has a number of variable rows depending of the choice of the user. But when the table has a large number of rows, the printing of the page...
2
by: Rick | last post by:
Let me start by saying that I know very little about JavaScript. The software that I use to convert my FrameMaker files to HTML uses a JavaScript to hide certain text. The user has to click the...
3
by: jonniethecodeprince | last post by:
Hello everyone. I want to change the style of a HTML element. I can change the paragraph alignment to left right, justify etc. But I can seem to find the code for any other style change, for...
1
by: Kayvine | last post by:
Hi guys, this is a question I have for an assignment, it is pretty long, but I am not asking for the code(well if someone wants to write I'll be really happy, lol), but I just want to know how to...
10
by: Mtek | last post by:
Hi, I have a web page with some drop downs. Once the user makes his selections the bottom half of the page is populated. When that happens, I want to create a formatted report in a hidden div....
3
by: gentsquash | last post by:
I'm trying to display a paragraph that has a centered phrase, such as this one, in the middle of the paragraph. An example is the section "End of semester project" on my course-page ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.