Exercise One - Fizz Buzz
Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz".
For numbers which are multiples of both three and five print "FizzBuzz".
Note: This question is somewhat famous within the professional programming world because it is concidered the baseline for what someone must be able to slove inorder to call oneself a "programmer".
Check out Jeff Atwood's blog to read more about why it's so important.
public class fizzbuzz {
public static void main (String[] args){
for (int i = 1; i <=100; i = i + 1) {
if ( i%3 == 0 && i%5 == 0 ) {
System.out.println(i+" FizzBuzz");
}
else if ( i%3 == 0 ) {
System.out.println(i+" Fizz");
}
else if (i%5 == 0) {
System.out.println(i+" Buzz");
}
else {
System.out.println(i);
}
}
}
}
Exercise Two - Flagged loops
Write a programm that takes the average of user input numbers. Keep prompting the user to input another number untill they enter -1. Then print the average of the numbers (besides -1).
import java.util.Scanner;
public class averager {
public static void main (String[] args){
Scanner input = new Scanner(System.in);
System.out.println("input numbers to be averaged. -1 to stop");
double sum = 0;
double nextNumber;
int numberOfInputs = 0;
nextNumber = input.nextDouble();
while ( nextNumber != -1 ){
sum = sum + nextNumber;
numberOfInputs++;
nextNumber = input.nextDouble();
}
System.out.println(sum/numberOfInputs);
}
}
or
import java.util.Scanner;
public class averager {
public static void main (String[] args){
Scanner input = new Scanner(System.in);
System.out.println("input numbers to be averaged. -1 to stop");
double sum = 0;
double nextNumber;
int numberOfInputs = 0;
while ( true ) {
nextNumber = input.nextDouble();
if (nextNumber == -1) {
break;
}
else {
sum = sum + nextNumber;
numberOfInputs++;
}
}
System.out.println(sum/numberOfInputs);
}
}
Exercise Three - boolean logic practice, string comparison
Figure out if you can skip class or not. Write a program that asks the user
Have you missed this class before? (yes or no)
and
Are you doing well in this class? (yes or no)
The programs output should follow this chart
Missed class before | Doing well | Output |
true | true | "I really shouldn't miss this class" |
true | false | "I really shouldn't miss this class" |
false | true | "I can afford to miss this class" |
false | false | "I really shouldn't miss this class" |
import java.util.Scanner;
public class ClassSkipper {
public static void main (String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Have you missed this class before? (yes or no)");
String missed = input.nextLine();
System.out.println("Are you doing well in this class? (yes or no)");
String doingWell = input.nextLine();
if ( missed.equals("no") && doingWell.equals("yes")){
System.out.println("I can afford to miss this class");
}
else {
System.out.println("I really shouldn\'t miss this class");
}
}
}
OR
import javax.swing.JOptionPane;
public class greatestInSet {
public static void main (String[] args){
/**
* show confirm dialog for missing class returns an int based on the outcome
* yes = 0, no = 1, cancel = 2, x = -1
*/
int missed = JOptionPane.showConfirmDialog( null, "Have you missed this class before?" );
int doingWell = JOptionPane.showConfirmDialog( null, "Are you doing well in this class?" );
if ( ( missed == 1 ) && ( doingWell == 0 ) ){
JOptionPane.showMessageDialog( null, "I can afford to miss this class" );
}
else {
JOptionPane.showMessageDialog( null, "I really shouldn\'t miss this class" );
}
}
}
Exercise Four - Input loops, Number Comparison
Find the greatest integer in a set of integers. First ask the user how many numbers in the set and then use a for loop to ask the user for each of the integers
example:1, 6, 2, 7, 12, 4, 3, 2, 8, 5, 7, 9, 2 will output The greatest number in the set is 12
import java.util.Scanner;
public class greatestInSet {
public static void main( String[] args ) {
// create an instance of scanner which listens to System.in
Scanner input = new Scanner(System.in);
int cardinality;
int greatest;
do {
// Ask the user how many numbers are in thier set
System.out.println("How many numbers in the set?");
cardinality = input.nextInt();
} while (cardinality <= 0);
// Find the first number in the set
System.out.println("Enter the first number");
int nextNumber = input.nextInt();
// if there is only 1 number in a set it must be the greatest
greatest = nextNumber;
// start our loop from 1
for( int i = 1; i < cardinality; i = i + 1 ){
//get the next number
System.out.println("Enter the next number");
nextNumber = input.nextInt();
//compare the next number to the prior greatest
if (greatest < nextNumber){
//if the new number is bigger than greatest, the new number will be the greatest
greatest = nextNumber;
}
}
System.out.println("The greatest number in the set is "+greatest );
}
}
Home Work
Recreate '99 bottles of beers on the wall' using a for loop. The output should look like this:
99 bottles of beer on the wall, 99 bottles of beer.
Take one down and pass it around, 98 bottles of beer on the wall.
98 bottles of beer on the wall, 98 bottles of beer.
Take one down and pass it around, 97 bottles of beer on the wall.
97 bottles of beer on the wall, 97 bottles of beer.
Take one down and pass it around, 96 bottles of beer on the wall.
96 bottles of beer on the wall, 96 bottles of beer.
Take one down and pass it around, 95 bottles of beer on the wall.
95 bottles of beer on the wall, 95 bottles of beer.
Take one down and pass it around, 94 bottles of beer on the wall.
....
3 bottles of beer on the wall, 3 bottles of beer.
Take one down and pass it around, 2 bottles of beer on the wall.
2 bottles of beer on the wall, 2 bottles of beer.
Take one down and pass it around, 1 bottle of beer on the wall.
1 bottle of beer on the wall, 1 bottle of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.
Things to study for next week
- Basic Output
- System.out.println();
- System.out.print();
- \n and \t
- Variables
- Declaring variables
- Assigning variables
- Types in Java (int, double, String, boolean)
- Strings
- String Concatenation
- String Comparison
- Operators
- Mathmatical operators (+ - * / %)
- Comparison operators (== != < > <= >=)
- Boolean Logic
- if statements
- loops
- for
- while
- You won't have to know do while... please don't use it on the test.
- Scanner
- importing Scanner
- making a new Scanner
- .nextLine()
- .nextInt()
- .nextDouble()