Java 211- Assignment #5

Mendelsohn

 

 

This week’s assignment is more about practicing techniques and solving “mini-puzzles” than doing a larger program. It will require you to go to your textbook or online to learn a couple of things—but no major topics. 

 

When sample output is provided, your output should closely match mine. 

You will need a separate file for each class. 

To submit your assignment, zip all your classes into one file and submit that single file to COL. 

 

  1. Create a class that has some code in it that outputs all of the numbers from 1 to 20 to the screen on the same line with a space and comma between in increments of 2.  There should be no comma after the last number.  You will need to use a loop to do this.  Bonus: For 5 bonus points, look up ‘for loops’ and and use that (instead of a while loop). 

1, 3, 5, 7, 9, 11, 13, 15, 17, 19

 

  1. Create a class in which you do the following.  Have a variable called ‘population’.  Set that variable to some random value between 1 and 1,000,000 (one million).  Have your program look at the value of ‘population’ and if the population is less than or equal to 100, your program should output “This is a village”.  If the population is between 101 and 10,000 it should output “This is a town”.  If the population is between 10,001 and 1,000,000 it should output “This is a city”.  I admit this isn’t the most interesting program in the world…

 

  1. Create a class in whch you do the following: Have a variable called ‘diceTotal’.  Randomly assign diceTotal to a number between 2 and 12.  If the number of dice total is ‘2’, the program should output “Snake Eyes!”.  If the number is ‘7’, the program should output Lucky sevens!”.  Otherwise the program should simply output the value of diceTotal.  Use a ‘switch’ statement to accomplish this task which you will have to look up in your textbook.  That is, do not use an if-statement. 

 

  1. Create a class in which you have a variable called ‘number’.  Prompt the user to enter a number between 1 and 100.  If the user enters a different number, keep prompting them until they enter between 1 and 100.  Then prompt them for a second number between 3 and 5.  Again, write code to ensure that they enter the proper number.  Your program should test to see if the first number they entered is evenly divisible by the second.  If it is, output that information to the screen.  Otherwise, output that it isn’t.  Your output should look identical to mine (except for the value of ‘number’).  For this example you will need to look up the modulus operator that we have discussed briefly in class.

Enter a number between 1 and 100: 42

Enter another number between 3 and 5:  3

42 is evenly divisible by 3.

 

Enter a number between 1 and 100: 85

Enter another number between 3 and 5:  2

85 is not evenly divisible by 2.

 

  1.  Write a program where you output the numbers 10 through 50 to the screen in increments of 3.  You must use a loop to do this.  On each line, output the value of that number and also the sum of that number plus the following one.  Remember: Build one piece of functionality before worrying about the next…

10      23   //ie 10+13

13      29   //ie 13+16

16   35   //ie 16+19 etc…

19   41

etc  etc

49   101