W13Assignment 11-2014

download W13Assignment 11-2014

of 10

Transcript of W13Assignment 11-2014

  • 7/24/2019 W13Assignment 11-2014

    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 orimmutable. (Review the javadocs for the class via the internet) If it ismutable, list a method from the class that makes it mutable.

    a. ircle

    Circle class is mutable. You can create a new instanceand supply new values to set a new circle.

    b. !ouble

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

    a new double variable. All we can do is return thevalue.

    c. boolean

    Immutable. You cannot change within a method.

    1

  • 7/24/2019 W13Assignment 11-2014

    2/10

    Problem 2

    Part 1 (1 point)"ou have been asked to write code for the following scenario. Identif#the classes that will be needed. onsider the conce$t of abstraction

    when making #our decisions. ("ou are not asked to write the code,just to identif# the classes.)

    % veterinar# o&ice is looking for some new software. "ou have beenasked to write the code. Included in the code should be a wa# totrack clients and their $ets, a$$ointments,, and invoices. 'hesoftware should be able to com$ute the cost associated with ana$$intment and to identif# invoices that have not been $aid.

    I would have a Client class to list the clients (pet owner) name,address, telephone number, etc !"ter that I will have a pet

    class #his will store the pets name, a$e, breed, illness, etc Iwill then have appointments class and then an invoice

    Part 2 (1 point)!raw the de$endenc# diagram for the classes #ou identied in $art of this $roblem.

    #he client (pet owner) is the main class because the% own theirpet #he% set the appointment, and the% pa% the bills &rom

    there, we have the driver that put ever%thin$ to$ether

    Invoice

    A ointments

    DRIVER

    2

  • 7/24/2019 W13Assignment 11-2014

    3/10

    Problem 3

    Part 1 (3 points)*iven the following arra# of integer values, write the code for a

    bubble sort to sort rst in ascending order, and then in descendingorder.

    $ublic class Integer+ort $ublic static void main (+tring- args) int- nums / 0, 1, , 2, 34, 0, 52, 06, 72, 5 89

    +#stem.out.$rintln(:;isting of values before sorting:)9

    for (int i / 49 i < nums.length9 ==i) +#stem.out.$rintln(nums-i)9

    +#stem.out.$rintln()9

    >> sort the arra# in ascending order

    int[] ascums ! bubble"ortAsc#nums$ %$nums.length&'

    +#stem.out.$rintln(:%fter ascending sort:)9

    for (int i / 49 i < nums.length9 ==i) +#stem.out.$rintln(ascums-i)9

    +#stem.out.$rintln()9

    >> sort the arra# in ascending order

    int[] decums ! bubble"ortDes#nums$ %$nums.length&'

    +#stem.out.$rintln(:%fter descending sort:)9 for (int i / 49 i < nums.length9 ==i) +#stem.out.$rintln(decums-i)9 8

    3

  • 7/24/2019 W13Assignment 11-2014

    4/10

    >@@ %scending Aubble +ort Bethod @>

    public static int[] bubble"ortAsc#int[] arr$ int le(t$int right&

    booleandidSwap= true;intlast= right-1;intleft= 0;while(didSwap){

    didSwap= false;

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

    if(arr[i] > arr[i+1]){

    didSwap= true;inttemp= arr[i];arr[i] = arr[i+1];arr[i+1] = temp;

    }}

    --last;}

    returnarr;}

    >@@

    !escending Aubble +ort Bethod @>

    public static int[] bubble"ortDes#int[] arr$ int le(t$int right&

    booleandidSwap= true;intlast= right- 1;intleft= 0;while(didSwap){

    didSwap= false;

    4

  • 7/24/2019 W13Assignment 11-2014

    5/10

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

    for(intj= 1;j nums.add(.4D)9 nums.add(2.6)9 nums.add(34.3D)9 nums.add(0.571)9 nums.add(52.4)9 nums.add(72.22)9 nums.add(5.46)9

    +#stem.out.$rintln(:;isting of values before sorting:)9

    for (int i / 49 i < nums.siEe()9 ==i)

    +#stem.out.$rintln(nums.get(i))9

    +#stem.out.$rintln()9

    >> sort the arra# in ascending order

    Array)ist*Double+ ascums !bubble"ortAsc#nums$ %$ nums.si,e#&&'

    5

  • 7/24/2019 W13Assignment 11-2014

    6/10

    +#stem.out.$rintln(:%fter ascending sort:)9

    for (int i / 49 i < nums.siEe()9 ==i) +#stem.out.$rintln(ascums.get(i))9

    +#stem.out.$rintln()9

    >> sort the arra# in ascending order

    Array)ist*Double+ decums !bubble"ortDes#nums$ %$ nums.si,e#&&'

    +#stem.out.$rintln(:%fter descending sort:)9 for (int i / 49 i < nums.siEe()9 ==i) +#stem.out.$rintln(decums.get(i))9 8

    >@@ %scending Aubble +ort Bethod

    I -"D C)I/" AD IT DIDT "T IT -/ 0IT1 IT)2T AD IT 3I41T. I C5/ID 01AT I C5DD.

    @>

    public static Array)ist*Double+bubble"ortAsc#Array)ist*Double+ arr$ int le(t$ intright&

    booleandidSwap= true;intlast= right-1;intleft= 0;while(didSwap){

    didSwap= false;

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

    if(arrget(i) > arrget(i+1)){

    didSwap= true;inttemp= arrget(i);arrset(i) = arrget(i+1);arrset(i+1) = temp;

    }}

    --last;}

    6

  • 7/24/2019 W13Assignment 11-2014

    7/10

    returnarr;}

    >@@ !escending Aubble +ort Bethod

    @>

    public static Array)ist*Double+bubble"ortDes#Array)ist*Double+ arr$ int le(t$ intright&

    booleandidSwap= true;intlast= right- 1;intleft= 0;

    while(didSwap){

    didSwap= false;

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

    for(intj= 1;j

  • 7/24/2019 W13Assignment 11-2014

    8/10

    Part 3 (3 points)*iven the following %rra#;ist of +tring values, write the code for abubble sort to sort rst in ascending order, and then in descendingorder.

    $ublic class +tring+ort $ublic static void main (+tring- args) %rra#;ist sort the arra# in ascending order

    Array)ist*"tring+ ascums !bubble"ortAsc#nums$ %$ nums.si,e#&&'

    +#stem.out.$rintln(:%fter ascending sort:)9

    for (int i / 49 i < nums.siEe()9 ==i) +#stem.out.$rintln(ascums.get(i))9

    +#stem.out.$rintln()9

    >> sort the arra# in ascending order

    Array)ist*"tring+ decums !bubble"ortDes#nums$ %$ nums.si,e#&&'

    +#stem.out.$rintln(:%fter descending sort:)9 for (int i / 49 i < nums.siEe()9 ==i)

    8

  • 7/24/2019 W13Assignment 11-2014

    9/10

    +#stem.out.$rintln(decums.get(i))9 8

    >@@ %scending Aubble +ort Bethod

    I -"D C)I/" AD IT DIDT "T IT -/ 0IT1 IT)2T AD IT 3I41T. I C5/ID 01AT I C5DD. @>

    public static Array)ist*"tring+bubble"ortAsc#Array)ist*"tring+ arr$ int le(t$ intright&

    booleandidSwap= true;intlast= le!gth-1;intleft= 0;

    while(didSwap){

    didSwap= false;

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

    if(arrget(i)"#mpare$#(arrget(i+1)) > 0){

    didSwap= true;inttemp= arrget(i);arrset(i) = arrget(i+1);

    arrset(i+1) = temp;}

    }--last;}

    returnarr;}

    }

    >@@ !escending Aubble +ort Bethod

    I -"D C)I/" AD IT DIDT "T IT -/ 0IT1 IT)2T AD IT 3I41T. I C5/ID 01AT I C5DD.

    @>

    9

  • 7/24/2019 W13Assignment 11-2014

    10/10

    public static Array)ist*"tring+bubble"ortDes#Array)ist*"tring+ arr$ int le(t$ intright&

    booleandidSwap= true;intlast= le!gth- 1;intleft= 0;while(didSwap){

    didSwap= false;

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

    for(intj= 1;j