Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

//////////////////////////////polymorphism//////////////////////////////

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {


public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n= sc.nextInt();
int p=m;
int q=n;
for(int i = m ; i <= n ; i++)
{
//logic for checking number is prime or not
int count = 0;
for(int j = 1 ; j <= i ; j++)
{
if(i % j == 0)
count = count+1;
}
if(count == 2)
System.out.print(i+" ");
}

int sum=0,r,i,n1;
System.out.println("");
for(i=m;i<=n;i++)
{
n1=i;
sum=0;
//checking for happy number
while(sum!=1 && sum!=4)
{
sum=0;
while(n1>0)
{
r=n1%10;
sum+=(r*r);
n1=n1/10;
}
n1=sum;
}
//printing the result
if(sum==1)
System.out.print(i+" ");
}

}
}
//////////////////////Multiple Inheritence////////////////////////
import java.util.Arrays;
import java.util.Scanner;
import java.util.*;

interface HockeyTeam{
public int calculateHockeyScore();
public int findHighestGoalByIndividualInHockey();
}
interface FootballTeam{
public int calculateFootballScore();
public int findHighestGoalByIndividualInFootball();
}
class Sport implements HockeyTeam,FootballTeam{
int []hockeyPlayers = new int[11];
int []FootballPlayers = new int[11];
//Scanner sc = new Scanner(System.in);
Sport(int[] paramHockeyPlayers,int[] paramFootballPlayers){
for(int i=0;i<hockeyPlayers.length;i++){
hockeyPlayers[i]=paramHockeyPlayers[i];
}
for(int k=0;k<FootballPlayers.length;k++){
FootballPlayers[k]=paramFootballPlayers[k];
}
}

public int calculateHockeyScore(){


int sum = 0;
//System.out.println("Enter the elements of the array one by one ");

for(int i=0; i<hockeyPlayers.length; i++){


//hockeyPlayers[i] = sc.nextInt();
sum = sum + hockeyPlayers[i];
}

return sum;
}
public int calculateFootballScore(){
int sum = 0;
//System.out.println("Enter the elements of the array one by one ");

for(int i=0; i<FootballPlayers.length; i++){


//FootballPlayers[i] = sc.nextInt();
sum = sum + FootballPlayers[i];
}

return sum;
}

public int findHighestGoalByIndividualInHockey(){


int max = hockeyPlayers[0];
//Loop through the array
for (int i = 0; i < hockeyPlayers.length; i++) {
//Compare elements of array with max
if(hockeyPlayers[i] > max)
max = hockeyPlayers[i];
}
return max;
}

public int findHighestGoalByIndividualInFootball(){


int max = FootballPlayers[0];
//Loop through the array
for (int i = 0; i < FootballPlayers.length; i++) {
//Compare elements of array with max
if(FootballPlayers[i] > max)
max = FootballPlayers[i];
}
return max;
}
}
ublic class Solution{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int[] hockeyPlayers = new int[11];
int[] footballPlayers = new int[11];

for(int i = 0; i < 11; i++)


{
hockeyPlayers[i] = sc.nextInt();
}

for(int i = 0; i < 11; i++)


{
footballPlayers[i] = sc.nextInt();
}

Sport s = new Sport(hockeyPlayers, footballPlayers);


try{
HockeyTeam.class.getMethod("calculateHockeyScore");
HockeyTeam.class.getMethod("findHighestGoalByIndividualInHockey");
FootballTeam.class.getMethod("calculateFootballScore");
FootballTeam.class.getMethod("findHighestGoalByIndividualInFootball");

if(s instanceof HockeyTeam && s instanceof FootballTeam)


{
System.out.println(s.calculateHockeyScore());
System.out.println(s.calculateFootballScore());
System.out.println(s.findHighestGoalByIndividualInHockey());
System.out.println(s.findHighestGoalByIndividualInFootball());
}
}
catch (NoSuchMethodException ex)
{
System.out.println("No such function is exits");
}
}
}

/////////////////////multi threading///////////////////////
import java.util.Scanner;

class Task1 extends Thread {


static int a = 0;
static int beg = 0;

public void run(){


for(int i=beg;i < a;i++)
Solution.threadArray[i] = i;
}
}

class Task2 extends Thread {


static int a = 0;
static int beg = 0;

public void run(){


for(int i=beg; i< beg + a;i++)
Solution.threadArray[i] = i;
}
}

class Task3 extends Thread{


static int a = 0;
static int beg = 0;
public void run(){
for(int i=beg;i< a + beg;i++)
Solution.threadArray[i] = i;
}
}

public class Solution


{
public static final int[] threadArray = new int[300];

public static volatile String i = 0+"";

public boolean test() throws InterruptedException


{
Task1 task1 = new Task1();
Task2 task2 = new Task2();
Task3 task3 = new Task3();
Thread task2Thread = new Thread(task2);
Thread task3Thread = new Thread(task3);
task1.start();
task2Thread.start();
task1.join();
task2Thread.join();
task3Thread.start();
int first = Task1.a+Task2.a;
int containsSecondThread = Task1.a;
String oneAndTwo = "";
String sizeOfTask1 = "";
for(int i=0;i<first;i++)
{
oneAndTwo += threadArray[i]+" ";
}
for(int i=0;i<containsSecondThread;i++)
{
sizeOfTask1 += threadArray[i]+" ";
}
int begOfTask3 = Task3.beg;
String checkingString = "";
for(int i=begOfTask3;i<threadArray.length;i++)
{
checkingString += i + " ";
}
String task3String = "";
for(int j = begOfTask3;j<threadArray.length;j++)
{
task3String += threadArray[j]+" ";
}
if((!oneAndTwo.contains(begOfTask3+"") &&
sizeOfTask1.contains(Task2.beg+"")) || task3String.equals(checkingString))
{
return true;
}
return false;
}

public static void main(String[] args) throws InterruptedException


{
Scanner sc= new Scanner(System.in);

Solution solution = new Solution();


int one = sc.nextInt();
Task1.a = one;
Task1.beg = 0;
int two = sc.nextInt();
Task2.a = two;
Task2.beg = one;
int three = sc.nextInt();
Task3.a = three;
Task3.beg = one+two;
System.out.print(solution.test());
}
}

You might also like