Archive for July 11th, 2007

Java – Activity1.1

July 11, 2007

Activity1.1            : Command line programming – Method Overloading

Program File         : MethodOverload.java

Procedures            : 1. Type the program file.     

                               : 2. Save/Compile the program.                       

                               : 3. Using the Command Prompt (C:\>), execute the following:

                                    Path = C:\jdk\bin (or look for the equivalent directory)                                   

                                    Javac MethodOverload.java          (compiling the program)                  

                               :  4. Execute the program with test data.                            

                                     Scenario1:       Java MethodOverload 5

                                    Output:            The area is : 78.54                             

                                    Scenario2:       Java MethodOverload 5 3                             

                                    Output:            The area is : 12.5 

                                     Scenario3:       Java MethodOverload                                      

                                     Output:            error 

                                     Scenario4:        Java MethodOverload 1 2 3                                     

                                     Output:            error

 

public class MethodOverload

{ static double n1,n2,a;

            static double area(double n1)

            { a = 3.1416 * n1*n1;

               return a;

            }

static double area(double n1, double n2)

{ a = 1/2.0 * n1*n1;

   return a;

}

public static void main(String[] args)

{

               if (args.length>2 || args.length<1)

                        System.out.println(“error”);

                        else

                        {

                            n1 = Double.parseDouble(args[0]);

                             if (args.length==1)

                                       System.out.println(“The area is: “+ area(n1));

                                      else

                                      {

                                            n2 = Double.parseDouble(args[1]);

                                         System.out.println(“The area is: “+ area(n1,n2));

                                      }

                        }

            }

}

Java – Activity1

July 11, 2007

Activity1: Command line programming.

Program File   : Sum.java

Procedures     : 1. Type the program file.

                        : 2. Save/Compile the program.

                        : 3. Using the Command Prompt (C:\>), execute the following:

                                    Path = C:\jdk\bin        (or look for the equivalent directory)

                                    Javac Sum.java          (compiling the program)

                                    Java Sum 2 3              (running the program with test data)

                        : 4. The result should look like this:

                                    The sum is: 5.0

                        : 5. If the command

                                    java Sum 5

or

java Sum 1 2 3

is executed, a message “Invalid number of inputs!” will be displayed.

 

public class Sum

{

            static double n1, n2;

           

            public static void main( String args[] )

            {

                        int numArgs = args.length;

                       

                        if( numArgs >2 || numArgs < 2 )

                                    System.out.println( “Invalid number of inputs.” );

                        else

                        {

                                    n1 = Double.parseDouble( args[0] );

                                    n2 = Double.parseDouble( args[1] );

                                               

                                    System.out.println( “The sum is: ” + (n1+n2));

                        }

            }

}

 

Note:   In the command Java Sum 2 3, the first number 2 is args[0] while 3 is args[1].

            The method length will count the number of arguments based from the user inputs.

Java – Activity3

July 11, 2007

Activity3: Reading a record from a source file and storing the results to an output file.

Program File   : StudGrade.java

Source File     : score.txt

Output File     : result.txt

Procedures     : 1. Type the program file.

                          2. Create a source file (score.txt) and provide a record, like:

                                    Sandy Tan 90.5 82.4 70

                          3. Compile the program.

                          4. Execute the program.

                          5. Open the output file (result.txt) and the result should look like this:

                                    Student Name: Sandy Tan

                                    Test Scores: 90.50 82.40 70.00

                                    Average: 80.97

                                   

import java.io.*;

import java.util.*;

public class StudGrade

{

            public static void main(String[] args) throws FileNotFoundException

            {

                        double score1, score2, score3, average;

                        String firstName, lastName;

                       

                        Scanner inputFile = new Scanner(new FileReader(“g:score.txt”));

                       

                        PrintWriter outputFile = new PrintWriter(“g:result.txt”);

                       

                        firstName = inputFile.next();

                        lastName  = inputFile.next();

                        score1    = inputFile.nextDouble();

                        score2    = inputFile.nextDouble();

                        score3    = inputFile.nextDouble();

                       

                        average   = (score1+score2+score3)/3;

                       

                        outputFile.println(“Student Name: ” + firstName + ” ” + lastName);

                        outputFile.printf(“Test Scores: %.2f %.2f %.2f %n”, score1, score2, score3);

                        outputFile.printf(“Average : %.2f”, average);

                       

                        inputFile.close();

                        outputFile.close();

            }

}


Design a site like this with WordPress.com
Get started