Assignment 17
Due Tuesday, June 16 at 11:59pm on Canvas Important:
To receive credit, you must only use concepts and methods taught in
class to complete these assignments - or You Will Receive a 0Pair Programming Required (or You Will Receive a 0)
Assignment 17.1: Newsletter Subscriptions (10 pts)
/** * @author * @author * CIS 36A, Assignment 17.1 */ import java.util.Scanner; public class Newsletter { /** * Removes all blank spaces in front of * and at the end of a String * Hint: use two while loops - one for * the front of the String, and one * for the end of the String. * Use substring to remove the blank space * from the String * @param email the email address * @return an email with no blank spaces * at the front or end of the String */ public static String cutSpace(String email) { //write one while loop here //write the second while loop here return email; } /** * Searches a String for an @ character * @param email the String to search * @return whether the String contains an '@' */ public static boolean hasAt(String email) { //write a for loop here to iterate through the String email return false; } /** * Uses a for loop to iterate through an array of * correct email extensions, comparing the last 4 * characters in a String parameter to each correct * extension. Hint: If the extension matches the * last 4 chars in the parameter return true * and return false only after checking all extensions * @param email the String, whose last 4 chars is * compared to each extension * @return whether or not the last 4 chars match one * of the extensions */ public static boolean correctExtension(String email) { //source: https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains String[] extensions = {".com", ".net", ".org", ".edu", ".gov", ".int"}; //write a for loop here to iterate through the array extensions return false; } /** * Determines whether a String contains a space * @param email the String to search for a space * @return whether the String contains a space */ public static boolean containsASpace(String email) { //Write a for loop here to iterate through the String email return false; } public static void main(String[] args){ Scanner input = new Scanner(System.in); int numEmails; String email; System.out.println("Welcome to our newsletter subscription service!\n"); System.out.print("Enter the number of emails to subscribe: "); //check for input mismatch exception with a while loop numEmails = input.nextInt(); //declare your array of Strings here //write a for loop here with if-else if-else if- else //In each if and else if statement, call one of your methods System.out.println("\nYou entered the following emails:\n"); //print the array to the console with a for loop input.close(); } }
Sample Output #1: Welcome to our newsletter subscription service! Enter the number of emails to subscribe: six Error! Please enter a number not text! Enter the number of emails to subscribe: 6d Error! Please enter a number not text! Enter the number of emails to subscribe: 6 Enter email #0: marianne2gmail.com Error! The email address must contain an @ symbol Enter email #0: marianne@gmail.com Enter email #1: mustafa@hotmail.co Error! The email address must have a valid extension Enter email #1: mustafa@hotmailcom Error! The email address must have a valid extension Enter email #1: mustafa@hotmail.com Enter email #2: xi@comcast .net Error! The email address cannot contain spaces Enter email #2: xi@comcast.net Enter email #3: kim sbcglobal .net Error! The email address must contain an @ symbol Enter email #3: kim@ sbcglobal .ne Error! The email address must have a valid extension Enter email #3: kim@ sbcglobal .net Error! The email address cannot contain spaces Enter email #3: kim@sbcglobal Error! The email address must have a valid extension Enter email #3: kim@sbcglobal.net Enter email #4: leila@gmail.com Enter email #5: lee@deanza.edu You entered the following emails: 0. marianne@gmail.com 1. mustafa@hotmail.com 2. xi@comcast.net 3. kim@sbcglobal.net 4. leila@gmail.com 5. lee@deanza.edu Enter the number of emails to subscribe: two Error! Please enter a number not text! Enter the number of emails to subscribe: sfdhjk Error! Please enter a number not text! Enter the number of emails to subscribe: dsfhklljkhfds Error! Please enter a number not text! Enter the number of emails to subscribe: two Error! Please enter a number not text! Enter the number of emails to subscribe: 2 Enter email #0: xiyuan @ hotmail .fr Error! The email address must have a valid extension Enter email #0: xiyuan @ hotmail .com Error! The email address cannot contain spaces Enter email #0: xiyuan@hotmail.com Enter email #1: yoko2yahoo.com Error! The email address must contain an @ symbol Enter email #1: yoko2@yahoo.com You entered the following emails: 0. xiyuan@hotmail.com 1. yoko2@yahoo.com |