CODING JOY

The stunning potpourri of coding and mundane life

CHOLESTEROL LEVEL ANALYSER GUI USING JAVA AWT



AMRUHA AHMED
30th March,2023.


This article will help you create a cholesterol level analyser using Java AWT.
Java AWT (Abstract Window Toolkit) is an API that aids in creation of window-based applications in Java Language.
Based on the readings of total cholesterol, LDL cholesterol,HDL cholesterol and triglycerides of a patient’s blood sample taken in mg/dL obtained from Cholesterol Test/ Lipid Profile, the task at hand is to interpret these readings and check whether the patient has an ideal level of cholesterol or not.


blog1

DELVING DEEPER:


Four parameters need to be provided to the program to check the normalcy of cholesterol level. The parameters are as follows:
1.total_cholesterol: this is the total of the blood sample’s cholesterol content
2.ldl_cholesterol: LDL is Low Density Lipoprotein. This is called as the “Bad cholesterol”
3.hdl_cholesterol: HDL is High Density Lipoprotein.This is also known as “Good Cholesterol”
4.triglycerides:this is a form of fat in the body which is harmful in large quantities.
For convenience during analysis, the above parameters are assumed to be in mg/dL.
Based on the parameters specified, the following conclusions are reached by the program:

blog1

COMPONENTS OF THE INTERFACE:
1. Frame:to contain the other components of the interface.It is named as f.
2. Labels :to display a single line of text. Here, 6 different labels are used:
l1-to display the text “Enter the total cholesterol in mg/dL”
l2- to display the message “Enter the ldl cholesterol in mg/dL”
l3-to display the message “Enter the hdl cholesterol in mg/dL”
l4-to display the text “Enter the triglycerides in mg/dL”
l5-to display the result
title-to display the following “Cholesterol Test Analyzer GUI”
3.TextField:This is used for accepting a single line of text from the user. Here, it is used for accepting data. The following are the TextFields used for creating this interface:
tf1- to enter the value of total cholesterol
tf2-to enter the value of LDL cholesterol
tf3-to enter the value of HDL cholesterol
tf4-to enter the value of triglycerides.
4.Button: When this button “Submit Results “ is clicked, the actionPerformed () will be invoked which interprets all the values inputted and the message is displayed in Label l5. It is named as b.



FUNCTIONS REQUIRED:
Firstly, a class named cholesterol level needs to be defined that implements ActionListener interface. Here the packages of java.awt and java.awt.event also need to be imported in the beginning of the program. The functions along with their uses are defined as follows:
1.addComponents() - this adds the various components such as Button, Labels and TextFields into the container.
2.fontSetter()-this function sets the font style, font size and font weight to the components of the interface.
3.boundSetter()-this function sets the x-coordinate,y-coordinate,width and height of each component of the interface.
4.actionPerformed()-this is a method contained in the ActionListener interface that collects the data entered by the user, converts it to float and interprets it accordingly.
5.cholesterollevel()-this is a constructor used for initializing the components of the interface and invokes all the methods appropriately.
6.main()-invokes the constructor.

ALGORITHM:
Since the actionPerformed function contains the main logic of the entire program, here is an algorithm for defining this function to satisfy the given problem.
Note: Button b needs to be associated with the ActionListener interface so that when it is clicked, the actionPerformed function gets invoked .
1.Fetch the inputted text from tf1 and store it in ‘total’ variable
2.Obtain the inputted text from tf2 and store it in the ‘ldl’ variable.
3.Fetch the inputted text from tf3 and store it in the ‘hdl’ variable
4.Obtain the inputted text from tf4 and store it in the ‘tri’ variable
5.Convert total,ldl,hdl,tri from String to float and store the results in total_cholesterol,ldl_cholesterol, hdl_cholesterol and triglycerides, respectively.
6.Now, these values are interpreted according to the conclusions drawn above and the appropriate resultant message is set as the text for Label l5.
The following program implements the algorithm stated above:


                
import java.awt.*;
import java.awt.event.*;
class cholesterollevel implements ActionListener
{
    Frame f;
    Label l1,l2,l3,l4,l5,title;
    TextField tf1,tf2,tf3,tf4;
    Button b;
    cholesterollevel()
    {//initializing the components
        f=new Frame("Cholesterol Test Analyzer GUI");
        title=new Label("Cholesterol Test Analyzer GUI");
        l1=new Label("Enter the total cholesterol in mg/dL");
        l2=new Label("Enter the ldl cholesterol in mg/dL");
        l3=new Label("Enter the hdl cholesterol in mg/dL");
        l4=new Label("Enter the triglycerides in mg/dL");
        l5=new Label();
        b=new Button("Submit Results");
        tf1=new TextField();
        tf2=new TextField();
        tf3=new TextField();
        tf4=new TextField();
        //calling the appropriate functions
        boundSetter();
        fontSetter();
        b.addActionListener(this);//linking Button to ActionListener
        f.setVisible(true);//setting visibility of Frame
        f.setLayout(null);//setting Layout of Frame
        //calling the appropriate function
        addComponents();
        
        
    }
    void addComponents()
    {//to add the various components to the container f
         f.add(l1);
        f.add(l2);
        f.add(l3);
        f.add(l4);
        f.add(l5);
        f.add(title);
        f.add(b);
        f.add(tf1);
        f.add(tf2);
        f.add(tf3);
        f.add(tf4);
    }
    
    void fontSetter()
    {//to set the font style,font weight, font size of each component
    title.setFont(new Font("Arial", Font.BOLD, 30));
      l1.setFont(new Font("Arial", Font.PLAIN, 22));
      tf1.setFont(new Font("Arial", Font.PLAIN, 22));
      l2.setFont(new Font("Arial", Font.PLAIN, 22));
      tf2.setFont(new Font("Arial", Font.PLAIN, 22));
      l3.setFont(new Font("Arial", Font.PLAIN, 22));
      tf3.setFont(new Font("Arial", Font.PLAIN, 22));
      l4.setFont(new Font("Arial", Font.PLAIN, 22));
      tf4.setFont(new Font("Arial", Font.PLAIN, 22));
      l5.setFont(new Font("Arial", Font.PLAIN, 22));
      b.setFont(new Font("Arial", Font.BOLD, 25));
    }
    void boundSetter()
    {//to set the bounds of each component
         f.setSize(1000,900);
      title.setBounds(300,50,900,30); 
      l1.setBounds(50,100,500,50);
      tf1.setBounds(600,100,200,50);
      l2.setBounds(50,170,500,50);
      tf2.setBounds(600,170,200,50);
      l3.setBounds(50,240,500,50);
      tf3.setBounds(600,240,200,50);
      l4.setBounds(50,310,500,50);
      tf4.setBounds(600,310,200,50);
      b.setBounds(350,380,200,50);
      l5.setBounds(250,500,500,50);
        b.setBackground(Color.PINK);
    }
    
    public void actionPerformed(ActionEvent e)
    {//fetching data from the TextFields
        String total=tf1.getText();
        String ldl=tf2.getText();
        String hdl=tf3.getText();
        String tri=tf4.getText();
        //converting the data fetched to float
        float total_cholesterol=Float.parseFloat(total);
        float ldl_cholesterol=Float.parseFloat(ldl);
        float hdl_cholesterol=Float.parseFloat(hdl);
        float triglycerides=Float.parseFloat(tri);
        //interpreting the cholesterol test values
        if(total_cholesterol<200 && ldl_cholesterol<130 && hdl_cholesterol>50 && triglycerides<200)
        l5.setText("Ideal Level of cholesterol");
        else if((total_cholesterol>=200 && total_cholesterol<=239) && (ldl_cholesterol>=130 && ldl_cholesterol<=159) && (hdl_cholesterol>=40 && hdl_cholesterol<=49) && (triglycerides>200 && triglycerides<=399))
        l5.setText("Borderline High Level of Cholesterol detected");
        else
        l5.setText("High Levels of Cholesterol detected");
        
        
    }
    
    void main()
    {//invoking the constructor
        cholesterollevel c=new cholesterollevel();
    }
}

                   
             
         
CODE COPIED

OUTPUT:
When an object of cholesterollevel class is created, the following interface is displayed.Here, the inputs are provided and the appropriate result is displayed.
blog1