Showing posts with label 1D and 2D arrays in java. Show all posts
Showing posts with label 1D and 2D arrays in java. Show all posts

Thursday, 1 July 2021

Java Program to add Football player details and sort them by number of players

 

A university records data of its Football players. Drawthe class diagram and develop the class FootballPlayerwith name, numberOfGoals and numberOfMatches.Code the constructor, toString() method. The main()method in Demo class must store data of footballplayers in a collection and is menu driven with thefollowing options: a) Add new player b)sort by numberof goals.

Class diagram                                                                                               

 

FootballPlayer

-name:String

-numberOfGoals:int

-numberOfMatches:int

+FootballPlayer(name: String, numberOfGoals:int, numberOfMatches:int)

+getNumberOfGoals():int

 

SortByNumberOfGoals

 

+compare(f1:FootballPlayer, f2: FootballPlayer):int

 

 

Demo

 

+main(args:String[]):void

 

 

 

FootballPlayer.java                                                                                        

package key;

 

public class FootballPlayer

{

            private String name;

            private int numberOfGoals,numberOfMatches;

           

            public int getNumberOfGoals() {

                        return numberOfGoals;

            }

 

            public void setNumberOfGoals(int numberOfGoals) {

                        this.numberOfGoals = numberOfGoals;

            }

 

            public FootballPlayer(String name, int numberOfGoals, int numberOfMatches)

            {

                        this.name = name;

                        this.numberOfGoals = numberOfGoals;

                        this.numberOfMatches = numberOfMatches;

            }

           

            public String toString()

            {

                        return "name=" + name + ", numberOfGoals=" + numberOfGoals + ", numberOfMatches="

                                                + numberOfMatches;

            }

           

           

}

 

Demo.java

package key;

 

import java.util.*;

class SortByNumberOfGoals implements Comparator<FootballPlayer>                    

{

            public int compare(FootballPlayer f1,FootballPlayer f2 )

            {

                        return (int)(f1.getNumberOfGoals() -f2.getNumberOfGoals());

            }

}

public class Demo                                                                                          

{

            public static void main(String[] args)

            {

                        List<FootballPlayer> fp = new ArrayList<FootballPlayer>();

                        Scanner sc= new Scanner(System.in);

                       

                        while(true)

                        {

                                    System.out.println("enter 1. Add new player 2.sort by numberof goals 3. exit");

                                    int choice=sc.nextInt();

                                    switch(choice)

                                    {

                                    case 1:

                                                System.out.println("enter player name, number of goals, numbe of matches");

                                                fp.add(new FootballPlayer(sc.next(),sc.nextInt(),sc.nextInt()));

                                                break;

                                    case 2:

                                                System.out.println("sorted players list based on number of goals");

                                                Collections.sort(fp,new SortByNumberOfGoals());

                                                for(FootballPlayer ob:fp)

                                                            System.out.println(ob);

                                                break;

                                    case 3:

                                                return;

                                    default:

                                                System.out.println("invalid choice");

                                               

                                    }

                        }

                       

            }

 

}

 

Output:

enter 1. Add new player 2.sort by numberof goals 3. exit

1

enter player name, number of goals, numbe of matches

sarvani

34

20

enter 1. Add new player 2.sort by numberof goals 3. exit

1

enter player name, number of goals, numbe of matches

lakshmi

12

40

enter 1. Add new player 2.sort by numberof goals 3. exit

2

sorted players list based on number of goals

name=lakshmi, numberOfGoals=12, numberOfMatches=40

name=sarvani, numberOfGoals=34, numberOfMatches=20

enter 1. Add new player 2.sort by numberof goals 3. exit

3

Wednesday, 24 March 2021

1D and 2D arrays in java

1D arrays in java

  1. Syntax to declare an integer array in java
    int []a;  or int a[];   or int[] a;
  2. Allocating memory to store 3 integers (instantiation)
    a=new int[3];
    you can declare and instantiate in same line by writing int a[]=new int[3];
  3.  Initialization or giving values
    a[0]=2;
    a[1]=12;
    a[2]=5;
    (or) take array values from user
    for(int i=0; i< a.length;i++)
                a[i]=sc.nextInt();
    here sc is an object of Scanner class
  4. you can declare, instantiate and initialize in same line as int a[]={1,2,3,4};
  5. Displaying all elements of array using for each loop
    for(int ele: a)
            System.out.println(ele);
  6.  Displaying elements using for loop
    for(int i=0; i< a.length;i++)
                System.out.println(a[i]);
    here length is a property of arrays that will return the size of array
  7. Linear Search function in java that returns index if search element found otherwise returns -1
    public static int linearSearch(int a[],int key)
    {
           for(int i=0;i<a.length;i++)
           {
                     if(a[i] == key)
                            return i;
            }
            return -1;
    }

2D arrays in java

Declaration:
int[][] a;  or int[][] a;   or int [][]a;

Instantiation:
a = new int[3][3];
We can do declaration and instantiation in same line by writing int a[][] = new int[3][3];

Initialization and giving values
for(int i=0;i<a.length;i++)
{
           for(int j=0;j<a[i].length;j++)
                     a[i][j]=sc.nextInt();
}
here  a.length will give number of rows and a[i].length gives number of columns in each row.
and sc is object of Scanner class

Initialization, instantiation and initialization in single line
int a[][]={{1,2,3},{4,5,6},{7,8,9}};