SlideShare a Scribd company logo
/*
* 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

More Related Content

PDF
Star pattern programs in java Print Star pattern in java and print triangle ...
DOCX
DOCX
Raiz cuadrada
PDF
AM2 MATLAB
PDF
Print Star pattern in java and print triangle of stars in java
PPTX
TXT
123
DOCX
programming for Calculator in java
Star pattern programs in java Print Star pattern in java and print triangle ...
Raiz cuadrada
AM2 MATLAB
Print Star pattern in java and print triangle of stars in java
123
programming for Calculator in java

What's hot (16)

PPTX
Dev Concepts: Data Structures and Algorithms
DOCX
Import java
DOCX
Problemas secuenciales.
PDF
Anjalisoorej imca133 assignment
PPT
PPTX
JPC#8 Introduction to Java Programming
PPTX
Collection and framework
PPTX
java-Unit4 chap2- awt controls and layout managers of applet
PPT
Data Structures by Maneesh Boddu
PPTX
Java simple programs
PPT
Engineering lecture ppt by venay magen
PPT
25 awt
DOCX
DSA- Unit III- STACK AND QUEUE
PPSX
Dr. Rajeshree Khande :Introduction to Java AWT
PPTX
queue & its applications
PPTX
Java calculator
Dev Concepts: Data Structures and Algorithms
Import java
Problemas secuenciales.
Anjalisoorej imca133 assignment
JPC#8 Introduction to Java Programming
Collection and framework
java-Unit4 chap2- awt controls and layout managers of applet
Data Structures by Maneesh Boddu
Java simple programs
Engineering lecture ppt by venay magen
25 awt
DSA- Unit III- STACK AND QUEUE
Dr. Rajeshree Khande :Introduction to Java AWT
queue & its applications
Java calculator
Ad

Similar to Java Unit 1 Project (20)

PDF
Best Java Problems and Solutions
DOCX
QA Auotmation Java programs,theory
PDF
merged_document_3
PDF
11-ch04-3-strings.pdf
PPTX
Parenthesization.pptx
PDF
Java Simple Programs
PDF
Simple Java Program for beginner with easy method.pdf
PPTX
Programing with java for begniers .pptx
PDF
81818088 isc-class-xii-computer-science-project-java-programs
PDF
Java_Programming_by_Example_6th_Edition.pdf
PPTX
OOPS with Java experiment related to fundamentals
PPTX
Virtusa questions placement preparation guide
PDF
JAVA PRACTICE QUESTIONS v1.4.pdf
PDF
Text adventure
PDF
JAVA DUMP SET.pdf
PPTX
บทที่ 3 พื้นฐานภาษา Java
PDF
Review Questions for Exam 10182016 1. public class .pdf
DOCX
Write a class of static methods that accomplish the following tasks. .docx
PDF
Sam wd programs
PPTX
Lab01.pptx
Best Java Problems and Solutions
QA Auotmation Java programs,theory
merged_document_3
11-ch04-3-strings.pdf
Parenthesization.pptx
Java Simple Programs
Simple Java Program for beginner with easy method.pdf
Programing with java for begniers .pptx
81818088 isc-class-xii-computer-science-project-java-programs
Java_Programming_by_Example_6th_Edition.pdf
OOPS with Java experiment related to fundamentals
Virtusa questions placement preparation guide
JAVA PRACTICE QUESTIONS v1.4.pdf
Text adventure
JAVA DUMP SET.pdf
บทที่ 3 พื้นฐานภาษา Java
Review Questions for Exam 10182016 1. public class .pdf
Write a class of static methods that accomplish the following tasks. .docx
Sam wd programs
Lab01.pptx
Ad

Java Unit 1 Project

  • 1. /* * 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();
  • 2. 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) {
  • 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 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(); } }
  • 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