In: Computer Science
2. Answer the following question
(a) Write a Java program to find the number of integers within
the range of two specified numbers and that are divisible by
another number. For example, x = 5, y=20 and p =3, find the number
of integers within the range [x, y] and that are divisible by
p.
(b) Write explicitly on the following OOP concepts:
i. Encapsulation
ii. Inheritance
iii. Polymorphism
(c) Develop a Java application that computes the two roots of a
first order polynomial equation and create an object from this
class inside another class. Note! both classes must be domiciled
inside separate packages with names of your choice.
a.Write a Java program to find the number of integers within the range of two specified numbers and that are divisible by another number.
Solution: Create one class with name RangeXY.java and paste the code given below
import java.util.Scanner;
public class RangeXY {
public static void main(String[] args) {
printRange();
}
//find the number of integers within the range [x, y] and that
are divisible by p.
static void printRange() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Value of x :");
int x = sc.nextInt();
System.out.println("Enter Value of y :");
int y = sc.nextInt();
System.out.println("Enter Value of p :");
int p = sc.nextInt();
System.out.println("integers within the range [x, y] and that are
divisible by p ");
for (int i = x; i <= y; i++) {
//check if number is divisible by p
if (i % p == 0) {
System.out.print(i + " ");
}
}
System.out.println("");
}
}
//output
Enter Value of x :
25
Enter Value of y :
100
Enter Value of p :
30
integers within the range [x, y] and that are divisible by p
30 60 90
b. Write explicitly on the following OOP concepts:
i. Encapsulation
ii. Inheritance
iii. Polymorphism
c.
//create one package name and create one class name with FindRoots.java. In my case package name is javaapplication5
package javaapplication5;
public class FindRoots {
public void calculateRoots(double a, double b, double c) {
// Prints roots of equation ax * 2 + bx + x
double d, sqrt;
if (a == 0) {
System.out.println("It is not in polynomial form");
return;
}
d = Math.pow(b, 2.0) - 4 * a * c;
sqrt = Math.sqrt(Math.abs(d));
//If b*b > 4*a*c, then roots are real and different.
if (d > 0) {
System.out.println(
"Roots of given equation are real and different \n");
System.out.println(
(double) (-b + sqrt) / (2 * a) + "\n"
+ (double) (-b - sqrt) / (2 * a));
}
//If b*b == 4*a*c, then roots are real and both roots are
same.
else if (d == 0) {
System.out.println(
"Roots of given equation are real and same \n");
System.out.println(-(double) b / (2 * a) + "\n"
+ -(double) b / (2 * a));
}
// If b*b < 4*a*c, then roots are complex (not real).
else
{
System.out.println("Roots of given equation are complex \n");
System.out.println(-(double) b / (2 * a) + " + i"
+ sqrt + "\n"
+ -(double) b / (2 * a)
+ " - i" + sqrt);
}
}
}
//create another package and class. In my case package name is javapplication6
package javapackage6;
import java.util.Scanner;
import javaapplication5.FindRoots;
public class FindRootsDemo {
public static void main(String[] args) {
double a,b,c;
Scanner in=new Scanner(System.in);
System.out.println("Enter the values of a b and c");
a=in.nextDouble();
b=in.nextDouble();
c=in.nextDouble();
//create object of FindRoots.java from another package
FindRoots findRoots=new FindRoots();
findRoots.calculateRoots(a, b, c);
}
}
//output
Enter the values of a b and c
4
-6
2
Roots of given equation are real and different
1.0
0.5