Classwork
-
CW 34 - Binary Search
DUE DATE:Binary Search
Save your work here: .../APCSA_1/apcsa-assignments-YourUsername/classwork/34_binary_search/BinarySearch.java
Implement the binary search algorithm recursively.
public static int binarySearch(int[ ] arr, int low, int high, int target){ // Solution must be recursive }
-
CW 33 - Hanoi
DUE DATE:Tower of Hanoi
Save your work here: .../APCSA_1/apcsa-assignments-YourUsername/classwork/33_hanoi/Hanoi.java
Mild
Write a function to move n rings from source rod to destination rod, print the moves of each ring.
public static void hanoiMild(int n, char source_rod, char destination_rod, char aux_rod){ YOUR CODE HERE }
Output with 3 rings:
Move ring 1 from source A to destination C Move ring 2 from source A to destination B Move ring 1 from source C to destination B Move ring 3 from source A to destination C Move ring 1 from source B to destination A Move ring 2 from source B to destination C Move ring 1 from source A to destination C
Medium
Write a function to move n rings from source rod to destination rod, print the moves of each ring, and the total number of moves. Check if your solution used the least possible number of moves.
public static int hanoiMedium(int n, char source_rod, char destination_rod, char aux_rod){ YOUR CODE HERE }
Output with 3 rings:
(Print moves as shown in previous method) Total number of moves: 7 The total number of moves (7) is equal to 2^n - 1 (2^3 - 1)
Spicy
Write a function to move n rings from source rod to destination rod, print the moves of each ring, and the list of rings on each rod after each move.
public static void hanoiSpicy(int n, char source_rod, char destination_rod, char aux_rod){ YOUR CODE HERE }
Example:
Current state of rods: Rod A: 3 2 1 Rod B: Rod C: ----------------------- Move disk 1 from Rod A to Rod C Current state of rods: Rod A: 3 2 Rod B: Rod C: 1 ….
-
CW 32 - Number Letter Counts
DUE DATE:Number Letter Counts
Project Euler - Problem 17
Save your work here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/32_recursion_number_letters/NumberLetter.java
This problem must be solved using recursion.
If the numbers 1 to 5 are to are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
Think about the problem:
First how can you implement this method to return the number in letters:
public static String numberToWords(int number) { }
- How can you have a reference to the numbers in words? Should these words be predefined maybe using arrays?
- How should the possible number cases be implemented?
- Handling Hundreds: Greater than or equal to 100
- Handling Tens: 20 to 99
- Handling Teens: 10 to 19
- Handling Ones:Less than 10
- How can you check the value of the number in the recursive method? How to decide whether to use the word for hundreds, tens, teens, or ones?
- What is the base case?
- What is the recursive case?
- What parameter must be sent in each recursive call?
Let's do it from 1-1000 to have the total number of letters in all those numbers:
Declare an integer variable 'result' to store total number of letters for each number from 1 to 1000 call recursive method numberToWords(number here), this return number in words get the length of the word returned from the recursive method and add it to 'result'
Debugging and Testing:
- Debug the program by running it with small inputs.
- Test the program with a range of numbers and verify the results.
-
CW 31 - More Recursion with Strings
DUE DATE:Recursion with Strings
Implement the following methods and save them here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/31_recursion_strings/Strings.java
Print Characters
Print each character of the string.
What String method/s do you need?
What is the base case?
What is the recursive case?
Hint: It can be solved using the head-and-tail algorithm (related to the strategy we learned yesterday), which consists of two parts:
- printing the head of the string, and,
- recursively printing its tail.
public static void printString(String word){ }
Print the string backwards
What String method/s do you need?
What is the base case?
What is the recursive case?
public static void printReverse(String word){ }
Counting Characters in a String
Suppose you are writing an encryption program and you need to count the frequencies of the letters of the alphabet. Let’s write a recursive method for this task.
This method will have two parameters:
- word: string that will be processed
- ch: char to store the target character—the one we want to count.
The method should return an int, representing the number of occurrences of the target character in the string:
What String method/s do you need?
What is the base case?
What is the recursive case?
public static int countChar(String word, char ch) { }
-
CW 30 - Recursion with Strings
DUE DATE:Let's solve the following recursive exercises CodingBat
-
CW 29 - Recursion
DUE DATE:Let's solve the following recursive exercises CodingBat
-
CW 28 - Recursion
DUE DATE:Save your work here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/28_recursion/
-
CW 27 - Console Game
DUE DATE:Time complexity
Save your answers here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/27_console_game
-
CW 26 - Time Complexity
DUE DATE:Time complexity
Save your answers here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/26_time_complexity/answers.md
-
CW 25 - Advent of Code
DUE DATE:Advent of Code 2016
Save here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/25_advent_of_code/
Due date: Wednesday, November 27 at 11:59 pm
Create an account and join leaderboard
- Go to https://adventofcode.com/2016 and create an account if you do not have one. Otherwise, log in to your account.
- Click on Leaderboard, then click on Private Leaderboard and join our class leaderboard using this code
3215640-7bad14a1
.
Classwork/Homework
-
Required: Work on part 1 problems of days 1, 2, 3, 6.
-
Optional: Choose a part 2 problem of days 1, 2, 3, 6. Or any other harder problem(s).
HAVE FUN!!!
-
CW 24 - Random
DUE DATE:Save here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/24_random/random.txt
-
CW 23 - Inheritance - Numbers
DUE DATE:Save here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/23_inheritance_numbers/
-
CW 22 - Inheritance Worksheet
DUE DATE:Save here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/22_library_books/
-
CW 21 - Inheritance Worksheet
DUE DATE:Save your answers here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/21_inheritance_worksheet/answers.txt
(or md file). -
CW 20 - Inheritance (Electric car)
DUE DATE:Directions here.
Save here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/20_electric_cars/
-
CW 19 - FRQ
DUE DATE:Work on question 1.
Save here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/19_frq_appointment_book/solution.txt
(or md file) -
CW 18 - Count Triangles
DUE DATE:Save here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/18_count_triangles/
-
CW 17 - ArrayList
DUE DATE:Directions have been posted here.
Save here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/17_ArrayList/
-
CW 16 - Exceptions
DUE DATE:Directions have been posted here.
Save here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/16_exceptions/
-
CW 15 - Wrapper Classes
DUE DATE:Directions have been posted here.
Save here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/15_wrapper_classes/
-
CW 14 - SuperArray
DUE DATE:Directions have been posted here.
Save here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/14_super_array/
-
CW 13 - Fraction Math
DUE DATE:Directions have been posted here.
Save here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/13_fraction_math/
-
CW 12 - Coordinate V2
DUE DATE:Go here and copy the Point.java and Driver.java in your classwork folder "12_more_coordinates".
Look at the Driver file, and follow the directions.
Save here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/12_more_coordinates/
-
CW 11 - Coordinate V1
DUE DATE:Part 1
Answer these questions:
- Can you call a static method from a non-static one?
- Or vice versa, can you call a non-static method from a static one?
- Try that in your class Employee, and write your answers as comments at the end of your Employee class.
Part 2
We want to implement (write) a class Point, which fields represent a 2D coordinate (x, y). Let's think about the class design.
- How many instance variable are needed? Private or Public?
- What constructors should be needed?
- What methods do you think should be needed?
- Would it be possible to make the Point class immutable? If so, how?
Write your answers to those questions in a txt, md or any text file.
Save here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/11_coordinate/answers.txt
Part 3
Go here and copy the Point.java and Driver.java in your folder classwork folder "11_coordinate".
Look at the Driver file:
There are a few questions to respond in that file. Please write your answers as comments in Driver.java
Optional: If you have time you may start writting the Point class. Otherwise, no worries we will work on this on Monday.
DO NOT FORGET TO COMMIT AND PUSH YOUR CHANGES AT THE END OF THE PERIOD.
I will look at this classwork. I will not accept late submissions for this one, exception a justified absence.
-
CW 10 - Design a class
DUE DATE:Imagine that someone comes to you and asks you to design a class that represents a Pizza.
-
What instance variables should the Pizza class have?
-
What do the instance variables represent? What are the types of those instance variables?
-
Methods
Save here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/10_classes/pizza_class.txt
-
-
CW 09 - 2D arrays
DUE DATE:Please complete these exercises.
-
CW 05 - Strings
DUE DATE:Get this starter code and follow those directions.
Save your file here: .../APCSA_1/apcsa-assignments-YourUsername/classwork/05_strings/MyStrings.java
-
CW 04 - Casting
DUE DATE:Create a java file Casting.java and implement a method that:
- Receives 2 integers
- Divides the two ints
- Prints the result
The trick here is that we want the division of the two ints to result in a double! Casting values to doubles will be necessary to solve this exercise. Here is an example:
x = 3 y = 4 Output => 0.75
Save your file here:
.../APCSA_1/apcsa-assignments-YourUsername/classwork/04_casting/Casting.java
-
CW 03 - Expressions
DUE DATE:Give the value and type of each of the following expressions. If the expression does not compile or causes a runtime exception, put an X in both boxes.
Expressions
2 + 5
8 / 10 * 1.5
12 % 7
2 + 3 * 4
2 + 3.0 * 4
1 / 1 / 0
1.0 / 1 / 0
“Happy” + “Face”
“8+2” + “5”
“10” + 8 + 12
2 + 4 + "5" + 6
27 % 4
-
CW 02 - Art
DUE DATE:Write a program to draw some ASCII art using:
System.out.println() System.out.print()
Save here: .../APCSA_1/apcsa-assignments-YourUsername/classwork/02_art/Art.java
-
CW 01 - Hello World and GitHub
DUE DATE:The objective of this exercise is to create a java file, add it, commit and push to a remote repository.
- In your local machine, go to the folder where your assignments repo was cloned ...../APCSA_1/apcsa-assignments-YourUsername/ and create a folder classwork.
- Create a folder 01_hello_world inside the classwork folder.
- Write a java file HelloWorld.java that prints the message "Hello World!".
- Run
git status
. What does it show? - Add the new file to the repo
- Run
git status
again. What does it show this time? - Commit and push the file to your repo.
- Go to GitHub, HelloWorld.java should be there.