programes on methods

programes on methods

  1. write a program to implement bubble sort
  2. write a program to demonstrate call by value and call by reference.(pass objects as parameters)
  3. write a program to calculate factorial of a no. using recursive function
  4. write a program to concatenate two strings entered by user
  5. design a class called cricket to store information of players. Read information of 50 players and display information w.r.t. batting average.
View Answers

March 26, 2011 at 4:04 PM


March 26, 2011 at 4:05 PM

3)

import java.util.*;
class  Factorial{
    public static int findFactorial(int n)
    {
        if (n == 1) {
            return n;
        }
        else {
            return n * findFactorial(n - 1);
        }
    }
    public static void main(String[] args) 
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter number: ");
        int num=input.nextInt();
        System.out.println("The factorial of " +num+"  is : " + findFactorial(num));
    }
}

March 26, 2011 at 4:05 PM

4)

import java.util.*;
class ConcatenateStrings 
{
    public static void main(String[] args) 
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter string 1: ");
        String st1=input.nextLine();
        System.out.print("Enter string 2: ");
        String st2=input.nextLine();
        String str=st1.concat(st2);
        System.out.println("New String is: "+str);
    }
}

March 26, 2011 at 4:06 PM

5)

import java.util.*;

class ShowData {
        String name;
        int age;
        int matches;
        int runs;
        double average;
        ShowData(String name,int age,int matches,int runs,double average){
        this.name=name;
        this.age=age;
        this.matches=matches;
        this.runs=runs;
        this.average=average;
        }
        public void setName(String name){
                this.name = name;
        }
        public String getName() {
                return name;
        }
        public void setAge(int age) {
                this.age = age;
        }
        public int getAge(){
                return age;
        }

        public void setMatches(int matches) {
                this.matches = matches;
        }
        public int getMatches(){
                return matches;
        }

        public void setRuns(int runs) {
                this.runs = runs;
        }
        public int getRuns(){
                return runs;
        }

        public void setAverage(double average) {
                this.average = average;
        }
        public double getAverage(){
                return average;
        }
       }
class AverageComparator implements Comparator{
public int compare(Object o1, Object o2) {
                                double d1 = ((ShowData) o1).getAverage();
                                double d2 = ((ShowData) o2).getAverage();
                                return Double.compare(d1, d2);
                        }
}



public class ArrayListEx{
         public static void main(String[] args){
             Scanner input=new Scanner(System.in);
               ArrayList<ShowData> list=new ArrayList<ShowData>();
               for(int i=0;i<5;i++){
                  System.out.print("Enter name: ");
                  String name=input.next();

                  System.out.print("Enter Age: ");
                  int age=input.nextInt();

                  System.out.print("Enter Matches: ");
                  int matches=input.nextInt();

                  System.out.print("Enter Runs: ");
                  int runs=input.nextInt();

                  System.out.print("Enter Batting Average: ");
                  double average=input.nextDouble();


               list.add(new ShowData(name,age,matches,runs,average));
               }
               System.out.println("Display record with respect to Batting Average: ");
               Collections.sort(list,new AverageComparator());
                for(ShowData data: list){
                System.out.println(data.getName()+"\t "+data.getAge()+"\t "+data.getMatches()+"\t "+data.getRuns()+"\t "+data.getAverage());
                }
            }
}

January 4, 2013 at 10:43 AM

  1. Boubble sort

public class BoubbleSort {

void boubbleSort(int[] array) { for(int i=0;iarray[j]) { int temp=array[i]; array[i]=array[j]; array[j]=temp; } } } }

void displayElements(int[] array) { for(int a:array) System.out.print(a +" "); System.out.println(); }

public static void main(String args[]) {

    int[] array={10,4,2,7,8,9};
    BoubbleSort bsort=new BoubbleSort();
    System.out.println("befor sorting ");
    bsort.displayElements(array);
    bsort.boubbleSort(array);
    System.out.println("after sorting");
    bsort.displayElements(array);

} }

Ads









Related Tutorials/Questions & Answers:
programes on methods
programes on methods   write a program to implement bubble sort write a program to demonstrate call by value and call by reference.(pass objects as parameters) write a program to calculate factorial of a no. using recursive
programes on for loop
programes on for loop  a. write a program to find squares and cubes of 1st 10 integers b. write a program to to calculate factorial of a no, c. write a program to print Fibonacci series d. write a program to check whether entered
Advertisements
programes on for loop
programes on for loop  a. write a program to find squares and cubes of 1st 10 integers b. write a program to to calculate factorial of a no, c. write a program to print Fibonacci series d. write a program to check whether entered
programes on for loop
programes on for loop  a. write a program to find squares and cubes of 1st 10 integers b. write a program to to calculate factorial of a no, c. write a program to print Fibonacci series d. write a program to check whether entered
programes on for loop
programes on for loop  a. write a program to find squares and cubes of 1st 10 integers b. write a program to to calculate factorial of a no, c. write a program to print Fibonacci series d. write a program to check whether entered
methods
methods  PrintStream class has two formatting methods,what
methods
methods  PrintStream class has two formatting methods,what
methods
methods  PrintStream class has two formatting methods,what
programes on strings
programes on strings   a. write a program to copy one array to another array using System.arrayCopy() method. b. write a program to check whether entered string is palindrome or not.   Have a look at the following link
programes on switch
programes on switch   write a program to design menu driven arithmetic calculator write a program to print no of days in a given month   import java.util.*; class Calculator { public static void main(String[] args
programes on if....else
programes on if....else   write a program to check whether entered year is leap year or not write a program to check whether entered no. ends with 5 or not write a program to find minimum of 3 nos using nested if else. write
programes on array
programes on array  a. write a program to find max and min element in an array of an integer b. write a program to convert decimal no. to binary and back to decimal. c. write a program to do following:- i) addition of two
programes on while loop
programes on while loop   write a program to calculate sum of an entered digit write a program to find gcd and lcm of 2 positive integers   Java sum of digits Java Find LCM and GCD
programes on do... while
programes on do... while   write a program to print reverse of a given digit write a program to check whether entered digit is palindrome or not write a program to check whether entered no. is Armstrong or not   1
static methods
static methods  why static methods cannot read or write the instance variables
native methods
native methods  what is native methods in java?   A native method is a method that is implemented in a language other than Java. The Java native method is a great way to gain and merge the power of C or C++ programming
Various methods of httpservletresponse interface
Various methods of httpservletresponse interface  What are the various methods of httpservletresponse interface
ModuleNotFoundError: No module named 'methods'
ModuleNotFoundError: No module named 'methods'  Hi, My Python... 'methods' How to remove the ModuleNotFoundError: No module named 'methods... to install padas library. You can install methods python with following command
GET and POST methods
GET and POST methods   What are the differences between GET and POST methods in form submitting, give the case where we can use GET and we can use POST methods
factory methods in java?
factory methods in java?  what are factory methods in java?   Hi Friend, Factory methods are static methods that return an instance of the native class like Pattern.compile(), Calendar.getInstance
Final Methods - Java Tutorials
methods The final method  can be declare as follows: public final String
Servlet Methods
Servlet Methods In this section we will read about the various methods... methods that are used to initialize a Servlet, handles the request received..., the Servlet methods are called life-cycle methods of Servlet. Following
abstract methods in java
abstract methods in java  what is abstract methods in java.give better examples for understanding   Hi Friend, Please visit the following link: http://www.roseindia.net/java/master-java/abstract-class.shtml Thanks
Callback Methods
Callback Methods       Callbacks methods are the way of managing life cycle of an instance. Callback methods are generally used by containers. The methods are called at specific time
looping through a list of methods
looping through a list of methods  I have a number of methods that do the almost same thing: public void setColorRed (int colorvalue); public...); Is there a way to place these methods in a list and then call each one from
What are weak methods?
What are weak methods?  Hi, I am beginner in Data Science... methods? Try to provide me good examples or tutorials links so that I can learn the topic "What are weak methods?". Also tell me which is the good
What modifiers are allowed for methods in an Interface?
What modifiers are allowed for methods in an Interface?   Hi, What modifiers are allowed for methods in an Interface? thanks
list of predeined methods and use java
list of predeined methods and use java  I need list of predefined methods in java like reverse,compare,tostring, etc
what is class methods in objective c
what is class methods in objective c  What is class methods in objective c? Explain the class method of objective c with the help of an example
what is class methods in objective c
what is class methods in objective c  What is class methods in objective c? Explain the class method of objective c with the help of an example
An application using swings and vector methods
An application using swings and vector methods   Hi, I want an application in Java swings which uses good selection of Vectors methods

Ads