Advertisement
jaredec18

Untitled

Sep 19th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. //################# PGM START ##############################################
  2.  
  3. import java.util.Scanner;
  4. /*
  5. * function to calculate sensitivity or specificity
  6. * sensitivity = true positive rate = the actual positive which are identified correctly
  7. * specificity = true negative rate = the actual negative which are identified correctly
  8. */
  9.  
  10. public class Solution {
  11. public double solution(int A[],int B[],boolean q){
  12. double result=0;
  13. double TP=0; //true positive (1 & 1)
  14. double FP=0; // true negative (0 & 1)
  15. double FN=0; // false positive (1 & 0)
  16. double TN=0; //false negative (0 & 0)
  17.  
  18. //finding the values of TP,FP,FN,TN
  19. for(int i=0;i<A.length;i++){
  20. if(A[i]==1 && B[i]==1){
  21. TP++;
  22. }else if(A[i]==1 && B[i]==0){
  23. FN++;
  24. }else if(A[i]==0 && B[i]==1){
  25. FP++;
  26. }else{
  27. TN++;
  28. }
  29. }
  30. //if q is false calculate sensitivity
  31. //else calculate specificity
  32. if(q==false)
  33. result=TP/(TP+FN);
  34. else
  35. result=TN/(FP+TN);
  36.  
  37. return result*100;
  38. }
  39. public static void main(String[] args) {
  40. Scanner s=new Scanner(System.in);
  41. Solution obj=new Solution();
  42. boolean q=false;
  43. int selection;
  44. //requesting user to enter number of patients
  45. System.out.print("Enter the number of patients on test is conducted:");
  46. int N=s.nextInt();
  47.  
  48. int A[]=new int[N]; //stores actual data
  49. int B[]=new int[N]; //stores predicted value
  50.  
  51. //requesting user for actual data
  52. System.out.print("\nEnter the actual values, 0 -> negative & 1 -> possitive:");
  53. for(int i=0;i<N;i++){
  54. A[i]=s.nextInt();
  55. }
  56. //requesting user for predicted data
  57. System.out.print("\nEnter the predicted values, 0 -> negative & 1-> possitive:");
  58. for(int i=0;i<N;i++){
  59. B[i]=s.nextInt();
  60. }
  61. //providing options for user
  62. System.out.print("Choice\n\n1.Sensitivity\n2.Specificity\n\nEnter your choice:");
  63. selection=s.nextInt();
  64. if(selection==1)
  65. q=false;
  66. else if(selection==2)
  67. q=true;
  68. else
  69. System.out.println("Wrong entry");
  70.  
  71. if(selection==1)
  72. System.out.println("Sensitivity = "+obj.solution(A,B,q));
  73. else
  74. System.out.println("Specificity = "+obj.solution(A,B,q));
  75.  
  76.  
  77.  
  78. }
  79.  
  80. }
  81.  
  82.  
  83. //############################## PGM END ####################################
  84.  
  85. OUTPUT
  86. #######
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement