Current Topic: 1.2.6.1. Java samples - working with numbers
You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
Java samples - working with numbers
Re-type these samples and try to define another similar problem.
Then solve the problem by creating another class with the reusable method and the main method for testing.
/** Divisor
* Create a reusable method with 2 int parameters
* the method should return the greatest common divisor of the two int numbers
* the greatest common divisor is the largest positive integer that can fully divide each of the integers.
* for example 12 and 30:
* 12 can be divided by 1,2,3,4,6,12
* 30 can be divided by 1,2,4,5,6,10,15,30
* the greatest common divisor is 6.
* The name of the reusable method is getGreatestCommonDivisor ()
* The main method is to call and test that reusable method.
*/
public class Divisor {
public static void main(String[] args) {
int result = getGreatestCommonDivisor(81,153);
System.out.println(result);
}
public static int getGreatestCommonDivisor (int first, int second) {
if ( first <10 || second < 10) {
return -1;
}
int divider = 1;
for (int i = 2; (i <= first || i <= second) ; i++ ) {
if ((first % i ==0) &&(second % i==0)) {
divider= i;
}
}
return divider;
}
}
/** InvalidValueChecker
* Write a reusable method with one int parameter.
* The method should check the argument value and if the number < 1 the method should print
"Invalid Value"
* Otherwise, the method should print all factors of the number.
* A factor of a number is an integer which divides that number wholly.
* For example, for 6 the factors are 1,2,3,6
*/
public class InvalidValueChecker {
public static void main(String[] args) {
printFactors(10);
}
public static void printFactors (int number) {
if (number <1) {
System.out.println("Invalid Value");
}
for (int i=1; i <= number; i++) {
if (number % i ==0) {
System.out.println(i);
}
}
}
}
Was it clear so far?
Assignments Assignments:
1. Create two similar problems
2. Create a Java class in Eclipse with the name to reflect the problem in the class name.
3. Type the problem in a class header.
4. In the header before the reusable method briefly state your plan for solution
5. Type and test the solution
6. Email the new classes to dean@ituniversity.us
<br/>/** Divisor
<br/>* Create a reusable method with 2 int parameters
<br/>* the method should return the greatest common divisor of the two int numbers
<br/>* the greatest common divisor is the largest positive integer that can fully divide each of the integers.
<br/>* for example 12 and 30:
<br/>* 12 can be divided by 1,2,3,4,6,12
<br/>* 30 can be divided by 1,2,4,5,6,10,15,30
<br/>* the greatest common divisor is 6.
<br/>* The name of the reusable method is getGreatestCommonDivisor ()
<br/>* The main method is to call and test that reusable method.
<br/>*/
<br/>
<br/>public class Divisor {
<br/>
<br/> public static void main(String[] args) {
<br/> int result = getGreatestCommonDivisor(81,153);
<br/> System.out.println(result);
<br/> }
<br/> public static int getGreatestCommonDivisor (int first, int second) {
<br/> if ( first <10 || second < 10) {
<br/> return -1;
<br/> }
<br/> int divider = 1;
<br/> for (int i = 2; (i <= first || i <= second) ; i++ ) {
<br/> if ((first % i ==0) &&(second % i==0)) {
<br/> divider= i;
<br/> }
<br/> }
<br/> return divider;
<br/> }
<br/>}
<br/>
<br/>/** InvalidValueChecker
<br/>* Write a reusable method with one int parameter.
<br/>* The method should check the argument value and if the number < 1 the method should print
<br/>"Invalid Value"
<br/>* Otherwise, the method should print all factors of the number.
<br/>* A factor of a number is an integer which divides that number wholly.
<br/>* For example, for 6 the factors are 1,2,3,6
<br/>*/
<br/>
<br/>public class InvalidValueChecker {
<br/>
<br/> public static void main(String[] args) {
<br/> printFactors(10);
<br/> }
<br/>
<br/> public static void printFactors (int number) {
<br/> if (number <1) {
<br/> System.out.println("Invalid Value");
<br/> }
<br/> for (int i=1; i <= number; i++) {
<br/> if (number % i ==0) {
<br/>
<br/> System.out.println(i);
<br/>
<br/> }
<br/> }
<br/> }
<br/>
<br/>}
<br/>
Was it clear so far?
onclick="window.location.href='/BASE/jsp/demo.jsp?checkFlavor=itsp&issueID=486&intro=general&group=aitu&ur=f'">
Assignments Assignments:
1. Create two similar problems
2. Create a Java class in Eclipse with the name to reflect the problem in the class name.
3. Type the problem in a class header.
4. In the header before the reusable method briefly state your plan for solution
5. Type and test the solution
6. Email the new classes to dean@ituniversity.us