Classes: String, Math, Random
Please make sure to include your name, Andrew ID, and the honor statement in all your code, using the format given in class.
Due Week 4: Thu by 10pm.
Note: a printed test plan for the WordCount program is due this week Wed by the start of lecture.
Write the following programs:
Math.round(Math.log10(n) / Math.log10(2.))
I strongly recommend having a TA or your instructor review your method signature before writing the code. Office hours or a private Piazza message is a good way to have it reviewed.
When determining if your main() should print "You know the secret!" or "You should be able to do better!", use the result of log2(maxRange) instead of a hard-coded 10.
Anything else should be considered "no". To help with the prompting design, code, and use these methods:
I strongly recommend having a TA or your instructor review your method signatures before writing the code. Office hours or a private Piazza message is a good way to have them reviewed.
Things to think about:
I strongly recommend having a TA or your instructor review your method signature before writing the code. Office hours or a private Piazza message is a good way to have it reviewed.
public static void printArray(String label, Object array[])
The label will be "letters", "words", or "sentences" depending on which you are passing as the second argument (see the sample output below). For instance, this is how you will call it from your main():
printArray("letter", letters);
printArray("word", words);
printArray("sentence", sentences);
Example dialog:
Please enter a string, press Enter to end: Hi! Please enter a string, press Enter to end: This is a test. How many sentences are there? Please enter a string, press Enter to end: Thank you for entering 2 lines of data. Here are your results: ** Line 1: Hi! Counts (letters, words, sentences): 3 1 1 letter[0]: H letter[1]: i letter[2]: ! word[0]: Hi sentence[0]: Hi ** Line 2: This is a test. How many sentences are there? Counts (letters, words, sentences): 45 9 2 letter[0]: T letter[1]: h letter[2]: i letter[3]: s letter[4]: letter[5]: i letter[6]: s letter[7]: letter[8]: a letter[9]: letter[10]: t letter[11]: e letter[12]: s letter[13]: t letter[14]: . letter[15]: letter[16]: H letter[17]: o letter[18]: w letter[19]: letter[20]: m letter[21]: a letter[22]: n letter[23]: y letter[24]: letter[25]: s letter[26]: e letter[27]: n letter[28]: t letter[29]: e letter[30]: n letter[31]: c letter[32]: e letter[33]: s letter[34]: letter[35]: a letter[36]: r letter[37]: e letter[38]: letter[39]: t letter[40]: h letter[41]: e letter[42]: r letter[43]: e letter[44]: ? word[0]: This word[1]: is word[2]: a word[3]: test word[4]: How word[5]: many word[6]: sentences word[7]: are word[8]: there sentence[0]: This is a test sentence[1]: How many sentences are there
Sample output:
java CoinToss 100
Tossed a coin 100 times.
Heads: 52 Tails: 48
Heads: 52% Tails: 48%
java CoinToss 200
Tossed a coin 200 times.
Heads: 112 Tails: 88
Heads: 56% Tails: 44%
Design, code, and use a method called formatPercent() in CoinToss that takes a number and a total and returns the string to print as the percent. For instance, if called with 112 and 200, it returns the string 56%; if called with 48 and 100, it returns the string 48%.
I strongly recommend having a TA or your instructor review your method signature before writing the code. Office hours or a private Piazza message is a good way to have it reviewed.
Start the Coin class with this code snippet:
import java.util.*;
public class Coin
{
private static Random r = new Random();
private CoinSide sideUp;
public Coin()
{
sideUp = CoinSide.HEADS;
}
public boolean isHeads()
{
}
public boolean isTails()
{
}
public void flip()
{
// Generate a random number, either 0 or 1, and use it to set sideUp to HEADS or TAILS
}
public String toString()
{
return ("Coins side up is: " + sideUp);
}
}
Update the Coin class to add the code for the CoinSide enumeration. It should have HEADS and TAILS as the enumeration values and the methods that have not been finished.
Use the following algorithm (called the Seive of Eratosthenes).
When you are done, only those indices that are prime will stil be true.
For instance, for the subscript 2, all elements beyond 2 that are multiple of 2 (i.e. 4, 6, 8, 10, ...) will be set to false, indicating that they are not prime. For the subscript 3, the subscripts 6, 9, 18, 21, ... will be set to false. Do not use any additional classes - use a local variable in main() for your array.
To complete this project, start with this code
ConnectTheDots.java
and update it based on the instructions in the comments. Only write
the code that has this in the comment: YOU WRITE THE CODE!
Make sure your name, Andrew ID, and the Academic Integrity statement are in all of your submitted code. Follow the instructions for submitting your homework 2 code.