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

Assignment 19.1: 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

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;
    }

}

When your program is giving the correct output as demonstrated below, upload your source code to Canvas.

Enter an email address: bob@jobs.net
Invalid email. Your email must contain a .com, .edu or .org extension.

Alternately,

Enter an email address: bobajobs.com
Invalid email. Your email must contain an @ symbol.

Alternately,

Enter an email address: bob @jobs.com
Invalid email. Your email address cannot contain a space.

Alternately,

Enter an email address: boba jobs.com
Invalid email. Your email must contain an @ symbol.

Alternately,

Enter an email address: bob@jobs.com
Your email address is valid.


Assignment 19.3: My Calculator (10 pts)

  • Your task is to write a calculator program in a file named Calculator.java.
  • This calculator should be able to perform 10 different operations.
  • Each of the operations needs to be its own method
  • Your program will begin by welcoming the user and listing the different operations it can perform (please see sample output below).
  • Your program should be controlled by a loop so that the user can perform multiple operations without rerunning the program (see sample output below).
  • The methods you need to write are as follows (in alphabetical order):
add: takes two integer parameters and returns an integer for the sum
absolute value: takes in one integer parameter (positive or negative), computes and returns the integer distance from 0.
ceiling: takes one double parameter and returns the next largest integer
divide: takes two integer parameters and returns the result of integer division
floor: takes one double parameter and returns the next smallest integer
greater than: takes two integer parameters and returns the integer that is greater
less than: takes two integer parameters and returns the integer that is lesser
multiply: takes two integer parameters, multiplies them together and returns the product
power: takes one integer parameter for the base, one integer parameter for the exponent and returns an integer for the base raised to the power. Note: Do not use the Math.pow method here. You must write your own power method.
subtract: takes two integer parameters, subtracts the second one from the first and returns the difference as an integer.

Your output should look identical to my sample output below: