A program for printing a paragraph in MLA style

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wshaer
    New Member
    • Feb 2007
    • 12

    A program for printing a paragraph in MLA style

    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(Stri ng title), printReference( String author, String title, int year, String publisher), printLine(Strin g) 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 RuntimeExceptio n 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(getA uthor()) 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:

    Code:
    public class Utility {
    	
    	private int columns;
    	private int lines;
    	private String author;
    	
    	Utility (int col, int lin, String aut){
    	columns = col;
    	lines = lin;
    	author = aut;
    	}
    	public void printParagraph(String test){
    	}
    	public void printTitle(String title){
    	}
    	public void printReference(String author, String title,  int year, String publisher){
    	}
    	public void printLine(String line){
    	}
    	public void breakPage(){
    		
    	}
    	
    }
    .
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Pardon me for saying so but I smell a bit of TeX and LaTeX here ...

    kind regards,

    Jos

    Comment

    • wshaer
      New Member
      • Feb 2007
      • 12

      #3
      Originally posted by JosAH
      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?

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by wshaer
        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

        Comment

        • wshaer
          New Member
          • Feb 2007
          • 12

          #5
          Originally posted by nepomuk
          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?

          Comment

          • wshaer
            New Member
            • Feb 2007
            • 12

            #6
            Is it possibal to do it with the format out put print out?

            Comment

            • Nepomuk
              Recognized Expert Specialist
              • Aug 2007
              • 3111

              #7
              Originally posted by wshaer
              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.prin tf() and format specifiers.

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

              Greetings,
              Nepomuk

              Comment

              • wshaer
                New Member
                • Feb 2007
                • 12

                #8
                This is what I came up with:

                Code:
                public class Utility {	
                	private int numberColumns;
                	private int numberLines;
                	private String author;
                	private int pageNumber;
                	private final int lMargin = 5;
                	private final int rMargin = 5;
                	private final int tMargin = 3;
                	private final int bMargin = 5;
                	private int lengthForCenter;
                	private int lineNumber;
                
                	
                	public Utility(int numberColumns, int numberLines, String author){
                			
                		this.author = author;
                		this.numberColumns = numberColumns;
                		this.numberLines = numberLines;
                		this.pageNumber = 1;
                		
                		
                	}
                	
                	public void printParagraph(String testParagraph){
                		int index= 0;
                		if (testParagraph.charAt(index) == ' ') {
                			
                			testParagraph.substring(10);	
                		}
                		else{
                			index++;
                		}
                		
                		
                	}
                	public void printTitle(String title){
                		String line = "";
                		for (int x = 0; x<this.tMargin; x++){
                			line += " ";
                				
                		}
                		lengthForCenter = (numberColumns - title.length())/2;
                		
                		for(int y =0; y < lengthForCenter; y++){
                			line += " ";
                		}
                		
                		line += title;
                		printLine(line);
                		printLine("");
                	
                			 
                		 }
                			 
                
                	private void printLine(String line) {
                		if(lineNumber == numberLines - bMargin){
                			System.out.println();
                			if(lineNumber == 5){
                				pageNumber++;
                			}
                			
                			lineNumber = 0;
                		}
                		System.out.println(line);
                		
                	}
                
                	public void printReference(String author, String title, int year, String publisher){
                		
                	}
                	public String getAuthor() {
                		return author;
                	}
                	public int getNumberColumns() {
                		return numberColumns;
                	}
                	public int getNumberLines() {
                		return numberLines;
                	}
                
                
                }
                This is the driving class

                Code:
                public class TestUtility {
                   public static void main(String[] args) {
                	   Utility util = new Utility(50, 25, "Sun News");
                	   util.printTitle("Sun Soloaris Express Developer Edition Gets Usability MakeOver");
                	   util.printParagraph("Sun Mircrosystems, Inc., anoynced new support subscriptions " +
                	   		"and enhanced graphical user interfaces to make Solaris Express Developer Edition " +
                	   		"software easier for developers to install and use. The new support offerings and usabitity "+
                	   		"enhancements in Solaris Express Developer Edition 9/07 software are designed to provide a faster route " +
                	   		"to profuctivity developing for the Solaris Operating System (OS), Java platform and Web 2.0 applications. ");
                   }
                }

                Comment

                • wshaer
                  New Member
                  • Feb 2007
                  • 12

                  #9
                  The problem it only prints the titel but not in the way I wanted.

                  Comment

                  • Nepomuk
                    Recognized Expert Specialist
                    • Aug 2007
                    • 3111

                    #10
                    Originally posted by wshaer
                    The problem it only prints the titel but not in the way I wanted.
                    How does it print the title?

                    Greetings,
                    Nepomuk

                    Comment

                    • wshaer
                      New Member
                      • Feb 2007
                      • 12

                      #11
                      Originally posted by nepomuk
                      How does it print the title?

                      Greetings,
                      Nepomuk
                      like this

                      Code:
                      Sun Soloaris Express Developer Edition Gets Usability MakeOver

                      Comment

                      • Nepomuk
                        Recognized Expert Specialist
                        • Aug 2007
                        • 3111

                        #12
                        Originally posted by wshaer
                        like this

                        Code:
                        Sun Soloaris Express Developer Edition Gets Usability MakeOver
                        And what should it be?

                        Comment

                        • wshaer
                          New Member
                          • Feb 2007
                          • 12

                          #13
                          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

                          Code:
                          12345678901234567890123456789012345678901234567890
                              Sun Soloaris Express Developer Edition Gets 
                                                  Usability MakeOver

                          Comment

                          • Nepomuk
                            Recognized Expert Specialist
                            • Aug 2007
                            • 3111

                            #14
                            Originally posted by wshaer
                            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

                            Code:
                            12345678901234567890123456789012345678901234567890
                                Sun Soloaris Express Developer Edition Gets 
                                                    Usability MakeOver
                            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:
                            Originally posted by wshaer
                            [CODE=java]
                            public void printTitle(Stri ng title)
                            {
                            String line = "";
                            for (int x = 0; x<this.tMargin ; x++){
                            line += " ";
                            }
                            lengthForCenter = (numberColumns - title.length())/2;

                            for(int y =0; y < lengthForCenter ; y++){
                            line += " ";
                            }

                            line += title;
                            printLine(line) ;
                            printLine("");
                            }
                            [/CODE]
                            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

                            Comment

                            • wshaer
                              New Member
                              • Feb 2007
                              • 12

                              #15
                              Thanks

                              I'll try it and see if I understode your point.

                              Comment

                              Working...