Search This Blog

Tuesday 31 August 2021

3. Write a java code to create a class with data members name, category, doj, and fees and static members total_fee, categorywise_no_students, methods to Insert data using parameterized constructor, display student information along with total fees and number of students in each category.

import java.io.*;
import java.util.*;

class Pro3
    {  
        String name1;
        String cat1;
        String doj1;
        float fees1;
        public static float total_fee=0.0F;
        public static int gm=0,sc=0,st=0,obc=0;
        //creating a parameterized constructor  
        Pro3(String name,String cat, String doj, float fees)
        {  
            name1 = name;
            cat1 = cat;
            doj1 = doj;
            fees1= fees;
        }  
        //method to display the values  
        void display()
        {
            System.out.println("STUDENT INFORMATION");
            System.out.println("NAME: "+name1);
            System.out.println("CATEGORY: "+cat1);
            System.out.println("DOJ: "+doj1);
            System.out.println("FEES: "+fees1);
            total_fee=total_fee+fees1;
            if(cat1.equals("gm") || cat1.equals("GM"))
                gm++;
            if(cat1.equals("sc") || cat1.equals("SC"))
                sc++;
            if(cat1.equals("st") || cat1.equals("ST"))
                st++;
            if(cat1.equals("obc") || cat1.equals("OBC"))
                obc++;
        }  
   
        public static void main(String args[])throws IOException
        {  
            //creating objects and passing values  
            DataInputStream in= new DataInputStream(System.in);
            System.out.println("Enter the name, category, doj and fees of student1");
            Pro3 s1 = new Pro3(in.readLine(),in.readLine(),in.readLine(),Float.parseFloat(in.readLine()));  
            System.out.println("Enter the name, category, doj and fees of student2");
            Pro3 s2 = new Pro3(in.readLine(),in.readLine(),in.readLine(),Float.parseFloat(in.readLine()));
            System.out.println("Enter the name, category, doj and fees of student3");
            Pro3 s3 = new Pro3(in.readLine(),in.readLine(),in.readLine(),Float.parseFloat(in.readLine()));
            //calling method to display the values of object  
            s1.display();  
            s2.display();
            s3.display();
            System.out.println("TotalFees="+total_fee);
            System.out.println("GM Category Students="+gm);
            System.out.println("SC Category Students="+sc);
            System.out.println("ST Category Students="+st);
            System.out.println("OBC Category Students="+obc);
        }  

Monday 30 August 2021

2. Write a Java program to sort the elements of a square matrix. Read the order and elements of the matrix during execution. Before sorting the elements of the matrix, display the source matrix.

import java.io.*;
import java.util.*;
class  Program2
{
    public static void main(String[] args) throws IOException
    {
        int a[]=new int[100];
        int b[]=new int[100];
        int temp,num,i,j;
        System.out.println("Enter the order for square matrix:");
        DataInputStream in=new DataInputStream(System.in);
        int m=Integer.parseInt(in.readLine());
        System.out.println("Enter the elements:");
        for( i=0;i<(m*m);i++)
        {
                temp=Integer.parseInt(in.readLine());
                b[i]=temp;
                a[i]=temp;
            
        }
        System.out.println("The Source Matrix:\n");
        i=0;
        while(i<m*m)
        {
            for( j=0;j<m;j++)
            {
                System.out.print(a[i] + "\t");
                
                i++;
            }
                System.out.println();
        }

        for( i=0;i<m*m;i++)
        {
            for( j=i+1;j<=m*m;j++)
            {
                
                
                
                    if(a[i]>a[j])
                    {
                        temp=a[i];
                        a[i]=a[j];
                        a[j]=temp;
                    }
                
            }
        }
    
        System.out.println("The Sorted Matrix:\n");
        i=1;
        while(i<m*m)
        {
            for( j=0;j<m;j++)
            {
                System.out.print(a[i] + "\t");
                
                i++;
            }
                System.out.println();
        }
    }
}


Thursday 26 August 2021

1. Write a Java program to display only those multi-digit prime numbers between a given range whose digit sum is prime. Display the prime number and its digit sum side by side. Read the value for the range using readLine() method of BufferedReader class.

import java.io.*;
import java.util.*;
class  Program1
{
    public static void main(String[] args) throws IOException
    {
        System.out.println("Enter the range:");
        BufferedReader br=new BufferedReader(new     InputStreamReader(System.in));
        int m= Integer.parseInt(br.readLine());
        int n= Integer.parseInt(br.readLine());
        int rem,sum=0,temp;
        System.out.println("Prime Number \t Sum of Digits");
        for (int i = m; i <= n; i++)
        {
            
            if (i == 1 || i == 0)    //0 and 1 are not prime.
                continue;
    
        
            int pflag = 1, sflag = 1; // Set the flag values.
    
            for (int j = 2; j <= i / 2; ++j)
            {
                if (i % j == 0)
                {
                    pflag = 0;  // i is not prime.
                    break;
                }
            }
    
        
                temp = i;
                sum=0;
                while(temp>0)        // This loop is used to find the digit sum.
                {
                    rem=temp%10;
                    sum=sum+rem;
                    temp=temp/10;
                }

                for ( int k = 2; k <= sum / 2; ++k)
                {
        
                    if (sum % k == 0)
                    {
                    sflag = 0;  // Digit sum is not prime.
                    break;
                    }
                }
            
                if(pflag==1 && sflag==1)

                System.out.println(i+"\t\t\t"+sum);
            
        }
    }
}

FREE Hit Counters