Assignment 19
Due Tuesday, December 4 at 9:20am on Canvas

Assignment 19.1: Getting Even More Methodical (10 pts)

  • Copy and paste the starter code into a new file called
  • Write the required methods as described by the comments (i.e. you fill in the body of the method).
  • Then, run the code when you are finished to see if you wrote the methods correctly.
  • Check the test results and make any alterations to your methods as necessary.
  • When all of the tests pass, upload your code to Canvas.
 * @author
 * CIS 36A
import java.util.Scanner;

public class EvenMore {
         Given a String, return true if the String starts
        with "hi" and false otherwise.
        startHi("hi there") → true
        startHi("hi") → true
        startHi("hello hi") → false

    public static boolean startHi(String str) {
        return false;
         Given a String and a non-negative int n,
        the front of the String is the first 3 chars,
        or whatever is there if the string is less than length 3
        Return n copies of the front;
        Hint: use a for loop and string concatenation
        frontTimes("Chocolate", 2) → "ChoCho"
        frontTimes("Abcd", 3) → "AbcAbcAbc"
        frontTimes("Oy", 4)  → "OyOyOyOy"
    public static String frontTimes(String str, int n) {
        return "";
        Given 2 int values, return whichever value is nearest to the value 10,
        or return 0 in the event of a tie.
        Hint: recall that Math.abs(n) returns the absolute value of a number.
        close10(8, 13) → 8
        close10(13, 8) → 8
        close10(13, 7) → 0
    public static int close10(int a, int b) {
        return 0;
         Given a String, return true if the first instance of "x" in
        the String is immediately followed by another "x".
        Hint: use a for loop and substring
        doubleX("axxbb") → true
        doubleX("axaxax") → false
        doubleX("xxxxx") → true

    public static boolean doubleX(String str){
        return false;


    public static void main(String[] args)
        String value;
        boolean result;
        int num;
        System.out.println("***Testing startHi***\n");
        result = startHi("hi there");
        System.out.println("Should be true: " + result);
        result = startHi("hi");
        System.out.println("Should be true: " + result);
        result = startHi("hello hi");
        System.out.println("Should be false: " + result +"\n");
        System.out.println("***Testing frontTimes***\n");
        value = frontTimes("Chocolate", 2);
        System.out.println("Should be ChoCho: " + value);
        value = frontTimes("Abcd", 3);
        System.out.println("Should be AbcAbcAbc: " + value);
        value = frontTimes("Oy", 4);
        System.out.println("Should be OyOyOyOy: " + value + "\n");
        System.out.println("***Testing close10***\n");
        num = close10(8, 13);
        System.out.println("Should be 8: " + num);
        num = close10(13, 8);
        System.out.println("Should be 8: " + num);
        num = close10(13, 7);
        System.out.println("Should be 0: " + num + "\n");
        System.out.println("***Testing doubleX***\n");
        result = doubleX("axxbb");
        System.out.println("Should be true: " + result);
        result = doubleX("axaxax");
        System.out.println("Should be false: " + result);
        result = doubleX("xxxxx");
        System.out.println("Should be true: " + result + "\n");
        System.out.println("***End of Tests***");



Assignment 19.2: Barnyard Animals (10 pts)

  • In this assignment we will write a program that stores 5 Strings in a String array, and 5 ints in an integer array and then writes the contents of the two arrays to the console.
  • Copy the following program into a Java file, and save it as
  • At the top of main declare a String array named animals of size 5. 
  • Then, let's declare an integer array of size 5 named number_animals.
  • Store the following values in the String array, one at each index:
  • Remember: you can do this, using either static or non-static intialization.
  • Pick ONE of the above options to store the String values in the animals array.
  • Next, pick one option to store the following values in the integer array (number_animals), one at each index:

  • We are now going to write the contents of our arrays to the console.
  • First, inside of main, add the following print statement:

    System.out.println("Animals at our farm:");

  • Next, write a method that prints out the the two arrays.
  • Copy and paste the below javadoc comment and method signature outside of main:
* Prints out the values of two arrays side-by-side
* @param array1 the first array to print
* @param array2 the second array to print
public static void printArrays(int array1[], String array2[]){}

  • Inside your method, you will need a for loop to print the arrays to the console, side-by-side, like this:
5 sheep
3 donkeys
8 goats
2 geese
4 cats
  • See the class notes if you get stuck for an example of using for loops with arrays.
  • Also see the class notes for examples of how to write a function to print the contents of an array.
  • Next, below your System.out.print statement inside of main, call the printArrays method, passing it the two arrays as arguments.
  • Finally, compile and run your code and verify that the following output appears in the console window:

    Animals at our farm:
    5 sheep
    3 donkeys
    8 goats
    2 geese
    4 cats
  • Submit your program to Canvas when you are finished

Assignment 19.3: Number of Numbers (10 pts)

  • For this assignment you will be working with arrays.
  • Open a new Java project called Nums
  • Prompt a user to enter in the length of the array (the number of numbers).

Enter the number of numbers:
  • Use this information to declare an array of the user-specified length.
  • Next, using a for loop, prompt the user to enter that many numbers, and store each one in your array until your array is at full capacity.
  • Using the same, or a second, for loop, sum the numbers and report their sum to the user.
  • Also, multiply the numbers together and report their product to the user.
  • The sum and the product should be printed to one decimal value only.
  • Your program should work identically to the sample output below:

Enter the number of numbers: 4
Enter 4 numbers:

Number 1: 3.2
Number 2: 6.7
Number 3: 2.9
Number 4: 4.6

The sum of the numbers is: 17.4
And the product is: 286.0

  • Another run of the program might give the following output:

Enter the number of numbers: 8
Enter 8 numbers:

Number 1: 2.1
Number 2: -5.6
Number 3: 9.0
Number 4: 8.7
Number 5: -2.2
Number 6: 8.2
Number 7: 9.5
Number 8: 1.4

The sum of the numbers is: 31.1
And the product is: 220931.3

  • Upload to Canvas when you are finished.