ExamIIPreparationExercises With Bold
ExamIIPreparationExercises With Bold
Solution
Worst case scenario: The statement (A[i]= 0) in if block always returns true
so that the inner for loop is always executed.
Value of ‘i' the statement A[i] = A[i] + A[j] is executed
1 2 times
2 3 times
3 4 times
..
..
..
..
n-1 n times
Total number of times the statement is executed: f(n)= 2+3+….+n =
n(n+1)/2 – 1 = O(n2)
String binaryEquivalent(int n) {
1. if (n == 0)
2. return "0";
3. if (n ==1)
4. return "1";
5. if (n % 2 == 0)
6. return binaryEquivalent(n/2) + "0";
7. else
8. return binaryEquivalent(n/2) + "1";
}
Solution
Solution
//*******************************************************
*********
// PalindromeDriver.java
//*******************************************************
**********
import java.util.Scanner;
while (another.equalsIgnoreCase("y"))
{
System.out.println ("Enter a potential palindrome:");
str = scan.nextLine();
if (test.isPalindrome())
System.out.println ("That string IS a palindrome.");
else
System.out.println ("That string is NOT a palindrome.");
System.out.println();
System.out.print ("Test another palindrome (y/n)? ");
another = scan.nextLine();
}
}
}
//*******************************************************
*********
// PalindromeSupport.java
//*******************************************************
**********
//-----------------------------------------------------------------
// Stores the string to be evaluated.
//-----------------------------------------------------------------
public PalindromeSupport(String test)
{
testString = convertString(test);
}
//-----------------------------------------------------------------
// Converts a string to correct format; removes all characters
// except for number and digits
//-----------------------------------------------------------------
private String convertString (String str)
{
String str2 = "";
str = str.toLowerCase();
return str2;
}
//-----------------------------------------------------------------
// Determines if the string is a palindrome.
//-----------------------------------------------------------------
public boolean isPalindrome()
{
return testPalindrome(0, testString.length()-1);
}
//-----------------------------------------------------------------
// Recursively determines if the string is a palindrome by
// testing smaller substrings.
//-----------------------------------------------------------------
private boolean testPalindrome (int startIndex, int endIndex)
{
boolean result;
return result;
}
}
File PermutationGeneratorTester.java
import java.util.ArrayList;
import java.util.Scanner;
/**
This program tests the permutation generator class
*/
public class PermutationGeneratorTester {
public static void main(String[] args) {
// Read a string from the end user
Scanner scan = new Scanner(System.in);
System.out.println(“Please enter a String:”);
String str = scan.nextLine();
// Create an instance of the Permutation
Generator
PermutationGenerator generator = new
PermutationGenerator(str);
ArrayList<String> permutations =
generator.getPermutations();
// Print all possible permutations out
for(int i=0; i<permutations.size(); i++)
System.out.println(permutations.get(i));
}
File PermutationGenerator.java
import java.util.ArrayList;
/**
This program generates permutations of a word
*/
public class PermutationGenerator {
// instance variable goes here
private String word;
/**
Constructs a permutation generator
@param aWord is the word to permute
*/
public PermutationGenerator(String aWord) {
// other code to initialize the instance variable
// would go here
word = aword;
}
/**
Gets all permutations of a given word
*/
Public ArrayList<String> getPermutations() {
/*
Loop through all character positions using a for
loop whose index i ranges from 0 to one less than
the length of the string. Form a simpler word by
removing the ith character then generate all
permutations of the simpler word. Finally, add
the removed character to the front of each
permutation of the simpler word. */
for(int i=0; i<word.length();i++) {
//Form a simpler word by removing ith char
String shorterWord = word.substring(0, i) +
word.substring(i+1);