You are on page 1of 9

COSC 1046 Spring 2014 - Assignment 3

Due Monday June 2


nd
2014

!"#$ &$$#'()*(+ #$ ,*$#'(*, +- '#.* /-0 $-)* 12&3+#3* &(, *41*2#*(3* 52#+#(' 6&.& &117#3&+#-($ 0$#('
)*+"-,$ &(, &22&/$8 9&3" :0*$+#-( $1*3#;#*$ +"* (&)* +"&+ *&3" &117#3&+#-( $"-07, "&.*8 <7*&$* ;-77-5 +"#$
(&)#(' 3-(.*(+#-(8 ="*( /-0 "&.* 3-)17*+*, +"* &$$#'()*(+ 3-)12*$$ &77 -; /-02 ;#7*$ #(+- & $#('7* &23"#.*
0$#(' =#(,-5$ >?#'"+ @7#3A ;-7,*2 BC D*(, !- BC @-)12*$$*, E-7,*2F -2 GD H >?#'"+ @7#3A ;-7,*2 BC @-)12*$$F8
I$#(' & J
2,
1&2+/ 3-)12*$$#-( 0+#7#+/ $03" &$ =#(?K? -2 LM#1 )&/ 2*(,*2 /-02 ;#7*$ 0(2*&,&N7* &(, 0(B
)&2A&N7*8 D0N)#+ $#('7* 3-)12*$$*, ;#7* +- OPQ8 R-0 3&( 2*$0N)#+ /-02 ;#7*$ &$ )&(/ +#)*$ &$ /-0 5-07, 7#A*
01 +- +"* ,0* ,&+* &(, +#)*8

!" $%&" '( )*+,%-" .(%& */0" /*- / 1&)"2 -"$+&)3')(* (2 .(%& 3&(4&/0 5/$ +(00"*'$6 /' '7" '(3 (2
"/+7 2),"8 <&/ &++*(+#-( +- 0$#(' '--, .&2#&N7* (&)*$8 S; /-0 "&.* &(/ :0*$+#-($ 17*&$* 3"*3A -2 1-$+ +- +"*
;-20)8

D0N)#+ $-70+#-($ +- :0*$+#-( PT J &(, 9S!U9? :0*$+#-( V -2 :0*$+#-( W GXQR8

Question 1. (question1.java) (20 marks) Hey, a science question! The resistance in a strand of wire can be
computed using:

! !
!"#
!!
!


where ! is the resistivity of the wire, L is the length and d is the diameter. The diameter of wire can be determined
given the American wire gauge number:

! ! !! !"# ! !"
!"!!
!"


where n is the AWG number.

Write a program that prompts the user for the AWG number (integer) and the length of a piece of wire (double).
Then compute the electrical resistance. Youll need one method to determine the diameter of the wire from the
AWG number. Then, pass this diameter and the length you collected from the user to two other methods that
compute two different resistance values. One method uses the formula above to compute the resistance in copper
wire (copper has a resistivity (!! of 1.68 ! 10
-8
!/m) and the other method uses the same formula to compute the
resistance in aluminum wire (aluminum has a resistivity (!! of 2.82 ! 10
-8
!/m). Have your program print out the
resistance for both. Hint, the resistivity values are not method arguments. They can be hard-coded in the method.
Each of these methods only has a couple of lines of code.

Remember, write a main() method to collect the necessary input, to call the other methods and collect their
returned values and then to display the output.

C:\cosc1046\a3\>java question1
java question1
Enter wire gauge:22
Enter wire length (in m.):10
Diameter of the wire is: 0.6438032984904795
Resistance in copper is: 5.160752189379868E-7 ohms
Resistance in aluminum is: 8.662691175030492E-7 ohms

C:\cosc1046\a3\>java question1
Enter wire gauge:2
Enter wire length (in m.):1825.23
Diameter of the wire is: 6.543707496202785
Resistance in copper is: 9.117782800853958E-7 ohms
Resistance in aluminum is: 1.5304849701433429E-6 ohms
Question 2. (question2.java) (20 marks) Write a class that has the following methods:

passwordCheck: This method takes a single String argument and returns a boolean value. The
purpose of this method is to test the strength of a password. To be considered strong a password
must have the following properties:
-at least 8 characters in length
-at least one uppercase and one lowercase letter
-at least one digit

If the string parameter passed to the method passes all of these tests, return true, else return false.
Use your main method to report whether or not the password is strong.

power: This method takes two integers as arguments, one representing the base and another
representing the exponent. The method then uses a for loop to compute and return the result of
base
exponent
. Eg. 2
3
=8.

compoundInterest: This method takes in all of the necessary arguments to compute and return
the future value (futureValue) of an investment given the initial investment (initialValue), an
interest rate (i) and a number of time periods (n).

!"#"$%&'("% ! !"!#!$%&$%'( ! !!
!


fix: Matlab
TM
has a great method called fix which returns the decimal portion of a floating point
number. Write a method that takes in a double and returns only the decimal portion. Eg.
3.14159 passed in would return 0.14159. There may be some rounding issues as you test your
method. Dont worry about that too much but if you want to try and solve that, thats ok too.

Finally, write a main() method that prompts the user to enter inputs appropriate for testing
your methods. Remember, to do all input and output in your main() method. Pass the input to
your methods and collect the results using assignment statements. You dont need to do any
input checking. Assume the user enters appropriate values. I suggest your write and test your
methods one at a time. You can get part marks for the methods that work.


See sample output on the next page.

C:\cosc1046\a4\>java question2
Tester password checker...

Enter a password: apples
Password is weak.

Testing method power...

Enter 2 integers, base then exponent: 2 3
2 to the power of 3 is 8

Testing compound interest...

Enter 3 integers, initial investment, interest rate and number of time units:
1000 0.05 20
Your investment will be worth: $2653.2977051444223

Testing the fix method ...

Enter a decimal number: 2.5
Decimal portion is 0.5


C:\cosc1046\a4\>java question2
Tester password checker...

Enter a password: iluvCOSC1046
Password is strong.

Testing method power...

Enter 2 integers, base then exponent: 10 3
10 to the power of 3 is 1000

Testing compound interest...

Enter 3 integers, initial investment, interest rate and number of time units:
125 0.025 10
Your investment will be worth: $160.01056802454457

Testing the fix method ...

Enter a decimal number: 3.14
Decimal portion is 0.14000000000000012
Question 3. (question3.java 20 marks) Write a program that prompts the user to enter in
an integer number representing the number of elements in an integer array. Create the array and
prompt the user to enter in values for each element using a for loop. When the array is full,
display the following:

The values in the array on a single line.
The array with all of the elements reversed. Just print the values in reverse order, dont alter
the array.
The array with all of the elements in the array shifted one to the left (the element in the first
index is moved to the end). Here, alter the array by moving the values then print the new
array.
The values from the shifted array that have odd numbered indexes. Do not use an if
statement here.

Use a separate for loop for each task.

Prompt the user one more time for a single integer. Print the index of each occurrence of that
number in the array or print nothing if the integer is not present. All of this work can be done in
a single main( ) method. See my sample output below for three separate executions of the
program:

C:\cosc1046\a3\>java question3
How many elements in your array?: 7
Enter number: 1
Enter number: 2
Enter number: 3
Enter number: 4
Enter number: 5
Enter number: 6
Enter number: 7

Your array:
1 2 3 4 5 6 7

Your array printed in reverse:
7 6 5 4 3 2 1

Your array shifted:
2 3 4 5 6 7 1

Values at odd-numbered indices:
3 5 7

Enter a value to look for: 5
Found at index: 3
C:\cosc1046\a3\>java question
How many elements in your array?: 5
Enter number: 2
Enter number: 2
Enter number: 5
Enter number: 6
Enter number: 2

Your array:
2 2 5 6 2

Your array printed in reverse:
2 6 5 2 2

Your array shifted:
2 5 6 2 2

Values at odd-numbered indices:
5 2

Enter a value to look for: 2
Found at index: 0
Found at index: 3
Found at index: 4


C:\cosc1046\a4\>java question3
How many elements in your array?: 5
Enter number: 12
Enter number: 20
Enter number: 51
Enter number: 2
Enter number: 19

Your array:
12 20 51 2 19

Your array printed in reverse:
19 2 51 20 12

Your array shifted:
20 51 2 19 12

Values at odd-numbered indices:
51 19

Enter a value to look for: 7
Question 4. (question4.java) (20 marks) Write a class that has the following methods. Use nested for loops for all of these
array operations:

public static void printArray(int[][] a) : print the values stored in the array

public static int sum(int[][] a) : compute and return the sum of the values in the array

public static int[][] sum(int[][] a, int[][] b) : compute the sum of two arrays and return a new array with those values.
Remember, each value in array a gets added to the corresponding value in array b. Assume that the arrays are the same size.

public static boolean isFound(int[][], int n) : return true if the value n is found in the array. Return false if it is not
found.

In your main() method prompt the user to enter two integers that will be the rows and columns for two 2D arrays. Create the
arrays according to the input dimensions and fill them with random values from 0 to 15. Then prompt for a third integer that will
be used to test your isFound() method. Remember, to do all input and output in your main() method. Pass the input to
your methods and collect the results using assignment statements. You dont need to do any input checking. Assume the user
enters appropriate values. I suggest your write and test your methods one at a time. You can get part marks for the methods that
work.


java question4
Enter two integers for rows and columns of the arrays:3 4

First array:
5 2 13 3
9 10 11 7
7 11 1 14

Second array:
7 15 2 1
1 2 2 9
11 2 5 4

Sum first array: 93
Sum second array: 61
Sum of first array + second array:
12 17 15 4
10 12 13 16
18 13 6 18

Enter an integer to search for: 3
3 found in first array?: true
3 found in second array?: false



java question4
Enter two integers for rows and columns of the arrays:2 6

First array:
3 9 10 10 0 4
4 6 3 3 15 5

Second array:
5 0 0 10 2 0
0 5 13 14 6 2

Sum first array: 72
Sum second array: 57
Sum of first array + second array:
8 9 10 20 2 4
4 11 16 17 21 7

Enter an integer to search for: 0
0 found in first array?: true
0 found in second array?: true

Question 5. (TicTacToe.java 20 marks) Write a text-based, 2-person, tic-tac-toe game.
This game will combine most of what you have learned so far. Youll use methods and a 2-D
array in this game. A general description of how to proceed and what is expected follows below.
The flow of your game should be roughly as follows:

Display the instructions for the game
Assume X always goes first
Create the empty board (initialize your 2D array)
Display the board
While there isnt a winner or a tie
Get the players move (collect a row and a column)
While the players move is not legal
Get the players move
Update the board with the move
Display the updated board
Switch turns
Congratulate the winner or declare a tie

The main method declares the 2D array, keeps track of whose turn it is, takes care of
collecting the players move (a row and a column) and executes the flow of the program as
described above by calling the necessary methods.

Here are the methods that I will be looking for in your program:

public static void displayInstructions( ): This is a void method that takes no arguments. Its
job is simply to display the instructions for the game (see below).

public char[][] createBoard( ): This method takes no arguments but returns a 3x3 array of characters
with each character set to (space).

public static void displayBoard(char[][] board): This method takes the current game board
as an argument and displays it to the screen.

public static boolean isWinner(char[][] board): This method takes the current game board as
an argument and determines if there is a winner. If there is, it returns true else return false.

public static boolean isTie(char[][] board): This method takes the current game board as an
argument and determines if there is a tie. If there is, it returns true else return false.

public static boolean legalMove(int row, int column, char[][] board): This method
checks to see if the users move is legal. A legal move is inside the board to a space that isnt currently occupied. It
returns true or false accordingly.

public static void updateBoard(int row, int column, char player, char[][]
board): This method updates the board with the players move.

Sample output from two separate games is shown below.



Instructions: Fill in a complete row, column or diagonal with either X's or O's to win
Recall that array indices start at 0!


| |
----------
| |
----------
| |


Player X--> Enter a row and column: 1
1

| |
----------
| X |
----------
| |
Player O--> Enter a row and column: 0 0

O | |
----------
| X |
----------
| |
Player X--> Enter a row and column: 1 1
Player X--> Enter a row and column: -1 -1
Player X--> Enter a row and column: 1 5
Player X--> Enter a row and column: 0 2

O | | X
----------
| X |
----------
| |
Player O--> Enter a row and column: 2 0

O | | X
----------
| X |
----------
O | |
Player X--> Enter a row and column: 1 0

O | | X
----------
X | X |
----------
O | |
Player O--> Enter a row and column: 2 2

O | | X
----------
X | X |
----------
O | | O
Player X--> Enter a row and column: 1 2

O | | X
----------
X | X | X
----------
O | | O

Game over!
Congratulations player X, you won!!!









Instructions: Fill in a complete row, column or diagonal with either X's or O's to win
Recall that array indices start at 0!


| |
----------
| |
----------
| |


Player X--> Enter a row and column: 1 1

| |
----------
| X |
----------
| |
Player O--> Enter a row and column: 0 0

O | |
----------
| X |
----------
| |
Player X--> Enter a row and column: 2 2

O | |
----------
| X |
----------
| | X
Player O--> Enter a row and column: 0 2

O | | O
----------
| X |
----------
| | X
Player X--> Enter a row and column: 0 1

O | X | O
----------
| X |
----------
| | X
Player O--> Enter a row and column: 2 1

O | X | O
----------
| X |
----------
| O | X
Player X--> Enter a row and column: 1 0

O | X | O
----------
X | X |
----------
| O | X
Player O--> Enter a row and column: 1 2

O | X | O
----------
X | X | O
----------
| O | X
Player X--> Enter a row and column: 2 0

O | X | O
----------
X | X | O
----------
X | O | X

Game over!
There was a tie

You might also like