Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Java program named Homework8 . Design a method printOddNumbers that pri

ID: 3604394 • Letter: C

Question

Create a Java program named Homework8.

Design a method printOddNumbers that prints all positive, odd numbers up to a given max.

Design a method printPrimeFactors that prints all positive prime factors of a given number.
-For example, 12 has the following factors: 1, 2, 3, 4, 6, 12. However, the only prime numbers are 2 and 3. I should expect to see 2 and 3.

Design a method printCorrectRange that accepts a positive integer number. If the number is between 1 (inclusive) and 69 (inclusive), print "You have failed the class". If the number is between 70 (inclusive) and 79 (inclusive) , print "You have barely passed". If the number is between 80 (inclusive) and 100 (inclusive), print "You have mastered the class". If the number is greater than 100, print "You're off the charts".

Note:

Answer it different than someone who already post this question

Thanks

Explanation / Answer

class Main{
public static void main(String[] args){
//calling methods
printOddNumbers(10);
printPrimeFactors(12);
printCorrectRange(100);
printCorrectRange(101);
printCorrectRange(79);
printCorrectRange(69);
}
//method to print all odd numbers
public static void printOddNumbers(int max){
for(int i=0;i<max;i++){
if(i%2==1){
System.out.print(" "+i);
}
}
System.out.println();
}
//method to check if a number is prime
public static boolean checkPrime(int n){
if(n<2){
return false;
}
else if(n==2){
return true;
}
else if(n%2==0){
return false;
}
else{
for(int i=3;i<(n/2)+1;i+=2){
if(n%i==0){
return false;
}
}
}
return true;
}
public static void printPrimeFactors(int n){
for(int i=1;i<n;i++){
// check if i is a factor of n
if(n%i==0){
//check if i is prime and print it
if(checkPrime(i)){
System.out.print(" "+i);
}
}
}
System.out.println();
}
  
//method to print correct range
public static void printCorrectRange(int n){
if(n>=1 && n<=69){
System.out.println("You have failed the class");
}
else if(n>=70 && n<=79){
System.out.println("You have barely passed");
}
else if(n>=80 && n<=100){
System.out.println("You have mastered the class");
}
else if(n>100){
System.out.println("You're off the charts");
}
}
}

/*
sample output
1 3 5 7 9
2 3
You have mastered the class
You're off the charts
You have barely passed
You have failed the class
*/