W13Assignment 11-2014
W13Assignment 11-2014
Problem 1
Part 1 (1 point)
What does it mean if a class is immutable?
Immutable means that once the constructor for an object has completed execution
that instance can't be altered.
Part 2 (3 points)
For each of the following class, determine if the class is mutable or
immutable. (Review the javadocs for the class via the internet) If it is
mutable, list a method from the class that makes it mutable.
a. Circle
Problem 2
Part 1 (1 point)
You have been asked to write code for the following scenario. Identify
the classes that will be needed. Consider the concept of abstraction
when making your decisions. (You are not asked to write the code,
just to identify the classes.)
A veterinary office is looking for some new software. You have been
asked to write the code. Included in the code should be a way to
track clients and their pets, appointments,, and invoices. The
software should be able to compute the cost associated with an
appintment and to identify invoices that have not been paid.
I would have a Client class to list the clients (pet owner) name,
address, telephone number, etc. After that I will have a pet
class. This will store the pets name, age, breed, illness, etc. I
will then have appointments class and then an invoice.
Part 2 (1 point)
Draw the dependency diagram for the classes you identified in part 1
of this problem.
The client (pet owner) is the main class because they own their
pet. They set the appointment, and they pay the bills. From
there, we have the driver that put everything together.
DRIVER
Pets
Clien
Appointments
Invoice
Problem 3
Part 1 (3 points)
Given the following array of integer values, write the code for a
bubble sort to sort first in ascending order, and then in descending
order.
public class IntegerSort
{
public static void main (String[] args)
{
int[] nums = { 5, 6, 1, 9, 20, 15, 79, 54, 89, 7 };
System.out.println("Listing of values before sorting");
for (int i = 0; i < nums.length; ++i)
System.out.println(nums[i]);
System.out.println();
// sort the array in ascending order
/**
Ascending Bubble Sort Method
*/
/**
Descending Bubble Sort Method
*/
}
return arr;
Part 2 (3 points)
Given the following ArrayList of Double values, write the code for a
bubble sort to sort first in ascending order, and then in descending
order.
public class DoubleSort
{
public static void main (String[] args)
{
ArrayList<Double> nums = new ArrayList<Double>();
nums.add(5.0);
nums.add(6.75);
nums.add(1.03);
nums.add(9.14);
nums.add(20.123);
nums.add(15.786);
nums.add(79.01);
nums.add(89.99);
nums.add(7.04);
System.out.println("Listing of values before sorting");
for (int i = 0; i < nums.size(); ++i)
System.out.println(nums.get(i));
System.out.println();
// sort the array in ascending order
ArrayList<Double> ascNums =
bubbleSortAsc(nums, 0, nums.size());
ArrayList<Double> decNums =
bubbleSortDes(nums, 0, nums.size());
System.out.println("After descending sort");
for (int i = 0; i < nums.size(); ++i)
System.out.println(decNums.get(i));
}
/**
Ascending Bubble Sort Method
--last;
}
return arr;
}
/**
Descending Bubble Sort Method
*/
Part 3 (3 points)
Given the following ArrayList of String values, write the code for a
bubble sort to sort first in ascending order, and then in descending
order.
public class StringSort
{
public static void main (String[] args)
{
ArrayList<String> nums = new ArrayList<String>();
nums.add("first");
nums.add("computer");
nums.add("yeah");
nums.add("fist");
nums.add("couch");
nums.add("zenith");
nums.add("help");
nums.add("yellow");
nums.add("hello");
System.out.println("Listing of values before sorting");
for (int i = 0; i < nums.size(); ++i)
System.out.println(nums.get(i));
System.out.println();
// sort the array in ascending order
ArrayList<String> ascNums =
bubbleSortAsc(nums, 0, nums.size());
System.out.println("After ascending sort");
for (int i = 0; i < nums.size(); ++i)
System.out.println(ascNums.get(i));
System.out.println();
// sort the array in ascending order
ArrayList<String> decNums =
bubbleSortDes(nums, 0, nums.size());
System.out.println("After descending sort");
for (int i = 0; i < nums.size(); ++i)
System.out.println(decNums.get(i));
}
/**
Ascending Bubble Sort Method
if (arr.get(i).compareTo(arr.get(i+1)) > 0)
{
didSwap = true;
int temp = arr.get(i);
arr.set(i) = arr.get(i+1);
arr.set(i+1) = temp;
}
--last;
}
return arr;
}
}
/**
Descending Bubble Sort Method
10