Skip to content

Commit f3e943e

Browse files
committed
Chapter 13 19/20 + update the README.md
1 parent b170694 commit f3e943e

File tree

9 files changed

+393
-65
lines changed

9 files changed

+393
-65
lines changed

README.md

+74-14
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
1-
# INTRODUCTION TO JAVA PROGRAMMING 10TH EDITION <br> _Exercise Solutions_
2-
### This repo contains solutions to the end-of-chapter exercise’s in <a href="https://www.amazon.com/Intro-Java-Programming-Comprehensive-Version/dp/0133761312">Y. Daniel Liang’s Intro to Java Programming</a>
3-
#### _About the 10th Edition_
4-
"Daniel Liang teaches concepts of problem-solving and object-oriented programming using a fundamentals-first approach
5-
. Beginning programmers learn critical problem-solving techniques then move on to grasp the key concepts of object-oriented, GUI programming, advanced GUI and Web programming using Java. Liang approaches Java GUI programming using JavaFX, not only because JavaFX is much simpler for new Java programmers to learn and use but because it has replaced Swing as the new GUI tool for developing cross-platform-rich Internet applications on desktop computers, on hand-held devices, and on the Web. Additionally, for instructors, JavaFX provides a better teaching tool for demonstrating object-oriented programming."
1+
# INTRODUCTION TO JAVA PROGRAMMING 10TH EDITION <br> _Exercise Solutions_
2+
### This repo contains solutions to the end-of-chapter exercise’s from <a href="https://www.amazon.com/Intro-Java-Programming-Comprehensive-Version/dp/0133761312">Y. Daniel Liang’s Intro to Java Programming (10th Edition)</a>
3+
##### _The goal of this repo is to solve the end-of-chapter exercises by satisfying the requirements of the <i><a href="https://liveexample.pearsoncmg.com/CheckExercise/faces/CheckExercise.xhtml?chapter=1&programName=Exercise01_01">Exercise Check Tool</a> that comes with the book</i>._
4+
#### - About the 10th Edition -
5+
_"Daniel Liang teaches concepts of problem-solving and object-oriented programming using a fundamentals-first approach
6+
. Beginning programmers learn critical problem-solving techniques then move on to grasp the key concepts of
7+
object-oriented, GUI programming, advanced GUI and Web programming using Java. Liang approaches Java GUI programming using JavaFX, not only because JavaFX is much simpler for new Java programmers to learn and use but because it has replaced Swing as the new GUI tool for developing cross-platform-rich Internet applications on desktop computers, on hand-held devices, and on the Web. Additionally, for instructors, JavaFX provides a better teaching tool for demonstrating object-oriented programming."_
8+
9+
### **Companion Content:**
10+
- ### <a href="http://liveexample.pearsoncmg.com/liang/intro10e">10th Edition Companion Website</a>
11+
- ### <a href="https://liveexample.pearsoncmg.com/CheckExercise/faces/CheckExercise.xhtml?chapter=1&programName=Exercise01_01">Exercise Check Tool</a>
12+
- ### <a href="https://liveexample.pearsoncmg.com/javarevel2e.html">Hints to Quizzes and Programming Projects</a>
13+
- ### <a href='#checkpoint-answers'>Checkpoint Answers</a>
14+
615

7-
### **Links:**
8-
### <i><a href="http://liveexample.pearsoncmg.com/liang/intro10e">10th Edition Companion Website</a></i>
9-
### <i><a href="https://liveexample.pearsoncmg.com/CheckExercise/faces/CheckExercise.xhtml?chapter=1&programName=Exercise01_01">Exercise Check Tool</a></i>
1016

1117
____
1218
### <em>Contributors:</em>
1319

14-
### Harry Dulaney
20+
### Harry Dulaney, _Your Name Here_
1521
____
1622
### <em>Pull requests:</em>
1723
_Please feel free to make pull requests. I will do my best to review them quickly._
18-
##### Pull Requests should be used for adding new solutions to the repo that do not already exist.
24+
- ##### Pull Requests should be used for adding new solutions to the repo that do not already exist.
1925
____
2026
### <em>Issue's:</em>
2127
_Please feel free to open new Issues._
22-
##### - To request a specific Exercise that you need answered be sure to include the Chapter and Exercise number.
23-
##### - To request a change to an existing solution.
24-
##### - If you find a solution that you think is incorrect or can be improved.
28+
- ##### To request a specific Exercise that you need answered be sure to include the Chapter and Exercise number.
29+
- ##### To request a change to an existing solution.
30+
- ##### If you find a solution that you think is incorrect or can be improved.
2531
____
26-
# Quick Solution Navigator:
32+
33+
<h1>Exercise Solutions:</h1>
2734
<ul>
2835
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_01"><strong
2936
>Chapter 1</strong></a> - Introduction to Computers, Programs, and Java (Complete)</li><br>
@@ -72,3 +79,56 @@ ____
7279
<li><!--<a href="https://github.com/HarryDulaney/java-programming-daniel-liang-10th/tree/master/src/">--><strong>Chapter 32</strong><!--</a>--> - Java Database Programming</li><br>
7380
<li><!--<a href="https://github.com/HarryDulaney/java-programming-daniel-liang-10th/tree/master/src/">--><strong>Chapter 33</strong><!--</a>--> - Java Server Faces</li><br>
7481
</ul>
82+
83+
____
84+
85+
<h1 id="checkpoint-answers">Checkpoint Answers</h1>
86+
87+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter1.html">Chapter 1 Check Point Questions</a>
88+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter2.html">Chapter 2 Check Point Questions</a>
89+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter3.html">Chapter 3 Check Point Questions</a>
90+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter4.html">Chapter 4 Check Point Questions</a>
91+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter5.html">Chapter 5 Check Point Questions</a>
92+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter6.html">Chapter 6 Check Point Questions</a>
93+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter7.html">Chapter 7 Check Point Questions</a>
94+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter8.html">Chapter 8 Check Point Questions</a>
95+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter9.html">Chapter 9 Check Point Questions</a>
96+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter10.html">Chapter 10 Check Point Questions</a>
97+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter11.html">Chapter 11 Check Point Questions</a>
98+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter12.html">Chapter 12 Check Point Questions</a>
99+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter13.html">Chapter 13 Check Point Questions</a>
100+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter14.html">Chapter 14 Check Point Questions</a>
101+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter15.html">Chapter 15 Check Point Questions</a>
102+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter16.html">Chapter 16 Check Point Questions</a>
103+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter17.html">Chapter 17 Check Point Questions</a>
104+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter18.html">Chapter 18 Check Point Questions</a>
105+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter19.html">Chapter 19 Check Point Questions</a>
106+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter20.html">Chapter 20 Check Point Questions</a>
107+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter21.html">Chapter 21 Check Point Questions</a>
108+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter22.html">Chapter 22 Check Point Questions</a>
109+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter23.html">Chapter 23 Check Point Questions</a>
110+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter24.html">Chapter 24 Check Point Questions</a>
111+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter25.html">Chapter 25 Check Point Questions</a>
112+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter26.html">Chapter 26 Check Point Questions</a>
113+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter27.html">Chapter 27 Check Point Questions</a>
114+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter28.html">Chapter 28 Check Point Questions</a>
115+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter29.html">Chapter 29 Check Point Questions</a>
116+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter30.html">Chapter 30 Check Point Questions</a>
117+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter31.html">Chapter 31 Check Point Questions</a>
118+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter32.html">Chapter 32 Check Point Questions</a>
119+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter33.html">Chapter 33 Check Point Questions</a>
120+
121+
<h2>Bonus Chapters</h2>
122+
123+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter34.html">Chapter 34 Check Point Questions</a>
124+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter35.html">Chapter 35 Check Point Questions</a>
125+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter36.html">Chapter 36 Check Point Questions</a>
126+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter37.html">Chapter 37 Check Point Questions</a>
127+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter38.html">Chapter 38 Check Point Questions</a>
128+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter39.html">Chapter 39 Check Point Questions</a>
129+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter40.html">Chapter 40 Check Point Questions</a>
130+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter41.html">Chapter 41 Check Point Questions</a>
131+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter42.html">Chapter 42 Check Point Questions</a>
132+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter43.html">Chapter 43 Check Point Questions</a>
133+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter44.html">Chapter 44 Check Point Questions</a>
134+
- ##### <a href="https://liveexample.pearsoncmg.com/checkpoint/Chapter45.html">Chapter 45 Check Point Questions</a>

src/ch_13/Exercise13_19.java

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package ch_13;
2+
3+
import ch_13.exercise13_15.Rational;
4+
5+
import java.util.Scanner;
6+
7+
/**
8+
* 13.19 (Convert decimals to fractions) Write a program that prompts the user to enter
9+
* a decimal number and displays the number in a fraction. Hint: read the decimal
10+
* number as a string, extract the integer part and fractional part from the string,
11+
* and use the BigInteger implementation of the Rational class in Programming
12+
* Exercise 13.15 to obtain a rational number for the decimal number. Here are some
13+
* sample runs:
14+
* Enter a decimal number: 3.25
15+
* The fraction number is 13/4
16+
* <p>
17+
* Enter a decimal number: -0.45452
18+
* The fraction number is -11363/25000
19+
*/
20+
public class Exercise13_19 {
21+
22+
public static void main(String[] args) {
23+
Scanner in = new Scanner(System.in);
24+
System.out.print("Enter a decimal number: ");
25+
String decVal = in.next();
26+
try {
27+
System.out.println("The fraction number is " + decimalToFraction(decVal));
28+
} catch (Exception e) {
29+
System.out.println(e.getMessage());
30+
}
31+
32+
in.close();
33+
}
34+
35+
private static String decimalToFraction(String val) throws Exception {
36+
boolean isNegative = val.startsWith("-");
37+
38+
String[] decimalNumberParts = val.split("\\.");
39+
if (decimalNumberParts.length < 2) {
40+
throw new Exception("You must enter a decimal number like: 123.12");
41+
}
42+
if (val.startsWith("-")) {
43+
isNegative = true;
44+
}
45+
Rational leftSideOfDecimal = new Rational(Long.parseLong(decimalNumberParts[0]));
46+
String denominatorRightSide = "1";
47+
for (int i = 0; i < decimalNumberParts[1].length(); i++) {
48+
denominatorRightSide += "0";
49+
}
50+
Rational rightSideOfDecimal = new Rational(Long.parseLong(decimalNumberParts[1]), Long.parseLong(denominatorRightSide));
51+
Rational result = leftSideOfDecimal.add(rightSideOfDecimal);
52+
return (isNegative ? "-" : "") + result.toString();
53+
54+
}
55+
}

src/ch_13/Exercise13_21.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ch_13;
2+
3+
/**
4+
* 13.21 (Algebra: vertex form equations) The equation of a parabola can be expressed
5+
* in either standard form (y = ax2 + bx + c) or vertex form (y = a(x - h)2 + k).
6+
* Write a program that prompts the user to enter a, b, and c as integers in standard
7+
* form and displays h and k in the vertex form. Here are some sample runs.
8+
* Enter a, b, c: 1 3 1
9+
* h is -3/2 k is -5/4
10+
* Enter a, b, c: 2 3 4
11+
* h is -3/4 k is 23/8
12+
*/
13+
public class Exercise13_21 {
14+
}

src/ch_13/exercise13_13/Course.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package ch_13.exercise13_13;
2+
3+
class Course implements Cloneable {
4+
/**
5+
* @return Course object with deep copy of students array
6+
* @throws CloneNotSupportedException for unsupported clone type
7+
*/
8+
@Override
9+
public Object clone() throws CloneNotSupportedException {
10+
Course course = (Course) super.clone();
11+
course.students = this.students;
12+
return course;
13+
}
14+
15+
private String courseName;
16+
private String[] students = new String[100];
17+
private int numberOfStudents;
18+
19+
public Course(String courseName) {
20+
this.courseName = courseName;
21+
}
22+
23+
public void addStudent(String student) {
24+
students[numberOfStudents] = student;
25+
numberOfStudents++;
26+
}
27+
28+
public String[] getStudents() {
29+
return students;
30+
}
31+
32+
public int getNumberOfStudents() {
33+
return numberOfStudents;
34+
}
35+
36+
public String getCourseName() {
37+
return courseName;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package ch_13;
1+
package ch_13.exercise13_13;
22

3-
import java.util.Arrays;
3+
import ch_13.exercise13_13.Course;
44

55
/**
66
* 13.13 (Enable the Course class cloneable) Rewrite the Course class in Listing 10.6
@@ -27,40 +27,3 @@ public static void main(String[] args) {
2727

2828
}
2929

30-
class Course implements Cloneable {
31-
/**
32-
* @return Course object with deep copy of students array
33-
* @throws CloneNotSupportedException for unsupported clone type
34-
*/
35-
@Override
36-
public Object clone() throws CloneNotSupportedException {
37-
Course course = (Course) super.clone();
38-
course.students = this.students;
39-
return course;
40-
}
41-
42-
private String courseName;
43-
private String[] students = new String[100];
44-
private int numberOfStudents;
45-
46-
public Course(String courseName) {
47-
this.courseName = courseName;
48-
}
49-
50-
public void addStudent(String student) {
51-
students[numberOfStudents] = student;
52-
numberOfStudents++;
53-
}
54-
55-
public String[] getStudents() {
56-
return students;
57-
}
58-
59-
public int getNumberOfStudents() {
60-
return numberOfStudents;
61-
}
62-
63-
public String getCourseName() {
64-
return courseName;
65-
}
66-
}

src/ch_13/exercise13_15/Rational.java

+16-12
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,32 @@ public class Rational extends Number implements Comparable<Rational> {
1818
public Rational() {
1919
this(0, 1);
2020
}
21+
/**
22+
* Construct a rational with default property of 1 for denominator
23+
*/
24+
public Rational(long numerator) {
25+
this(numerator, 1);
26+
}
27+
2128

2229
/**
2330
* Construct a rational with specified numerator and denominator
2431
*/
2532
public Rational(long numerator, long denominator) {
26-
long gcd = gcd(numerator, denominator);
27-
this.numerator = BigInteger.valueOf(((denominator > 0) ? 1 : -1) * numerator / gcd);
28-
this.denominator = BigInteger.valueOf(Math.abs(denominator) / gcd);
33+
BigInteger gcd = gcd(numerator, denominator);
34+
this.numerator = BigInteger.valueOf(((denominator > 0) ? 1 : -1) * numerator).divide(gcd);
35+
this.denominator = BigInteger.valueOf(Math.abs(denominator)).divide(gcd);
2936
}
3037

3138
/**
3239
* Find GCD of two numbers
40+
*
41+
* @return BigInteger value of gcd
3342
*/
34-
private static long gcd(long n, long d) {
35-
long n1 = Math.abs(n);
36-
long n2 = Math.abs(d);
37-
int gcd = 1;
38-
for (int k = 1; k <= n1 && k <= n2; k++) {
39-
if (n1 % k == 0 && n2 % k == 0)
40-
gcd = k;
41-
}
42-
return gcd;
43+
private static BigInteger gcd(long n, long d) {
44+
BigInteger b1 = BigInteger.valueOf(n);
45+
BigInteger b2 = BigInteger.valueOf(d);
46+
return b1.gcd(b2);
4347
}
4448

4549
/**
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package ch_13.exercise13_18;
2+
3+
import ch_13.exercise13_15.Rational;
4+
5+
/**
6+
* 13.18 (Use the Rational class) Write a program that computes the following summation series using the Rational class:
7+
* 1/2 + 2/3 + 3/4 + ... + 98/99 + 99/100
8+
* You will discover that the output is incorrect because of integer overflow (too
9+
* large).
10+
* <p>
11+
* To fix this problem, see Programming Exercise 13.15.
12+
*/
13+
public class Exercise13_18 {
14+
public static void main(String[] args) {
15+
Rational resultRational = new Rational();
16+
long i = 2;
17+
while (i <= 100) {
18+
Rational rational = new Rational(i - 1, i);
19+
System.out.print(rational.getNumerator() + "/" + rational.getDenominator());
20+
21+
if (i == 100) System.out.print(" = ");
22+
else if (i % 10 == 0) System.out.println(" + ");
23+
else System.out.print(" + ");
24+
25+
resultRational = resultRational.add(rational);
26+
i++;
27+
}
28+
System.out.println(resultRational.getNumerator() + " / " + resultRational.getDenominator());
29+
30+
}
31+
}

0 commit comments

Comments
 (0)