Q. Create a package named Series having three different classes to print series:
a. Prime numbers
b. Fibonacci series
c. Squares of numbers
Write a program to generate ‘n’ terms of the above series.
NOTE: THIS QUESTION HAS TWO PROGRAMS TO BE WRITTEN AND COMPILED DIFFERENTLY.
Program 1:
package series;
public class Prime {
int flag;
public void prime(int n){
for(int i=2;i<n;i++){
if(n%i==0)
{
flag=0;
break;
}
else
flag=1;
}
if(flag==1)
System.out.println(n+" is a prime number.");
else System.out.println(n+" is not a prime number.");
}
public void fibonacci(int n){
int first=0, second=1, c, next;
System.out.println("Fibonacci Series:");
for(int i=0;i<=n;i++)
{
if(i<=1)
next=i;
else
{
next=first+second;
first=second;
second=next;
}
System.out.println(next);
}
}
public void square(int n){
System.out.println("Square of the number is "+(n*n));
}
}
a. Prime numbers
b. Fibonacci series
c. Squares of numbers
Write a program to generate ‘n’ terms of the above series.
NOTE: THIS QUESTION HAS TWO PROGRAMS TO BE WRITTEN AND COMPILED DIFFERENTLY.
Program 1:
package series;
public class Prime {
int flag;
public void prime(int n){
for(int i=2;i<n;i++){
if(n%i==0)
{
flag=0;
break;
}
else
flag=1;
}
if(flag==1)
System.out.println(n+" is a prime number.");
else System.out.println(n+" is not a prime number.");
}
public void fibonacci(int n){
int first=0, second=1, c, next;
System.out.println("Fibonacci Series:");
for(int i=0;i<=n;i++)
{
if(i<=1)
next=i;
else
{
next=first+second;
first=second;
second=next;
}
System.out.println(next);
}
}
public void square(int n){
System.out.println("Square of the number is "+(n*n));
}
}
Program 2:
import series.*;
import java.io.*;
public class SeriesMain {
public static void main(String [] args)throws IOException{
Prime p=new Prime();
int i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println("Enter a number / 0 to exit");
i=Integer.parseInt(br.readLine());
p.prime(i);
p.fibonacci(i);
p.square(i);
}
while(i>0);
}
}
Output:
Enter a number / 0 to exit
5
5 is a prime number.
Fibonacci Series:
0
1
1
2
3
5
Square of the number is 25
Enter a number / 0 to exit
import java.io.*;
public class SeriesMain {
public static void main(String [] args)throws IOException{
Prime p=new Prime();
int i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println("Enter a number / 0 to exit");
i=Integer.parseInt(br.readLine());
p.prime(i);
p.fibonacci(i);
p.square(i);
}
while(i>0);
}
}
Output:
Enter a number / 0 to exit
5
5 is a prime number.
Fibonacci Series:
0
1
1
2
3
5
Square of the number is 25
Enter a number / 0 to exit
2 Comments
Slip9_2: Write a program to using marker interface create a class Product (product_id,
ReplyDeleteproduct_name, product_cost, product_quantity) default and parameterized constructor. Create
objectsof class product and display the contents of each object and Also display the object
count.
import java.util.*;
interface MarkerInt {
}
class product implements MarkerInt {
int pid, pcost, quantity;
String pname;
static int cnt;
// Default constructor
product() {
pid = 1;
pcost = 10;
quantity = 1;
pname = "pencil";
cnt++;
}
// Parameterized constructor
product(int id, String n, int c, int q) {
pid = id;
pname = n;
pcost = c;
quantity = q;
cnt++;
System.out.println("\nCOUNT OF OBJECT IS : " + cnt + "\n");
}
public void display() {
System.out.println("\t" +pid + "\t" + pname + "\t" + pcost + "\t" + quantity);
}
}
class MarkerInterface {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number of Product : ");
int n = sc.nextInt();
product pr[] = new product[n];
for (int i = 0; i < n; i++) {
System.out.println("\nEnter " + (i + 1) + " Product Details :\n");
System.out.println("Enter Product ID: ");
int pid = sc.nextInt();
sc.nextLine();
System.out.println("Enter Product Name: ");
String pn = sc.nextLine();
System.out.println("Enter Product Cost:");
int pc = sc.nextInt();
System.out.println("Enter Product Quantity:");
int pq = sc.nextInt();
pr[i] = new product(pid, pn, pc, pq);
}
System.out.println("\n\t\t Product Details\n");
System.out.println("\tId\tPname\tCost\tQuantity\n");
for (int i = 0; i < n; i++) {
pr[i].display();
}
sc.close();
}}
Slip10_1: Write a program to find the cube of given number using functional interface.
ReplyDeleteimport java.util.*;
interface Cube
{
float cube();
}
class Draw implements Cube
{
public float cube()
{
System.out.println("enter the number");
Scanner sc= new Scanner (System.in);
float cu = sc.nextInt();
double cue = cu*cu*cu;
System.out.println("cube of no is:"+cue);
return 0;
}
public static void main(String args[])
{
Draw d = new Draw();
d.cube();
}
}