0% found this document useful (0 votes)
63 views

W13Assignment 11-2014

The document discusses homework problems involving classes, sorting, and bubble sort algorithms. Problem 1 asks about immutable and mutable classes like Circle and Double. Problem 2 involves designing classes for a veterinary software like Client, Pet, Appointment, and Invoice. Problem 3 provides code to sort arrays of integers, doubles, and strings in ascending and descending order using bubble sort algorithms.

Uploaded by

matt g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

W13Assignment 11-2014

The document discusses homework problems involving classes, sorting, and bubble sort algorithms. Problem 1 asks about immutable and mutable classes like Circle and Double. Problem 2 involves designing classes for a veterinary software like Client, Pet, Appointment, and Invoice. Problem 3 provides code to sort arrays of integers, doubles, and strings in ascending and descending order using bubble sort algorithms.

Uploaded by

matt g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Week 13 Homework

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

Circle class is mutable. You can create a new instance


and supply new values to set a new circle.
b. Double

Double objects cant be changed. There is no way to set


a new double variable. All we can do is return the
value.
c. boolean

Immutable. You cannot change within a method.

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

int[] ascNums = bubbleSortAsc(nums, 0,


nums.length);
System.out.println("After ascending sort");
for (int i = 0; i < nums.length; ++i)
System.out.println(ascNums[i]);
System.out.println();
// sort the array in ascending order

int[] decNums = bubbleSortDes(nums, 0,


nums.length);
System.out.println("After descending sort");
for (int i = 0; i < nums.length; ++i)
System.out.println(decNums[i]);
}

/**
Ascending Bubble Sort Method
*/

public static int[] bubbleSortAsc(int[] arr, int left,


int right)
{
boolean didSwap = true;
int last = right-1;
int left = 0;
while (didSwap)
{
didSwap = false;
for (i = left; i < last; ++i)
{
if (arr[i] > arr[i+1])
{
didSwap = true;
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
--last;
}
return arr;
}

/**
Descending Bubble Sort Method
*/

public static int[] bubbleSortDes(int[] arr, int left,


int right)
{
boolean didSwap = true;
int last = right - 1;
int left = 0;
while (didSwap)
{
didSwap = false;

for (i = left; i < last; ++i)


{
for(int j = 1; j <= last; j++){
if(arr[j-1] < arr[j]){
int temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}

}
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());

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<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

I USED ECLIPSE AND IT DIDNT SET IT UP WITH INT


LEFT AND INT RIGHT. I COPIED WHAT I CODED.
*/

public static ArrayList<Double>


bubbleSortAsc(ArrayList<Double> arr, int left, int
right)
{

boolean didSwap = true;


int last = right-1;
int left = 0;
while (didSwap)
{
didSwap = false;
for (i = left; i < last; ++i)
{

if (arr.get(i) > arr.get(i+1))


{
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
*/

public static ArrayList<Double>


bubbleSortDes(ArrayList<Double> arr, int left, int
right)
{

boolean didSwap = true;


int last = right - 1;
int left = 0;
while (didSwap)
{
didSwap = false;
for (i = left; i < last; ++i)
{
for(int j = 1; j <= last; j++){
if(arr.get(j-1) < arr.get(j)){
int temp = arr.get(j-1);
arr.set(j-1) = arr.get(j);
arr.set(j) = temp;
}
}
}
}
return arr;
}

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

I USED ECLIPSE AND IT DIDNT SET IT UP WITH INT


LEFT AND INT RIGHT. I COPIED WHAT I CODED.
*/

public static ArrayList<String>


bubbleSortAsc(ArrayList<String> arr, int left, int
right)
{

boolean didSwap = true;


int last = length-1;
int left = 0;
while (didSwap)
{
didSwap = false;
for (i = left; i < last; ++i)
{

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

I USED ECLIPSE AND IT DIDNT SET IT UP WITH INT


LEFT AND INT RIGHT. I COPIED WHAT I CODED.
*/

public static ArrayList<String>


bubbleSortDes(ArrayList<String> arr, int left, int
right)
{

boolean didSwap = true;


int last = length - 1;
int left = 0;
while (didSwap)
{
didSwap = false;
for (i = left; i < last; ++i)
{
for(int j = 1; j <= last; j++){
if(arr.get(j-1).compareTo(arr.get(j)) < 0){
int temp = arr.get(j-1);
arr.set(j-1) = arr.get(j);
arr.set(j) = temp;
}
}
}
}
return arr;
}

10

You might also like