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


No comments:

Post a Comment