AP Computer Science Assignments

Learn Java Programming
Supplemental, free, online textbook called Introduction to Programming Using Java
Java Cheat Sheet
Classes, Objects, & Constructors
AP Quick Reference Guide
Online Java Compiler
Java Examples
Arithmetic Operators
Digital Textbook
Visualize Java code execution

Nov 20

Writing Standard Array Algorithms

As you have seen in previous activities, iteration and array traversing are powerful constructs that can process a lot of data at once. Adding selections to the iteration makes for even more powerful constructs. You have already used selections, iteration, and array traversals in a program, specifically summing the goals of a game as shown.

Player is a Class that will be used by StandardArrayAlgorithms.





RandomPermutation is a Class that will be used by RandomPermRunner.





Save your files in a folder called 3.6.4 You will turn in your programs and a screenshot of the output to our Google Classroom.



Nov 18

A New Type of Loop

Now that you have a basic understanding of how array traversal works, it’s time to examine the third kind of loop in Java, the enhanced for loop. An enhanced for loop is an easy way to iterate over each and every element in an array.



Save your files in a folder called 3.6.3 You will turn in your programs and a screenshot of the output to our Google Classroom.



Nov 13

Next is Unit 2: Lesson 8 - Math Functions. In this unit we have learned to use classes and objects, including calling methods. Today we are going to see a new class, the Math class, which has a load of useful methods to help us do calculations with numbers in Java. In this lesson, you will be introduced to Java’s math package and learn about static methods.





Save your files in a folder called U2_L8 You will turn in your programs and a screenshot of the output to our Google Classroom.



Nov 7

We continue with Unit 2: Lesson 7 - Wrapper Classes. We have now encountered several different class data types. In this lesson we will look at wrapper classes, which allow us to use primitive data types as objects. In this lesson, you will learn about wrapper classes, and about the processes of autoboxing and unboxing.





Save your files in a folder called U2_L7 You will turn in your programs and a screenshot of the output to our Google Classroom.



Nov 5

Create a folder called 3.6.2 in your java folder. Save you work to this folder.

Iteration and Arrays

Iteration statements such as for and while loops can be used to access all the elements in an array. This is called traversing the array. Recall the goals array from the previous activity that tracks the number of goals scored in a season.

Compare the following traversal algorithms. These algorithms traverse the array with an indexed for loop and a while loop. Both require the array elements to be accessed using their indices/indexes. Type, compile, and run each program and compare the output.

Save your files in a folder called 3.6.2 You will turn in your programs and a screenshot of the output to our Google Classroom.



Oct 31

Create a folder called 3.6.1 in your java folder. Download GoalKeeper.java and compile (javac) and run it. What does it return?

You should see an unfamiliar series of characters such as[I@15db9742. This is an address, the reference to where the actual data resides in memory. To access the individual values in each element, use the array name, square brackets [ ], and an index value. For example, the first element of array goals would be accessed as goals[0], the second element as goals[1].

Now try replacing the print line with this line of code and compile (javac) and run it. What does it return?

System.out.println("goals in highest scoring game:" + goals[5]);

Now modify the score for Game 3 to indicate that one goal was scored (do not change the initializer list). and compile (javac) and run it. What does it return?

goals[2] = 1;
System.out.println("Game 3 goals:" + goals[2]);

Dogs Revisited

Arrays can hold any type of object, not just String objects. You decide to create a program to keep a list of the dogs in your neighborhood. Type these files and save them in your 3.6.1 folder. Compile (javac), run, and screenshot them.

Upload the Dog.java & the DogArray.java files along with the screenshots.

Oct 29

Next is Unit 2: Lesson 4 - Classes and Objects.In the first few lessons of this unit, we learned what a class data type was. Now we will explore in greater depth what we mean by a "class" and an "object". This lesson explains the class-object structure found in Java (and other object-oriented programming languages).

Type this into the compiler



Oct 24

We end the week with Unit 2: Lesson 3 - String Methods. Today we are going to learn about some of the functions in that class. In this lesson, you will learn about some methods in the String class. There will be review questions and coding exercises.



Save your files in a folder called U2_L3. You will turn in your programs and a screenshot of the output to our Google Classroom.



Oct 21

Problem 2

You have been hired by a pet food company to generate a targeted advertisement for their cat and dog food. Their advertisement is:
We bet your furry friend would love to smell our pet food!

You will use the DataCollector methods shown below to load a file containing social media messages, read through each of those messages, and create a file containing all targeted users. Details are in the Javadoc-generated API documentation for SellMyPetFood, so familiarize yourself with the following methods:
setData
getNextPost
getNextTargetWord
prepareAdvertisement
printAllPosts
printAllTargetWords

Copy each of these files into the text editor and save them in a folder called Assignmnet2.

socialMediaPosts
targetWords

Next, type the 2 java files and save them in the same folder.

TargetedAd.java

DataCollector.java

Save your files in a folder called Assignment2. You will turn in your programs and a screenshot of the output to our Google Classroom.



Oct 14

Next up is Unit 2: Lesson 2 - Escape Sequences and String Concatenation. This lesson explores how to have more control over text output with escape sequences, and outlines some things to be aware of when concatenating Strings with numbers.






Save your files in a folder called Escape Sequences and String Concatenation. You will turn in your programs and a screenshot of the output to our Google Classroom.



Oct 8

Next is Unit 2: Lesson 1 - Strings and Class Types. Today you will learn how Strings function in memory. You will also be introduced to class data types, which can hold many pieces of data at one time.





Save your files in a folder called Strings & Class Types. You will turn in your programs and a screenshot of the output to our Google Classroom.



Oct 2

Nested Iteration

There may come a time in coding when you want to repeat an iteration multiple times. In this situation, you will use nested iteration.
Nested iteration is also called nested looping. When a loop is nested inside another loop, the inner loop must complete all its iterations before the outer loop can continue. The slideshow below describes the syntax of nested loops.



Write the code below and predict what it will do.



Now modify the program to use a while loop for the outer loop and a for loop for the inner loop.



Nested looping with Strings Nested iteration can be used to find patterns in String variables. One example of this is determining if a word or phrase is a palindrome. A palindrome is a word or phrase that is spelled the same when written backward. For example, the word radar is a palindrome, but the word father is not. The phrase “was it a rat I saw” is also a palindrome. The following program will test if words are palindromes.



Write a new version of the Palindrome program below to use a while loop for the outer loop and a for loop for the inner loop.



Now here is a more complicated palindrome program.



Here's some text to try -- A man, a plan, a canoe, pasta, heros, rajahs, a coloratura, maps, snipe, percale, macaroni, a gag, a banana bag, a tan, a tag, a banana bag again (or a camel), a crepe, pins, Spam, a rut, a Rolo, cash, a jar, sore hats, a peon, a canal--Panama

Another example of nested iteration with string variables is frequency analysis. In frequency analysis, you analyze a message by how often the most common letters (like E and T) occur in the message. This is a useful tool in cybersecurity, particularly in the field of cryptography.
Your task is to write a program, LetterFrequencies, that prints each letter of the alphabet and the number of times that letter occurs in the phrase. To complete this task, you will need to incorporate the following constructs.
>br> Loop to iterate through the alphabet (the outer loop)
Loop to iterate through all the letters of a phrase (the inner loop)
Counter variable to count the number of times a letter is in the phrase
Print statement to display the frequencies



Save your files in a folder called 2.4.4. You will turn in your programs and a screenshot of the output to our Google Classroom.



Sep 30

Standard String Algorithms

When software engineers and programmers apply for a job, they are often given a programming task to complete right then. This helps the hiring staff weed out candidates who don't really know how to code. Write a program that prints the numbers from 1 to 100.
For multiples of three print “Fizz” instead of the number.
For the multiples of five print “Buzz.”
For numbers that are multiples of both three and five print “FizzBuzz.”



Create a program called RemoveEveryOther.java using the starter code below. Write an algorithm to create a String with every other character from a given String.



Create a program called RemoveVowels.java using the starter code below. Get an input String from the user and create an algorithm that removes all vowels.



Create a program called Dyslexia.java using the starter code below. Get an input String from the user and create an algorithm that replaces each d, b, p, and q with a random d, b, p, or q. Use the Math class to help you achieve randomness.



Create a program called Anagram.java using the starter code below. Get two input Strings from the user and create an algorithm that determines if they are anagrams. You may assume the user does not use any punctuation or capital letters. You need to account for spaces between words.



Get an input String from the user and create an algorithm that replaces all instances of two consecutive spaces with a single space.



Get an input String from the user and create an algorithm that prints all duplicate characters. For example, the String aabbc would produce aabb.



Create a program that determines whether a String has more than five a’s in it.



Save your files in a folder called 2.4.3. You will turn in your programs and a screenshot of the output to our Google Classroom.



Sep 26

Today we do Assignment 1: Star Wars Characters. You will naming and classifying Star Wars Characters. Enter the names, species, and affiliation as shown to get the desired result.





Enter the number of Star Wars characters you want to input:
3

Enter details for character 1:
Name: Luke Skywalker
Species: Human
Affiliation: Rebel Alliance

Enter details for character 2:
Name: Darth Vader
Species: Human
Affiliation: Sith

Enter details for character 3:
Name: Yoda
Species: Yoda Species
Affiliation: Jedi Order



Character Details:
-----------------------------
Name: Luke Skywalker
Species: Human
Affiliation: Rebel Alliance
-----------------------------
Name: Darth Vader
Species: Human
Affiliation: Sith
-----------------------------
Name: Yoda
Species: Yoda Species
Affiliation: Jedi Order



-----------------------------

Save your files in a folder called Assignment 1. You will turn in your programs and a screenshot of the output to our Google Classroom.



Sep 25

Next is Unit 1: Lesson 6 - Numeric Casts. In the last two lessons, we learned how to do calculations with numbers, including division with integers. In this lesson, we are going to learn how to switch between double and integer data types when we need to. There will be review questions and coding exercises.





Save your files in a folder called NumericCasts. You will turn in your programs and a screenshot of the output to our Google Classroom.



Sep 20

Next up is Unit 1: Lesson 5 - Modular Division. By the end of this lesson, you will learn modular division, also known as remainder division, a type of division that is fundamental to various Java applications. There will be review questions and coding exercises.





Save your files in a folder called ModularDivision. You will turn in your programs and a screenshot of the output to our Google Classroom.



Sep 18

We're going to write 3 programs, and then write them again with changes. For Birthday1 you will need to enter your birthday as digits in this format-- 00/00/0000. For StringChecker1 you will need to write a complete sentence when prompted to enter a string. For ArrayCopier1 you will need to enter a starting number, an increment to jump, and the number of elements in the array.





Save your files in a folder called Changes. You will turn in your programs and a screenshot of the output to our Google Classroom.



Sep 17

ThreeSum

You are going to write a java program that when given an array of integers and a value, determine if there are any three integers in the array whose sum equals the given value. Write the ThreeSum code, javac it, and run it. Make a screen shot of the output and put it on a Google Doc. You will turn in ThreeSum java file and the screenshot to our Google Classroom





Sep 16

for Loops

A for loop provides a more concrete framework for common loop use cases. One kind of loop is no better or worse than another, but the type of loop you choose can affect the readability of your program.
In the previous activity, you used variables to track how many times a loop had executed. In a for loop, the variable that does this is called a “control variable” and is explicitly included in the syntax of the loop. In the loop shown below, the loop control variable is i.

The two programs below create identical output. The one on the left uses a while loop, and the one on the right uses a for loop.



Experiment with the code below. Modify the for loop header by changing the initialization, condition, and/or modifier so that the loop prints the values from 0 to positive infinity.



Here is a program that prompts for user input using the Boolean expression of the for loop header. Note how the loop control variable i is not used in there or in the loop body. As you review the program, think about how the programmer could have chosen a better algorithm.





The decrement operator is --. The statement i--; results in i getting the value i-1. Modify the code that follows so that it prints the integers from 10 down to 1 inclusive, in strictly decreasing order.





Save your files in a folder called 2.4.2. You will turn in your programs and a screenshot of the output to our Google Classroom.



Sep9

Spend your time this week catching up. Ask me if you want to know where you stand.



Sep 5

We're going to work with 3 files that will track a robot on Mars

1 of the files is a class files and will not run, but the MarsApplication file and the MarsRobot2 file will run after you javac all the files.





Now write the MarsApplication with some changes. Name it MarApplication1.java and put it in the same folder. javac it and run it.





Now write the MarsRobot2 with some changes. Name it MarsRobot3.java and put it in the same folder. javac it and run it.



Sep 3

Save your files in a folder called MarsRobot. You will turn in your programs and a screenshot of the output to our Google Classroom.



Iteration (Looping)

In a previous lesson, you learned how control can be disrupted through the use of conditional statements. The second way in which the flow of control can be disrupted is through iteration. Iteration is another building block of algorithms and allows for the repetition of a statement or block of statements for a set number of times or until the desired state is reached.

Building off of the knowledge you gained as you explored conditionals and Boolean logic, this unit covers two ways of accomplishing iteration using while and for loops. In addition, standard algorithms that use iteration are introduced. As with conditional statements, iterative statements can be nested to form more complex blocks of code to solve more difficult problems. In this unit, you will begin to analyze the run-times of various algorithms to compare them.





Save your files in a folder called 2.4.1. You will turn in your programs and a screenshot of the output to our Google Classroom.



Aug 29

In this lesson, you will learn about arithmetic calculations in Java. There are 4 coding exercises.



Save your files in a folder called U1_L4. You will turn in your programs and a screenshot of the output to our Google Classroom.



Aug 26

Escape Room

You are going to make an escape room game. Download the Escape Room zip file to your Desktop, open it and it will become your EscapeRoom Folder. Open the 2 text files and save them as java files. Write the Escape Room code, javac it, and run it. Play the game, Make some screen shots and put them on a Google Doc. You will turn in EscapeRoom java file and a screenshot of the output to our Google Classroom





Aug 22

Let Them Eat Cake. Part 2

We'll continue the cake theme, but these must go into a folder called Cake2. Some of them have the same name as other files you have written, but they do different things, so they must be in a seperate folder.





The Cake file will not run, but javac it, anyway. You will turn in your programs and a screenshot of the output to our Google Classroom.



Aug 21

Let Them Eat Cake

Throughout this chapter, you may have noticed some discussions and comments related to objects. We'll watch a video to learn about what they are and how object-oriented programming works.

Origami Cake

To further explore the difference between a class (a design) and an object (the instance), you will create a cake by following a recipe, but not a flour and egg cake, an origami cake. As you create your cake, think about how you are instantiating a cake from the instructions. Let's try an Origami Cake!







Now type these 4 programs, compile and run them, and screenshot the results. Save them in a folder called Cake.You will only be able to run the CakeRunner files, as the othes are just classes that the CakeRunner files use, but javac all but the origamiCake file.



You will turn in your programs and a screenshot of the output to our Google Classroom.



Aug 19

This lesson introduces new data types, spends more time with variables, and teaches how to store numbers instead of Strings.



Project STEM Videos



Now type these 3 programs, compile and run them, and screenshot the results.

Save your files in a folder called DataTypes. You will turn in your programs and a screenshot of the output to our Google Classroom.



Aug 15

Sign in to Project STEM sign up with your school email account.

Project STEM Videos

Variables

When creating programs, you may find it necessary to store certain values so you can process or use those values later. For this, you create variables. A variable is a container that holds information while your program runs. All variables use memory locations to hold their values.

In Java, three things must be defined to create and store a value:

The type of the variable

The identifier or name of the variable

The value of the variable

Data Types

There are different types of data in Java. Each data type has a set of values, known as a domain, and a set of operations that can be performed on variables of the type. Each data type can be categorized as primitive or reference. Primitive types are basic data types, such as int, double and boolean, and are built into the language. Reference data types are custom, more complex data types that users can create. You will learn more about reference types later in this course.



Now type these 4 programs, compile and run them, and screenshot the results.



Save them in a folder called Aug15 in your java folder.



You will turn in your programs and a screenshot of the output to our Google Classroom.



Aug 13

Join our Google Classroom. Sign in with your school email account and the code that I give you.

What is Java?

Java is a popular programming language, created in 1995.

It is owned by Oracle, and more than 3 billion devices run Java.

It is used for:


Why Use Java?


Getting started with java. You will first need to create a folder inside your documents folder called java and then another folder called Aug13 inside the java folder. Next, open Notepad++ and a new document and type in the following code:



The name of the java file must match the class name. When saving the file, save it using the class name and add .java to the end of the filename.

Save your file as HelloWorld.java in your Aug13 folder

Next, open the Powershell(X86)

type in cd Documents/java/Aug13

Now type ls

You should see your HelloWorld.java file

Every line of code that runs in Java must be inside a class. In our example, we named the class HelloWorld. A class should always start with an uppercase first letter.

Note: Java is case-sensitive: MyClass and myclass have different meanings.

The main Method

The main method is required and you will see this in every Java program:

public static void main (String[] args)

Any code inside the main method will be executed.

Java files need to be compiled before they will run, so type in javac HelloWorld.java

If there are no errors, you should see this



Next, type java HelloWorld

You should see this



You've just created your first java program!

System.out.println()

The println() method, short for "print line", is used to print a value to the screen and then adds a line break. print() also prints whatever is inside the () to the screen but does not add a line break.

You should note that the contents of the () are also in " " . This means that what is inside the quotes will print exactly to the screen.

This is called String literal, and we will see that we can put other things inside the () that will do math, call functions and more.

Each code statement must end with a semicolon ;

The /* and // signify comments. This is code that does not run, but it lets others who read your code know what you had in mind when you wrote it.

The curly braces {} mark the beginning and the end of a block of code and they always come in pairs.


Now do the same with these 3 programs



You will turn in your programs and a screenshot of the output to our Google Classroom.



Click here for the course syllabus.

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.