# 05 Self Tests

# The League of Amazing Programmers

View Source View Source

# Which Data Type?

# Fill in the blank with the correct data type.


   x = 5; 

  
   age = 12; 

  
   name = "Thadius"; 

  
   location = "San Diego"; 

  
   totalParties = 5400; 

  
   value =  JOptionPane.showInputDialog("Enter some data");

  
   input = JOptionPane.showInputDialog("Enter your age"); 

   inputAsInteger = Integer.parseInt(input); 

  

SUBMIT YOUR ANSWERS

# The League of Amazing Programmers

View Source View Source

# Which Data Type? Part 2

# Fill in the blank with the correct data type.


   maxValue = 1000000; 

  
   someWords = "Here are some words"; 

  
   c3PO = new Robot(); 

  
   rand = new Random(); 

  
   minValue = -23; 

  
   value =  JOptionPane.showInputDialog("Enter some data");

  
   myObject = new Object(); 

  
   randValue = random.nextInt(); 

  

SUBMIT YOUR ANSWERS

# The League of Amazing Programmers

View Source View Source

# What's the Output?

# Each number is a complete Java program. If you were to run this program, what output would show up on the screen?

Type your answers in the box. Read each question carefully. Your answers must be exact.


1.
class MyJavaProgram{
	public static void main(String[] args){
		System.out.println("Tacos");
	}
}

2.
class MyJavaProgram{
	public static void main(String[] args){
		String book = "Cat in the Hat";
		System.out.println(book);
	}
}

3.
class MyJavaProgram{
	public static void main(String[] args){
		String book = "Frankenstein";
		System.out.println(book + " " + book);
	}
}

4.
class MyJavaProgram{
	public static void main(String[] args){
		String book = "Tale of Two Cities";
		System.out.println("book");
	}
}

5.
class MyJavaProgram{
	public static void main(String[] args){
		int i = 5;
		System.out.println(i);
	}
}

6.
class MyJavaProgram{
	public static void main(String[] args){
		int x = -3;
		int y = 6;
		System.out.println(x + y);
	}
}

7.
class MyJavaProgram{
	public static void main(String[] args){
		int s = 4;

		if(s == 4){
			System.out.println("FOUR");
		} 
		else {
			System.out.println("FIVE");
		}
	}
}

8.
class MyJavaProgram{
	public static void main(String[] args){
		int j = 1;

		if(j > 0){
			System.out.println("CLICK");
		} 
		else if ( j == 2){
			System.out.println("CLICK");
		}

		System.out.println("CLICK");

	}
}

9.
class MyJavaProgram{
	public static void main(String[] args){
		int j = 1;
		int k = 2;

		if(j != k){
			System.out.println("1");
		} 
		else if (k != j){
			System.out.println("2");
		}
		else{
			System.out.println("3");

		}

		System.out.println("4");

	}
}

10.
class MyJavaProgram{
	public static void main(String[] args){
		int i = 0;
		int j = 1;
		int k = 2;

		if(i == 0){
			System.out.println(i);
		}
		if(j == 1){
			System.out.println(j);
		}
		if(k == 2){
			System.out.println(k);
		}
	}
}

11.
class MyJavaProgram{
	public static void main(String[] args){
		String x = "X";
		String y = "Y";

		System.out.print(x);
		System.out.print(y);
	}
}

12.
class MyJavaProgram{
	public static void main(String[] args){
		String a = "A";
		String b = "B";
		String c = "C";

		System.out.print(c + a + b);
	}
}

13.
class MyJavaProgram{
	public static void main(String[] args){
		String a = "C";
		String b = "B";
		String c = "A";

		System.out.print(c + a + b);
	}
}

14.
class MyJavaProgram{
	public static void main(String[] args){
		for(int i = 0; i < 5; i++){
			System.out.print(i);
		}
	}
}

15.
class MyJavaProgram{
	public static void main(String[] args){
		for(int i = 5; i >= 0; i--){
			System.out.println(i);
		}
	}
}

16.
class MyJavaProgram{
	public static void main(String[] args){
		String h = "horse";
		for(int i = 5; i >= 0; i--){
			System.out.println(h);
		}
	}
}

17.
class MyJavaProgram{
	public static void main(String[] args){
		String d = "dynomite";
		for(int i = 5; i >= 0; i--){
			System.out.println(i);
		}
		System.out.println(d);
	}
}

18.
class MyJavaProgram{
	public static void main(String[] args){
		for(int i = 0; i < 20; i++){
			if(i % 2 == 0){
				System.out.print(i + ", ");
			}
		}
	}
}

19.
class MyJavaProgram{
	public static void main(String[] args){
		String count = "";
		for(int i = 5; i >= 0; i--){
			count += "count" + i + " ";
		}
		System.out.println(count);
	}
}

20.
class MyJavaProgram{
	public static void main(String[] args){
		int x = 7;

		for(int i = 0; i < 22; i++){
			if(i % x == 0){
				System.out.print(i + ", ");
			}
		}
	}
}

SUBMIT YOUR ANSWERS

# The League of Amazing Programmers

View Source View Source

# Moustaches

# Put the moustaches where they belong


  
public class Moustache2

      public static void main(String[] args) 

          	for (int j = 0; j < 10; j++) 

                  if (input.equals("are cool"))

                      for (int i = 0; i < 50; i++) 

                            System.out.println("{}");

                  else 

                        System.out.println("you don't like moustaches... no soup for you");

 

  
  

SUBMIT YOUR ANSWER

# For Loop Onslaught


# Level 0 Summary

View Source View Source

# Module 0

#

# Robot

# Strings

# JOptionPane

# Module 1

#

# main method

# color

# if statements

# int

# increment

# for loop

#

# Module 2

#

# Random

# call methods

# Integer.parseInt

# Module 3

#

# if / else if / else

# for loop variables

# modulo

#

#

# Module 4/5

#

# double,boolean,char

# nested loops

# writing methods

# Level 0 Study Guide

View Source View Source

#

Variables ‘Declare and initialize a variable’ means make a variable and give it a value. int age = 12; String name = “Sarah”; Remember, String is capitalized and the value always has quotes around it. Every time you want to hold a new piece of information you need to declare the variable and initialize it, by setting it equal to something. If you want to generate a random number you do it like this. int x = new Random().nextInt(10); The number in parenthesis limits the value of the random number you will get. In this case, x will be a number between 0 and 9.

#

if Statements if (condition){

} else if (next condition){

} else{

} The question looks like this for a String (name .equals (“Sarah”)) You use .equals to compare a String. The String value is in parenthesis and in quotes. The question looks like this for an int (age == 12) Remember you use == for a comparison. It is a good idea to use an else with an if statement to catch any options you didn’t think of.

#

for loop for ( int i = 0; i<10; i++){

} int i = 0; This is the starting point of the loop. You must declare the int. i<10; This is the finishing point for the loop. The loop will continue as long as i is less than 10. i++ This is how much the loop counter changes by each time. In this case it increments (goes up) by 1. for ( int i = 10; i>0; i-=2){

} This loop counts down in 2s starting at 10. 10, 8 , 6, 4, 2. It doesn’t go to 0 because it must be greater than 0. Notice that you have to change from less than (<) to greater than (>) when you count down.

#

JOptionPane If you want a pop up to give the user a message use JOptionPane.showMessageDialog(null, “Hello ”+ name); Don’t forget the null , or that any text needs to be in quotes. If you want to get an answer from the user, use: String ageString = JOptionPane.showInputDialog(“How old are you?”); Remember you must catch the user’s answer in a String, otherwise you can’t use it. Because the answer is always a String, you sometimes want to convert it to a number so you can do a calculation. This is how you do it. int age = Integer.parseInt(ageString); If you want to write something to the console, instead of using a pop-up System.out.println(age);

# Methods

You must have a main method in order to start your class running. public static void main(String[] args){

} Other methods looks like this void drawSquare(){ for (int i= 0; i<4; i++){ Tortoise.move(100); Tortoise.turn(90); } } void This tells you what the method will return - in this case nothing. drawSquare This is the name of the method, that you use to call it. ( ) This tells you what parameters the method needs. In this case it doesn’t need any. You call the method like this: drawSquare(); Look at this method: int sevenTimes(int number){ return number * 7; } int This method is going to a return an int so you need to create an int variable to store it. In the example below, the variable is named "answer". sevenTimes This is the name of the method, that you use when you call it. (int number) This tells you what parameters the method needs. In this case it needs you to give an int. In the example below, the int is the number 2. int answer = sevenTimes(2); The value of answer will be 14.

#

Modulo

Modulo means remainder. The value of 11%5 is 1, because 5 goes into 11, 2 times (5x2=10) with a remainder of 1.

It is useful to use modulo to alternate between options. e.g.

for (int i = 0; i<1000;i+=10){ if (i%3==0){ fill(255,0,0); } else if (i%3==1){ fill(0,255,0); } else if (i%3==2){ fill(0,0,255); } ellipse(i,i,10,10); }

This code will draw a line of circles alternating in red, green and blue.

``` ```