C# Lab Exercise

advertisement
Computer Programming-2 (CS2300)
Java Lab Exercise #12
Strings & Characters
Strings & Characters
Student Name & Number
Instructor:
Lab Session-#12
Evaluation:
Date:
Q1. Write the output of the following Java code segment
Program
Output
String s1 = "Welcome to Java";
String s2 = new String("Welcome to Java");
String s3 = "Welcome to Java";
StringBuilder sb = new StringBuilder();
if
if
if
if
if
(s1==s2) System.out.println("s1 & s2 have the same address");
(s1==s3) System.out.println("s1 & s3 have the same address");
(s1.equals(s2)) System.out.println("s1 & s2 have the same content");
(s1.equals(s3)) System.out.println("s1 & s3 have the same content");
("Ahmad".compareTo("Yousef")>0)
System.out.println("\"Ahmad\" is greater than \"Yousef\"");
s1 = s2.substring(11);
s2 = s2.substring(0, 11).concat("c++");
System.out.println(s1.toUpperCase());
System.out.println(s2.length());
s1 = s1.replaceFirst("a", "or");
s1 = s1.replaceAll("v", "dan");
System.out.println(s1);
System.out.println(s3.indexOf('e', 4));
String[] tokens = s2.split(" ", 0);
for (int i = 0; i < tokens.length; i++)
System.out.print(tokens[i] + " ");
sb.append("Good day");
sb.delete(5, 8);
sb.setCharAt(0, 'r');
System.out.println("\n" + sb.reverse());
1
s1 & s3
s1 & s2
s1 & s3
JAVA
14
Jordana
6
Welcome
door
have the same address
have the same content
have the same content
to c++
Computer Programming-2 (CS2300)
Strings & Characters
Lab Session-#12
Q2. Write a program – Counting Each Letter in a String (Characters)
Write a Java program that prompts the user to enter a string and counts the number of occurrences
of each letter in the string regardless of case
Example:
Enter a string: abababx
a appears 3 times
b appears 3 times
x appears 1 time
Solution:
import java.util.Scanner;
public class CountEachLetter {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
String s = input.nextLine();
int[] counts = countLetters(s.toLowerCase());
// Display results
for (int i = 0; i < counts.length; i++) {
if (counts[i] != 0)
System.out.println((char)('a' + i) + " appears " +
counts[i] + ((counts[i] == 1) ? " time" : " times"));
}
}
/** Count each letter in the string */
public static int[] countLetters(String s) {
int[] counts = new int[26];
for (int i = 0; i < s.length(); i++) {
if(Character.isLetter(s.charAt(i)))
counts[s.charAt(i) - 'a']++;
}
return counts;
}
}
2
Computer Programming-2 (CS2300)
Strings & Characters
Lab Session-#12
Q3. Write a program - Calculator (Strings - Tokenization)
Write a Java program that will perform the basic binary operations on integers. The program
receives three parameters: an operator and two integers.
Example:
input: 2 + 5
output: 7
input:
12 / 3
output: 4
Solution:
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Enter operand operator operand : ");
String st = input.nextLine();
String[] s = st.split(" ");
int result=0;
switch (s[1].charAt(0))
{
case '+': result= Integer.parseInt(s[0])
break;
case '-': result= Integer.parseInt(s[0])
break;
case '*': result= Integer.parseInt(s[0])
break;
case '/': result= Integer.parseInt(s[0])
break;
}
System.out.println(result);
}
}
3
+ Integer.parseInt(s[2]);
- Integer.parseInt(s[2]);
* Integer.parseInt(s[2]);
/ Integer.parseInt(s[2]);
Download