Escolar Documentos
Profissional Documentos
Cultura Documentos
Problem 1
Write a main method class that prompts the user to enter a string
value. 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 been
entered and stored, write a second while loop to read through the
values, displaying them to the console in the order which they were
entered. Write a third while loop to read through the values,
displaying them to the console in reverse order.
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class Week10Prob1 {
public static void main(String[] args) {
String myArr[] = new String[10];
int myTries = 10;
int myInput = 0;
Scanner myScan = new Scanner(System.in);
String scanInput;
while(myInput < myTries){
System.out.println(" ");
while (myTries == 10){
System.out.println("Here are your words in order:");
Arrays.sort(myArr);
for (String str : myArr){
System.out.println(str);
}
break;
}
System.out.println(" ");
while (myTries == 10){
System.out.println("Here are your words in reverse order:");
Arrays.sort(myArr, Collections.reverseOrder());
Week 10 Homework
}
}
Week 10 Homework
Part 2 (3 points)
Rewrite your code from Part 1, replacing each of the while loops with
a for loop and replacing the array with an array list.
import
import
import
import
java.util.ArrayList;
java.util.Arrays;
java.util.Collections;
java.util.Scanner;
Week 10 Homework
Part 3 (1 point)
Rewrite your code from Part 2 above, replacing the last two for loops
the loops used to display the values of the array list, with an
enhanced for loop.
import
import
import
import
java.util.ArrayList;
java.util.Arrays;
java.util.Collections;
java.util.Scanner;
}
}
Week 10 Homework
Problem 4 Battleship
BattleShip is a game where ships are hidden in an ocean and
are then searched for using coordinates. In our game, the
ocean will be represented by a matrix of data type char. A
hidden ship is represented by the char value X. A found ship
is represented by the char value B. An unsearched coordinate
in the ocean is represented by the char value .. A searched
coordinate in the ocean is represented by the char value ~.
The matrix could be any size, 2x2, 3x3, 10x10, 20x20, etc.
An example would be: (DO NOT code for the example, just use
as reference)
0
1
2
0
.
B
X
1
.
~
~
2
X
X
X
Week 10 Homework
@Test
public void testIsHit()
{
BattleShip myBattle = new BattleShip();
myBattle.isHit(0, 0);
assertEquals(true, myBattle.isHit(0,0));
myBattle.isHit(0, 1);
Week 10 Homework
assertEquals(false, myBattle.isHit(0,0));
}
Week 10 Homework
Week 10 Homework
0
.
B
X
1
.
~
~
2
X
X
X
0
.
B
.
1
.
~
~
2
.
.
.
{
}
int numberOfX = 0;
String boardOut = " ";
for ( int i = 0; i < this.ocean[0].length; i++) {
boardOut = boardOut + "\t" + i;
}
boardOut = boardOut + "\n";
Week 10 Homework
if (numberOfX == 0) {
boardOut = boardOut + "All ships have been found - AWESOME.";
}
return boardOut;
import java.util.Scanner;
public class BattleShipDriver {
public static void main(String[] args)throws java.lang.Exception {
Week 10 Homework
10
};
myBattle.BattleShip(ocean);
System.out.println(myBattle.toString());
boolean startGame = true;
while(startGame){
System.out.print("Enter a row number: ");
myRShot = myScan.nextInt();
System.out.print("Enter a column number: ");
myCShot = myScan.nextInt();
Week 10 Homework
System.out.println(" ");
}
System.out.println(myBattle.toString());
11
if (myBattle.toString().contains("All")){
startGame = false;
}
}
}
}
Week 10 Homework
12