User:Baest83457

// Program: cs2prog3_15.java // Programmer: Michael Graczyk // Description: This program uses a do while loop to allow the user to enter a password up to three times. If they succeed, the password's //				letters are changed with the replace method, and then the new password is shown. If the user fails, a message saying that they //				used up all their chances is displayed. // Date: 1/2/07 // Methods: main

import java.util.Scanner;

public class cs2prog3_15 {	private static Scanner input = new Scanner(System.in); private static String password = "password"; public static void main(String args[]) {		boolean correct = false; int tries = 0; do {			tries++; System.out.print("Enter the password: ");

correct = input.nextLine.equals(password);

if(!correct); {				System.out.println("Try Again!"); System.out.println; }		}while(tries<3&&!correct); if(correct) {			password = password.replace('s','m'); System.out.println("Congratulations!\n"); System.out.println("When all of the s's are changed to m's, the new password is \"" + password + "\"."); }else {			System.out.println("Your three chances are up! The End!"); }	} }

// Program: cs2prog3_16.java // Programmer: Michael Graczyk // Description: This program allows the user to enter a sentence, then figures and prints the amount of words in the sentence. // Date: 1/2/07 // Methods: main

import java.util.Scanner;

public class cs2prog3_16 {	private static Scanner input = new Scanner(System.in); public static void main(String args[]) {		String sentence; int numWords = 0;

System.out.print("What is your sentence? "); sentence = input.nextLine + " "; System.out.println; for(int j=0; j<sentence.length; j++) {			if(sentence.charAt(j)==' ') {				numWords++; }		}		System.out.println("There are " + numWords + " words in the sentence \"" + sentence.trim + "\"."); } }

// Program: cs2prog3_17.java // Programmer: Michael Graczyk // Description: This program allows the user to enter a sentence, then saves another string contisting of the //				first word in the sentence. This word is then printed with a statement of whether the word //				appears more than once in the sentence or not, and how many times it appears. The program uses the equals method to //				determine this. The words are not case sensitive. // Date: 1/2/07 // Methods: main

import java.util.Scanner;

public class cs2prog3_17 {	private static Scanner input = new Scanner(System.in); public static void main(String args[]) {		String sentence; String sentenceLowerCase; String words[] = new String[50]; String wordsLowerCase[] = new String[50]; int numInstances = 1;

System.out.print("What is your sentence? "); sentence = input.nextLine + " "; System.out.println; sentenceLowerCase = sentence.toLowerCase; for(int k=0, j=0, m=0; j<sentence.length; j++) {			if(sentence.charAt(j)==' ') {				words[k] = sentence.substring(m, j); k++; m=j+1; }		}		for(int k=0, j=0, m=0; j<sentenceLowerCase.length; j++) {			if(sentenceLowerCase.charAt(j)==' ') {				wordsLowerCase[k] = sentenceLowerCase.substring(m, j); k++; m=j+1; }		}		for(int k=1; k<wordsLowerCase.length; k++) {			if(wordsLowerCase[0].equals(wordsLowerCase[k])) {				numInstances++; }			}		System.out.println("There " + (numInstances==1?"is ":"are ") + numInstances + (numInstances==1?" instance":" instances") + " of \"" + words[0] + "\" in \"" + sentence.trim + "\".");

} }