Hello. Anyone familiar with the Halstead product metrics? we are going to implement it. and again i need some help. here is our algorithm:

1. The user runs the program.
2. The program will ask the user to browse the file he/she wants to be evaluated using the programer's own definition of halstead's product metrics.
3. The program will count the number of operators and operands of the browsed file. The operators and operands will depend in the own definition of the programmer.
3.1 Initialize variable "Doperand" and "Doperator" where the number of distinct operands and distinct operators will be stored respectively.
3.2 The Doperand variable will increment everytime the method of counting the tokens will encounter an operand that is not yet counted by the method.
3.3 Same goes to Doperator variable.
3.4 Initialize variable "operand" and "operator" where the total number of operands and operators will be stored respectively.
3.5 The operand variable will increment everytime the method of counting the tokens will encounter an operand.
3.6 Same goes to operator variable.
3.7 When the method count will no longer encounter a token, the program will display the total number of operands, operators , distinct operators and distinct operands.
4. The user will now then click the "Compute" button to compute for the Program Vocabulary, Program Length, Volume, Difficulty, Level, Effort, Time and Fault Prediction.
4.1 The program will use the counted number of operands, operators, distinct operands and distinct operators.
4.2 The formulas to be used are based on Halstead's formulas.
5. When the program is finish computing, the program will display the computed outputs.

I like to ask if you think this algorithm is okay. And ideas on how to implement it. thanks.

Member Avatar for coil

To determine if it's "okay", all you need to do is see if it fits your requirements and does what you want it to do. If it does, then it's "okay".

As for implementation, it really depends on what you want to do as well. Do you want to do a Swing application, or a command-line application? What is your definition of the Halstead metrics? How do you use your variables to determine program vocabulary, length, etc?

Other people can help you by posing questions that will get you on the right track, or helping you debug your code, but ultimately, planning and writing a program comes down to you.

I was thinking using the LOC program that i have.. It can read file already, but still lost on what to do next.

here's the code:

import java.io.*;
import java.awt.*;
import javax.swing.*;
class FileRead2 extends JFrame {
	static File name;
	static int count =0;
	static int count2=0;


public 	FileRead2(){
	try{
	  FileDialog fd = new FileDialog(this,"",FileDialog.LOAD);
      fd.setDirectory(System.getProperty("user.dir"));
      fd.setVisible(true);
   	  name= new File(fd.getDirectory(), fd.getFile());
   	 }catch(Exception e){
   	 	System.err.println(e.getMessage());
   	 	}
   	setSize(400,400);
   	setVisible(true);
   	 }

   	 public void paint(Graphics g){
   	 	super.paint(g);
   	 	g.drawString("Testing open file", 30,100);
   	 	g.drawString("Number of characters' lines: " + count,30,200);
   	 	g.drawString("Number of empty lines :" + count2, 30, 250);
   	 }

   public static void main(String args[]){
   	FileRead2 fr = new FileRead2();
   	fr.setDefaultCloseOperation(2);

      try{
    FileInputStream fstream = new FileInputStream(name);
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;


    while((strLine = br.readLine())!= null ){
    	if (strLine.trim().length() != 0){
    		System.out.println(strLine);
    		count++;
    	}else{
    		count2++;
    	}
    }
    System.out.println("number of lines:" + count);
    System.out.println("number of lines:" + count2);

    in.close();
    }catch (Exception e){
      System.err.println("Error: " + e.getMessage());
    }
    fr.repaint();
}
}

how do i count the operators and operands, i find it hard.. thinking bout the operators, (), {}, +, -, *...

Member Avatar for coil

Check out this document. It covers operators and operands for Halstead in Java.

thanks for the link. its helpful. Maybe we'll just limit the operators that the were gonna include to count. am really confused as to how to count dem. i'm thinking of comparing. Like wen the code reads the file, if it encounters like for example +=, it checks if it, lets say i have an array containing the operators that we defined for our halstead, has that operator in the array. if yes, increment the operator variable. Would this be ok?

Member Avatar for coil

Yes, that theoretically should work. I've never personally done this project though. The best way to find the answer is simply to code it and test it out.

thanks for the link. its helpful. Maybe we'll just limit the operators that the were gonna include to count. am really confused as to how to count dem. i'm thinking of comparing. Like wen the code reads the file, if it encounters like for example +=, it checks if it, lets say i have an array containing the operators that we defined for our halstead, has that operator in the array. if yes, increment the operator variable. Would this be ok?

How are you going to distinguish between += and + or ++ ? When you're looking at
j+=1, how do you know you're looking at a += and not a simple +?
That is, how are you going to get your tokens for comparison?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.