Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

Computer Programs Java

The document describes a phone book program implemented using Java and recursion. It includes methods to create a phone book file by taking user input, print the phone book, append new contacts, edit existing contacts by replacing the data, and delete contacts. The main method runs a menu loop allowing the user to choose these options and call the respective methods to manage the phone book stored in a text file.

Uploaded by

Savitha Dhilip
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Computer Programs Java

The document describes a phone book program implemented using Java and recursion. It includes methods to create a phone book file by taking user input, print the phone book, append new contacts, edit existing contacts by replacing the data, and delete contacts. The main method runs a menu loop allowing the user to choose these options and call the respective methods to manage the phone book stored in a text file.

Uploaded by

Savitha Dhilip
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

// Factorial - Recursion

import java.util.*;
class recuFact
{
public static Scanner sc=new Scanner(System.in);
static int fact;
static int rf(int n)
{
if(n<=1)
return 1;
else
{
return n*rf(n-1);
}
}

public static void main(String args[])


{
System.out.println("Enter n");
int n=sc.nextInt();
int dup=n;
System.out.println("FACTORIAL of "+dup+" : "+rf(n));
}
}

Output:

Enter n
4
FACTORIAL of 4 : 24
// Sum of Digits - Recursion

import java.util.*;
class recuSum
{
public static Scanner sc=new Scanner(System.in);
static int s=0;
public static int sum(int n)
{
if(n<=0)
return s;
else
{
s=s+(n%10);
return sum(n/10);
}
}
public static void main(String args[])
{
System.out.println("Enter n");
int n=sc.nextInt();
System.out.println("SUM OF THE DIGITS: "+sum(n));
}
}

Output:

Enter n
5642832
SUM OF THE DIGITS: 30
// Armstrong – Recursion

import java.util.*;
class recuArm
{
public static Scanner sc=new Scanner(System.in);
static int sum=0,l;
static int recuArmstrong(int n)
{
if(n==0)
{
return sum;
}

else
{
sum+=Math.pow(n%10,l);
return recuArmstrong(n/10);
}
}

static void check(int sum,int n)


{
if(sum==n)
System.out.println(n+" is an Armstrong number");
else
System.out.println(n+" is not an Armstrong number");
}

public static void main(String args[])


{
System.out.println("Enter n");
int n=sc.nextInt();
String s=Integer.toString(n);
l=s.length();
check(recuArmstrong(n),n);
}
}

Output:

Enter n
153
153 is an Armstrong number
// Pallindrome - Recursion

import java.util.*;
class recuPallin
{
static Scanner sc=new Scanner(System.in);
static int rev=0;
static int recuPallin(int n)
{
if(n<=0)
return rev;
else
{
rev=(rev*10)+(n%10);
return recuPallin(n/10);
}
}
static boolean check(int rev,int dup)
{
if(rev==dup)
return true;
else
return false;
}
public static void main(String args[])
{
System.out.println("Enter n");
int n=sc.nextInt();
int dup=n;
recuPallin(n);
System.out.println(check(rev,dup));
}
}

Output:

Enter n
151
true
// Phone File

import java.io.*;
import java.util.Scanner;
public class Phone
{
static Scanner sc = new Scanner(System.in);
static File f1 = new File("Phone");
static File f2 = new File("Temp");

static void create() throws IOException


{

FileWriter fw = new FileWriter(f1);


BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter (bw);
String name , add;
long phone;
int nos,i;

System.out.println("Enter no of records to be created");

nos= sc.nextInt();
for(i=0;i<nos;i++)
{
System.out.println("Enter Name");
name = sc.next();
System.out.println("Enter Phone Number");
phone = sc.nextLong();
sc.nextLine();
System.out.println("Enter Adrres");
add = sc.nextLine();

pw.println(name);pw.println(phone);pw.println(add);

}
pw.close();bw.close();fw.close();
}

static void print() throws IOException


{
String name , add;
String phone;
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
System.out.format("%-10s %30s %20s" , "Name" , "Phone" , "Address\n");
while((name = br.readLine())!=null)
{
phone = br.readLine();
add = br.readLine();
System.out.format("%-10s %30s %20s" , name , phone , add);
System.out.println();
}
br.close();fr.close();
}

static void append() throws IOException


{
String name , add;
long phone;
FileWriter fw = new FileWriter(f1 , true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter (bw);
System.out.println("Enter Name");
name = sc.next();
System.out.println("Enter Phone Number");
phone = sc.nextLong();
sc.nextLine();
System.out.println("Enter Adrres");
add = sc.nextLine();
pw.println(name);pw.println(phone);pw.println(add);
pw.close(); bw.close();fw.close();
}

static void delete() throws IOException


{
String name , add , del;
long phone;
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter(f2);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter (bw);
System.out.println("Enter the name of contact to delete");
del = sc.next();
while((name = br.readLine())!=null)
{
phone = Long.parseLong(br.readLine());
add = br.readLine();
if(name.equals(del)==false)
{pw.println(name);pw.println(phone);pw.println(add);
}
}
pw.close();bw.close();fw.close();
br.close();fr.close();f1.delete();f2.renameTo(f1);
}

static void edit() throws IOException


{
String name , add , inpt;
long phone ;
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter(f2);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter (bw);
System.out.println("Enter the name of contact to edit");inpt = sc.next();
while((name = br.readLine())!=null)
{
phone = Long.parseLong(br.readLine());
add = br.readLine();
if(name.equals(inpt))
{
System.out.println("Enter Name");
name = sc.next();
System.out.println("Enter Phone Number");
phone = sc.nextLong();
sc.nextLine();
System.out.println("Enter Adrres");
add = sc.nextLine();
pw.println(name);pw.println(phone); pw.println(add);
}
else
{
pw.println(name);pw.println(phone);pw.println(add);
}
}
pw.close();bw.close();fw.close();br.close();fr.close();
f1.delete();f2.renameTo(f1);
}

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


{
int ch;
while(true)
{
System.out.println(" 1 : Create 2 : Print 3 : Edit 4 : Add 5 : Delete 6:Exit");
ch = sc.nextInt();
switch(ch)
{
case 1 : create();
break;
case 2 : print();
break;
case 3 : edit();
break;
case 4 : append();
break;
case 5 : delete();
break;
case 6 : System.exit(0);
break;
default:System.out.println("invalid");
}

}
}
}

Output:

1 : Create 2 : Print 3 : Edit 4 : Add 5 : Delete 6:Exit


1
Enter no of records to be created
2
Enter Name
dk
Enter Phone Number
8248897964
Enter Adrres
velapadi
Enter Name
kala
Enter Phone Number
7871522365
Enter Adrres
kalaspalayam
1 : Create 2 : Print 3 : Edit 4 : Add 5 : Delete 6:Exit
2
Name Phone Address
dk 8248897964 velapadi
kala 7871522365 kalaspalayam
1 : Create 2 : Print 3 : Edit 4 : Add 5 : Delete 6:Exit
6

You might also like