# Code by keralaguest

VIEWS: 4 PAGES: 5

• pg 1
Exercise 4.1

import java.io.*;

public class Factorial {

public static void main(String[] args) {
int n=0;
double fact=1;
System.out.println("Enter a number");
try {
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (n>0)
for (int i=n;i>1;i=i-2)
{
fact = fact * i * (i-1);
}
System.out.println("The factorial of " + n + " is "+ fact);
}
Exercise 4.2

import   java.io.FileNotFoundException;
import   java.io.IOException;

public class Average {

public static void main(String[] args) {

try {
String line;
int sum = 0, count = 0, avg = 0;
{
sum = sum + Integer.parseInt(line);//Modified code
count++;
}
if (count!=0)
{
avg = sum/count;
}

System.out.println("Number of integers = " + count);
System.out.println("Average of integers = " + avg);

} catch (FileNotFoundException e1)   {
// TODO Auto-generated catch   block
e1.printStackTrace();
} catch (NumberFormatException e) {
// TODO Auto-generated catch   block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch   block
e.printStackTrace();
}
}
}
Exercise 4.3

import java.io.*;

public class Writer {

public static void main(String[] args) {
try {
String line;
String emails = "";
{
String[] arr = line.toLowerCase().split(":");
emails = emails + arr[1].charAt(0) +
arr[0].charAt(0) + arr[2].substring(5,9) + "@se.depaul.edu \r\n";

}
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter(args[1])));
out.print(emails);
out.flush();
out.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Exercise 4.7a

package myprog.single;//Package creation. Remaining code is unchanged
import java.io.FileNotFoundException;
import java.io.IOException;

public class Average {

public static void main(String[] args) {
try {
String line;
int sum = 0, count = 0, avg = 0;
{
sum = sum + Integer.parseInt(line);
count++;
}
if (count!=0)
{
avg = sum/count;
}

System.out.println("Number of integers = " + count);
System.out.println("Average of integers = " + avg);

} catch (FileNotFoundException e1)   {
// TODO Auto-generated catch   block
e1.printStackTrace();
} catch (NumberFormatException e) {
// TODO Auto-generated catch   block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch   block
e.printStackTrace();
}
}
}
Exercise 4.8a, 4.8b

import    java.io.FileNotFoundException;
import    java.io.IOException;

public class Average {

public static void main(String[] args) {
try {
String line;
int sum = 0, count = 0, avg = 0;
{
try{
sum = sum + Integer.parseInt(line);
count++;
}
catch (NumberFormatException e) {
}// Number Format exception is ignored by doing
nothing in catch field
}
if (count!=0)
{
avg = sum/count;
}

System.out.println("Number of integers = " + count);
System.out.println("Average of integers = " + avg);

} catch (FileNotFoundException e1) {