Java Exam (2007 Spring)

advertisement
Name and surname: ___________________________________________________________
CS 111 J AVA EX AMIN ATIO N
Date:23.05.2007
Time:12:15 -14:15
OPEN BOOK, OPEN NOTES EXAM...
Sign the attendance sheet when you submit your exam to the assistant. Make sure that you submit
all the pages and you have written your name on each paper.
GOOD LUCK!..
Q1. (50 points) Given the following Java program, determine the errors
(add/delete/change on the program itself) and then find and write the output of the
program. Note that the errors are related with method declaration and invocation.
public class jm2 {
private static String ms (String S){
int i=0,k=0,n, m=0;
String T,X, Y;
S=S.trim();
while (S.indexOf(' ',i) != -1){
i=S.indexOf(' ',i);
T=S.substring(k,i);
Y=S.substring(m,i);
n=Y.length();
X=""+n;
S=T+n+S.substring(i);
i=i+X.length()+1;
m=i;
}
T=S.substring(i);
n=T.length();
S=S+n;
return S;
}
private static int[] ma (int[] arr, int value) {
int k, n=0;
for (k=0;k<arr.length; k++)
if (arr[k]==value) n++;
int [] newarr = new int[arr.length+n];
for (k=0;k<n;k++)
newarr[k]= (int) Math.cos(Math.PI/4*k);
for (k=0;k<arr.length;k++)
newarr[n+k]=arr[k];
return newarr;
}
private static int mi (int a, int b) {
return (a+b)%100/10;
}
public static void main (String [] args){
int [] A={1,1,2,3,5,2,1};
Page 1 / 4
Name and surname: ___________________________________________________________
int k,n;
for (k=3;k>-1;k--){
A=ma(A,k);
}
for (n=0;n<A.length;n++){
System.out.print(A[n]+"*");
}
System.out.println();
String mystr=" This is your Java programming
String result=mystr.toUpperCase();
result=ms(mystr);
System.out.println(result);
examination
";
int num1=9876;
int num2=26445;
int num3;
num3=mi(num1,num2);
System.out.println(num3);
}
}
The output is:
1*0*0*0*1*0*0*0*-1*1*0*1*1*1*2*3*5*2*1*
This4 is2 your4 Java4 programming11 examination11
2
Press any key to continue...
Page 2 / 4
Name and surname: ___________________________________________________________
Q2. (50 points) The following method returns true if x is a prime number and false
otherwise:
private static boolean isPrime (int x) {
if (x==1)
return false;
for (int i=2;i<x/2+1;i++)
if (x%i==0)
return false;
return true;
}
Now, write the main method (you can assume that java.util.Scanner is imported and
the method isPrime is included in your Java program already) for a Java program
that
 Reads in two positive integers N and M (display error message if non-positive; include the
necessary Java code such that M>N),
 Finds out the exact number of prime numbers between N and M (excluding N and M),
 Creates an integer array of this exact number of elements,
 Place the prime numbers between N and M in this array, and
 Display this array
Your program should work for different (N,M) pairs as long as the user wants to continue the
program.
Sample output:
N:-9
You must input a positive number!
Do you want to continue (y/n):y
N:8
M:-7
You must input a positive number!
Do you want to continue (y/n):y
N:1
M:5
The 2 prime numbers between 1 and 5 are as follows:
{2,3}
Do you want to continue (y/n):y
N:2
M:3
There isn't any prime numbers between 2 and 3.
Do you want to continue (y/n):y
N:149
M:100
The 9 prime numbers between 100 and 149 are as follows:
{101,103,107,109,113,127,131,137,139}
Do you want to continue (y/n):n
Press any key to continue...
Page 3 / 4
Name and surname: ___________________________________________________________
public static void main(String[] args) {
Scanner oku = new Scanner (System.in);
char ans='y';
int N, M, T, i, k, pn;
int [] primes;
while (ans=='y') {
System.out.print("N:");
N=oku.nextInt();
if (N<=0)
System.out.println("You must input a positive number!");
else {
System.out.print("M:");
M=oku.nextInt();
if (M<=0)
System.out.println("You must input a positive number!");
else {
if (N>M) {
T=N; N=M; M=T;
}
pn=0;
for (k=N+1;k<M;k++)
if (isPrime(k)) pn++;
if (pn==0)
System.out.println("There isn\'t any prime numbers"
+" between "+N+" and "+M+".");
else {
primes=new int[pn];
i=0;
for (k=N+1;k<M;k++)
if (isPrime(k)) {
primes[i]=k;
i++;
}
System.out.print("The "+pn+" prime numbers "+
"between "+N+" and "+M+" are as follows:\n{");
for (k=0;k<pn;k++) {
System.out.print(primes[k]);
if (k!=(pn-1))
System.out.print(',');
else
System.out.println('}');
}
}
}
}
System.out.print("\nDo you want to continue (y/n):");
ans = oku.next().toLowerCase().charAt(0);
}
}
Page 4 / 4
Download