AiS Challenge-Team 013

Code




Team Number: 013

School Name: Bosque School

Area of Science: Medicine and Health

Project Title: Modeling the effictiveness of a vaccine against cervical cancer.


This is our code. It is written in java, and we explain it at the end.
/*
 * VirusSpread22.java
 *
 * Created on February 15, 2004, 8:24 AM
 */
package com.swaungcenter.VirusSpread2;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 * @author Colin Williams
 * @version 1
 */
public class VirusSpread2 extends JFrame implements ActionListener {
    //Declare all the labels
    private JLabel prop_Pop_High_Label;
    private JLabel prop_Pop_Med_Label;
    private JLabel prop_Pop_Low_Label;
    
    private JLabel risk_High_Label;
    private JLabel risk_Med_Label;
    private JLabel risk_Low_Label;
    
    private JLabel condom_Use_Label;
    
    private JLabel vacc_Eff_Label;
    
    private JLabel vacc_Acc_Label;
    
    private JLabel overall_Rate_Label;
    
    
    //Declare all the input fields
    private JTextField prop_Pop_High_TextField;
    private JTextField prop_Pop_Med_TextField;
    private JTextField prop_Pop_Low_TextField;
    
    private JTextField risk_High_TextField;
    private JTextField risk_Med_TextField;
    private JTextField risk_Low_TextField;
    
    private JTextField condom_Use_TextField;
    
    private JTextField vacc_Eff_TextField;
    
    private JTextField vacc_Acc_TextField;
    
    private JTextField overall_Rate_TextField;
    
    private JSeparator separator;
    
    //declare all the variables, claculate them if necessary
    double prop_Pop_High = .1;
    double prop_Pop_Med = .5;
    double prop_Pop_Low = .4;
        
    double risk_High = .6;
    double risk_Med = .3;
    double risk_Low = .15;
        
    double risk_Based_Level_High = ( prop_Pop_High * risk_High );
    double risk_Based_Level_Med = ( prop_Pop_Med * risk_Med );
    double risk_Based_Level_Low = ( prop_Pop_Low * risk_Low );
    
    double condom_Use;
    double condom_Effic = 0.6 ;
    double condom_Eff = condom_Use * condom_Effic;
    
    double vacc_Eff = 1;
        
    double vacc_Acc = 0.0;
    
    double vacc_Data = ( 1 - ( vacc_Acc * vacc_Eff ));
        
    double overall_Rate_High = ( risk_Based_Level_High * ( 1 - condom_Eff ) * vacc_Data );
    double overall_Rate_Med = ( risk_Based_Level_Med * ( 1 - condom_Eff ) * vacc_Data );
    double overall_Rate_Low = ( risk_Based_Level_Low * ( 1 - condom_Eff ) * vacc_Data );
        
    double overall_Rate_All = ( overall_Rate_High + overall_Rate_Med + overall_Rate_Low );
    
   
    
    
    public VirusSpread2() {
        //Set the title bar of the GUI
        super( "HPV 16 and 18 Simulation Console" );
        
        //get content pane and set its layout
        Container container = getContentPane();
        container.setLayout( new FlowLayout() );
        
        //create label and textfield for first set of variables
        prop_Pop_High_Label = new JLabel( "Proportion of population with a high sexual activity:                                                " );
        container.add( prop_Pop_High_Label );
        prop_Pop_High_TextField = new JTextField( "" + prop_Pop_High, 3 );
        prop_Pop_High_TextField.addActionListener( this );
        container.add( prop_Pop_High_TextField );
        
        //create label and textfield for second set of variables
        prop_Pop_Med_Label = new JLabel( "Proportion of population with a medium sexual activity:                                         " );
        container.add( prop_Pop_Med_Label );
        prop_Pop_Med_TextField = new JTextField( "" + prop_Pop_Med, 3 );
        prop_Pop_Med_TextField.addActionListener( this );
        container.add( prop_Pop_Med_TextField );
        
        //create label and textfield for third set of variables
        prop_Pop_Low_Label = new JLabel( "Proportion of population with a low sexual activity:                                                 " );
        container.add( prop_Pop_Low_Label );
        prop_Pop_Low_TextField = new JTextField( "" + prop_Pop_Low, 3 );
        prop_Pop_Low_TextField.addActionListener( this );
        container.add( prop_Pop_Low_TextField );
        
        //create label and textfield for fourth set of variables
        risk_High_Label = new JLabel( "Average risk of a person with a high sexual activity to get HPV 16 OR 18:        " );
        container.add( risk_High_Label );
        risk_High_TextField = new JTextField ( "" + risk_High, 3 );
        risk_High_TextField.addActionListener( this );
        container.add( risk_High_TextField );
        
        //create label and textfield for fifth set of variables
        risk_Med_Label = new JLabel( "Average risk of a person with a medium sexual activity to get HPV 16 OR 18: " );
        container.add( risk_Med_Label );
        risk_Med_TextField = new JTextField( "" + risk_Med, 3 );
        risk_Med_TextField.addActionListener( this );
        container.add( risk_Med_TextField );
        
        //create label and textfield for sixth set of variables
        risk_Low_Label = new JLabel( "Average risk of a person with a low sexual activity to get HPV 16 OR 18:          " );
        container.add( risk_Low_Label );
        risk_Low_TextField = new JTextField( "" + risk_Low, 3 );
        risk_Low_TextField.addActionListener( this );
        container.add( risk_Low_TextField );
        
        //create label and textfield for seventh set of variables
        condom_Use_Label = new JLabel( "Percent of population who use a condom                                                                   " );
        container.add( condom_Use_Label );
        condom_Use_TextField = new JTextField( "" + condom_Use, 3 );
        condom_Use_TextField.addActionListener( this );
        container.add( condom_Use_TextField );
        
        //create label and textfield for eighth set of variables
        vacc_Eff_Label = new JLabel( "Efficacy of the vaccine (i.e. how often does it work):                                               " );
        container.add( vacc_Eff_Label );
        vacc_Eff_TextField = new JTextField( "" + vacc_Eff, 3 );
        vacc_Eff_TextField.addActionListener( this );
        container.add( vacc_Eff_TextField );
        
        //create label and textfield for ninth set of variables
        vacc_Acc_Label = new JLabel( "Percentage of willing to take the vaccine:                                                                   " );
        container.add( vacc_Acc_Label );
        vacc_Acc_TextField = new JTextField( "" + vacc_Acc, 3 );
        vacc_Acc_TextField.addActionListener( this );
        container.add( vacc_Acc_TextField );
        
        //create textfield for results of simulation
        overall_Rate_Label = new JLabel( "Percentage of population with HPV 16 OR 18:                                                       " );
        container.add( overall_Rate_Label );
        overall_Rate_TextField = new JTextField( "" + overall_Rate_All, 5 );
        overall_Rate_TextField.setEditable( false );
        container.add( overall_Rate_TextField );
        
        //Create a separator to separate the two parts of the GUI
        separator = new JSeparator();
        container.add( separator );
        
        //set size and visibility for the GUI components
        setSize( 500,  300 );
        setVisible( true );
        
    }
    
    //Updater
    //Purpose:
    //update all variables, the set text in results field
    //Called when an action is performed
    public void updater() {
        double risk_Based_Level_High = ( prop_Pop_High * risk_High );
        double risk_Based_Level_Med = ( prop_Pop_Med * risk_Med );
        double risk_Based_Level_Low = ( prop_Pop_Low * risk_Low );
        
        double vacc_Data = ( 1 - ( vacc_Acc * vacc_Eff ));
        
        double condom_Eff = ( condom_Effic * condom_Use );
        
        double overall_Rate_High = ( risk_Based_Level_High * ( 1 - condom_Eff ) * vacc_Data );
        double overall_Rate_Med = ( risk_Based_Level_Med * ( 1 - condom_Eff ) * vacc_Data );
        double overall_Rate_Low = ( risk_Based_Level_Low * ( 1 - condom_Eff ) * vacc_Data );
        
        double overall_Rate_All = ( overall_Rate_High + overall_Rate_Med + overall_Rate_Low );
        
        overall_Rate_TextField.setText( "" + overall_Rate_All );
    }
    
    //Handle all ActionEvents from all textfields
    public void actionPerformed(ActionEvent event) {
            //Get text from first TextField and set its variable
            this.prop_Pop_High = Double.parseDouble( prop_Pop_High_TextField.getText() );
            updater();
            
            //Get text from second TextField and set its variable
            this.prop_Pop_Med = Double.parseDouble( prop_Pop_Med_TextField.getText() );
            updater();
            
            //Get text from third TextField and set its variable
            this.prop_Pop_Low = Double.parseDouble( prop_Pop_Low_TextField.getText() );
            updater();
            
            //Get text from fourth TextField and set its variable
            this.risk_High = Double.parseDouble( risk_High_TextField.getText() );
            updater();
            
            //Get text from fifth TextField and set its variable
            this.risk_Med = Double.parseDouble( risk_Med_TextField.getText() );
            updater();
            
            //Get text from sixth TextField and set its variable
            this.risk_Low = Double.parseDouble( risk_Low_TextField.getText() );
            updater();
            
            //Get text from seventh TextField and set its variable
            this.condom_Use = Double.parseDouble( condom_Use_TextField.getText() );
            updater();
            
            //Get text from eighth TextField and set its variable
            this.vacc_Eff = Double.parseDouble( vacc_Eff_TextField.getText() );
            updater();
            
            //Get text from ninth TextField and set its variable
            this.vacc_Acc = Double.parseDouble( vacc_Acc_TextField.getText() );
            updater();
    }
    public static void main( String args[] ) {
        // Create a new instance of this class and set it to close when the 'x' is clicked
        VirusSpread2 app = new VirusSpread2();
        app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    }
    
}

The first thing it does is declare nine JLabels-strings of text that can be displayed in a Graphical User Interface (GUI). It then declares nine JTextFields-areas with editable text fields which are used to input data. It then declares all variables which will be used in the program-see for explanations of each. The next thing to appear is its constructor. The first thing it does in the constructor is use JFrame’s constructor to set the title bar of the window which will be displayed. It then declares and creates a top-level window to be used for created an effective GUI. After this, it tells the window to arrange its contents in a FlowLayout (i.e. insert the first component in the top-left, the next to its right, and so on until it reaches the border of the window; then, start a new line). It then creates all the JLabels and JTextFields and adds them to the window, adding ActionListeners to the JTextFields. It sets the size of the window and tells the computer to display it. Then, the constructor’s done. The method updater is next, which recalculates all the calculated variables, the displays the result of the equation in the appropriate JTextField. Method actionPerformed is called when the user presses ‘Enter’ in one of the JTextFields, and gets all the variables’ new values, setting them correctly. It also calls updater(). The main method starts the whole process and sets the application to close when the ‘x’ button is clicked.



Team Members:

Sponsoring Teacher: Project Advisor: