The number of lines that can be printed on a paper depends on the paper size, th
ID: 672082 • Letter: T
Question
The number of lines that can be printed on a paper depends on the paper size, the point size of each character in a line, whether lines are double-spaced or single-spaced, the top and bottom margin, and the left and right margins of the paper. Assume that all characters are of the same point size, and all lines are either single-spaced or double-spaced. Note that l inch = 72 points. Moreover, assume that the lines are printed along the width of the paper. For example, if the length of the paper is 11 inches and width is 8.5 inches, then the maximum length of a line is 8.5 inches. Write a Java program that calculates the number of characters in a line and the number of lines that can be printed on a paper based on the following input from the user: The length and width, in inches, of the paper. The top, bottom, left, and right margins The point size of the line If the lines are double-spaced, then double the point size of each character Use constants. Output should look similar to below. Sample Run: Enter the length of the paper: 11 Enter the width of the paper: 8.5 Enter the left margin of the paper: 1 Enter the right margin of the paper: 1 Enter the top margin of the paper: 1.5 Enter the bottom margin of the paper: 1 Enter the point size of a character: 12 Enter line spacing, s or S (single spaced), d or D (double spaced): s The number of lines that can be printed: 48 The number of characters that can be printed in a line: 39Explanation / Answer
import java.util.Scanner;
public class Printing {
public static void main(String[] args) {
// Constants
final int>
final char DOUBLE_LOWER = 'd';
final char DOUBLE_UPPER = 'D';
final char SINGLE_LOWER = 's';
final char SINGLE_UPPER = 'S';
// input fields accept from user
char space = 0;
double length = 0;
double width = 0;
double leftMargin = 0;
double rightMargin = 0;
double topMargin = 0;
double bottomMargin = 0;
double pointSize = 0;
int totalCharacters = 0;
int totalLines = 0;
int finalLength = 0;
double finalWidth = 0;
int pointsLength = 0;
double pointsWidth = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of the paper:");
length = sc.nextDouble();
System.out.println("Enter the width of the paper:");
width = sc.nextDouble();
System.out.println("Enter the left margin of the paper:");
leftMargin = sc.nextDouble();
System.out.println("Enter the right margin of the paper:");
rightMargin = sc.nextDouble();
System.out.println("Enter the top margin of the paper:");
topMargin = sc.nextDouble();
System.out.println("Enter the bottom margin of the paper:");
bottomMargin = sc.nextDouble();
System.out.println("Enter the point size of the paper:");
pointSize = sc.nextDouble();
System.out
.println("Enterline spacing,s or S (Single spaced),d or D (Double spaced):");
String lineSpace = sc.next();
space = lineSpace.charAt(0);
finalLength = (int) (length - (topMargin + bottomMargin));
finalWidth = width - (leftMargin + rightMargin);
pointsLength = finalLength * ONE_INCH;
pointsWidth = finalWidth * ONE_INCH;
if (space == DOUBLE_UPPER || space == DOUBLE_LOWER) {
pointSize += pointSize;
totalLines = (int) (pointsLength / pointSize);
totalCharacters = (int) (pointsWidth / pointSize);
System.out.println("The number of lines that can be printed : "
+ totalLines);
System.out
.println("The number of characters that can be printed in a line: "
+ totalCharacters);
} else if (space == SINGLE_UPPER || space == SINGLE_LOWER) {
totalLines = (int) (pointsLength / pointSize);
totalCharacters = (int) (pointsWidth / pointSize);
System.out.println("The number of lines that can be printed : "
+ totalLines);
System.out
.println("The number of characters that can be printed in a line: "
+ totalCharacters);
}
sc.close();
}
}
Output:
Enter the length of the paper:
11
Enter the width of the paper:
8.5
Enter the left margin of the paper:
1
Enter the right margin of the paper:
1
Enter the top margin of the paper:
1.5
Enter the bottom margin of the paper:
1
Enter the point size of the paper:
12
Enterline spacing,s or S (Single spaced),d or D (Double spaced):
s
The number of lines that can be printed : 48
The number of characters that can be printed in a line: 39