COE 212 Problems Final, Spring 2013
Lebanese American University · Engineering Programming · 9 pages · Dr. George Sakr
More COE 212 files
- Final · Fall 2016Final examView
- Exam Final Exam · Fall 2015Final examView
- Final Exam · Spring 2013Final examView
- Final · Spring 2013Final examView
- Final · Spring 2013Final examView
- Final + Solutions · Spring 2012Final exam · with solutionView
- Final · Fall 2012Final examView
- Final Exam · Fall 2012Final examView
Show the text of this final exam
Primes.java
import java.util.Scanner;
public class Primes {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("Enter a number: ");
int n=s.nextInt();
System.out.println("The first "+n + " prime numbers are");
int i=2;
int cnt=0;
while(cnt<n)
{
if (isprime(i))
{
System.out.print(i+ " ");
cnt++;
if (cnt%10==0) //return to a new line after every 10 numbers
System.out.println();
}
i++;
}
}
public static boolean isprime(int x)
{
for (int i=2;i<=Math.sqrt(x);i++)
if (x%i==0)
return false;
return true;
}
}
Page 1
PalindromePrime.java
import java.util.Scanner;
public class PalindromePrime {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("Enter a number: ");
int n=s.nextInt();
System.out.println("The first "+n + " palindrome prime numbers are");
int i=2;
int cnt=0;
while(cnt<n)
{
if (isprime(i)&&isPalindrome(i))
{
System.out.print(i+ " ");
cnt++;
if (cnt%10==0) //return to a new line after every 10 numbers
System.out.println();
}
i++;
}
}
public static boolean isprime(int x)
{
for (int i=2;i<=Math.sqrt(x);i++)
if (x%i==0)
return false;
return true;
}
public static boolean isPalindrome(int n)
{
return (n==reverse(n));
}
public static int reverse(int n)
{
int rev=0;
int lastdigit;
while(n>0)
{
lastdigit=n%10;
rev=10*rev+lastdigit;
n/=10;
}
return rev;
}
}
Page 1
Vowels.java
import java.util.Scanner;
public class Vowels {
/**
* @param args
*/
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("Enter a sentence: ");
String l=s.nextLine();
l=l.toLowerCase();
int cnt=0;
for (int i=0;i<l.length();i++)
if (isVowel(l.charAt(i)))
cnt++;
System.out.println("The number of vowels is: "+cnt);
}
public static boolean isVowel(char c)
{
String vowels="aeiou";
return (vowels.indexOf(c)!=‐1);
}
}
Page 1
Password.java
import java.util.Scanner;
public class Password {
/**
* @param args
*/
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("Enter a Password: ");
String pass=s.nextLine();
String brokenRules="";
if (validLength(pass)&&!isAllAlphabetic(pass)&&!isAllDigit(pass)&&!is99(pass))
System.out.println("Valid Password");
else
{
if (!validLength(pass))
brokenRules+="1,";
if (isAllAlphabetic(pass))
brokenRules+="2,";
if (isAllDigit(pass))
brokenRules+="3,";
if (is99(pass))
brokenRules+="4,";
brokenRules=brokenRules.substring(0,brokenRules.length()‐1);
System.out.println("not a valid password (rule(s): "+brokenRules+" violated)");
}
}
public static boolean validLength(String s)
{
return ((s.length()>=6)&&(s.length()<=15));
}
public static boolean isAllAlphabetic(String s)
{
for (int i=0;i<s.length();i++)
{
char c=s.charAt(i);
boolean isAlphabetic='A'<=c&& c<='Z'||'a'<=c&&c<='z';
if (!isAlphabetic)
return false;
}
return true;
}
public static boolean isAllDigit(String s)
{
for (int i=0;i<s.length();i++)
{
char c=s.charAt(i);
boolean isDigit='0'<=c&&c<='9';
if (!isDigit)
return false;
}
return true;
}
public static boolean is99(String s)
{
if (isAllAlphabetic(s.substring(0,s.length()‐2)))
{
Page 1
Password.java
if (s.substring(s.length()‐2, s.length()).equals("99"))
return true;
else
return false;
}
else
return false;
}
}
Page 2
DigitRange.java
import java.io.*;
import java.util.Scanner;
public class DigitRange {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Scanner s=new Scanner(new File("Digit.txt"));
FileWriter fw=new FileWriter("DigitRange.out");
PrintWriter pw=new PrintWriter(fw);
while (s.hasNext())
{
int i=s.nextInt();
int r=getRange(i);
pw.println("The digit range of \""+i+"\" is "+r+".");
}
pw.close();
}
public static int getRange(int n)
{
int min=n%10;
int max=min;
n/=10;
while(n>0)
{
int lastDigit=n%10;
if (lastDigit>max)
max=lastDigit;
if (lastDigit<min)
min=lastDigit;
n/=10;
}
return max‐min+1;
}
}
Page 1
Programming Problems Chapter 5
Problem 1 (Primes)
Write a program that reads an integer N from the keyboard, and displays the first N prime
numbers. You should display 10 numbers per row. It is advisable to split your program into 2
methods: main method and isprime method.
Sample output
Enter a number: 25
The first 25 prime numbers are
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97
Problem 2 (Palindrome Prime)
Write a program that reads an integer N from the keyboard, and displays the first N prime AND
palindrome numbers. (To display a number it must be both prime and palindrome). You should
display 10 numbers per row. It is advisable to split your program into 4 methods: main method,
ispalindrome method, reverse method and isprime method.
Sample output
Enter a number: 25
The first 25 prime numbers are
2 3 5 7 11 101 131 151 181 191
313 353 373 383 727 757 787 797 919 929
10301 10501 10601 11311 11411
Problem 3
Modify problem 2 so that the output is written to the file “result.txt” and not on the screen.
Problem 4 (Vowel Count)
Write a program that reads a sentence from the keyboard and counts how many vowels
(Uppercase or Lowercase) are found in this sentence.
Sample output
Enter a sentence: Hello World How ARE YOU TODAY???
The number of vowels is: 10
Problem 5 (Password Checker)
A password is a sequence of characters that can be used for authentication purposes. Passwords are
widely used in computer systems and networks; and they are often used to authenticate the identity of
an automated data processing (ADP) system user.
A valid password is a personal password that will authenticate the identity of an individual when
presented to a password system.
In this problem, you are asked to check the validity of passwords through the development of some
routines that will play the role of a password system. Basic factors shall be considered in the design,
implementation, and use of a password system used to authenticate the identity of a person. In this
problem, we only focus on two main factors, comprising 4 distinct rules:
Length Range Factor: Length Range is the set of acceptable lengths of passwords, expressed as a
minimum length through a maximum length.
o Rule 1: acceptable number of characters in a valid password must be more than 6 and less
than 15.
Composition Factor: Composition is the set of acceptable characters which may be used in a valid
password.
o Rule 2: valid password should not be only alphabetic characters
o Rule 3: valid password should not be only numeric characters
o Rule 4: valid password should not be a series of alphabetic characters ending with ‘99’
Passwords are alphanumeric characters with the ASCI code in the following ranges:
48 – 57 for numeric values (0, 1, …, 9)
65 – 90 : upper case alphabetic characters (A, B, …, Z)
97 – 122: lower case alphabetic characters (a, b, …, z)
Write a program, PasswordChecker, which asks the user to enter a password and then checks its validity.
The program should print “valid password” if the password follows the rules presented above. If the
password violates any of the rules, the program should print “not a valid password” along with the rules
that are violated. The program should keep repeating until the user enters a valid password.
Sample Run
Please enter your password: COE
not a valid password (rule(s): 1,2 violated)
Please enter your password: a199
not a valid password (rule(s): 1 violated)
Please enter your password: 99
not a valid password (rule(s): 1,3 violated)
Please enter your password: COE99
not a valid password (rule(s): 1,4 violated)
Please enter your password: COE212
valid password
Problem 6 (Digit Range)
Write a method named digitRange that accepts an integer as a parameter and returns the range of
values of its digits. The range is defined as 1 more than the difference between the largest and smallest
digit value. For example, the call of digitRange(68437) returns 6 because the largest digit value is 8 and
the smallest is 3, so 8 - 3 + 1 = 6. If the number contains only one digit, return 1. You should solve this
problem without using strings.
Input (digitin.txt)
The input file consists of a series of integers one per line. All integers will be greater than 0. The main
method should obtain numbers from the input file, invoke the digitRange method for each number, and
then write the results to the output file.
Output
For each input integer, output the digit range of that integer exactly as it appears in the sample run
below with one line of output for each line of input.
Sample Input File: DigitRangeIn.txt
68437
287649
8741
909090
Sample Output File: DigitRangeOut.txt
The digit range of "68437" is 6.
The digit range of "287649" is 8.
The digit range of "8741" is 8.
The digit range of "909090" is 10.