Question

In: Computer Science

2. Answer the following question (a) Write a Java program to find the number of integers...

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.

Solutions

Expert Solution

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


Related Solutions

Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
Write a Java program to find the sum of all integers between 200 to 250 which...
Write a Java program to find the sum of all integers between 200 to 250 which are divisible by 7. Sample Output: Numbers between 200 and 250, divisible by 7: 203 210 217 224 231 238 245 The sum is: 1568
Write a Java program to initialize an array with the even integers from 2 to 20...
Write a Java program to initialize an array with the even integers from 2 to 20 and print the result then pass this array to a method in order to create a new array which is the inverse of the array you passed (print the result). You cannot use a third array. Insert comments and version control in the program to document the program.
Use Java (Find the number of days in a month) Write a program that prompts the...
Use Java (Find the number of days in a month) Write a program that prompts the user to enter the month and year and displays the number of days in the month. For example, If the user entered month 2 and year 2012, the program should display that February 2012 has 29 days. If the user entered month 3 and year 2015, the program should display that March 2015 has 31 days. Sample Run 1 Enter a month in the...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of miles, and the class of journey (1,2, or 3, for first, second, and third class respectively), for a train journey. The program should then calculate and display the fare of journey based on the following criteria: Note: Use Switch...case and if...else construct First (1) Class Second (1) Class Third (3) Class First 100 mile $ 3 per mile $ 2 per mile $ 1.50...
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Write a Java program using using WHILE loop to find the sum of all integers between...
Write a Java program using using WHILE loop to find the sum of all integers between 200 to 250 which are divisible by 7. Sample Output: Numbers between 200 and 250, divisible by 7: 203 210 217 224 231 238 245 The sum is: 1568
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers as follows: Assign {35,20,-43,-10,6,7,13} -Create a one dimensional array of 7 Boolean values as follows: Assign {true,false,false,true,false,true,false} -Create a one dimensional array of 7 floating-point values as follows: Assign {12.0f,1.5f,-3.5f,-2.54f,3.4f,45.34f,22.13f} -Declare sum as integer and set it to 0. -Declare sumf as float and set it to 0.0f. -Use a for loop to go through each element of the Boolean array, and if an...
JAVA PROGRAM 1. Write a program to find the factorial value of any non-negative number entered...
JAVA PROGRAM 1. Write a program to find the factorial value of any non-negative number entered through the keyboard.(method) (Factorial of n: n! = 1*2*3*…*n, 0! = 1.) 2. Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (method) Please post a screenshot of the codes. Thanks!
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT