Homework

  1. HW 24 - Review Questions


    DUE DATE:

    Select 5 challenging/tricky questions from previous exams, College Board, or write your own questions. We will go over those questions in class tomorrow.

    Save your file here: .../APCSA_1/apcsa-assignments-YourUsername/homework/24_apcs_review_questions/questions.md (or txt, docx, pdf)

  2. HW 23 - Recursion CodingBat


    DUE DATE:

    The following sets of CodingBat problems should be completed:

    First Set (We worked on this last Friday)

    Second Set (This one, we started in class today)

  3. HW 22 - Recursion


    DUE DATE:

    Save your files here: .../APCSA_1/apcsa-assignments-YourUsername/homework/22_recursion/RecursiveFunctions.java

    Exercise 1: Sum digits

    Write the solution to the following problem:

    Given a non-negative int n, return the sum of its digits recursively (no loops). Do not use strings to solve the problem.

    Example: 12945 -> 1 + 2 + 9 + 4 + 5 = 21

    Hints:

    How to get the digit at the end 12945 => 5?

    How to remove the last digit 12945 => 1294?

    These questions will guide you in finding the solution:

    • What is the base case?
    • Which operations can break up the numbers into a single digit and the rest?
    • What is the recursive case?

    Exercise 2: Fibonacci Sequence

    The sequence follows the rule that each number is equal to the sum of the preceding two numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233 … )

    image

    These questions will guide you in finding the solution:

    • What is the recursive case?
    • When do you call the recursive case?
    • What is(are) the base case(s)?

    When you test your fibonacci method:

    1. Check how long it takes to run the method. Run this command in your terminal: time java Fibonacci.java

    2. What is the largest number such that fibonacci(number) fits in an int? (does not overflow)

  4. HW 21 - More Shapes


    DUE DATE:

    Save your files here: .../APCSA_1/apcsa-assignments-YourUsername/homework/21_inheritance_more_shapes/

    • Write a superclass Circle. The instance variable should be radius. There will be one constructor to initialize the instance variable. The following methods should be implemented: getRadius(), toString(), getArea().

    • Write a subclass Cylinder which extends from Circle. Define the instance variable height. There will be one constructor to initialize the instance variable. The following methods should be implemented: getHeight(), getVolume(), getArea(), toString(). If you are overriding a method, please use the @Override annotation.

    • Write a Driver.java to test your code.

    image

  5. HW 20 - Shapes


    DUE DATE:

    Save your files here: .../APCSA_1/apcsa-assignments-YourUsername/homework/20_inheritance_shapes/

    • Create the following classes: Shape (superclass), Rectangle, Triangle.
    • Rectangle and Triangle should inherit from Shape.
    • Declare private instance variables in class Shape: double height and double width.
    • Write a constructor in class Shape to initialize the values for height and width.
    • Write public methods in class Shape to get/set height and width values: getHeight(), getWidth(), setHeight() and setWidth().
    • Write a method getArea() in both of the subclasses Rectangle and Triangle which calculates and returns the area the result.
    • You will need to define the constructors in Rectangle and Triangle classes (which call the Shape constructor).

  6. HW 19 - File Handling


    DUE DATE:

    Save your file here: .../APCSA_1/apcsa-assignments-YourUsername/homework/19_handling_files/

    Longest Word

    Write a Java program (LongestWord.java) to find and print the longest word inside "my_text.txt". If there are more than one word with the same lenght, you must print the first longest word.

    TwoD

    Write a Java script (ReadFile.java) that reads the file "2d.txt", creates a 2D array with that data and prints the 2D array.

    Find the txt files here.

  7. HW 18 - ArrayList


    DUE DATE:

    Save your file here: .../APCSA_1/apcsa-assignments-YourUsername/homework/18_ArrayList/ArrayListPractice.java

    A Driver is not needed. You may write the main method in ArrayListPractice.java

    1. Return the sum of all the numbers at even positions in the array list.
    public int listSumEvenIndexes(ArrayList<Integer> numsList) {     
      
    }
    

    Examples:

    listSumEvenIndexes([1, 2, 3, 4]) → 4
    listSumEvenIndexes([8, 16, 10]) → 18
    listSumEvenIndexes([5, 0, -10]) → -5
    
    1. An array list contains a list of animals. If the animal is a cat (i.e. the animal's description contains the word "cat" or "Cat"), then add it to a new array list. Return the new array list of cats.
    public ArrayList<String> listCatty(ArrayList<String> animalsList) {    
      
    }
    

    Examples:

    listCatty(["buffalo", "dog"]) → []
    listCatty(["bobcat", "siamese cat", "catbird"]) → ["bobcat", "siamese cat", "catbird"]
    listCatty(["Cat", "frog", "mouse"]) → ["Cat"]
    
    1. Return the sum of the numbers in the array list that are odd and have an even index, or the number is even and its index is odd.
    public int listFairlyOddNums(ArrayList<Integer> numsList) { 
      
    }
    

    Examples:

    listFairlyOddNums([1, 2, 3, 4]) → 10
    listFairlyOddNums([8, 16, 10]) → 16
    listFairlyOddNums([5, 0, 13, 51]) → 18
    
    1. Complete the classwork ClimbInfo.

  8. HW 17 - SuperArray Exceptions


    DUE DATE:

    Directions are here

  9. HW 16 - APLine


    DUE DATE:

    Directions are here

    Save your file here: .../APCSA_1/apcsa-assignments-YourUsername/homework/16_APLine/

  10. HW 15 - Employee Java class


    DUE DATE:

    • Create a class Employee

    • Define instance variables

    • Define at least 3 construtor (1 of them default constructor)

    • Declare non-static methods (3 or more)

    • Declare static methods (1 or more)

    • Create multiple objects from your Driver and test all your constructors and all your methods

  11. HW 14 - Employee class design


    DUE DATE:

    This homework is exactly the same as your classwork, but now we want to design an Employee class.

    A big part of using classes in Java is thinking about the design of the class. You’ll need to figure out what information needs to be in the class For instance, in our Rectangle class, we needed to know the width and height. This exercise is a free response question. Imagine that someone comes to you and asks you to design a class that represents a Employee.

    1. What instance variables should the Employee class have?

    2. What do the instance variables represent? What are the types of those instance variables?

    3. What methods would you implement in your Employee class?

    Save your file here: .../APCSA_1/apcsa-assignments-YourUsername/homework/14_classes/employee_class.txt (or md, docx, pdf files)

  12. HW 13 - Playing with 2D arrays


    DUE DATE:

    • Define a class MyArrayPractice with its main method.
    • Write the following methods:
    // It should create a copy of the array nums. If you change the new array, nums should not change.
    public static int[][] copy(int[][] nums){
    
    }
    
    
    // Modify nums. Replace negative values:
    // When the row index is the same as the column index replace that value with 0.
    // Otherwise, replace negative values with 1.
    public static int[][] updateValues(int[][] nums){
    
    }
    
    • Write a few test cases in the main method to test your code.

    Save your file here: .../APCSA_1/apcsa-assignments-YourUsername/homework/13_copy_arrays/MyArrayPractice.java

  13. HW 12 - CodingBat


    DUE DATE:

    CodingBat

  14. HW 11 - String Methods


    DUE DATE:

    Write your own version of the methods: indexOf, compareTo, parseInt. Get this starter code to implement your methods.

    Also, using your own words explain how each of those methods works. Type your anwers as comments in your java file.

    Save your file here: .../APCSA_1/apcsa-assignments-YourUsername/homework/11_string_methods/MyStringMethods.java

  15. HW 10 - CodingBat - Strings


    DUE DATE:

    CodingBat

    More CodingBat

  16. HW 09 - Scanner and CodingBat


    DUE DATE:

    Part 1:

    Go inside the folder homework, and create a subfolder 09_snooze. Inside this folder, you will write the following java file:

    NoonSnooze.java: This program should ask the user for a number that represents the number of minutes, snooze, that have elapsed since 12:00 pm (noon) and prints the resulting time. Assume a 12-hour clock (with 'am' and 'pm'). You must not use loops. If the snooze value is negative, print a warning message: "No negative values are allowed".

    Examples:

    If the user input is 50, the output should be 12:50 pm

    If the user input is 100, the output should be 1:40 pm

    If the user input is 721, the output should be 12:01 am

    If the user input is 11111, the output should be 5:11 am

    If the user input is -10, the output should be No negative values are allowed

    Part 2:

    Have fun with CodingBat

  17. HW 08 - Scanner


    DUE DATE:

    Save your file here: .../APCSA_1/apcsa-assignments-YourUsername/homework/08_scanner/NightOut.java

    Follow these directions to write your NightOut.java program:

    You and a friend are going out for the night. You have decided to treat your friend, so you’re paying for the whole night. However, since you have a fixed amount of money to spend on fun things, you need to track how much the outing will cost so you can update your budget.

    Write a program to help yourself estimate what the total cost of the night will be. Your program will estimate the cost by taking the cost of the activities for one person and estimating how much it will cost for two people.

    Here’s what you know about your activities: Dinner - you know you typically get cheap dinners, so you expect that your friend’s dinner will be twice as expensive as yoursLaser Tag - since laser tag is charged per person, you and your friend will cost the sameIce cream - you like the triple scoop, but your friend likes a single scoop. Your friend’s ice cream will cost 1/3 as much as yours.

    Your program should ask how much YOUR dinner cost, how much laser tag costs per person, and how much YOUR ice cream costs. It should then compute how much your friend’s costs will be based on the information above. Be sure your program takes the input in this exact order. Then print how much dinner will cost (for both of you), how much laser tag will cost (for both of you), and how much the ice cream will cost (for both of you). Then print the grand total for the evening.

    Your output should look something like this:

    How much does dinner usually cost? 
    
    12.63
    
    How much is laser tag for one person? 
    
    17.50
    
    How much does a triple scoop cost? 
    
    27.00
    
    Dinner: $37.89
    
    Laser Tag: $35.0  
    
    Ice cream: $36.0
    
    Grand Total: $108.89
    

  18. HW 07 - Practice Variables and Expressions


    DUE DATE:

    Go inside the folder homework, and create a folder 07_practice. Inside this folder, you will write the following programs:

    1. Write a Java program (Fractions.java). In this program you will initialize 4 integers that represent each part of two fractions, namely the numerator and denominator of the first fraction and the numerator and denominator of the second fraction. Your program should add the two fractions and print out the result.

    For example, a sample program run might look like this:

      The numerator of the first fraction is 1
      The denominator of the first fraction is 2
      The numerator of the second fraction is 2
      The denominator of the second fraction is 5
      The sum of 1/2 + 2/5 = 9/10
    
    1. Write a Java program (WorkShift.java). A doctor works 20 hours, 42 minutes, and 16 seconds in one shift at a hospital. Convert the total shift time into seconds and display that information.

    NOTE: You must use at least ONE compound operator (+=, -=, *=, /=, %=) to complete this program.

  19. HW 06 - Data Types and Variables


    DUE DATE:

    Data Types and Variables

    Directions: Create a homework folder in your assignments repo .../APCSA_1/apcsa-assignments-YourUsername/homework/ then create a folder 06_variables inside .../homework/06_variables/

    Inside that folder, you will write the following programs:

    1. Write a Java program (Temperature.java) to convert temperature from Fahrenheit to Celsius degrees. You must use variables.

    Output:

    50.0 degrees Fahrenheit is equal to 10.0 Celsius

    1. Write a Java program (Calculator.java) that calculates the sum of two numbers (int or double).

    Output:

    10 + 12 = 22

    1. Create a file TrickyCalc.java and copy the following code inside. Run the program. Create a file answers.txt or answers.md and respond to the following questions:
    • What do you notice about the mySum value?
    • What is the value of checkResult, and why do we get that result?
    public class TrickyCalc{
       public static void main(String[] args){
           double mySum = 0.1 + 0.1 +0.1;
           double tmp = 0.3;
           boolean checkResult = mySum == tmp;
    
           System.out.println("0.1 + 0.1 + 0.1 = " + mySum);
           System.out.println(mySum + " == " + tmp + " is " + checkResult);
       }
    }
    
    1. Double check you have your files are organized like this in your repo:
    apcsa-assignments-YourUsername
      classwork
        01_hello_world
          HelloWorld.java
        02_art
          Art.java
      homework
        06_variables
          Temperature.java
          Calculator.java
          TrickyCalc.java
          answers.txt or answers.md
    

  20. HW 05 - Art


    DUE DATE:

    1. Complete CW 1 and 2.
    2. Remember to complete this form. It is the same one I posted on Piazza.

  21. HW 04 - README.md


    DUE DATE:

    Edit your README.md file at home. It should have:

    • Name:
    • Period:
    • Email:

    Tomorrow, we will pull the changes using the lab machines.

  22. HW 03 - GitHub


    DUE DATE:

    1. Configure your machines at home to connect to GitHub with SSH.
    2. If you did not accept the assignments repo in class, please do it at home. This is the link. After accepting that assignment, you should have a repo created on your GitHub.
    3. Clone the assignments repo on your home machine:

            - Go to your GitHub assignments repository that you accepted for this class. You will see a green button, "Code". You must click on that button and select SSH to have the appropriate link that will allow you to clone your repository and copy the link you see there.

            - Create a folder APCSA_1 anywhere in your home computer.

            - On your terminal, go inside the folder APCSA_1 to clone your repository and type:

              git clone PASTE_THE_LINK_YOU_COPIED_FROM_GITHUB_(git@...)

    QUIZ: The summer assignment quiz will be on Monday, September 16.

  23. HW 02 - Summer Assignment


    DUE DATE:

    1. Follow the next steps to share your CodingBat Summer work with me.
    • Log in to your CodingBat account.
    • Click on prefs
    • Teacher Share section: type the teacher's email jnovillo@stuy.edu
    • Click on Share
    • Update the Memo section: type YourPeriod_YourLastName_YourFirstName (no spaces) like this 1_smith_peter
    • Click on Update Memo
    1. Submit the complete Java program that was assigned on Google Classroom.
    2. Create a new post on Piazza. This post could be a question or comment about the summer assignment. Reply to at least one peer's comment/question to generate a productive discussion.
    3. If you do not have a GitHub account, create one please GitHub (you may use any email to create your GitHub account).

  24. HW 01 - Forms


    DUE DATE:

    Complete the following forms: