Java: Programs of Ch 6 Constructors

CONSTRUCTOR  PROGRAMS

 [EVERY PROGRAM IS CROSS CHECKED AND VERIFIED]

 

 PROG 1: Make a class FibSeries to generate fibonacci series: upto 30 terms

      (i) private variables: first second third (int)

     (ii) public functions:  

          FibSeries: to give 0 to first and 1 to second;

          void PrintSeries() : to print fibonacci series using while loop upto 30 terms


class FibSeries
{
private int first,second,third;
public FibSeries()
{
first = 0;
second = 1;
}
void PrintSeries()
{
  System.out.println("Fibonnaci series till 30 terms");
int i = 1, n = 30;
while (i <= n) {
      System.out.print(first+ ", ");

      int next = first + second;
      first = second;
      second = next;

      i++;
    }
    
}
public static void main(String args[])
{
FibSeries fs=new FibSeries ();
fs.PrintSeries();
}
}
Fibonnaci series till 10 terms
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 


 

 PROG 2: Make a class factorial

      (i) private variables: num,f (int)

     (ii) public functions:  

          factorial: a constructor to assign 1 to f

          factorial(int n): a constructor to assign num=n

          int GetFactorial() : to find and return factorial of f

import java.util.*;
class factorial{
  private int num,f;
  factorial(){
    f=1;
    num=0; //default value provided thus initialised
           //this is the main function of a construct to initialise
          //EVERY data members
  }
  factorial(int n){
    num=n;
    f=1;
  }
 
  int GetFactorial(){
        for(int i = 1; i <= num; ++i)
        {
           
            this.f *= i;
        }
        
        return f;
  }
  public static void main (String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter a no. to get factorial:");
    int m=sc.nextInt();
    factorial a=new factorial(m);
    System.out.print("Factorial of "+ m+"="+ a.GetFactorial() );
  }
}

Enter a no. to get factorial: Factorial of 5=120

 

PROG 3: Make a class Compound

               CI = [ p * ( 1 + r/100 ) ^t ) - p

      (i) data members: Pamt,rate (double)

                                   time (int)


     (ii) public functions:  

        Compound(double p,double r, double t)

       double FindInterest()

        void printData() 

 

 

import java.util.*;
class Compound{
  double Pamt,rate;
  int Time;
  Compound(){ //default constructor
    Pamt=0;
    rate=0;
    Time=0;
  }
  Compound(double p,double r, double t){
    Pamt=p;
    rate=r;
    Time=(int)t;
  }
  double FindInterest(){
    double CI =  Pamt * Math.pow(( 1 + rate/100 ) ,Time) - Pamt;
    return CI;
  }
  void PrintData(){
    System.out.println("Principal amount= Rs."+ Pamt);
    System.out.println("Rate of Interest="+ rate+ "%");
    System.out.println("Time Period="+ Time+ " Years");
  }
  public static void main (String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter principal amount :");
    double pr=sc.nextDouble();
    System.out.println("Enter rate of Interest :");
    double ra=sc.nextDouble();
    System.out.println("Enter time period :");
    double tr=sc.nextDouble();
    Compound A=new Compound(pr,ra,tr);
    A.PrintData();
    System.out.println("Compound Interest: Rs. "+ A.FindInterest());
    
  }
}

Enter principal amount :
2000
Enter rate of Interest :
Enter time period :
2
Principal amount= Rs.2000.0
Rate of Interest=5.0%
Time Period=2 Years
Compound Interest: Rs. 205.0

 

 

PROG 4: Make a class result

      (i) data members: nam (string)

                                   s1,s2,s3,max (double)


     (ii) public functions:  

       
        result()

       void accept()

        void Compute()

        void display()


import java.util.*;
public class result
{
    static String nam;
    static double s1,s2,s3,max;
    result() //initialise data members
             //default cons
    {
      nam="";
      s1=0;
      s2=0;
      s3=0;
      max=0;
    }
    result(String n,double eng,double phy,double math)
    {        //initialise with given values
      nam=n;
      s1=math;
      s2=eng;
      s3=phy;
      max=0;
    }
    void accept()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter name:");
        nam=sc.nextLine();
        System.out.println("Enter marks in 3 subjects:");
        s1=sc.nextDouble();
        s2=sc.nextDouble();
        s3=sc.nextDouble();
          
    }
    void Compute()
    {
        max=Math.max(s1,Math.max(s2,s3));
    }
    void display()
    {
        System.out.println("Name:"+nam);
        System.out.println("Subject 1 marks:"+s1);
        System.out.println("Subject 2 marks:"+s2);
        System.out.println("Subject 3 marks:"+s3);
        System.out.println("Maximum marks:"+max);
    }
    public static void main(String[]args)
    {
        result obj=new result(nam,s1,s2,s3);
        obj.accept();
        obj.Compute();
        obj.display();
    }
}

 

Enter name:
Rajat
Enter marks in 3 subjects:
45
46
78
Name:Rajat
Subject 1 marks:45.0
Subject 2 marks:46.0
Subject 3 marks:78.0
Maximum marks:78.0
 
 
 

 

PROG 5: Make a class movieMagic

      (i) data members:int year

                                  string title

                                  float rating


     (ii) public functions:  

       
        movieMagic()

       void accept()

        void display()



import java.util.*;
class movieMagic
{
    int year;
    String title;
    float rating;
    movieMagic()
    {
        year=0;
        title="";
        rating=0;
    }

    void accept()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter year:");
        year=sc.nextInt();
        System.out.println("Enter title:");
        title=sc.next();
        System.out.println("Enter rating:");
        rating=sc.nextFloat();
    }

    void display()
    {
        if(rating>=0.0&&rating<=2.0)
        {
            System.out.println("Title:"+title);
            System.out.println("The movie is Flop");
        }
        else if(rating>=2.1&&rating<=3.4)
        {
            System.out.println("Title:"+title);
            System.out.println("The movie is Semi Hit");
        }
        else if(rating>=3.5&&rating<=4.5)
        {
            System.out.println("Title:"+title);
            System.out.println("The movie is Hit");
        }
        else if(rating>=4.6&&rating<=5.0)
        {
            System.out.println("Title:"+title);
            System.out.println("The movie is Super Hit");
        }
    }

    public static void main(String[]args)
    {
        movieMagic mm=new movieMagic();
        mm.accept();
        mm.display();
    }
}

Enter year:
2022
Enter title:
Brahmastra
Enter rating:
4.5
Title:Brahmastra
The movie is Hit

 

PROG 6: Make a class Student

      (i) data members:int rollnum age

                                  double m1,m2,m3,maximum,averag


     (ii) public functions:  

       
       Student()

       void accept()

        void compute() 

        void display()

 

import java.util.*;
class Student{
  int rollNum,age; double m1,m2,m3,maximum,average;
  Student(){
    rollNum=0;
    age=0;
    m1=m2=m3=maximum=average=0;
  }
  Student(int r,int age,double eng,double hin,double bio){
    rollNum=r;
    this.age=age;
    m1=eng;m2=hin;m3=bio;
    maximum=0;
    average=0;
  }
  void accept(){
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter roll no:");
    rollNum=sc.nextInt();
    System.out.println("Enter age:");
    age=sc.nextInt();
    System.out.println("Enter marks in 3 subjects:");
    m1=sc.nextDouble();
    m2=sc.nextDouble();
    m3=sc.nextDouble();
    compute();
}
void compute(){
  maximum= Math.max(m1,Math.max(m2,m3));
  average= (m1+m2+m3)/3.0;
}
void display(){
  System.out.println("Roll number ="+rollNum);
  System.out.println("Age ="+age);
  System.out.println("Subject 1 marks ="+m1);
  System.out.println("Subject 2 marks="+m2);
  System.out.println("Subject 3 marks ="+m3);
  System.out.println("Maximum marks ="+maximum);
  System.out.println("Average ="+average);
}
  public static void main(String args[]){
    Student obj =new Student();
    obj.accept();
    obj.display();
  }
}

Enter roll no:
22
Enter age:
16
Enter marks in 3 subjects:
67
76
98
Roll number =22
Age =16
Subject 1 marks =67.0
Subject 2 marks=76.0
Subject 3 marks =98.0
Maximum marks =98.0
Average =80.33333333333333

 

Hence The Chapter finished 

Give your attaindence by typing name in comment

 

Termiraider

Rajat LFS here! I am a student at LFS MRJ. My stream is Biology and I am a villager and a farmer I guess. My interest is reading history.

1 Comments

Post a Comment
Previous Post Next Post