NOTE: Before working on the labs today, I need you to join AP classroom. You can join at myap.collegeboard.org and join using the code for your period:
Period 3: E76VNG
Period 4: JX6ZQW
You will need to log in via a collegeboard account, or create one if you have never had one before!
ALSO! Please join the google classroom for AP info at Hart. The code is: nylroej
-----------------------------------------------------------------------
This is due at 11:59 pm on THURSDAY, August 24th. Have your six files in your google drive named folder by that time. WARNING: some of these six assignments are difficult and involve some problem solving. I have provided some hints for the hardest ones – if you want a true college-level challenge, ignore the hints.
(To see the lesson for this topic in video form, you can watch https://www.youtube.com/watch?v=dNtAza8UH3Q and https://www.youtube.com/watch?v=idwPA-7gjgY )
0. Please delete all of your old labs from the turn-in folder with your name on it. This folder should just be for turning things in rather than collecting files.
1. Write a program called StringCaps.java (so public class StringCaps) that inputs a sentence from the user. You may assume they will type in at least five letters. It should put the first five characters in all capital letters (if they are spaces or numbers, they will remain unchanged, that is fine) and then put the rest in lower case and print that. For instance, if the user types in "Batman: The Dark", it would print out "BATMAn: the dark". Submit StringCaps.java
2. Write a program (public class StringEnds - save as StringEnds.java) that asks the user to type in a String. After they type in the String, your program should cut off the first two letters and last two letters. For instance, if the user types in "Enter Sandman", your program should print out "ter Sandm". You may assume the user will type in a phrase with at least 4 letters so you do not have to worry about out of bounds errors.
3. In Java, the symbol + can be used to add numbers or to concatenate strings. This exercise shows both uses. You have already seen how to combine strings with variables by concatenating them by doing things like:
System.out.println("hello sir, you are " + age + "years old");
So, when working with strings the + symbol means to concatenate the strings (join them). BUT, when working with numbers the + means what it has always meant -- add!
To see the behavior of + in different settings do the following:
a. Copy paste in the program below.
public class StringPlus
{
public static void main(String[] args)
{
System.out.println ("This is a long string that is the " +
"concatenation of two shorter strings.");
System.out.println ("The first computer was invented about" + 70 + "years ago.");
System.out.println ("8 plus 5 is " + 8 + 5);
System.out.println ("8 plus 5 is " + (8 + 5));
System.out.println (8 + 5 + " equals 8 plus 5.");
}
}
b. Compile and run the program. For each of the last three output statements (the ones dealing with 8 plus 5) write down what was printed in a comment above the line. Now for each, think about (and be able to explain) why the computer printed what it did given that the following rules are used for +. Write your explanations in a comment at the top of the program about what rules java is following..
c. Writing Your Own Program With + : Now, below the printlns that are already there, add in two variables (int robins = 10 and int canaries = 13) and then add an additional println that prints out the following sentence:
10 robins plus 13 canaries is 23 birds.
Your program must use only one statement that calls the println method. It must use the + operator both to do arithmetic and string concatenation (so there should be no numbers in quotes or elsewhere in the println - just variables and +). Submit StringPlus.java, which should include your comments and the new sentence.
4. Write a program called StringTypo.java that takes in a string from the user, selects and removes a RANDOM character from the string and prints out whatever is left of the user’s original string. The program should be able to remove any character. For instance, if the user inputs "I am the greatest," your program could print "Iam the greatest" (removing the space) OR perhaps "I am the geatest"” or even " am the greatest" [you should not need special cases if the endpoints are randomly generated, java will handle them nicely]. When you are done, submit StringTypo.java
NOTE: do not use .replace for this lab - you only want to remove one letter, not all instances of that letter!
Hint: There is a reason I am giving this after the lab named "plus."
5. Write a program (StringLetterSwitcher.java) that will input a sentence from the user, and then two letters (as Strings not chars) from the users, and then switch all instances of those letters with each other. It will then print out the switched sentence. For instance, they could input "I like to eat bananas" and then type in "e" and "a" and your program would print "I lika to aet benenes" (notice that it still has both a's and e's in the sentence, they are just switched). Assume that the user will enter a string without any numbers or strange characters like "#". When you are done, submit StringLetterSwitcher.java
Hint: this one is tough too. You will need to use more than 2 replace statements to make all the necessary changes. Just because the user is not going to use numbers, doesn’t mean that you can't in order to help you switch the letters...
6. Write a program called StringStart.java that will have the user input a string, and then a second string (that is contained in the first). Using the string functions from class, it will then print out the part of the string from the second string on. For instance, if the user types in:
"Batman: The Dark Knight Rises"
"an"
Your program should output the string starting from "an", so it should print out:
an: The Dark Knight Rises
You may assume that the user will enter something for the second string that IS contained in the first. Submit StringStart.java and pat yourself on the back!
Want a couple points of extra credit? Update your letter switcher so it is not case sensitive (so if you type in "a" and "e" for the two inputs, it switches "a" to "e" AND "A" to "E" and vice versa - it should switch both upper and lower case versions of both letters while maintaining that case). If you choose to do this, save it as StringLetterSwitcherEC.java