Task - Interfaces - Primes in a Range #17
Replies: 44 comments 1 reply
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`import java.util.Scanner; interface in1{ class testClass implements in1{ } public class InterfaceOneDriver { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
import java.util.*; |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`import java.util.Scanner; } } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`import java.util.Scanner; } }` |
Beta Was this translation helpful? Give feedback.
-
|
interface in1 { class TestClass implements in1 { } public class Interface1 { |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`import java.util.Scanner; } public class Inteface { } |
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
interface in1 {
void display(int p);
}
class Test implements in1 {
private static boolean isPrime(int n) {
if (n < 2)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
@Override
public void display(int k) {
int count = 0;
for (int i = 2; i <= k; i++) {
if (isPrime(i))
count++;
}
System.out.println(count);
}
}
public class Interface {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Test t = new Test();
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
for (int i : arr)
t.display(i);
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
interface in1 {
void display(int k);
}
class TestClass implements in1 {
private static boolean isPrime(int n) {
if (n < 2)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
@Override
public void display(int k) {
int count = 0;
for (int i = 2; i <= k; i++) {
if (isPrime(i))
count++;
}
System.out.println(count);
}
}
public class TotalPrime {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
TestClass test = new TestClass();
System.out.println("Enter an integer:");
int n = sc.nextInt();
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
for (int i : arr) {
test.display(i);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
interface in1 {
void display(int p);
}
class TestClass implements in1 {
boolean checkPrime(int n) {
if (n < 2)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
int count = 0;
public void display(int p) {
for (int i = 2; i <= p; i++) {
if (checkPrime(i)) {
count++;
}
}
System.out.println(count);
}
}
public class d17 {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
int n = obj.nextInt();
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = obj.nextInt();
}
for (int i = 0; i < n; i++) {
TestClass test = new TestClass();
test.display(a[i]);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
interface in1 {
void display(int p);
}
class TestClass implements in1 {
public boolean isPrime(int n) {
boolean prime = true;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
prime = false;
}
return prime;
}
public int count(int p) {
int count = 0;
for (int i = 2; i <= p; i++) {
if (isPrime(i))
count++;
}
return count;
}
public void display(int p) {
System.out.println(count(p));
}
}
public class discussion17 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
TestClass t1 = new TestClass();
System.out.println("How many numbers?");
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
System.out.println("Enter numbers");
int p = sc.nextInt();
t1.display(p);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
interface in1 {
int display(int p);
}
class Test implements in1 {
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public int display(int p) {
int count = 0;
for (int i = 2; i <= p; i++) {
if (isPrime(i)) {
count++;
}
}
return count;
}
}
public class TestInterface {
public static void main(String[] args) {
System.out.println("Enter n: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Test T = new Test();
for (int i = 0; i < n; i++) {
System.out.println("Enter a number");
System.out.println(T.display(sc.nextInt()));
}
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
public class task17 {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.print("Enter the no. of test Cases:");
int n = scn.nextInt();
for (int i = 0; i < n; i++) {
TestClass test = new TestClass();
System.out.println("Enter a number");
test.display(scn.nextInt());
}
}
}
interface in1 {
void display(int p);
}
class TestClass implements in1 {
public void display(int k) {
System.out.println("Prime numbers within this number are " + count(k));
}
public boolean isPrime(int n) {
boolean prime = true;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0)
prime = false;
}
return prime;
}
public int count(int k) {
int cnt = 0;
for (int i = 2; i <= k; i++) {
if (isPrime(i))
cnt++;
}
return cnt;
}
} |
Beta Was this translation helpful? Give feedback.
-
package java_programs;
interface myInteface {
void display(int p);
}
class TestClass implements myInteface {
private static boolean isPrime(int n) {
if (n < 2)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
@Override
public void display(int k) {
// TODO Auto-generated method stub
int count = 0;
for (int i = 2; i <= k; i++) {
if (isPrime(i))
count++;
}
System.out.println(count);
}
}
public class Interfaces {
public static void main(String[] args) {
TestClass test = new TestClass();
test.display(13);
test.display(19);
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
public class D17 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter no of test Cases:");
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
testClass test = new testClass();
test.display(sc.nextInt());
}
}
}
interface in1 {
void display(int p);
}
class testClass implements in1 {
public boolean isPrime(int n) {
boolean prime = true;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0)
prime = false;
}
return prime;
}
public int count(int p) {
int res = 0;
for (int i = 2; i <= p; i++) {
if (isPrime(i))
res++;
}
return res;
}
public void display(int p) {
System.out.println(count(p));
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
interface in1 {
void display(int p);
}
class testclass implements in1 {
private boolean prime(int n) {
for (int i = 2; i < n ; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public void display(int p) {
int count = 0;
for (int i = 2; i <= p - 1; i++) {
if (prime(i)) {
count++;
}
}
System.out.println(count);
}
}
public class interfaces {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
testclass test = new testclass();
test.display(sc.nextInt());
}
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
interface In1 {
void display(int k);
}
class TestClass implements In1 {
private static boolean isPrime(int n) {
if (n < 2)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public void display(int k) {
int count = 0;
for (int i = 2; i <= k; i++) {
if (isPrime(i))
count++;
}
System.out.println(count);
}
}
public class Interface {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
TestClass testclass = new TestClass();
System.out.print("Enter Number of Test Cases : ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter " + n + " Integers : ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.println();
for (int i : arr) {
testclass.display(i);
}
sc.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
interface In1 {
void display(int k);
}
class TestClass implements In1 {
private static boolean isPrime(int n) {
if (n < 2)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public void display(int k) {
int count = 0;
for (int i = 2; i <= k; i++) {
if (isPrime(i))
count++;
}
System.out.println(count);
}
}
public class Interface {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
TestClass testclass = new TestClass();
System.out.print("Enter Number of Test Cases : ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter " + n + " Integers : ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.println();
for (int i : arr) {
testclass.display(i);
}
sc.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
|
lohith R import java.util.Scanner; } } interface in1{ class TestClass implements in1{ } |
Beta Was this translation helpful? Give feedback.
-
|
Naga Nandhini import java.util.Scanner;
interface in1 {
void display(int p);
}
public class TestClass implements in1 {
private boolean isPrime(int num) {
if (num < 2) return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return false;
}
return true;
}
public void display(int k) {
int count = 0;
for (int i = 2; i <= k; i++) {
if (isPrime(i)) {
count++;
}
}
System.out.println(count);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
TestClass obj = new TestClass();
for (int i = 0; i < T; i++) {
int n = sc.nextInt();
obj.display(n);
}
sc.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Task 1 - Sayan Dey Completed import java.util.Scanner; // Interface declaration // Class implementing the interface } } |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Like a class, an interface can have methods and variables, but the methods declared in the interface are by default abstract (only method signature, no method body).
Given an interface in1 which includes a method display that takes an integer as input.
The task is to write a class TestClass which implements interface in1 and has a method
display()that takes an integer as input and prints the total number of prime numbers between2and integerk(including it).Input:
The first line will contain an integer T (number of test cases). Each test case consists of an integer n.
Output:
Print total number of prime numbers between 2 and the given number ( including them ).
Example:
Beta Was this translation helpful? Give feedback.
All reactions