Assignment 19
Due Tuesday, March 20 at 11:20am on Canvas

Assignment 19.1: Getting Even More Methodical (10 pts)

  • Copy and paste the starter code into a new file called EvenMore.java
  • 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: Valid Email Address? (10 pts)
  • Let's write a program, in a file named Email.java, to check an email address to see if it is valid.
  • For the purposes of this assignment, we will use three criteria to determine if an email address is valid:
    • The address must end with one of the following extensions: .com, .org or .edu
    • The address must not contain a space
    • The address must have an @ character contained somewhere in the String
  • You will write 3 methods and 3 Javadoc comments. Each method will check each of the above conditions.
  • Your first method signature should look like this:

public static boolean validExtension(String address) {

    //method body will go here

}

  • The second method signature should look like this:

public static boolean containsSpace(String address) {

    //method body will go here

    }

  • The third method signature should look like this:

public static boolean containsAt(String address) {

    //method body will go here

    }
  • Hint: Do not alter the starter code, given below, in any way. Your job is only to add to it where indicated:
/**
* @author Name
* section info
*/

import java.util.Scanner;

public class Email {
    public static void main(String[] args) {
        String address;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter your email address: ");
        address = input.nextLine();

        if (!containsAt(address)) { // the method will return true or false so
                                    // we can check in an if statement
            System.out.println("Invalid email. Your email address must contain an @ symbol");
        } else if (false) { // replace false with method call here
            System.out.println("Invalid email. Your email address cannot contain a space");
        } else if (false) { // replace false with method call here
            System.out.println("Invalid email. Your email must contain a .com, .edu or .org extension");
        } else {
            System.out.println("Your email address is valid.");

        }
    }
   
    //Write JAVADOC comment here for validExtension
    public static boolean validExtension(String address) {
        // write method body here
        return false;
    }
   
   
    //Write JAVADOC comment here for containsSpace
    public static boolean containsSpace(String address) {
        // write method body here
        return false;
    }


    //Write JAVADOC comment here for containsAt
    public static boolean containsAt(String address) {
        // write method body here
        return false;
    }

}

Assignment 19.3: Barnyard Animals (10 pts)

  • In this assignment we write a program that copies five numbers from an input stream to an output stream.
  • Create a new Java project called Animals
  • Then, let's create a text file to test our program.
  • In Eclipse, create two new empty files. Name the files infile.txt and outfile.txt.
  • In the file, add a list of numbers each on their own line, like this:
5
3
8
2
4
  • Important: You need to save this file in the Animals project folder (not in the src folder). Right click the Animals folder, go to File->New->File and name your file infile.txt.
  • We will read from this file after writing our program.

  • Add the necessary import statements to your source code file:
  • Inside main(), declare two new File variables, one for infile.txt and one for outfile.txt
  • Connect these two files to a Scanner and a PrintWriter respectively.
  • Add statements to read two numbers from the input stream.
  • For example, here is possible code for reading the first number:
    int first = input.nextInt();
    
  • Add 4 more pairs of statements like the ones above to read in the next 4 numbers.
  • Then, add an print statement to print out a title at the top of your file
    output.println("Animals at our farm");
    
  • Next, write out the contents of your five variables to the file. For example:
output.println(first + " chickens");
  • Add 4 more statements like the one above, so that your file contains the following when you are finished:
Animals at our farm
5 chickens
3 cows
8 ducks
2 horses
4 pigs
  • Close the streams when finished reading and writing:
    input.close();
    output.close();
    
  • Compile and run your program to make sure you made the changes correctly.
  • Notice that you do not see any output on the console screen. The output stream wrote the program output to the output file.
  • Open the output file you created and verify that your file contains the following:

    Animals at our farm
    5 chickens
    3 cows
    8 ducks
    2 horses
    4 pigs
  • Submit  your program to Canvas when you are finished