SphereAndCone .java

SphereAndCone .java

Question 1.   (SphereAndCone .java)  (20 marks) Write a single class that has the methods:

public static double sphereVolume(double r)
public static double sphereSurface(double r)
public static double coneVolume(double r, double h)
public static double coneSurface(double r, double h)

Then write a main()  method that prompts the user for values for r  (radius)  and  h (height) and calls all of the
other methods  to  displays the results.  Your  main()  method takes care of all input collection and printing of the
results  – make use of the method arguments and return values.   You do not need to do any error checking on the
input values.

java SphereAndCone
Enter a value for radius:1
Enter a value for  height:1
Sphere Volume: 4.1887902047863905
Sphere Surface Area: 12.566370614359172
Cone Volume: 1.0471975511965976
Cone Surface Area: 7.584475591748159

java SphereAndCone
Enter a value for radius:5.5
Enter a value for height:12.2
Sphere Volume: 696.9099703213358
Sphere Surface Area: 380.132711084365
Cone Volume: 386.46825626910436
Cone Surface Area: 326.26533476656743
Question 2.   (Methods .java 20 marks) Write a class that has the following methods:

isPrime: This method takes a single positive integer and checks to see if it is a prime number.  The method
returns true or false (boolean, not String).

vowels :   This method takes a single String argument and returns the number of vowels  (a, e, i, o or u) contain ed
in  the String.

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.

halfLife:   Radioactive decay of materials can be modeled by the equation shown below.  Write a method that
takes in all the necessary arguments to compute and then return the amount of material  (A
t
) after it has decayed
for a period of time  (t) given a half -life value (h).

?
?
= ?
?
?
! ? (
????
?
)

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 don’t 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.

READ ALSO :   Market Penetration and Branding

C:\ Users\ aaron\ Desktop\ a4- sol>java Methods
Testing method isPrime…

Enter an integer to test if it is prime: 10
10 is not prime.

Testing method vowels…

Enter a string to count the vowels in: I love apples
and banana
I love apples and bananas!  Really! has 11 vowels!

Testing method power…

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

Testing method halfLife…

Enter the initial amount: 100
Enter the half- life: 50
Enter the time to  run for: 50
The amount left after 50.0 time units is 50.0

C:\ Users\ aaron\ Desktop\ a4- sol>java Methods
Testing method isPrime…

Enter an integer to test if it is prime: 17
17 is prime!

Testing method vowels…

Enter a string to count the vowels in: abc  123 xyz
abc 123 xyz has 1 vowels!

Testing method power…

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

Testing method halfLife…

Enter the initial amount: 1000
Enter the half- life: 211000
Enter the time to run for: 20000
The amount left after 20000.0 time units is
936.41066454235
Question 3.    (20 marks) (CursedGold .java) Write a game called Cursed Gold.  The object of the game is to make
your opponent select the last gold coin from the pile.   There are initially 16 gold coins in the pile and you and your
opponent take turns selecting 1,2 or 3 pieces of gold.  The person who has to take the last piece from the pile loses.

The idea here is to create three functions that represent different strategies.  Two of your  methods  (named
playerSimple()  and  playerSmart() ) are completely automated  –   think artificial intelligence.  They take a
single argument  –   the number of coins currently in the pile.  Your functions use this information to decide how to
strategize.  One of them can be very simple and do the same thing every time but the other function should be a bit
more … intelligent.  Random selections are also valid but will only work to a point.  These functions print (yes,
printing in a function is allowed here)  how many coins were removed and they return how many coins are left –   this
is important as this number is needed by the next “player”.

READ ALSO :   The Carbon, Phosphorus, and Nitrogen Cycles

The third function ( playerHuman() ) is a human player.  This means that it still takes a single argument  –   the
number o f coins remaining but it then prompts the user for input (1,2 and 3 are the only valid inputs) and returns the
number of gold pieces remaining.

All three of the functions can work in different ways but ultimately, all three will have to deal with the pile  of coins
in a special way when it is down to three or less  –   keep this in mind.

You’ll need a main function that initializes the pile to 16 gold pieces.  It will also be responsible for looping over the
player turns, for printing out the number of pieces  remaining after each turn and for determining who won.  It also
determines whether or not the second player is human and calls the appropriate functions.

A few hints from my solution (take them or leave them):
– I have a function called checkWinner() th at determines if a winner exists   (only 1 coin left!!)   and stops the loop.
– In the output below, my player 1 is my smarter computer player while player 2 is my simpler computer player
–   only slightly but there is a difference.
– My solution has a lot of error checking  –   making sure I don’t take too many coins for example –   even in the
computer players.

Output from the two computer players playing (no user input):

Welcome to Cursed Gold!.  Here be the rules:
– Ye can only take 1, 2 or 3 gold coins on yer tu rn.
– The poor soul that takes the last coin walks the plank (loses).
———————————————

Be playarr 1 human?: (y/n) n
Be playarr 2 human?: (y/n) n

Arrr.  I be stacking up a pile of coins 16 high.

Player 1:Taking 1 gold coin(s)
Thar be 15 gold left matey.
Player 2:Taking 1 gold coin(s)
Thar be 14 gold left matey.
——————————-
Player 1:Taking 3 gold coin(s)
Thar be 11 gold left matey.
Player 2:Taking 1 gold coin(s)
Thar be 10 gold left matey.
——————————-
Player 1:Taking 3 gold coin(s)
Thar be 7 gold left matey.
Player 2:Taking 1 gold coin(s)
Thar be 6 gold left matey.
——————————-
Player 1:Taking 1 gold coin(s)
Thar be 5 gold left matey.
Player 2:Taking 1  gold coin(s)
Thar be 4 gold left matey.
——————————-
Player 1:Taking 3 gold coin(s)
Thar be 1 gold left matey.
Winner is player 1
Question 4.  (20 marks,  Recursion.java) Write a java program that has three static recursive methods
and a  main method. In your main method (5 marks) prompt for user input and display the results of each
of your recursive methods as shown in the sample output below.

READ ALSO :   Essay, History and Political Science

(a)  (5 marks) Write a method that uses recursion to compute the value of ?
!
, where a and n  are both
arguments to the method.  If  n   = 0, the method should return 1 as ?
!
= 1 .   If n = 1, the method should
return  a   as ?
!
= ? .  If  n   is any other number … that’s for you to determine but remember,  ?
!
= ?   ×
?
! ! !
.

(b)  (5 marks) Write a method that   uses recursion to return the reverse of a String that is passed to the
method as an argument.  Hint:  For base cases, consider a string that has 1 or fewer characters…how
much work is there to reverse them?  Otherwise a reversed string is the last letter of the original string
plus the reverse of the rest.  Try it out on paper first.

(c) (5 marks) Write a recursive method that determines the number of digits in an integer, n.  Hint:  If n <
10, there is one digit.  Otherwise, it has one more digit than n   / 10.

For both of these questions and with recursion in general, the trick is to determine 1) the base
case(s) that will cause the function to simply return and end the recursion and 2) the recursive
case(s) that will cause the function to call itself with a smaller version of the same problem. All
input and output should take place in your main method.  *** Don’t forget to “eat the new line”
if you use your scanner between numbers and strings.

PLACE THIS ORDER OR A SIMILAR ORDER WITH US TODAY AND GET AN AMAZING DISCOUNT 🙂