0% found this document useful (0 votes)
138 views

Some Java Programs :)

The document contains guidelines for a computer applications lab program project. It includes instructions to define an Employee class with data members like PAN number, name, tax income, tax amount and member functions to input data, calculate tax, and display employee details. It also provides a sample output for the program. The document is certified by an external and internal examiner. It acknowledges the help received and provides a table of contents listing 20 programs to be developed.

Uploaded by

Yuvan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views

Some Java Programs :)

The document contains guidelines for a computer applications lab program project. It includes instructions to define an Employee class with data members like PAN number, name, tax income, tax amount and member functions to input data, calculate tax, and display employee details. It also provides a sample output for the program. The document is certified by an external and internal examiner. It acknowledges the help received and provides a table of contents listing 20 programs to be developed.

Uploaded by

Yuvan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

GRADE – X Chrysalis High

Arnav Pawar 2021-22


PROJECT GUIDELINES
COMPUTER APPLICATIONS
Lab Programs – Part II

1. Define a class employee having the following description:


Data members/instance variables:
int pan — to store personal account number
String name — to store name
double tax income — to store annual taxable income
double tax — to store tax that is calculated
Member functions:
input () — Store the pan number, name, taxable income
calc () — Calculate tax for an employee
display () — Output details of an employee
Write a program to compute the tax according to the given conditions
and display the output as per
given format. Write a main method to create object of a class and call
the above member methods.
Ans-
Certificate
This is to certify that the content of this project entitled, Lab set
Programs is a bona fide work of Arnav Pawar, submitted to Ms.
Chitchula Gouthami for consideration in partial fulfilment of the
requirement of CISCE, New Delhi for the award of Indian
Certificate of Secondary Education. 
The research was carried out by him/her under my supervision
in the academic year 2021-22. On the basis of the declaration, I
recommend the project for evaluation.

Signature of External Examiner                                Signature of


Internal Invigilator

_________________________                                
_________________________
Acknowledgement

I express my sincerest gratitude to CISCE for providing me with


the opportunity to work on this project. The project involved
extensive research that has deepened my understanding of the
topic. I would like to thank my Principal, Ms. Sukanya Maity for
her constant support and guidance. I would like to thank my
subject teacher, Ms. Chitchula Gouthami, whose valuable
suggestions helped me understand the project better. I would
also want to thank my parents, friends, and all those who were
directly or indirectly involved in enabling me to complete the
project. 
Sl.no.                               Content Pg.no.
1 Introduction 5

2 Program 1: Computing Tax 6

3 Program 2:  Computing Volume 9

4 Program 3:  Displaying Two Series 11

5 Program 4: Railway Coach 14

6 Program 5: Spy Number or Pronic Number 18

7 Program 6: Electric Bill  21

8 Program 7:  Sum of Two Series  24

9 Program 8: Discount on Laptop  26

10 Program 9: Maximum and Average of Three Marks 29

11 Program 10: Composite Number and Smallest Number  31

12 Program 11: Type of Triangle 34

13 Program 12: Library Charges 36

14 Program 13: Palindrome 38

15 Program 14: Number to Words 40

16 Program 15:Tech Number 43

17 Program 16: Neon Number 44

18 Program 17: Frequency of a Word in a Sentence 46

19 Program 18:  Initials with Surname 48

20 Program 19: Pig Latin Word 50

21 Program 20: Linear Search 52

22 Conclusion  54

23 Bibliography 55
Ans program 1-
import java.util.*;
public class employee //class
{
   int pan;
   String name;
   double tax_income, tax;
   Scanner sc= new Scanner(System.in);
   void input()   //input method
    {       
       System.out.println("\fEnter Name");
       name=sc.nextLine();
       System.out.println("Enter Pan Number"); 
       pan=sc.nextInt();
       System.out.println("Enter Taxable Income"); 
       tax_income= sc.nextDouble();       
    }
   void calc()     //calculate method
   { 
       if(tax_income>=0 && tax_income<=100000)
       {
           tax=0;
        }
        else if (tax_income>=100001 && tax_income<=150000)
        {
            tax=100000.0*0+ (tax_income-100000)*10.0/100;
        }
        else if(tax_income>=150001 && tax_income<=250000)
        {
            tax = 100000.0*0+ (tax_income-150000)*20/100.0 + 5000;
        }
        else if (tax_income> 250000)
        {
            tax= 100000.0*0+ (tax_income-250000)*30/100.0 + 25000;
        }
        else if(tax_income<0)     
        {
            System.out.println("Sorry there cannot be negative payment.");
         }
    }
    void display()   //display method
    {
        System.out.println(" Pan Number\t Name\t Tax Income\t Tax ");
        System.out.print( pan+"\t "+ name+"\t "+ tax_income+"\t "+tax);
    }
    public static void main(String args[])
    {
       employee obj=new employee();
       obj.input();
       obj.calc();
       obj.display();
    }
}
Variable Description Table 

Variable Data Type         Purpose

pan integer Stores Pan number

name String Stores Name 

tax_income Double Stores Taxable Income

tax Double Calculates and stores the tax.

Output 1: 

Enter Name
Robin Rathore
Enter Pan Number
23221
Enter Taxable Income
150000
Pan Number   Name              Tax Income Tax 
23424         Raju Rathore         150000.0 5000.0
Output 2:

Enter Name
Robin Rathore
Enter Pan Number
23221
Enter Taxable Income
230000
 Pan Number   Name           Tax Income   Tax 
23221         Robin Rathore    230000.0 21000.0
Output 3:

Enter Name
Robin Rathore
Enter Pan Number
54632
Enter Taxable Income
-2544
Sorry there cannot be negative payment.
 Pan Number   Name   Tax Income   Tax 
54632           Robin Rathore     -2544.0     0.0
2. Write a class with the name volume using function overloading that
computes the volume of a cube,
a sphere and a cuboid.
Formula
volume of a cube (vc) = s*s*s
volume of a sphere (vs) = 4/3* π* r* r*r (where π = 3.14 or 22/7)
Volume of a cuboid (vcd) = l* b* h

Ans-
/** A program showing function overloading */
// To find volume of a cone,a sphere and a cuboid using overloading.
 import java.util.*;
 public class Volume
 {
     double vc=0.0D,vs=0.0D, vcd=0.0D;
     void volume (int s)  //volume of cube
     // Here we are using void as we aren't returning any value to the main
and just directly printing the output.
        
     {
         vc=s*s*s;
         System.out.println("The volume of a cube = "+vc);
      }
     void volume(float r) //volume of sphere
     {
       vs=4/3*22/7*r*r*r;
       System.out.println("The volume of the sphere is= "+vs);
     }
     void volume(int l,int b,int h) //volume of cuboid
     {
         vcd=l*b*h;
         System.out.println("The volume of the cuboid = "+vcd);
        }
     public static void main(String args[])
        {
          Scanner in=new Scanner(System.in);
          int s,l,b,h;
          float r;
          System.out.println("Enter the value of side of a cube, radius of
sphere,sides of cuboid");
          s=in.nextInt();
          r=in.nextFloat();
          l=in.nextInt();
          b=in.nextInt();
          h=in.nextInt();
          Volume ob=new Volume();
          ob.volume(s);
          ob.volume(r);
          ob.volume(l,b,h);
        }
    }

                                            Variable Description Table

Variable Data Type Purpose


vc double To calculate and store volume of cube
vs double To calculate and store volume of sphere
vcd double To calculate and store volume of cuboid
s integer To store the value of the side of the cube.
r float To store the value of the radius of the sphere
l integer To store the value of the length of the cuboid
b integer To store the value of the breadth of the cuboid
h integer To store the value of the height of the cuboid

Output :
Enter the value of side of a cube, radius of sphere,sides of cuboid
5 3.2 3 4 7
The volume of a cube = 125.0
The volume of the sphere is= 98.30400848388672
THe volume of the cuboid = 84.0

3. Design a class to overload a function series() as follows: [15]


(a) void series (int x, int n) – To display the sum of the series given
below:
x
1 + x2 + x3 + ................ xn
terms

(b) void series (int p) – To display the following series:


0, 7, 26, 63 p terms.
(c) void series () – To display the sum of the series given below:

Ans-
/** 
 * Design a class to overload a function series() as follows: 
(a) void series (int x, int n) – To display the sum of the series given
below:
     x^1 + x^2 + x^3 + ................ x^n terms
(b) void series (int p) – To display the following series:
     0, 7, 26, 63 p terms.
(c) void series () – To display the sum of the series given below:
     1/2+1/3+1/4.....+1/10
 */
import java.util.*;
public class Series
{    
    void series(int x,int n) //series 1
    {
        double sum=0,i; 
        for (i=1;i<=n;i++)
         sum= sum+ Math.pow(x,i);
         System.out.println("The sum of series one: "+sum);
    }
    void series( int p) //series 2
    {
        int i,num=0;
        for(i= 1; i<= p; i++)
        {
         num=  (i*i*i)-1;
         System.out.print(num);
         if( i== p )
         System.out.print(" ");
         else
         System.out.print(", ");
        }
        System.out.println();
    }
    void series () //series 3
    {
        double sum=0,i;
        for( i= 2;i<=10;i++)
         {
            sum= sum+ 1/i;
         }
        System.out.println("The sum of series three: "+sum);
    }
    public static void main(String args[]) //main function
    {
        System.out.println("\f");
        Scanner sc= new Scanner(System.in);
        int no,pow,sr;
        System.out.println("Enter(one by one) :\n The number and the till
which power of it do you want to add up for 1st series.\n The number of
terms till which you want the series to continue for series 2");
        no= sc.nextInt();
        pow= sc.nextInt();
        sr=sc.nextInt();
        Series obj= new Series();
        obj.series(no,pow);
        obj.series(sr);
        obj.series();
    }
}
                                               

                                          Variable Description Table

Variables Data Purpose


Type
x integer To send the value of variable ‘no’ and save it to
the called method.
n integer To send the value of variable ‘pow’ and save it to
the called method.
i double Counter variable.
p integer To send the value of variable ‘sr’ and save it to
the called method.
sum double To calculate and store the sum of series 1.
no integer To accept and store the number 
pow integer To accept and store the power
sr integer To store till which number the series 2 is to be
continued.
num integer To calculate and store the values of the numbers
of series 2

Output:

Enter(one by one) :
 The number and the till which power of it do you want to add up for 1st
series.
 The number of terms till which you want the series to continue for series
2
17
5
The sum of series one: 7.0
0, 7, 26, 63, 124 
The sum of series three: 1.928968253968254

4. Design a class Railway Ticket with following description :


Instance variables/s data members :
String name : To store the name of the customer
String coach : To store the type of coach customer wants to travel
long mobno : To store customer’s mobile number
int amt : To store basic amount of ticket
int totalamt : To store the amount to be paid after updating the original
amount
Member methods
void accept ( ) — To take input for name, coach, mobile number and
amount
void update ( )— To update the amount as per the coach selected
void display( ) — To display all details of a customer such as name,
coach, total amount and mobile
number.
Write a main method to create an object of the class and call the above
member methods.

Ans-

import java.util.*;
public class Railway_ticket
{
    String name;  //declaring all variables
    String coach;
    long mobno;
    int amt;
    int totalamt;
    public void accept()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter your name:");
        name=sc.nextLine();   //accepting name
        System.out.println("Which coach would you prefer:(please do not
write the option number, just write the option name)");
        System.out.println("1) First_AC\n2) Second_AC\n3) Third_AC\n4)
Sleeper");
        coach= sc.nextLine();
        System.out.println("Enter your mobile number:");
        mobno=sc.nextLong();
        System.out.println("Enter the basic amount of the ticket:");
        amt=sc.nextInt();
        }
    public void update()
    {
      if(coach.equalsIgnoreCase("First_AC")) //to check equality between
two strings by ignoring the case
      totalamt=amt+700;
      else if (coach.equalsIgnoreCase("Second_AC"))
      totalamt= amt+ 500;
      else if (coach.equalsIgnoreCase("Third_AC"))
      totalamt= amt+ 250;
      else if (coach.equalsIgnoreCase("Sleeper"))
      totalamt= amt+ 0;
    }
public void display()    //method to display the data collected and
evaluated.
    {
        System.out.println("Name: "+ name);
        System.out.println("Coach: "+ coach);
        System.out.println("Total amount: "+ totalamt);
        System.out.println("Mobile number: "+ mobno);
    }
    public static void main (String args[])
    {
        System.out.println("\f");
        Railway_ticket obj=new Railway_ticket();
        obj.accept();
        obj.update();
        obj.display();
    }
}

Variable Description Table

Variable Data   Purpose


Type
name  String To accept and store the name.
coach String To accept and store the coach name.
mobno Long To accept and store the mobile number.
amt Integer  To accept and store the basic amount of the
ticket.
totalamt Integer  To calculate and store the final amount.

Output 1:

Enter your name:


Issac Newton
Which coach would you prefer:(please do not write the option number,
just write the option name)
1) First_AC
2) Second_AC
3) Third_AC
4) Sleeper
First_AC
Enter your mobile number:
3453533222
Enter the basic amount of the ticket:
350
Name: Issac Newton
Coach: First_AC
Total amount: 1050
Mobile number: 3453533222

Output 2: 

Enter your name:


Robert Browning
Which coach would you prefer:(please do not write the option number,
just write the option name)
1) First_AC
2) Second_AC
3) Third_AC
4) Sleeper
Second_ac
Enter your mobile number:
1234567890
Enter the basic amount of the ticket:
350
Name: Robert Browning
Coach: Second_ac
Total amount: 850
Mobile number: 1234567890

Output 3: 

Enter your name:


Jay Kumar
Which coach would you prefer:(please do not write the option number,
just write the option name)
1) First_AC
2) Second_AC
3) Third_AC
4) Sleeper
Third_aC
Enter your mobile number:
3452679203
Enter the basic amount of the ticket:
350
Name: Jay Kumar
Coach: Third_aC
Total amount: 600
Mobile number: 3452679203

Output 4:

Enter your name:


Arnav pawar
Which coach would you prefer:(please do not write the option number,
just write the option name)
1) First_AC
2) Second_AC
3) Third_AC
4) Sleeper
Sleeper
Enter your mobile number:
3453320961
Enter the basic amount of the ticket:
350
Name: Arnav Pawar
Coach: Sleeper
Total amount: 350
Mobile number: 3453320961

5. Write a menu driven program to accept a number from the user and
check
(a). Whether it is a Pronic number or not. Pronic number is the number
which is the product of two
consecutive integers
Examples : 12 = 3 × 4 .
20 = 4 × 5
42 = 6 × 7
(b) Whether it is a spy number or not. A number is spy if the sum of its
digits equals to the product of
its digits.

Ans-
import java.util.Scanner;
public class Pronic_Spy
{
    public static void main(String args[])
    {
        System.out.println("\f");
        Scanner sc=new Scanner(System.in);
        int n,i,a,sum=0,product=1,digit,flag=0; //declaring and initializing the
variables.
        System.out.println("\t\tMENU");
        System.out.println("1)Pronic Number\n2) Spy number");
        System.out.println("Enter your option number:");
        n=sc.nextInt();
      switch(n)
      {
          case 1:
          System.out.println("Enter your number:");
          n = sc.nextInt();
          for(i=1;i<n;i++)
          {
              if (i*(i+1)==n)
               {
                  flag=1;
                  break;
               }
          }
          if(flag==1)
           System.out.println(n+" is a pronic number!");
          else 
           System.out.println("It is not a pronic number!");
           
          break;
          
          case 2:  
          System.out.println("Enter a number:");
          n=sc.nextInt();         // accepting the number.
          a=n;
          while(n>0)
          { 
             digit=n%10;   // Extracting digits.
             sum= sum+digit;
             product=product*digit;
             n=n/10;
          }
          if(sum==product)    // checking if sum and product are the same.
            System.out.println(a+" is a spy number.");
          else 
            System.out.println(a+" is not a spy number.");
          break;
        }
    } 
}
    

                                           Variable Description Table

Variable Data Purpose


Type
n integer To accept the option number
i integer Loop Control Variable
a integer To store the value of the accepted number
temporarily.
sum integer To store the sum of the digits.
product integer To store the product of the digits.
digit integer To extract the last digit
flag integer Used as a counter variable.

Output 1:
MENU
1)Pronic Number
2) Spy number
Enter your option number:
1
Enter your number:
12
12 is a pronic number!

Output 2:
MENU
1)Pronic Number
2) Spy number
Enter your option number:
1
Enter your number:
15
It is not a pronic number!
Output 3:
MENU
1)Pronic Number
2) Spy number
Enter your option number:
2
Enter a number:
1124
1124 is a spy number.

Output 4:
MENU
1)Pronic Number
2) Spy number
Enter your option number:
2
Enter a number:
1753
1753 is not a spy number.

6. Define a class ElectricBill with the following specifications :


class : ElectricBill
Instance variables /data member :
String n – to store the name of the customer
int units – to store the number of units consumed
double bill – to store the amount to be paid
Member methods :
ElectricBill( ): a constructor to initialize n=””,units=0,bill=0.0
void accept ( ) – to accept the name of the customer and number of units
consumed
void calculate ( ) – to calculate the bill as per the following tariff :
A surcharge of 2.5% charged if the number of units consumed is above
300 units.

Write a main method to create an object of the class and call the above.

Ans-

A program to calculate the amount of an Electric bill as per the accepted


units.

import java.util.*;   //importing package.


public class ElectricBill
{
   String n;
   int units;
   double bill,surcharge,bill1;
   ElectricBill()
   {
     n="";
     units=0;
     bill=0.0;
     bill1=0.0;
   }
   void accept()   //method to accept inputs.
   {  
     Scanner sc=new Scanner(System.in);
     System.out.println("Enter name of the customer:");
     n=sc.nextLine();
     System.out.println("Enter number of units consumed:");
     units=sc.nextInt();
    }
   void calculate()
   {
     if(units<=100)
     bill=units*2.0;
     else if (units>100&& units <= 300)
     bill= 100.0*2 +(units-100)*3.0;
     else if(units>300)
     {
         bill1= 100.0*2+ 200.0*3+ (units-300)*5.0;   //bill before the
surcharge  
         surcharge= (2.5* bill1)/100;
         bill=bill1+surcharge;   // total bill cost after adding surcharge
     }
   }
   void print()
   {
       System.out.println("Name of the customer: "+n);
       System.out.println("Number of units consumed: "+units);
       System.out.println("Bill amount: "+bill);
    }
   public static void main(String args[])
   {
     System.out.println("\f");  //to clear the screen.
     ElectricBill ob=new ElectricBill();
     ob.accept();
     ob.calculate();
     ob.print();
    }
}                                      
                                         Variable Description Table

Variable Data                               Purpose


Type
n String Accepts and stores the name of the customer.
units integer Accepts and stores the units consumed by the
customer.
surcharge double Calculates the surcharge.
bill1 double Calculates and stores the amount before adding
the surcharge.
bill double Calculates and stores the final bill amount .

Output 1:

Enter name of the customer:


Harry Jones
Enter number of units consumed:
50
Name of the customer: Harry Jones
Number of units consumed: 50
Bill amount: 100.0

Output 2:

Enter name of the customer:


John 
Enter number of units consumed:
293
Name of the customer: John 
Number of units consumed: 293
Bill amount: 779.0

Output 3:

Enter name of the customer:


Varun Chaudhary
Enter number of units consumed:
400
Name of the customer: Varun Chaudhary
Number of units consumed: 400
Bill amount: 1332.5

7. Design a class to overload a function SumSeries( ) as follows:


i. void SumSeries(int n, double x)with integer argument and double
argument to find and display the
sum of the series given below:
s=(x/1)-(x/2)+ (x/3)-(x/4)+------ to n terms
ii. void SumSeries( ): to find and display the sum of the series given
below:
s=1+(1*2)+(1*2*3)+------------(1*2*3*----*20)

Ans-
A program to find the sum of two different series.

public class SUMSERIES    // declaring a class.


{
    void SumSeries(int n, double x)
    {
        double sum = 0;   // initializing variables.
        for (int i = 1; i <= n; i++) 
        {
            double t = x / i;
            if (i % 2 == 0)
                sum -= t;
            else
                sum += t;
        }
        System.out.println("Sum = " + sum);  //printing the sum for the 1st
series
    }
    
    void SumSeries()  
    {
        long sum = 0, term = 1;
        for (int i = 1; i <= 20; i++) 
        {
            term *= i; 
            sum += term;
        }
        System.out.println("Sum=" + sum);   //printing the sum for the 2nd
series.
    }
}

                                    Variable Description Table


Variable Data Purpose
Type 
n integer To which denominator the series has to be
carried on.
x double It is the numerator of the fractions in series 1.
sum double Calculated and stores the sum of the terms of
the series.
i integer Loop Control Variable.
t double Stores the fraction in series one.
term long Stores and multiplies the term number.

Output 1:( For series 1 ; when x= 1 and n=7)


Sum = 0.7595238095238095

Output 2:  (For series 2)


Sum=2561327494111820313

8. An electronic shop has announced a special discount on the purchase


of Laptop as given below:

Define a class Laptop described as follows:


Data members or instance variables: name, price,dis,amt;
Member Methods:
i. A Parameterised constructor to initialize the data members
ii. To accept the details(name and price)
iii. To compute the discount
iv. to display the name , discount and amount to be paid after discount.
Write a main method to create an object of the class and call the above.

Ans-

A program to calculate discount on a laptop.

t
                                        
                                     Variable Description Table

Variable Data Purpose


Type
name String  Stores the value of variable ‘s’.
price double Stores the value of variable ‘p’ and calculates and
stores the original price. 
dis double Calculates and stores discount amount.
amt double Calculates and stores total amount after discount.
s String Stores the value of variable ‘str’ using a
parameterized constructor.
p double Stores the price of the laptop..
str String Stores name.

  Output 1:

Enter name:
Rabindra Singh
Enter price:
23000
Name: Rabindra Singh
Discount: 1150.0
Amount to be paid: 21850.0

Output 2:
Enter name:
Mohan P
Enter price:
42000
Name: Mohan P
Discount: 3150.0
Amount to be paid: 38850.0

Output 3:

Enter name:
Arjun Kumar
Enter price:
79000
Name: Arjun Kumar
Discount: 7900.0
Amount to be paid: 71100.0

Output 4:

Enter name:
Jitendra Ravi
Enter price:
150999
Name: Jitendra Ravi
Discount: 22649.85
Amount to be paid: 128349.15

9. Define a class Marks as per the given specification:


Data Members/instance variables:
name, age, m1, m2, m3 (marks in three subjects), maximum and
average
Member methods:
i. A Parameterised constructor to input the details of the student.
ii. To compute the average and the maximum out of them.
iii. To display name, age, m1, m2, m3, maximum and average
Write a main method to create an object of the class and call the above.
Ans-
A program to find the maximum of three marks and average marks.

import java.util.*;
public class Marks
{
    String name;
    int age,m1,m2,m3,max;
    double avg;
    Marks(String n,int d,int a, int b, int c)       //parameterized constructors
    {
        name= n;
        age=d;
        m1=a;
        m2=b;
        m3=c;
    }
    void compute()               //all calculations are done here
    {
        avg=(m1+m2+m3)/3;          //average marks.
        if(m1>m2&&m1>m3)
        max=m1;
        if (m2>m1&& m2>m3)
        max=m2;
        if (m3>m1&& m3> m2)
        max=m3;
    }
    void display()                // method to display the data.
    {
       System.out.println("Name: "+name);
       System.out.println("Age: "+age+" years");
       System.out.println("Marks in three subjects:  "+m1+" "+m2+" "+m3);
       System.out.println("The highest marks out of the three subject
marks: "+max);
       System.out.println("The average marks: "+avg);
     }
    public static void main(String args[])
    {
       
        Scanner sc= new Scanner(System.in);
        String st;
        int ag, ma1,ma2,ma3;
        System.out.println("Enter the Student's Name, Age, Marks in three
subjects one by one:");
        st=sc.nextLine();
        ag=sc.nextInt();
        ma1=sc.nextInt();
        ma2=sc.nextInt();
        ma3=sc.nextInt();
        Marks obj=new Marks(st,ag,ma1,ma2,ma3);
        obj.compute();
        obj.display();
    }
}                                       

                                   Variable Description Table


 
Variable Data Purpose
Type
name String To store the name of the student.
age integer To store the age of the student.
m1 integer To store the marks obtained in the first subject.
m2 integer To store the marks obtained in the second
subject.
m3 integer To store the marks obtained in the third subject.
max integer Calculates and stores the maximum marks out of
the three subjects.
avg double Calculates and stores the average marks .

Output:
Enter the Student's Name, Age, Marks in three subjects one by one:
Ashwas Aayu
13
96 89 98
Name: Ashwas Aayu
Age: 13 years
Marks in three subjects:  96 89 98
The highest marks out of the three subject marks: 98
The average marks: 94.0
10. Using a switch statement, write a menu driven program:

1. To check and display whether a number input by the user is a


composite number or not.
2. To find the smallest digit of an integer that is in input.
Sample input: 6524
Sample output: Smallest digit is 2
For an incorrect choice, an appropriate error message should be
displayed.

Instructions:
1. While taking printouts follow the pattern for each program.
i) program description
ii) source code – with comments
iii) Format for variable description:

iv) output (minimum 3 sets)


eg: if checking for Armstrong number , then 2 outputs showing the
number is armstrong number
and 1 output showing the number is not Armstrong number.
If menu driven, then output for each case should be included.

Ans-
A menu driven program to check whether a number is a composite
number or not and to find the smallest number of an accepted number. 

import java.util.Scanner;
public class Number
{
    public static void main(String args[])          //main function
     {
        Scanner in = new Scanner(System.in);
        System.out.println("\t\tMENU");
        System.out.println("1) Composite Number\n2) Smallest Digit.");
        System.out.print("Enter your choice: ");
        int ch = in.nextInt();
        
        switch (ch)         //switch statement
        {
            case 1:   
            System.out.print("Enter Number: ");
            int n = in.nextInt();
            int c = 0;
            for (int i = 1; i <= n; i++) 
            {
                if (n % i == 0)
                    c++;
            }
            
            if (c > 2)
                System.out.println("Yes, it is a Composite Number");
            else
                System.out.println("No, it is not a Composite Number");
            break;            //To avoid fall-through.
            
            case 2:
            System.out.print("Enter Number: ");
            int num = in.nextInt();
            int s = 10;
            while (num != 0) 
            {
                int d = num % 10;
                if (d < s)
                    s = d;
                num /= 10;
            }
            System.out.println("The smallest digit is " + s);
            break;
            
            default:             //default case.
            System.out.println("Wrong choice");
        }
    }
}

                                       Variable Description Table


Variable Data Purpose
Type
ch integer To accept the option number.
n integer To accept number (in case 1).
c integer Counter variable.
i integer Loop Control Variable.
num integer To accept number (in case 2)
s integer Calculates and stores the smallest digit.
d integer Extracts the last digit.

Output 1:
MENU
1) Composite Number
2) Smallest Digit.
Enter your choice: 1
Enter Number: 13
No, it is not a Composite Number

Output 2:
MENU
1) Composite Number
2) Smallest Digit.
Enter your choice: 1
Enter Number: 12
Yes, it is a Composite Number

Output 3:
MENU
1) Composite Number
2) Smallest Digit.
Enter your choice: 2
Enter Number: 89734
The smallest digit is 3

Output 4:
MENU
1) Composite Number
2) Smallest Digit.
Enter your choice: 3
Wrong choice
Conclusion
Java is a high level, object oriented programming language developed by James
Gosling at Sun Microsystem in 1991. 
There are four fundamental concepts of Object-oriented programming – Inheritance,
Encapsulation, Polymorphism, and Data abstraction.

This project has helped me to enhance my programming skills and at the same time
has made my concepts stronger. We have used methods/ functions and various other
String functions to run our programs successfully. A data type in java is a term that
specifies memory size and type of values that can be stored into the memory location.
In other words, data types define different values that a variable can take.
There is a difference between declaring a variable and initializing a variable.
Declaration tells the compiler about the existence of an entity in the program and its
location while initialization is the process of assigning a value to the variable(entity).

There are different ways to programme for the same desired result and each procedure
is appreciable till we get the required output. 

Overall this was a really fun-filled project which made me ponder a lot about the
program logic and hence it helped me to utilise my time in a productive manner. 

You might also like