Docsity

Midterm Exam with Solution - Computer | CSC 120, Exams of Computer Science

Toggle sidebar
bg1
Midterm Solutions (10) Page 1 of 1
------------- Count.java -------------
import java.io.*;
/*************************************
* Counts from a to b.
* If (a <= b), it counts up from a to b
* Otherwise, it counts down from a to b
*************************************/
public class Count {
public static void main(String[] args) {
System.out.println("Please enter a start integer");
int a = Keyboard.inputInteger();
System.out.println("Please enter an end integer");
int b = Keyboard.inputInteger();
if (a <= b) {
System.out.println("Counting up from " + a +
" to " + b + ".");
while (a <= b) {
System.out.println(a);
a++;
}
} else {
System.out.println("Counting up from " + a +
" to " + b + ".");
while (a >= b) {
System.out.println(a);
a--;
}
}
}
}
------------- Factorial.java -------------
import java.io.*;
/*************************************
* Computes the Factorial of an inputted integer (n)
* n! = n * (n-1) * (n-2) * ... * 1
*************************************/
public class Factorial {
public static void main(String[] args) {
System.out.println("Please enter an integer (n).");
int n = Keyboard.inputInteger();
int fact = 1;
int count = n;
while (count > 0) {
fact *= count;
count--;
}
System.out.println("The value of " + n + "! = " + fact);
}
}
------------- SubstringTesterLong.java -------------
import java.io.*;
/*************************************
* The long solution to Problem 10c
* This goes through the first string and compares
* all the substrings of first string with second string
*************************************/
public class SubstringTesterLong {
public static void main(String[] args) {
System.out.println("Please enter first string");
String first = Keyboard.inputString();
System.out.println("Please enter second string");
String second = Keyboard.inputString();
int i = 0;
boolean isFound = false;
while ((i <= (first.length() - second.length())) &&
(isFound == false)) {
String sub = first.substring(i,i+second.length());
if (sub.equals(second)) {
isFound = true;
}
i++;
}
if (isFound) {
System.out.println("Yes, " + second +
" is a substring of " +
first);
} else {
System.out.println("No, " + second +
" is not a substring of " +
first);
}
}
}
------------- SubstringTester.java -------------
import java.io.*;
/*************************************
* The shorter, easier version of Problem 10c.
* Here we use a method from String class
* which finds the first occurrence of
* second string in first string.
* Returning -1 if not found...
*************************************/
public class SubstringTester {
public static void main(String[] args) {
System.out.println("Please enter first string");
String first = Keyboard.inputString();
System.out.println("Please enter second string");
String second = Keyboard.inputString();
if (first.indexOf(second) != -1) {
System.out.println("Yes, " + second +
" is a substring of " +
first);
} else {
System.out.println("No, " + second +
" is not a substring of " +
first);
}
}
}

Partial preview of the text

Download Midterm Exam with Solution - Computer | CSC 120 and more Exams Computer Science in PDF only on Docsity!

Midterm Solutions (10)

Page 1 of 1

------------- Count.java ------------- import

java.io.; _/************************************** Counts from a to b. If (a <= b), it counts up from a to b* Otherwise, it counts down from a to b*************************************/_ public

class

Count {

public

static

void

main(String[] args) {

System.out.println(

"Please enter a start integer"

int

a = Keyboard.inputInteger(); System.out.println(

"Please enter an end integer"

int

b = Keyboard.inputInteger(); if (a <= b) { System.out.println(

"Counting up from "

  • a +

" to "

  • b +

while

(a <= b) {System.out.println(a);a++; } }^ else

System.out.println(

"Counting up from "

  • a +

" to "

  • b +

while

(a >= b) {System.out.println(a);a--; } } } } ------------- Factorial.java ------------- import

java.io.; _/************************************** Computes the Factorial of an inputted integer (n) n! = n * (n-1) * (n-2) * ... * 1*************************************/_ public

class

Factorial {

public

static

void

main(String[] args) {

System.out.println(

"Please enter an integer (n)."

int

n = Keyboard.inputInteger(); int

fact = 1; int

count = n; while

(count > 0) {fact *= count;count--; }System.out.println(

"The value of "

  • n +
  • fact);

} ------------- SubstringTesterLong.java ------------- import

java.io.; _/************************************** The long solution to Problem 10c This goes through the first string and compares* all the substrings of first string with second string*************************************/_ public

class

SubstringTesterLong {

public

static

void

main(String[] args) {

System.out.println(

"Please enter first string"

String first = Keyboard.inputString();System.out.println(

"Please enter second string"

String second = Keyboard.inputString(); int

i = 0; boolean

isFound =

false

while

((i <= (first.length() - second.length())) &&

(isFound ==

false

String sub = first.substring(i,i+second.length()); if (sub.equals(second)) { isFound =

true

}i++; } if (isFound) { System.out.println(

"Yes, "

  • second +

" is a substring of "

first);

}^

else

System.out.println(

"No, "

  • second + " is not a substring of "

first);

} ------------- SubstringTester.java ------------- import

java.io.; _/************************************** The shorter, easier version of Problem 10c. Here we use a method from String class* which finds the first occurrence of* second string in first string.* Returning -1 if not found...*************************************/_ public

class

SubstringTester {

public

static

void

main(String[] args) {

System.out.println(

"Please enter first string"

String first = Keyboard.inputString();System.out.println(

"Please enter second string"

String second = Keyboard.inputString(); if (first.indexOf(second) != -1) { System.out.println(

"Yes, "

  • second +

" is a substring of "

first);

}^

else

System.out.println(

"No, "

  • second + " is not a substring of "

first);