CH 1 Solutions Manual For Data Structures With Java.
CH 1 Solutions Manual For Data Structures With Java.
Object-Oriented Programming
Exercises
1.1 The requirements stage would generate a user manual like that shown here:
User Manual
Enter the string: java CelsiusToFahrenheit <first> <last> <incr>
at the command line, where <first> is the first Celsius temperature to be
converted, <first> is the last Celsius temperature, and <incr> is the Celsius
increment for the table. The output will be a conversion table with that range
and increment.
Example:
Input: java Convert 0 40 10
Output: 032
1050
2068
3086
40104
The design stage could adapt the same class as shown in Listing 1.1.
The implementation stage could adapt the same class as shown in Listing 1.2.
The testing stage could run a test driver like this:
public class CelsiusToFahrenheit {
public static void main(String[] args) {
if (args.length!=3) exit();
double first = Double.parseDouble(args[0]);
double last = Double.parseDouble(args[1]);
double incr = Double.parseDouble(args[2]);
for (double i=first; i<=last; i += incr)
System.out.println(i + “\t” + new MyTemperature(value,'F') );
}
1
2 Chapter 1 Binary Trees
-n1:int
-n2:int
-n3:int
-open:boolean
+changeComb(int,int,int,int,int,int):boolean
+close()
+isOpen():boolean
+open(int,int,int):boolean
1.4 If d is a divisor of n that is greater than n , then m = n/d must be a whole number (i.e., an
integer), and therefore another divisor of n. But since d > n , we have m = n/d < n/ n =
n . So by checking for divisors only among those integers that are less than n ,
the existence of the divisor d > n will be found indirectly from m = n/d < n .
1.5
Course
Section
Instructor Student
Person
Solutions 3
1.6
Department Course
Chair Section
Instructor Student
Person
1.7
Course
Section Undergraduate
Instructor Graduate
Person Student
1.8
Vehicle
Bus Car
Driver Owner
Person
4 Chapter 1 Binary Trees
1.9
Bank Manager Person
BankBranch Customer
1.10
Media
Programming Problems
1.1 /**
* An interface for representing temperatures, with functionality
* for converting their values between Celsius and Fahrenheit.
* @author John R. Hubbard
* @see MyTemperature
*/
Solutions 5
}
public void setTime(String time) {
this.time = time;
}
public void setInstructor(Instructor instructor) {
this.instructor = instructor;
}
public void setStudents(Student[] students) {
int n = students.length;
// duplicate the array object:
this.students = new Student[n];
// but do not duplicate the Student objects:
for (int i=0; i<n; i++)
this.students[i] = students[i];
}
public String toString() {
return course + ": " + term + ", " + place + ", " + time + ", "
+ instructor;
}
public static void main(String[] args) {
Course course = new Course("CMSC", "221", "Data Structures", 4);
Section section = new Section(course, "Fall 2004", null, null);
System.out.println(section);
}
}
import java.util.*;
public class Person {
protected int yob;
protected String email;
protected String id;
protected boolean male;
protected String name;
public Person(String name, String id, String sex, int yob) {
this.id = id;
this.male = (sex.substring(0,1).toUpperCase() == "M");
this.name = name;
this.yob = yob;
}
public int getYob() {
return yob;
}
12 Chapter 1 Binary Trees
import java.util.*;
public class Student extends Person {
protected String country;
protected int credits;
protected double gpa;
public Student(String name, String id, String s, int y, String c) {
Solutions 13
import java.util.*;
public class Instructor extends Person {
protected String dept;
protected String office;
protected String tel;
public Instructor(String name, String id, String sex, int yob) {
super(name, id, sex, yob);
}
public String getDept() {
return office;
}
public String getOffice() {
return office;
}
14 Chapter 1 Binary Trees
}
public void close() {
open = false;
}
public boolean isOpen() {
return open;
}
public boolean open(int n1, int n2, int n3) {
if (this.n1 == n1 && this.n2 == n2 && this.n3 == n3) open = true;
else open = false;
return open;
}
}
package chap01.prob07;
public class TestStudent {
public static void main(String[] args) {
Student joe = new Student("Joe", true, 1983, "IT");
joe.updateTranscript(new Section("CS211.02"), new Grade("A-"));
joe.updateTranscript(new Section("EC110.07"), new Grade("B+"));
joe.printTranscript();
}
}
1.8 class Phone {
private String areaCode, number;
public Phone(String areaCode, String number) {
this.areaCode = areaCode;
this.number = number;
}
public Phone(Phone that) {
this.areaCode = that.areaCode;
this.number = that.number;
}
public void setAreaCode(String areaCode) {
this.areaCode = areaCode;
Solutions 17
}
public String toString() {
return "(" + areaCode + ")" + number.substring(0, 3)
+ "-" + number.substring(3);
}
}