|
This is the solution for one of the assigment for a 1st year Java subject that I tought in 2006.
Assignment Description:
As a keen photographer, I found my hard disk quickly filling up with snapshots and videos. I decided to offload a lot of the files to DVD. Each DVD is in its own plastic case, and the contents are clearly described on the front of the case. As the number of DVDs increased, I built myself a shelf on which the DVDs stand vertically. I need your help! I have written a title for each DVD in my notepad, but now I need the titles to appear vertically so that I can put them in the spine of the DVD cases and be able to read them easily from the shelf. I want you to write a program that will output my titles vertically. I need lines between each title so that, when I print them, I can easily cut along the lines. I have worked out that I can fit 36 characters into the available space, so all output titles must be 36 characters long, padded with spaces at the end where necessary. If I accidentally make a title too long, only output the first 36 characters.
Input Format: Input will be at most 50 titles, one to a line. Each title consists of 1 to 100 arbitrary characters. A single '#' on a line by itself indicates the end of input.
Output Format: Output will be the same titles presented vertically, where the left to right order will be the same as the order of the input. There will be a column of bar ('|') characters at each end, and separating each title, and a row of minus ('-') characters (1 per column) at the beginning and end.
Solution: /**
* @author T I M Shaniur Nabi
* @version 1.0
* @link http://www.shaniur.com/code
*
* Compiler Version: JDK 1.5
*
* Date written: Thursday, 23rd March 2006
* Contact: http://www.shaniur.com/contact
*
* Subject Code:
* COSC1290/95 [Java for C Programmers]
* COSC1095 [Programming Principles 2J]
* This is the source code for first part of assignment 1.
*
* School of Computer Science and Information Technology
* RMIT University
*/
import java.util.Scanner;
public class DVDTitles {
/**
* @param args
*/
public static void main(String[] args) {
/**
* Declaring constants and variables.
*/
final int LINE_SIZE = 36;
final int ARRAY_SIZE = 50;
String current = "";
String[] titles = new String[ARRAY_SIZE];
int entryCounter = 0;
Scanner keyboard = new Scanner(System.in);
/**
* Accepting input:
* Algorithm used:
* In this loop, the user input for each title is stored
* temporarily in the variable called 'current'. If the length of
* 'current' is less than 36 characters, then it is padded up
* with white spaces upto the length of 36. If the length is more
* than 36, it is truncated to 36 characters. Once the value of
* 'current' is filtered, it is added to the String array called
* 'titles'. Therefore all the values in 'titles' array will be of
* length 36.
*/
for (int i = 0; i < ARRAY_SIZE; i++) {
titles[i] = " ";
current = keyboard.nextLine();
/**
* Condition to break out of the loop if # is entered.
*/
if (current.compareTo("#") == 0) {
i = ARRAY_SIZE;
} else {
/**
* if: Pads up 'current' if length is less than 36.
* else: Truncates length to 36. Uses substring method of
* String class.
*/
if (current.length() < LINE_SIZE) {
int pad = LINE_SIZE - current.length();
for (int j = 0; j < pad; j++) {
current = current + " ";
}
} else if (current.length() > LINE_SIZE) {
current = current.substring(0, LINE_SIZE);
}
/**
* Adds current of the main String array and increments counter
* to keep track of how many titles has been successfully added.
*/
titles[i] = current;
entryCounter++;
}
}
/**
* Printing out the output header.
*/
System.out.print("-");
for (int i = 0; i < entryCounter; i ++) {
System.out.print("--");
}
System.out.println();
/**
* Printing out the main output:
* Algorithm used: A nested loop is used here.
* Outer Loop: This is the one that determines how many rows there
* will be. The number of rows in the output is 36 (we are printing
* them vertically), the length of each title.
*
* Inner Loop: For every title we will be having a seperate column.
* Therefore this should be equal to the number of titles that has
* been successfully entered. As the maximum number of inputs can
* be 50, the limit is the length of 'title' array.
*
* When printing, it starts to print the first character of each
* title. Therefore the first line of the output will have first
* character of each title. The "|" in print statement is used for
* formatting. Note that after each loop that completes the column
* (inner loop) an extra "|" is printed. This is for formatting
* only.
* Once the first row is printed, the second row should print 2nd
* chracters of each title. That is why charAt(i) is used.
* Variable 'i' has the row value and as 2nd row should have
* 2nd characters, 3rd row should have 3rd characters and so on,
* this will print the derised output.
*/
for (int i = 0; i < LINE_SIZE; i++) {
for (int j =0; j < entryCounter; j++) {
if (titles[j] != null) {
System.out.print("|" + titles[j].charAt(i));
}
}
System.out.println("|");
}
/**
* Printing out the output footer.
*/
System.out.print("-");
for (int i = 0; i < entryCounter; i ++) {
System.out.print("--");
}
System.out.println();
}
}
/*****************
*End of Solution*
*****************/Sample Input:
King Kong
God Father
The Rock
AI
Little Miss Sunshine
The Departed
Scarface
The Holiday
# Output:
-----------------
|K|G|T|A|L|T|S|T|
|i|o|h|I|i|h|c|h|
|n|d|e| |t|e|a|e|
|g| | | |t| |r| |
| |F|R| |l|D|f|H|
|K|a|o| |e|e|a|o|
|o|t|c| | |p|c|l|
|n|h|k| |M|a|e|i|
|g|e| | |i|r| |d|
| |r| | |s|t| |a|
| | | | |s|e| |y|
| | | | | |d| | |
| | | | |S| | | |
| | | | |u| | | |
| | | | |n| | | |
| | | | |s| | | |
| | | | |h| | | |
| | | | |i| | | |
| | | | |n| | | |
| | | | |e| | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
-----------------
Add as favourites (6) | Quote this article on your site | Views: 765
Powered by AkoComment Tweaked Special Edition v.1.4.6 AkoComment © Copyright 2004 by Arthur Konze - www.mamboportal.com All right reserved |