I have written below code in which there is one Parent class and two child classes. The child classes extends the parent class and override the function filter from the parent class. However when I try to execute , the objects return null. Below are two version of my program. The first version is showing incorrect output and second version is showing correct output. Please let me know why my first version is not working. Where am I going wrong?
First Version
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Parent {
public static int startElement;
public static int endElement;
public static String filter() {return null;}
// Parent(int a, int b)
// {
// startElement = a;
// endElement = b;
// }
public void takeInput(int start, int end)
{
startElement = start;
endElement = end;
}
}
class ChildOne extends Parent {
static boolean isPrime(int num) {
int i = 2;
boolean flag = true;
if(num == 0 || num == 1){
flag = false;
}
else {
while(i <= num/2 && num != 2){
int rem = num%i;
if(rem == 0 ) {
flag = false;
break;
}
i++;
}
}
return flag;
}
public static String filter()
{
int x, y, flg;
List<String> result = new ArrayList<>();
int i = 0;
for (x = startElement; x <= endElement; x++)
{
boolean prime = isPrime(x);
if(prime)
{
result.add(Integer.toString(x));
i++;
}
}
String final_result = String.join(" ", result);
return final_result;
}
}
class ChildTwo extends Parent {
public static int isHappyNumber(int number)
{
int rem, sum,x;
x = number;
rem = 0;
sum = 0;
while (x > 0)
{
rem = x % 10;
sum = sum + (rem * rem);
x = x / 10;
}
return sum;
}
public static String filter()
{
int x,y;
List<String> happy_num = new ArrayList<>();
for (x = startElement; x <= endElement; x++)
{
int result = x;
while(result != 1 && result != 4)
{
result = isHappyNumber(result);
}
if (result == 1)
{
happy_num.add(Integer.toString(x));
}
}
String final_happy = String.join(" ",happy_num);
return final_happy;
}
}
public class Solution {
public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Scanner readInput = new Scanner(System.in);
int inputOne = readInput.nextInt();
int inputTwo = readInput.nextInt();
Parent p1 = new Parent();
p1.takeInput(inputOne, inputTwo);
//readInput.close();
Parent p2 = new ChildOne();
String prime = p2.filter();
System.out.println(prime);
Parent p3 = new ChildTwo();
String happy = p3.filter();
System.out.println(happy);
}
}
Output of First Version
1
100
null
null
Second Version
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Parent {
public static int startElement;
public static int endElement;
public static String filter() {return null;}
// Parent(int a, int b)
// {
// startElement = a;
// endElement = b;
// }
public void takeInput(int start, int end)
{
startElement = start;
endElement = end;
}
}
class ChildOne extends Parent {
static boolean isPrime(int num) {
int i = 2;
boolean flag = true;
if(num == 0 || num == 1){
flag = false;
}
else {
while(i <= num/2 && num != 2){
int rem = num%i;
if(rem == 0 ) {
flag = false;
break;
}
i++;
}
}
return flag;
}
public static String filter()
{
int x, y, flg;
List<String> result = new ArrayList<>();
int i = 0;
for (x = startElement; x <= endElement; x++)
{
boolean prime = isPrime(x);
if(prime)
{
result.add(Integer.toString(x));
i++;
}
}
String final_result = String.join(" ", result);
return final_result;
}
}
class ChildTwo extends Parent {
public static int isHappyNumber(int number)
{
int rem, sum,x;
x = number;
rem = 0;
sum = 0;
while (x > 0)
{
rem = x % 10;
sum = sum + (rem * rem);
x = x / 10;
}
return sum;
}
public static String filter()
{
int x,y;
List<String> happy_num = new ArrayList<>();
for (x = startElement; x <= endElement; x++)
{
int result = x;
while(result != 1 && result != 4)
{
result = isHappyNumber(result);
}
if (result == 1)
{
happy_num.add(Integer.toString(x));
}
}
String final_happy = String.join(" ",happy_num);
return final_happy;
}
}
public class Solution {
public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Scanner readInput = new Scanner(System.in);
int inputOne = readInput.nextInt();
int inputTwo = readInput.nextInt();
Parent p1 = new Parent();
p1.takeInput(inputOne, inputTwo);
//readInput.close();
ChildOne p2 = new ChildOne();
String prime = p2.filter();
System.out.println(prime);
ChildTwo p3 = new ChildTwo();
String happy = p3.filter();
System.out.println(happy);
}
}
Second Version Output
1
100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100
From what can I observer is that I am using the polymorphism in my first version , but I am not getting why it doesn't work. Any help appreciated.