/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
*
* @author Matthew
*/
import java.util.Scanner;
import java.util.Arrays;
public class HomeAssignment {
// Question 1 [ Display a Square of Asterisks ]:
public static void squareOfAsterisks(int side) {
for (int row = 1; row <= side; row++) {
for (int column = 1; column <= side; column++) {
System.out.print("* ");
}
System.out.println();
}
}
// Question 2 [ Display a Square of Any Character ]:
public static void square(int side, char fillCharacter) {
for (int row = 1; row <= side; row++) {
for (int column = 1; column <= side; column++) {
System.out.print(fillCharacter + " ");
}
System.out.println();
}
}
// Question 3 [ Factorial ]:
public static int factorial(int n) {
System.out.println("nFactorial Table");
System.out.println("_______________");
System.out.println();
System.out.println("n t n!");
System.out.println("_______________");
System.out.println();
int ans = 1;
for (int k = 1; k <= 10; k++) {
ans = ans * k;
System.out.println(k + "!" + "t" + ans);
}
System.out.println("_______________");
int answer = 1;
for (int i = 1; i <= n; i++) {
answer = answer * i;
}
return answer;
}
// Question 4 [ Anagram ]:
public static String sort(String s) {
char[] anagrams = s.toCharArray();
Arrays.sort(anagrams);
return String.valueOf(anagrams);
}
public static boolean isAnagram(String w1, String w2) {
w1 = sort(w1.toLowerCase());
w2 = sort(w2.toLowerCase());
if (w1.length() != w2.length()) {
return false;
} else if (w1.equals(w2)) {
return true;
}
return false;
}
public static void main(String[] args) {
/*
Question 1 [ Displaying a Square of Asterisks ]:
------------------------------------------------
Write a method "squareOfAsterisks()" that displays a solid square (the same number of
rows and columns) of asterisks whose side is specified in an integer parameter "side".
Hint: Add a space after each asterisk to make the square look more realistic. For
example, if the "side" is "4", the method should display:
* * * *
* * * *
* * * *
* * * *
Call the method from the "main()" method and display a square of asterisk of size 5.
*/
System.out.println("Question 1 [ Displaying a Square of Asterisks ]:");
System.out.println("________________________________________________");
System.out.println();
squareOfAsterisks(5);
System.out.println();
/*
Question 2 [ Displaying a Square of Any Character ]:
----------------------------------------------------
Create a new method "square()" which is a modification of the method created above. It
should receive a second parameter of type "char" called "fillCharacter". From the suqare
using the "char" provided as an argument. Thus, if "side" is "5" and "fillCharacter"
is "#", the method should display:
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
Call the method from the "main()" method and display a square of 'x' of size 5.
*/
System.out.println("Question 2 [ Displaying a Square of Any Character ]:");
System.out.println("____________________________________________________");
System.out.println();
square(5, 'x');
System.out.println();
/*
Question 3 [ Factorial ]:
-------------------------
The factorial of a non-negative integer "n" is written as "n!" (pronounced "n factorial")
and is defined as follows:
n! = n . (n - 1) . (n - 2) . ... . 1 (for values of n greater than or equal to 1)
and
n! = 1 (for n = 0)
a) Write a method "factorial(int n)" that takes a non-negative integer "n" and returns
"n!" according to the rules above.
b) Call the function from the "main()" method to get 5! and print the result in the
console.
*/
System.out.println("Question 3 [ Factorial ]:");
System.out.println("_________________________");
System.out.println();
Scanner userinput = new Scanner(System.in);
System.out.print("Enter a non-negative integer [from 1 to 10]: ");
int n = userinput.nextInt();
int answer = factorial(n);
System.out.println();
System.out.println("The factorial of " + n + '!' + " is " + answer + ".");
System.out.println();
/*
Question 4 [ Anagram ]:
-----------------------
An anagram is a word that can be rearranged to another word. THe examples below are
anagrams:
-------------------
Gel and leg
Dog and god
Care and race
Leap, pale and plea
-------------------
a) Write a method "isAnagram(String w1, String w2)" that takes two words and returns
"true" if the words are anagrams to each other, and "false" otherwise.
The best way to implement this method is as follows:
i) First convert both strings to upper/lower case using the appropriate methods from
the "String" class.
ii) Then use "toCharArray()" method of the "String" class to get an array of characters
from each of the strings.
iii) Then sort the contents of both arrays using the "sort()" method of the
"java.util.Arrays" class (see API).
iv) Then use the "Arrays.equals()" method to compare the contents of the resulting arrays.
If they are equal, that means that they are anagrams.
b) Finally call the "isAnagram()" method from the "main()" method to check the following
examples in the given order.
i) Care and race
ii) Pale and plea
iii) Sister and soldier
*/
System.out.println("Question 4 [ Anagram ]:");
System.out.println("_______________________");
System.out.println();
String w1 = "Care";
String w2 = "race";
if (isAnagram(w1, w2)) {
System.out.printf("%s is an anagram of %sn", w1, w2);
} else {
System.out.printf("%s & %s are not anagrams!n", w1, w2);
}
System.out.println();
}
}
Output:
Question 1 [ Displaying a Square of Asterisks ]:
________________________________________________
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Question 2 [ Displaying a Square of Any Character ]:
____________________________________________________
x x x x x
x x x x x
x x x x x
x x x x x
x x x x x
Question 3 [ Factorial ]:
_________________________
Enter a non-negative integer [from 1 to 10]: 5
Factorial Table
_______________
n n!
_______________
1! 1
2! 2
3! 6
4! 24
5! 120
6! 720
7! 5040
8! 40320
9! 362880
10! 3628800
_______________
The factorial of 5! is 120.
Question 4 [ Anagram ]:
_______________________
Care is an anagram of race

Java Unit 1 Project

  • 1.
    /* * To changethis license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /* * * @author Matthew */ import java.util.Scanner; import java.util.Arrays; public class HomeAssignment { // Question 1 [ Display a Square of Asterisks ]: public static void squareOfAsterisks(int side) { for (int row = 1; row <= side; row++) { for (int column = 1; column <= side; column++) { System.out.print("* "); } System.out.println(); } } // Question 2 [ Display a Square of Any Character ]: public static void square(int side, char fillCharacter) { for (int row = 1; row <= side; row++) { for (int column = 1; column <= side; column++) { System.out.print(fillCharacter + " "); } System.out.println(); } } // Question 3 [ Factorial ]: public static int factorial(int n) { System.out.println("nFactorial Table"); System.out.println("_______________"); System.out.println();
  • 2.
    System.out.println("n t n!"); System.out.println("_______________"); System.out.println(); intans = 1; for (int k = 1; k <= 10; k++) { ans = ans * k; System.out.println(k + "!" + "t" + ans); } System.out.println("_______________"); int answer = 1; for (int i = 1; i <= n; i++) { answer = answer * i; } return answer; } // Question 4 [ Anagram ]: public static String sort(String s) { char[] anagrams = s.toCharArray(); Arrays.sort(anagrams); return String.valueOf(anagrams); } public static boolean isAnagram(String w1, String w2) { w1 = sort(w1.toLowerCase()); w2 = sort(w2.toLowerCase()); if (w1.length() != w2.length()) { return false; } else if (w1.equals(w2)) { return true; } return false; } public static void main(String[] args) {
  • 3.
    /* Question 1 [Displaying a Square of Asterisks ]: ------------------------------------------------ Write a method "squareOfAsterisks()" that displays a solid square (the same number of rows and columns) of asterisks whose side is specified in an integer parameter "side". Hint: Add a space after each asterisk to make the square look more realistic. For example, if the "side" is "4", the method should display: * * * * * * * * * * * * * * * * Call the method from the "main()" method and display a square of asterisk of size 5. */ System.out.println("Question 1 [ Displaying a Square of Asterisks ]:"); System.out.println("________________________________________________"); System.out.println(); squareOfAsterisks(5); System.out.println(); /* Question 2 [ Displaying a Square of Any Character ]: ---------------------------------------------------- Create a new method "square()" which is a modification of the method created above. It should receive a second parameter of type "char" called "fillCharacter". From the suqare using the "char" provided as an argument. Thus, if "side" is "5" and "fillCharacter" is "#", the method should display: # # # # # # # # # # # # # # # # # # # # # # # # # Call the method from the "main()" method and display a square of 'x' of size 5. */ System.out.println("Question 2 [ Displaying a Square of Any Character ]:"); System.out.println("____________________________________________________"); System.out.println(); square(5, 'x'); System.out.println(); /*
  • 4.
    Question 3 [Factorial ]: ------------------------- The factorial of a non-negative integer "n" is written as "n!" (pronounced "n factorial") and is defined as follows: n! = n . (n - 1) . (n - 2) . ... . 1 (for values of n greater than or equal to 1) and n! = 1 (for n = 0) a) Write a method "factorial(int n)" that takes a non-negative integer "n" and returns "n!" according to the rules above. b) Call the function from the "main()" method to get 5! and print the result in the console. */ System.out.println("Question 3 [ Factorial ]:"); System.out.println("_________________________"); System.out.println(); Scanner userinput = new Scanner(System.in); System.out.print("Enter a non-negative integer [from 1 to 10]: "); int n = userinput.nextInt(); int answer = factorial(n); System.out.println(); System.out.println("The factorial of " + n + '!' + " is " + answer + "."); System.out.println(); /* Question 4 [ Anagram ]: ----------------------- An anagram is a word that can be rearranged to another word. THe examples below are anagrams: ------------------- Gel and leg Dog and god Care and race Leap, pale and plea ------------------- a) Write a method "isAnagram(String w1, String w2)" that takes two words and returns "true" if the words are anagrams to each other, and "false" otherwise.
  • 5.
    The best wayto implement this method is as follows: i) First convert both strings to upper/lower case using the appropriate methods from the "String" class. ii) Then use "toCharArray()" method of the "String" class to get an array of characters from each of the strings. iii) Then sort the contents of both arrays using the "sort()" method of the "java.util.Arrays" class (see API). iv) Then use the "Arrays.equals()" method to compare the contents of the resulting arrays. If they are equal, that means that they are anagrams. b) Finally call the "isAnagram()" method from the "main()" method to check the following examples in the given order. i) Care and race ii) Pale and plea iii) Sister and soldier */ System.out.println("Question 4 [ Anagram ]:"); System.out.println("_______________________"); System.out.println(); String w1 = "Care"; String w2 = "race"; if (isAnagram(w1, w2)) { System.out.printf("%s is an anagram of %sn", w1, w2); } else { System.out.printf("%s & %s are not anagrams!n", w1, w2); } System.out.println(); } }
  • 6.
    Output: Question 1 [Displaying a Square of Asterisks ]: ________________________________________________ * * * * * * * * * * * * * * * * * * * * * * * * * Question 2 [ Displaying a Square of Any Character ]: ____________________________________________________ x x x x x x x x x x x x x x x x x x x x x x x x x Question 3 [ Factorial ]: _________________________ Enter a non-negative integer [from 1 to 10]: 5 Factorial Table _______________ n n! _______________ 1! 1 2! 2 3! 6 4! 24 5! 120 6! 720 7! 5040 8! 40320 9! 362880 10! 3628800 _______________ The factorial of 5! is 120. Question 4 [ Anagram ]: _______________________ Care is an anagram of race