CIS 17: Java Programming
Project B
Objective:
To exercise selected operators from C/C++ and to determine their behaviors in the Java environment.
Strings and Operators
- Write a static method, compare(), that takes three String arguments and uses all the relational and equivalence operators (< <= > >= == !=) to compare the second and third Strings and print the results using the first String as an identifying label for the comparison.
- Also perform the equals() test as in:
// ...
boolean result;
String string1, string2;
// ...
result = string1.equals(string2);
// ...
- Use the following definition for main():
public static void main(String[] args) {
compare("\"Hello\" vs \"Hello\"", "Hello", "Hello");
String s = new String("Hello");
compare("\"Hello\" vs s", "Hello", s);
compare("\"Hello\" vs \"Goodbye\"", "Hello", "Goodbye");
}
- If any comparisons fail to compile, comment them out, do not remove them.
- Name your main() class CompareStrings.
Using the Modulus Operator and the for Statement
- Write a program that uses two nested for loops and the modulus operator (%) to detect and print prime numbers (integers that are not evenly divisible by any other numbers except for themselves and 1).
- The program should print all primes from 1 to 100, inclusive.
- Name your main() class Primes.
Submit your work via the CATE form for Project B.
Legend: method/function keyword literal
2007/03/03