The language is introduction to computer science, JAVA Method writing, you are o
ID: 3699119 • Letter: T
Question
The language is introduction to computer science, JAVA
Method writing, you are only required to implement methods.
1. Implement a method called isPrime which takes an integer parameter, say num, and returns true if number is prime, false otherwise.
Example:
isPrime(11) returns true
isPrime(15) returns false
---------------------------------------
2. Implement a method called discount which takes two parameters: double price and int discount. Method computes discounted price using price and discount and returns discounted price.
Example:
discount (1000.00,10) returns 900.00 //10% discount of original price
discount (850.50,15) returns 722.92 //15% discount of original price
Explanation / Answer
Methods.java
public class Methods {
public static void main(String[] args) {
System.out.println(isPrime(11));
System.out.println(isPrime(15));
System.out.println(discount (1000.00,10));
System.out.println(discount (850.50,15));
}
public static double discount (double price, double dis) {
return price-(price*dis)/100;
}
public static boolean isPrime(int num) {
for (int f = 2; f <= num / 2; f++) {
if (num % f == 0) {
return false;
}
}
return true;
}
}
Output:
true
false
900.0
722.925
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.