Thursday 1 July 2021

Java Program to add new element, its atomic number and atomic weight, sort based on atomic number and search based on element name

 

A chemist needs to record data of elements in periodic table. Draw the class diagram and develop the classChemicalElement with name, atomicNum andatomicWeight as private attributes. Code the constructorand toString() method. The main() method inChemicalDemo class must store data of chemicals in acollection and is menu driven with the followingoptions: a) Add new element, b)sort based onatomicNum and c)search based on name.

Sol:

Class Diagram:                                                                                              

 

ChemicalElement

-name:String

-atomicNum:int

-atomicWeight:float

+ChemicalElement(name: String, atomicNum:int, atomicWeight:float)

+getName():String

+getAtomicNum():int

 

SortByAtomicNumber

 

+compare(c1: ChemicalElement, c2: ChemicalElement):int

 

 

Demo

~cel:ArrayList<ChemicalElement>

~sc:Scanner

+main(args:String[]):void

 

 

 

package key;

 

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.Scanner;

 

class ChemicalElement                                                                                    

{

            private String name;

            private int atomicNum;

            private float atomicWeight;

            public ChemicalElement(String name, int atomicNum, float atomicWeight)

            {

                        this.name = name;

                        this.atomicNum = atomicNum;

                        this.atomicWeight = atomicWeight;

            }

            public String getName() {

                        return name;

            }

            public int getAtomicNum() {

                        return atomicNum;

                       

            }

            public String toString() {

                        return "name=" + name + ", atomicNum=" + atomicNum + ", atomicWeight=" + atomicWeight;

            }         

}

class SortByAtomicNumber implements Comparator<ChemicalElement>                  

{

            public int compare(ChemicalElement c1, ChemicalElement c2)

            {

                        return (int)(c1.getAtomicNum()-c2.getAtomicNum());

            }

}

public class ChemicalDemo                                                                                        

{

            static ArrayList<ChemicalElement> cel= new ArrayList<ChemicalElement>();

            static Scanner sc = new Scanner(System.in);

            public static void main(String[] args)

            {

                        while(true)

                        {

                                    System.out.println("enter 1. Add new element 2. sort based on atomicNum 3. search based on name 4. exit");

                                    int choice=sc.nextInt();

                                    switch(choice)

                                    {

                                    case 1:

                                                System.out.println("enter name, atomic number and its atomic weight");

                                                cel.add(new ChemicalElement(sc.next(),sc.nextInt(),sc.nextFloat()));

                                                break;

                                    case 2:

                                                System.out.println("sorted names based on atomic number");

                                                Collections.sort(cel,new SortByAtomicNumber());

                                                for(ChemicalElement ob:cel)

                                                            System.out.println(ob);

                                                break;

                                    case 3:

                                                System.out.println("enter name to be searched");

                                                String key=sc.next();

                                                for(ChemicalElement ob:cel)

                                                            if(ob.getName().equals(key))

                                                            {

                                                                        System.out.println(ob);

                                                                        break;

                                                            }

                                                break;

                                    case 4:

                                                return;

                                    default:

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

                                               

                                    }

            }

 

}

}

 Output:

enter 1. Add new element 2. sort based on atomicNum 3. search based on name 4. exit

1

enter name, atomic number and its atomic weight

hydrogen

1

1.007

enter 1. Add new element 2. sort based on atomicNum 3. search based on name 4. exit

1

enter name, atomic number and its atomic weight

helium

2

4.002

enter 1. Add new element 2. sort based on atomicNum 3. search based on name 4. exit

2

sorted names based on atomic number

name=hydrogen, atomicNum=1, atomicWeight=1.007

name=helium, atomicNum=2, atomicWeight=4.002

enter 1. Add new element 2. sort based on atomicNum 3. search based on name 4. exit

3

enter name to be searched

helium

name=helium, atomicNum=2, atomicWeight=4.002

enter 1. Add new element 2. sort based on atomicNum 3. search based on name 4. exit

4


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