Solutions Manual for Building Java Programs 6th Edition by Reges, Stepp
ISBN: 9780135429914
![]()
ISBN: 9780135429914
Note: Some exercises ask the student to complete something of their own choosing or do not have one specific correct answer. For such problems, the answer will be listed as “(answers vary).”
1. (answers vary)
2. public class MyFirstProgram { public static void main(String[] args) { System.out.println("Hello, world!"); System.out.println("I am learning to program in Java."); System.out.println("I hope it is a lot of fun!"); System.out.println(); System.out.println("I hope I get a good grade!"); System.out.println(); System.out.println("Maybe I'll change my major to computer science."); } }
3. (answers vary)
4. Testing, testing, one two three.
How much output will there be?
5. Shaq is 7'1
The string "" is an empty message. \'""
6. public class MuchBetter { public static void main(String[] args) { System.out.println("A \"quoted\" String is"); System.out.println("'much' better if you learn"); System.out.println("the rules of \"escape sequences.\""); System.out.println("Also, \"\" represents an empty String."); System.out.println("Don't forget: use \\\" instead of \" !"); System.out.println("'' is not the same as \""); } }
7. public class Spikey { public static void main(String[] args) { System.out.println(" \\/"); System.out.println(" \\\\//"); System.out.println("\\\\\\///"); System.out.println("///\\\\\\"); System.out.println(" //\\\\"); System.out.println(" /\\"); } }
8. public class Icky { public static void main(String[] args) { System.out.println("Well-indented programs"); System.out.println("look much better."); System.out.println("Please fix me"); System.out.println("so I look nicer"); }
9. public class Tricky { public static void main(String[] args) { System.out.println("Hello world"); System.out.println("Do you like this program?"); System.out.println();
System.out.println("I wrote it myself.");
10. (answers vary)
1. public class Spikey { public static void main(String[] args) { System.out.println(" \\/"); System.out.println(" \\\\//"); System.out.println("\\\\\\///"); System.out.println("///\\\\\\"); System.out.println(" //\\\\"); System.out.println(" /\\"); }
2. // This program prints a pattern of vaguely lantern-like figures. // The code uses methods for structure and to remove redundancy.
public class Lanterns { public static void main(String[] args) { figure1(); System.out.println();
lantern1(); System.out.println();
lantern2(); System.out.println(); }
public static void figure1() { shortLong(); }
public static void lantern1() { shortLong();
middle(); longLine(); shortLong(); }
public static void lantern2() { shortLong(); shortLine(); middle(); middle(); shortLine(); shortLine(); }
public static void shortLong() { shortLine(); System.out.println(" *********"); longLine(); }
public static void shortLine() { System.out.println(" *****"); }
public static void longLine() { System.out.println("*************"); }
public static void middle() { System.out.println("* | | | | | *"); } }
3. (answers vary)
4. // This program prints a college "fight song" with verses and repetition. // The code uses methods for structure and to remove redundancy.
public class FightSong { public static void main(String[] args) { goTeamGo(); System.out.println(); bigVerse(); bigVerse(); goTeamGo(); }
public static void goTeamGo() { System.out.println("Go, team, go!"); System.out.println("You can do it."); }
public static void bigVerse() { goTeamGo(); System.out.println("You're the best,");
System.out.println("In the West."); goTeamGo(); System.out.println();
1.
2.
Expression Result 12 / 5 + 8 / 4 4
3 * 4 + 15 / 2 19
(1 + 2 * 3 + (1 + 2) * 3) 16
42 % 5 + 16 % 3 3
2.5 * 2 + 17 / 4 9.0
4.5 / 3 / 2 + 1 1.75
Expression Result 2 + 6 + "cs" "8cs" "cs 123" + 2 + 6 "cs 12326"
1 + 9 / 2 * 2.0 9.0
46 / 3 / 2.0 / 3 * 4/5 2.0
50 / 9 / 2.0 + 200 / 10 / (5.0 / 2) 10.5
3.
Expression Result 123 * 456 789 55299
3.14 + 1.59 * 2.65 7.3535
2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 1024 2 + 2 + "xyz" + 3 + 3 "4xyz33"
4. double grade = 4.0;
5. grade = 4.0;
6. balance = balance + 5;
7. a = 6, b = 9, c = 16
8. public class Oops { public static void main(String[] args) { double x = 0.0; System.out.println("x is" + x);
x = 15.2; // set x to 15.2 System.out.println("x is now " + x);
double y; // set y to 1 more than x y = x + 1;
System.out.println("x and y are " + x + " and " + y); }
9. i = 4, j = 2, k = 1
10.double y = 12.3*x*x*x*x 9.1*x*x*x + 19.3*x*x 4.6*x + 34.2;
11. (answers vary)
1. for (int i = 10; i > 0; i ) {
System.out.println(i + " bottles of beer on the wall, " + i + " bottles of beer");
System.out.println("Take one down, pass it around, " + (i 1) + " bottles of beer on the wall");
System.out.println(); }
2. line stars 1 7 2 5 3 3 4 1 multiplier: 2 shift: 9
3. for (int i = 1; i <= 4; i++) { System.out.println("2 times " + i + " = " + (2 * i)); }
4. public class SequenceOfNumbers { public static void main(String[] args) { for (int i = 1; i <= 6; i++) { System.out.println(3 * i + 5); } } }
5. public class SequenceOfStars { public static void main(String[] args) { for (int i = 1; i <= 6; i++) { for (int j = 1; j <= 3 * i + 5; j++) { System.out.print("*"); } System.out.println(); } } }
7. for (int i = 1; i <= 3; i++) { for (int j = 0; j <= 9; j++) { for (int k = 1; k <= 3; k++) { System.out.print(j); } }
System.out.println(); }
8. for (int i = 1; i <= 5; i++) { for (int j = 9; j >= 0; j ) { for (int k = 1; k <= 5; k++) { System.out.print(j); } }
System.out.println(); }
9. for (int i = 1; i <= 4; i++) { for (int j = 9; j >= 0; j ) { for (int k = 1; k <= j; k++) { System.out.print(j); } }
System.out.println();
12. line dashes numbers
1 5 1
2 4 3
3 3 5
4 2 7
5 1 9
dashes: 1 * line + 6 numbers: 2 * line + 1
13. line \ ! /
\ and /:
size 6: 2 * line + 2
size 4: 2 * line + 2
size SIZE: 2 * line + 2
!:
size 6: 4 * line + 26
size 4: 4 * line + 18
size SIZE: 4 * line + (4 * SIZE + 2)
15. public class SlashFigure2 { public static final int SIZE = 4; public static void main(String[] args) { for (int line = 1; line <= SIZE; line++) { for (int i = 1; i <= 2 * line 2; i++) { System.out.print("\\"); } for (int i = 1; i <= 4 * line + (4 * SIZE + 2); i++) { System.out.print("!"); } for (int i = 1; i <= 2 * line 2; i++) { System.out.print("/"); } System.out.println(); } } }
1. 15 42 10 25
2. say coke not pepsi or pop say soda not soda or pepsi say pepsi not koolaid or pop say say not pepsi or pepsi
3. three times two = 6
1 times three = 28
1 times 1 = 42 three times 1 = 2
1 times eight = 20
4. to walk the walk is good to hear the good is bad to feel the walk is song to claim the talk is feel
5.
line 5: cannot use variable y without declaring and initializing it line 5: cannot declare the type of y in the method call
line 6: cannot call printer without the correct number of parameters (2, in this case)
line 7: cannot call printer by passing the correct type of parameters (double, in this case)
line 8: cannot refer to the variable z: it is in scope inside printer, not main line 11: must provide a type for x
line 11: must provide a variable name for the second parameter
line 12: must refer to the parameters using the exact same spelling line 13: cannot refer to variables in main that were not passed into printer as a parameter
6. public static void printGrid(int rows, int cols) { for (int i = 1; i <= rows; i++) { System.out.print(i); for (int j = 1; j <= cols 1; j++) { System.out.print(", " + (i + rows * j)); } System.out.println(); } }
7. public static void printSquare(int min, int max) { int range = max min + 1; for (int i = 0; i < range; i++) { for (int j = 0; j < range; j++) { System.out.print((j + i) % range + min); } System.out.println(); } }
1. double grade = 2.7; Math.round(grade); // grade = 2.7 grade = Math.round(grade); // grade = 3.0
double min = Math.min(grade, Math.floor(2.9)); // min = 2.0
double x = Math.pow(2, 4); // x = 16.0 x = Math.sqrt(64); // x = 8.0
int count = 25; Math.sqrt(count); // count = 25 count = (int) Math.sqrt(count); // count = 5
int a = Math.abs(Math.min( 1, 3)); // a = 3
2. public static double area(double radius) { double answer = Math.PI * Math.pow(radius, 2); return answer; }
3. public static double pay(double salary, int hours) { if (hours < 8) { return salary * hours; } else { double regularPay = salary * 8; double overtimePay = (1.5 * salary) * (hours 8); return regularPay + overtimePay; } }
1.
Expression Result
str1.length() 13
str1.charAt(7) 'a'
str2.charAt(0) 'G'
str1.indexOf("o") 2
str2.toUpperCase() "GANDALF THE GRAY"
str1.toLowerCase().indexOf("B") 1
str1.substring(4) "o Baggins"
str2.substring(3, 14) "dalf the GR"
str2.replace("a", "oo") "Goondoolf the GRAY"
str2.replace("gray", "white") "Gandalf the GRAY" "str1".replace("r", "range") "strange1"
2. public class StringOops { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Type your name: "); String name = console.next(); process(name); } public static void process(String name) { if (name.equals("Whitaker")) { System.out.println("You must be really awesome."); } name = name.replace("a", "e"); name = name.toUpperCase(); name = name.substring(0, 3); System.out.println(name + " has " + name.length() + " letters"); } }
3. public static void processName(Scanner input) { System.out.print("Please enter your full name: "); String first = input.next(); String last = input.next(); System.out.print("Your name in reverse order is " + last + ", " + first); }
1. public class CollegeAdmit { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("University admission program");
System.out.print("What is your GPA? "); double gpa = console.nextDouble(); System.out.print("What is your SAT score? "); int sat = console.nextInt(); if (gpa < 1.8) { System.out.println("Your GPA is too low."); } else if (sat < 900) { System.out.println("Your SAT score is too low."); } else { System.out.println("You were accepted!"); } } }
2. public class SumNumbers { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("low? "); int low = console.nextInt(); System.out.print("high? ");
int high = console.nextInt(); int sum = 0; for (int i = low; i <= high; i++) { sum += i; } System.out.println("sum = " + sum); } }
3. public static void longestName(Scanner console, int names) { System.out.print("name #1? "); String longest = console.next(); int count = 1; for (int i = 2; i <= names; i++) { System.out.print("name #" + i + "? "); String name = console.next(); if (name.length() == longest.length()) { count++; } if (name.length() > longest.length()) { longest = name; count = 1; } }
String fixedName = longest.substring(0, 1).toUpperCase(); fixedName = fixedName + longest.substring(1).toLowerCase(); System.out.println(fixedName + "'s name is longest"); if (count > 1) { System.out.println("(There was a tie!)"); } }
1. import java.awt.*;
public class PrettyPicture { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(220, 150); panel.setBackground(Color.YELLOW);
Graphics g = panel.getGraphics();
g.setColor(Color.BLUE);
g.drawRect(50, 25, 10, 10);
g.setColor(Color.RED);
g.fillOval(130, 25, 42, 40); } }
2. import java.awt.*;
public class Face1 { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(220, 150);
Graphics g = panel.getGraphics();
g.drawOval(10, 30, 100, 100); // face outline
g.setColor(Color.BLUE);
g.fillOval(30, 60, 20, 20); // eyes
g.fillOval(70, 60, 20, 20);
g.setColor(Color.RED); // mouth
g.drawLine(40, 100, 80, 100);
3. stair x y width height 1 5 5 10 10
4. public class Stairs { public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(110, 110); Graphics g = panel.getGraphics(); for (int i = 0; i < 10; i++) {
g.drawRect(5, 5 + 10 * i, 10 + 10 * i, 10); } } }
5. public class Stairs2 { public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(110, 110); Graphics g = panel.getGraphics(); for (int i = 0; i < 10; i++) { // output 2
g.drawRect(5, 5 + 10*i, 100 10*i, 10); // output 3 // g.drawRect(95 10*i, 5 + 10*i, 10 + 10*i, 10); // output 4 // g.drawRect(5 + 10*i, 5 + 10*i, 100 10*i, 10); } }
6. import java.awt.*;
public class Face2 { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(320, 180); Graphics g = panel.getGraphics(); drawFace(g, 10, 30); drawFace(g, 150, 50); }
public static void drawFace(Graphics g, int x, int y) { g.setColor(Color.BLACK);