Docsity

AP Comp Sci Assignments: Video Game Spending & String Manipulation, Assignments of Computer Science

Toggle sidebar
bg1
AP Computer Science String Program
Due: Wednesday, October 7th, 2009
Program #1: Video Game Mania
Billy loves video games! In fact, he spends all of his money on them. He has a weekly
allowance and each game costs a certain amount of money. (Both of these are values
entered by the user.) You will write a program to simulate Billy's spending habits for a
particular number of weeks, also a value entered by the user. At the end of each week,
determine whether or not Billy buys a video game. Also, any money not spent on a video
game carries over to the following week. You are also guaranteed that his allowance is a
positive integer number of dollars but is less than the price of one game, which is also an
integer. At the end of your program, you write out how much money Billy has left and
how many video games he has bought. Write a program that executes this program. A
sample run of the program is included below (Program output is in bold, the user input is
in italics):
Please enter Billy's weekly allowance in dollars.
10
Please enter the cost of a video game in dollars.
33
Please enter the number of weeks to follow Billy's spending habits.
8
After week 1, Billy has 10 dollars and does not buy a video game.
After week 2, Billy has 20 dollars and does not buy a video game.
After week 3, Billy has 30 dollars and does not buy a video game.
After week 4, Billy has 40 dollars and then buys a video game!
After week 5, Billy has 17 dollars and does not buy a video game.
After week 6, Billy has 27 dollars and does not buy a video game.
After week 7, Billy has 37 dollars and then buys a video game!
After week 8, Billy has 14 dollars and does not buy a video game.
At the end of 8 weeks, Billy has 14 dollars and 2 video games.
Call your program Game.java. In your comments at the end of the program, explain any
problems you had in creating the program and how you caught and fixed the mistakes.
Only do this for the most important issues you dealt with.
pf3
pf4

Partial preview of the text

Download AP Comp Sci Assignments: Video Game Spending & String Manipulation and more Assignments Computer Science in PDF only on Docsity!

AP Computer Science String Program

Due: Wednesday, October 7

th

Program #1: Video Game Mania Billy loves video games! In fact, he spends all of his money on them. He has a weekly allowance and each game costs a certain amount of money. (Both of these are values entered by the user.) You will write a program to simulate Billy's spending habits for a particular number of weeks, also a value entered by the user. At the end of each week, determine whether or not Billy buys a video game. Also, any money not spent on a video game carries over to the following week. You are also guaranteed that his allowance is a positive integer number of dollars but is less than the price of one game, which is also an integer. At the end of your program, you write out how much money Billy has left and how many video games he has bought. Write a program that executes this program. A sample run of the program is included below (Program output is in bold, the user input is in italics): Please enter Billy's weekly allowance in dollars. 10 Please enter the cost of a video game in dollars. 33 Please enter the number of weeks to follow Billy's spending habits. 8 After week 1, Billy has 10 dollars and does not buy a video game. After week 2, Billy has 20 dollars and does not buy a video game. After week 3, Billy has 30 dollars and does not buy a video game. After week 4, Billy has 40 dollars and then buys a video game! After week 5, Billy has 17 dollars and does not buy a video game. After week 6, Billy has 27 dollars and does not buy a video game. After week 7, Billy has 37 dollars and then buys a video game! After week 8, Billy has 14 dollars and does not buy a video game. At the end of 8 weeks, Billy has 14 dollars and 2 video games. Call your program Game.java. In your comments at the end of the program, explain any problems you had in creating the program and how you caught and fixed the mistakes. Only do this for the most important issues you dealt with.

Program 2: Using Java Classes Read chapter 3, paying careful attention to section 3.5. Also, read over the specifications of the following methods in the back of your book (ppg 878 โ€“ 882): char charAt(int i) int compareToIgnoreCase(String s) boolean contains(String s) int length() String replace(char c, char d) String toLowerCase() Write a program that first prompts the user to enter 2 Strings. Store these in two different String objects. Then present the user with the following menu:

  1. Print out which of the two strings comes first, alphabetically. (Or, if they are equal, print this out.) In making the comparison, ignore the case of the letters.
  2. Print out whether or not the second String entered is a substring/ is contained within the first String entered.
  3. Print out the lowercase version of the second String entered.
  4. Print out the number of times the first letter in the String appears in the String.
  5. Print out the result when you replace all occurrences of 'e' in the second String with 'o'.
  6. Quit Incrementally develop this program and test it. In particular, first write the portion that reads in the two Strings from the user and then prints out the menu and reads in the user's choice. Then run this and see if it works. Then set up the looping structure, so that after the user makes a menu choice, then get to make another one, until they choose quit. Test this, make sure it works. Then put in the structure to allow "work" for all of the different cases. Then, write the code for menu item #1. Test just this item. Repeat for each of the menu items. Just like the first program, document the major problems you initially had, and how you discovered and fixed them. Write all of this in a comment at the bottom of the program. Once again, only include the most important issues you dealt with and how you fixed them. Call your program StringTest.java. A sample output from this program is included on the next page.
  1. Quit 4 H appears in the first string 1 times. Here are your choices:
  2. Print out which String comes first.
  3. Print out whether the second string is a substring of the first.
  4. Print out the lowercase version of the second string.
  5. Print out how many times the first letter in the first word appears in the String.
  6. Print out the result when you replace all e's in the second String with o's.
  7. Quit 5 Replacing all e's with o's in the second String yields: foBruArY. Here are your choices:
  8. Print out which String comes first.
  9. Print out whether the second string is a substring of the first.
  10. Print out the lowercase version of the second string.
  11. Print out how many times the first letter in the first word appears in the String.
  12. Print out the result when you replace all e's in the second String with o's.
  13. Quit 6 Goodbye! Deliverables Please email([email protected]) me two files, VideoGame.java and StringGame.java that solve the two problems given above, respectfully.