Chapter 7 Code
Chapter 7 Code
import java.util.Scanner; /** * Josh Trout * JAVA P. 5 * 12/18/12 * * This program displays a box with a given width and height. You can also specify a character * to use to draw the box. If no character is given then it will use an asterisk. */
public class displayBox_2 { /** * @param args */ /** * This method draws a box with a given width and height */ public static void drawBox(int width, int height) { for (int i = 0; i < height; i++) { drawSegment(width); } } /** * This method draws a box with a given width, height, and character */ public static void drawBox(int width, int height, String character) { for (int i = 0; i < height; i++) { drawSegment(width, character); } } /** * This method draws a segment with a given length */ public static void drawSegment(int length) { for (int i = 0; i <= length; i++) { System.out.print("* "); } System.out.println(); } /** * This method draws a segment with a given length and character */ public static void drawSegment(int length, String character) { for (int i = 0; i <= length; i++) { System.out.print(character + " " ); } System.out.println();
} public static void main(String[] args) { // TODO Auto-generated method stub int height, width; String specific = "", character; Scanner input = new Scanner(System.in); System.out.print("Enter the box's width: "); width = input.nextInt(); System.out.print("Enter the box's height: "); height = input.nextInt(); do { System.out.print("Would you like to use a specific character for display? (y = yes, n = no): "); specific = input.nextLine(); } while (!specific.equals("y") && !specific.equals("n")); if (specific.equals("y")) { System.out.print("What character would you like to use to draw the box: "); character = input.nextLine(); drawBox(width, height, character); } else { drawBox(width, height); } } }
Console:
Enter the box's width: 7 Enter the box's height: 8 Would you like to use a specific character for display? (y = yes, n = no): Would you like to use a specific character for display? (y = yes, n = no): y What character would you like to use to draw the box: b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b
Grade Converter:
import java.util.Scanner; /** * Josh Trout * JAVA P. 5 * 12/20/12 * * This program displays the grade letter for a percent entered.
*/ public class gradeConverter { /** * @param args */ public static boolean isValidNumber(int userNum, int minNum, int maxNum) { if (minNum <= userNum && userNum <= maxNum) { return(true); } else { return(false); } } public static String getLetterGrade(int numGrade) { if (numGrade < 60) { return("F"); } else if (numGrade == 69) { return("D+"); } else if (numGrade < 70) { return("D"); } else if (numGrade == 79) { return("C+"); } else if (numGrade < 80) { return("C"); } else if (numGrade == 89) { return("B+"); } else if (numGrade < 90) { return("B"); } else if (numGrade == 100) { return("A+"); } else { return("A"); } } public static void main(String[] args) { // TODO Auto-generated method stub final int FLAG = -1; final int minValue = 0; final int maxValue = 100; int numericGrade; String letterGrade; Scanner input = new Scanner(System.in); System.out.print("Enter a numeric grade (-1 to quit): "); numericGrade = input.nextInt(); while (numericGrade != FLAG) { if (isValidNumber(numericGrade, minValue, maxValue)) { letterGrade = getLetterGrade(numericGrade); System.out.println("The grade " + numericGrade + " is a(n) " + letterGrade + "."); } else { System.out.println("Grade entered is not valid.");
Console:
Enter a numeric grade (-1 to quit): 53 The grade 53 is a(n) F. Enter a numeric grade (-1 to quit): 79 The grade 79 is a(n) C+. Enter a numeric grade (-1 to quit): 73 The grade 73 is a(n) C. Enter a numeric grade (-1 to quit): 69 The grade 69 is a(n) D+. Enter a numeric grade (-1 to quit): 83 The grade 83 is a(n) B. Enter a numeric grade (-1 to quit): 99 The grade 99 is a(n) A. Enter a numeric grade (-1 to quit): 93 The grade 93 is a(n) A. Enter a numeric grade (-1 to quit): 89 The grade 89 is a(n) B+. Enter a numeric grade (-1 to quit): 100 The grade 100 is a(n) A+. Enter a numeric grade (-1 to quit): 52 The grade 52 is a(n) F. Enter a numeric grade (-1 to quit): 43 The grade 43 is a(n) F. Enter a numeric grade (-1 to quit): -1