Last active
August 29, 2015 13:58
-
-
Save QuantumFractal/9977432 to your computer and use it in GitHub Desktop.
Java 227 - Exam 2 Review Guide Solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package exam2review; | |
import java.util.ArrayList; | |
/* | |
* <<<<NOTE>>>>>>> | |
* Please work the review on your OWN first! It will help your understanding immensely. | |
* only use this guide to check your own work. | |
* | |
* To Use: | |
* -Make a package called "exam2review". | |
* -Create a class called "ExamReviewSolutions". | |
* -Paste this code into that class. | |
*/ | |
public class ExamReviewSolutions { | |
//Setup variables | |
static double[] doublearray = {3.5,15.2,6.2}; | |
static String sentence = "The lazy brown fox jumped over the fence."; | |
static String nonalpha = "Hello, world!"; | |
static ArrayList<Integer> intList = new ArrayList<Integer>(); | |
public static void main(String[] args) | |
{ | |
//This Main tests all of the method solutions below. | |
//Try playing around with some of the values! | |
System.out.println("[Exam 2 Review Solutions]\n->Checkout the source!<-"); | |
System.out.println("Question 1a) 8.3 = "+q1a(doublearray)); | |
System.out.println("Question 1b) jumped = "+q1b(sentence)); | |
System.out.println("Question 1c) Hello##World# = "+q1c(nonalpha)); | |
System.out.println("Question 1d) 25 = "+q1d(.04,2000)); | |
intList.add(1);intList.add(2);intList.add(3);intList.add(4); | |
System.out.println("Question 1e) Increasing? = "+q1e(intList)); | |
intList.add(2);intList.add(1);intList.add(10);intList.add(1); | |
System.out.println("Question 1e) Increasing? = "+q1e(intList)); | |
System.out.println("Question 1f) 1 = "+q1f(nonalpha)); | |
} | |
public static double q1a(double[] array) | |
{ | |
//Initialize total variable. | |
double total = 0; | |
//Iterate over the array. | |
for(int i=0; i< array.length; i++) | |
{ | |
//Add each new value to total. | |
total+=array[i]; | |
} | |
//Divide by number of values and return. | |
return total/array.length; | |
} | |
public static String q1b(String sentence) | |
{ | |
//Split the sentence into an array of words. | |
String wordlist[] = sentence.split(" "); | |
//Initialize the variable for the longest word. | |
String longest = ""; | |
//Iterate over the words in the array. | |
for(int i=0; i<wordlist.length; i++) | |
{ | |
//If the current word is bigger than the longest, replace longest with that word. | |
if(wordlist[i].length() > longest.length()) | |
longest = wordlist[i]; | |
} | |
return longest; | |
} | |
public static String q1c(String phrase) | |
{ | |
//Convert the string into an array (because Strings are immutable) | |
char[] characters = phrase.toCharArray(); | |
//Iterate through the characters. | |
for(int i=0; i<characters.length; i++) | |
{ | |
//If the Character is NOT a letter, swap it for a '#'. | |
if(!Character.isLetter(characters[i])) | |
characters[i] = '#'; | |
} | |
//Change the array into a string again. | |
phrase = String.valueOf(characters); | |
return phrase; | |
} | |
public static int q1d(double givenRate, double givenBalance) | |
{ | |
/*Note: I know there's a mathematical way to do this. | |
* I'm using loops because the focus of this exam is | |
* not on your math abilities. */ | |
//Months must be declared outside of the loop. | |
int months = 0; double balance = givenBalance; | |
//Loop until the balance is doubled. Note the exit condition! | |
while(balance<givenBalance*2) | |
{ | |
//Increase the balance and increment the month. | |
balance+=givenBalance*givenRate; | |
months++; | |
} | |
return months; | |
} | |
@SuppressWarnings("unused") | |
public static boolean q1e(ArrayList<Integer> list) | |
{ | |
boolean flag = true; | |
//Loop through ArrayList | |
for(int i=1; i<list.size(); i++) | |
{ | |
//If one element is not bigger than the next, return false | |
if(!(list.get(i-1)<list.get(i))) | |
flag = false; | |
} | |
return flag; | |
} | |
public static int q1f(String phrase) | |
{ | |
//Sanitize the string | |
phrase=phrase.toUpperCase(); | |
//Initialize i outside of for loop (due to scope) | |
int i; | |
for(i=0; i<phrase.length(); i++) | |
{ | |
//If a character is in the list of vowels, break; | |
if("AEIOU".contains(phrase.charAt(i)+"")) | |
break; | |
} | |
return i; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment