This is a Factorial calculator. Enter an integer (small number please). Click the "Compute Payment" button, its factorial will be computed.

test 123

 

Planning Document:

Use Case : To calculate the factorial of a number

Primary Actor: User

-Create Factorial class

-Create User Interface prompting user for number

-Compute factorial and display output

-code/test/debug

 

Time Sheet (hours):

Planning : 20 – (time spent in studying)

Coding : 2

Test/Debug : 0.5

Total: 22.5

 

References used:  Intro to Java Programming; By Daniel Liang

 

Code:

<!-- HTML code, this code is separated from the preceding Java code -->

<html>

  <head>

    <title>Factorial Applet</title>

  </head>

  <body>

    This is a Factorial calculator. Enter an integer(small number please). Click the "Compute Payment" button, you will

    get the factorial. <p>

    <applet

      code = "ex_4dot1_jia_applet.class"

      width = 300

      height = 300

      alt = "You must have a Java 2-enabled browser to view the applet">

              test 123

    </applet>

  </body>

</html>

 

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

public class  ex_4dot1_jia_applet extends JApplet

{

            //declare fields to enter the number and display calculated factorial

            private JTextField jtfInputNumber = new JTextField();

            private JTextField jtfCalculatedFactorial = new JTextField();

 

            //declare a "compute factorial" button

            private JButton jbtComputeFactorial = new JButton("Compute Factorial");

 

            //create user interface

            public void init(){

                        jtfCalculatedFactorial.setEditable(false);

 

                        //panel to hold label and text fields

                        JPanel p1 = new JPanel(new GridLayout(5,1));                    

 

                        //right align text fields

                        jtfInputNumber.setHorizontalAlignment(JTextField.RIGHT);

                        jtfCalculatedFactorial.setHorizontalAlignment(JTextField.RIGHT);

 

 

                        //add components to panel

                        p1.add(new JLabel("Factorial Calculator"));

                        p1.add(jtfInputNumber);

                        p1.add(jtfCalculatedFactorial);

                        p1.add(jbtComputeFactorial);

 

                        //now add components to applet

                        add(p1);

 

                        //register an event listener to button

                        jbtComputeFactorial.addActionListener(new ButtonListener());

 

            }

 

            //event handler for compuet loan button

 

            private class ButtonListener implements ActionListener{

                        public void actionPerformed(ActionEvent e){

                                    //obtain the number entered by user

                                    double inputNumber = Double.parseDouble(jtfInputNumber.getText());

                                    //create Factorial object of Factorial class

                                    Factorial factorial_class = new Factorial(inputNumber);

                                    //now calculate factorial

                                    double calculatedFactorial = factorial_class.factorialCalculator();

                                   

                                    //display calculated factorial

                                    jtfCalculatedFactorial.setText(String.format("%5.0f",calculatedFactorial));

 

 

            }

 

            }

}

 

//program that calculates the factorial of an integer that is entered as input

import java.util.*;

public class Factorial

{

            private double number;

            //default constructor

            public Factorial(){

            }

 

            //constructor

            public Factorial( double number){

                        this.number = number; //initialize number field

            }

 

            //method to calculate factorial

            public double factorialCalculator(){

                        double product = 1.0;

                        for (int i = (int)number; i >= 1 ; i-- )

                        {

                                    product *= i;

                        }

                                    return product;

            }

 

}

 

 

 



Ex 4-3 (Screen shots of before and after)

Application to read,parse and write out to txt file

4.3b4
4.3b4

Java Code: EmailApp


package myprog.single;
import java.io.*;
class  EmailApp
{
	public static void main(String[] args) {
		String[] stringArray = new String[3];
		String fname;
		String lname;
		String ssn,email;

			if (args.length >= 1) {
				try {
						BufferedReader in = new BufferedReader(new FileReader(args[0]));
						PrintWriter out = new PrintWriter( new BufferedWriter( new FileWriter( args[1])));
						
						String record, field;
						char delim =':';   // the delimiter
						for (int n = 1; (record = in.readLine()) != null; n++) {

							//System.out.println("Record  " + n);
							int begin, end, i;
							begin = 0;

							///*
							
							for (i = 0; (end = record.indexOf(delim, begin)) >= 0; i++) {
								
								field = record.substring(begin, end);
								stringArray[i] = field;
								begin = end + 1;  // skip the delimiter
								//System.out.println("\tField " + i + ": " + field);

								}
								
								String s = stringArray[0];
								String t = s.toLowerCase();
								//System.out.println(t);
								fname = t.charAt(0) + "";
								//System.out.println(fname);
								
								String s2 = stringArray[1];
								String t2 = s2.toLowerCase();
								//System.out.println(t2);
								lname = t2.charAt(0) + "";
								//System.out.println(lname);

								field = record.substring(begin);  // the last field
								stringArray[2] = field;

								ssn = stringArray[2].substring(5,9);

								email = fname + lname + ssn + "@se.depaul.edu";

								//*/
								
								out.println(email + "\n");
						}

						out.flush();
						out.close();
				}

				catch(FileNotFoundException e){
						System.out.println("Incorrect input file name entered as argument: " + e.getMessage());
				}

				catch(NumberFormatException e){
						System.out.println("The file contains non integrs, please check and correct " + e.getMessage());
				}	  
		  
				catch (IOException e) {
						e.printStackTrace();
						}

			}
		}
}




Ex 4-2 / 4.7(a) / 4.8 combined (Screen shots of output)

Application to read in file, calculate average, implement error handling and use packages

4.7a
4.8a
4.8b

Java Code: Ex_4dot2_average


package myprog.single;
import  java.io.*;
import java.util.*;

/**
 *  This program copies a text file to antoher file. 
 *  The first argument is the input file name. The second argument is the output file name.
 */

//package myprog.single;
public class Ex_4dot2_average {

  public static void main(String[] args) {
    if (args.length >= 1) {
      try {
	BufferedReader in = new BufferedReader(
				new FileReader(args[0]));	
	String  line, outputDisplay = "";
	//sum of integers
	int sum =0;
	//count of number of integers
	int count = 0;
	//convert string read in to integer
	int numberRead;
	double average;
	while ((line = in.readLine()) != null) {
	  //create string tokenizer
	  StringTokenizer st = new StringTokenizer(line);	  
	  while (st.hasMoreTokens())
	  {
		  numberRead = Integer.parseInt(st.nextToken());
		  sum += numberRead;
		  count++;
	  }
	  outputDisplay += (line + " ") ;
	}
	//calculate average
	  average = sum/(count*1.0);

	  //print output
	  System.out.println ("The following integers were read in: " + outputDisplay + "\n The total number of integers: " + count + " \n Their average = " + average);  
      } 
	  
	  catch(FileNotFoundException e){
		  System.out.println("Incorrect input file name entered as argument: " + e.getMessage());
	  }

	  catch(NumberFormatException e){
		  System.out.println("The file contains non integrs, please check and correct " + e.getMessage());
	  }	  
		  
	  catch (IOException e) {
		  e.printStackTrace();
	  }
    }
  }

}