W10Assignment 11-2014

download W10Assignment 11-2014

of 12

Transcript of W10Assignment 11-2014

  • 7/24/2019 W10Assignment 11-2014

    1/12

    Week 10 HomeworkProblem 1Write a main method class that prompts the user to enter a stringvalue. Complete as indicated below in each section.

    Part 1 (3 points)Using a while loop, prompt the user 10 times to enter a string value.Store the values entered in an array. After the values have beenentered and stored, write a second while loop to read through thevalues, displaying them to the console in the order which they wereentered. Write a third while loop to read through the values,displaying them to the console in reverse order.

    importjava.util.Arrays;importjava.util.Collections;importjava.util.Scanner;

    publicclassWeek10Prob1 {

    publicstaticvoidmain(String[ args! {

    String myArr[ " newString[10;intmy#ries" 10;intmy$n%ut" 0;Scanner myScan" newScanner(System.in!;String scan$n%ut;

    while(my$n%ut& my#ries!{

    System.out.%rintln('nter a single )or*+ '!;scan$n%ut" myScan.ne,t-ine(!;myArr[my$n%ut " scan$n%ut;my$n%ut;

    /

    System.out.%rintln(' '!;

    while(my#ries"" 10!{System.out.%rintln('ere are your )or*s in or*er+'!;Arrays.sort(myArr!;for(String str+ myArr!{

    System.out.%rintln(str!;

    /break;

    /

    System.out.%rintln(' '!;

    while(my#ries"" 10!{System.out.%rintln('ere are your )or*s in reverse or*er+'!;Arrays.sort(myArr Collections.reverseOrder(!!;

    Week 10 Homework 1

  • 7/24/2019 W10Assignment 11-2014

    2/12

    for(String str+ myArr!{System.out.%rintln(str!;

    /break;

    /

    /

    /

    Week 10 Homework 2

  • 7/24/2019 W10Assignment 11-2014

    3/12

    Part 2 (3 points)ewrite your code from !art 1, replacing each of the while loops witha for loop and replacing the array with an array list.

    importjava.util.Array-ist;importjava.util.Arrays;importjava.util.Collections;importjava.util.Scanner;

    publicclassWeek10Prob1 {

    publicstaticvoidmain(String[ args! {

    Array-ist&String2 myArr" newArray-ist&String2(!;Scanner myScan" newScanner(System.in!;

    for(inti" 0; i& 10; i! {

    System.out.%rintln('nter a single )or*+ '!; String inPut" myScan.ne,t-ine(!; myArr.a**(inPut!; /

    System.out.%rintln('ere are your )or*s in or*er+'!;

    for(inti" 0; i& myArr.si3e(!; i!{ Collections.sort(myArr!; System.out.%rintln(myArr.get(i!!; /

    System.out.%rintln(' '!;

    System.out.%rintln('ere are your )or*s in reverse+'!;for(inti" 0; i& myArr.si3e(!; i!{

    Collections.sort(myArr Collections.reverseOrder(!!;System.out.%rintln(myArr.get(i!!;

    /

    /

    /

    Week 10 Homework 3

  • 7/24/2019 W10Assignment 11-2014

    4/12

    Part 3 (1 point)ewrite your code from !art " above, replacing the last two for loops# the loops used to display the values of the array list, with anenhanced for loop.

    importjava.util.Array-ist;importjava.util.Arrays;importjava.util.Collections;importjava.util.Scanner;

    publicclassWeek10Prob1 {

    publicstaticvoidmain(String[ args! {

    Array-ist&String2 myArr" newArray-ist&String2(!;

    Scanner myScan" newScanner(System.in!;

    for(inti" 0; i& 10; i! { System.out.%rintln('nter a single )or*+ '!; String inPut" myScan.ne,t-ine(!; myArr.a**(inPut!; /

    System.out.%rintln('ere are t4e )or*s in t4e array+'!;

    for(String my5orArr+ myArr!{ System.out.%rintln(my5orArr!; /

    /

    /

    Week 10 Homework 4

  • 7/24/2019 W10Assignment 11-2014

    5/12

    Problem 4 Battleship

    BattleShip is a game where ships are hidden in an ocean andare then searched or !sing coordinates" #n o!r game$ theocean will be represented b% a matri& o data t%pe char" '

    hidden ship is represented b% the char al!e *+" ' o!nd shipis represented b% the char al!e B+" 'n !nsearched coordinatein the ocean is represented b% the char al!e "+" ' searchedcoordinate in the ocean is represented b% the char al!e ,+"-he matri& co!ld be an% si.e$ 2&2$ 3&3$ 10&10$ 20&20$ etc"

    'n e&ample wo!ld be/ ( - code or the e&ample$ !st !seas reerence)

    0 1 20 " " *

    1 B , *2 * , *

    The . in position 0,0 indicates that this position does not

    contain a ship and it has not yet been search.

    The B in position 1,0 indicates that it has been search and

    it contains a ship.

    The X in position 2,0 indicates that it has not been search

    and it contains a ship.

    The ~ in position 1,1 indicates that it has been searched

    and it does not contain a ship.

    Week 10 Homework 5

  • 7/24/2019 W10Assignment 11-2014

    6/12

    Part 1 -est ase (2 points)

    Write test cases for the following constructor and method

    Battle!hip"char#$#$ in%cean&

    This constructor creates a Battle!hip ob'ect. (t acceptsone e)picit constructor, a matri) consisting of charob'ects. *ou will need to build an ob'ect and pass it to theconstructor and then test to ma+e sure the ob'ect hasbeen successfully created "not null&.

    boolean isit"int row, int col&This method returns true if a ship is found at the speci-edcoordinate. (f a ship is found, the X at the position ischanged from an X to a B. (t returns false if a ship is notfound at the speci-ed coordinate. (f a ship is not found,

    the . at the position is changed from . to ~.

    ( (/T T(3 4%5 T(!. ( 67T !%3T(/8T%8T5 599* :7(;

  • 7/24/2019 W10Assignment 11-2014

    7/12

    assertEquals(false my8attle.isit(00!!;/

    /

    Week 10 Homework 7

  • 7/24/2019 W10Assignment 11-2014

    8/12

    Part 2 BattleShip lass (3 points)

    Write the Battle!hip ;lass with the following methods

    (nstance =ariable > matri) of data type char, do not specify a

    si?e. This will be determined by the matri) passed to theconstructor.

    Battle!hip"char#$#$ in%cean&This constructor creates a Battle!hip ob'ect. (t acceptsone e)picit constructor, a matri) consisting of charob'ects. !et the matri) instance =ariable to the passed ine)plicit parameter.

    boolean isit"int row, int col&

    This method returns true if a ship is found at the

    speci-ed coordinate. (f a ship is found, the X at theposition is changed from an X to a B.

    (t returns false if a ship is not found at the speci-ed

    coordinate. (f a ship is not found, the . at the positionis changed from . to ~.

    !tring to!tring"&

    Build a string containing the matri).

    The -rst row should contain the column labels of each

    column. !+ip the -rst column as this will contain the

    row labels. The -rst column should contain the row labels of each

    row.

    (f a position in the matri) contains a X, display . to

    show it has not yet been searched.

    (f all of the ships ha=e been found "no X@s in the

    matri)&, display AW(//5 ll ships ha=e been found.

    To ha=e eCual spacing when printing the matri) use ADt

    to tab between elements and ADn at the end of eachrow to start a new row.

    (f the matri) consists of

    0 1 20 " " *1 B , *

    2 * , *

    Week 10 Homework 8

  • 7/24/2019 W10Assignment 11-2014

    9/12

    The string returned should contain

    0 1 20 " " "1 B , "

    2 " , "

    publicclass8attleS4i% {

    esult; if( (ro)2 this.ocean.lengt4! ?? (col2 this.ocean[ro).lengt4!! { returnfalse;

    / if( ocean[ro)[col "" @@! { this.ocean[ro)[col " @8@;

    my>esult" true; / elseif(ocean[ro)[col "" @.@! { this.ocean[ro)[col " @B@; my>esult" false; / else{ my>esult" false; / returnmy>esult;

    /

    publicString toString(! {

    intnumber=7" 0; String boar*=ut" ' ';

    for( inti" 0; i& this.ocean[0.lengt4; i! {

    boar*=ut" boar*=ut 't' i; /

    boar*=ut" boar*=ut 'n';

    Week 10 Homework 9

  • 7/24/2019 W10Assignment 11-2014

    10/12

    for( inti" 0; i& this.ocean[0.lengt4; i! { boar*=ut" boar*=ut i; for( intj" 0;j& this.ocean[i.lengt4;j! { if( this.ocean[i[j "" @@! { boar*=ut" boar*=ut 't.'; number=7;

    / else{ boar*=ut" boar*=ut 't' this.ocean[i[j; / / boar*=ut" boar*=ut 'n'; /

    if(number=7"" 0! {

    boar*=ut" boar*=ut 'All s4i%s 4ave been 7oun* D AWS=E.'; / returnboar*=ut; /

    /

    Part 3 BattleShiprier lass (3 points)

    Write the Battle!hipri=er ;lass to do the following

    Build a char matri) of any si?e

    o 6opulate the matri) with E.@ and EX@ where E.@

    indicates an empty position and EX@ indicates ahidden ship.

    7se the Battle!hip class written in 6art 2

    ;onstruct a Battleship ob'ect, passing your matri) as the

    e)plicit parameter

    isplay the Battle!hip ob'ect matri)

    (n a loop

    o ;ontinue looping until sentinel =alue is entered

    o 6rompt the user to enter a row coordinate

    o 6rompt the user to enter a column

    o ;hec+ to see if the entered coordinates are a hit

    (f they are a hit display (T

    (f they are not a hit display 3(!!o isplay the matri)

    importjava.util.Scanner;

    publicclass8attleS4i%Friver {

    publicstaticvoidmain(String[ args!throwsjava.lang.,ce%tion {

    Week 10 Homework 10

  • 7/24/2019 W10Assignment 11-2014

    11/12

    Scanner myScan" newScanner(System.in!;

    intmy>S4ot;

    intmyCS4ot;

    8attleS4i% my8attle" new8attleS4i%(!;

    System.out.%rintln('-et@s %lay some 8attleS4i%G'!;

    System.out.%rintln(' '!;

    System.out.%rintln('@.@ stan*s 7or unsearc4e* )ater.'!;

    System.out.%rintln('@B@ stan*s 7or searc4e* )ater.'!;

    System.out.%rintln('@8@ means you 4it a s4i%G'!;

    System.out.%rintln(' '!;

    System.out.%rintln(' '!;

    System.out.%rintln('AKAJA$I.'!;

    System.out.%rintln(' '!;

    /

    System.out.%rintln(my8attle.toString(!!;

    Week 10 Homework 11

  • 7/24/2019 W10Assignment 11-2014

    12/12

    if(my8attle.toString(!.contains('All'!!{

    startJame" false;

    /

    /

    /

    /

    Week 10 Homework 12