#7,#8 2998

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

EXPERIMENT 7

AIM: Create a menu based Java application with the following options.1.Add an
Employee2.Display All3.Exit If option 1 is selected, the application should gather details of
the employee like employee name, employee id, designation and salary and store it in a file.
If option 2 is selected, the application should display all the employee details. If option 3 is
selected the application should exit.

SOURCE CODE:

import java.util.*;
import javax.swing.plaf.synth.SynthSplitPaneUI;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.Serializable;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class emplll implements Serializable{
static void adde(){
try{
String s=new String();
File file=new File("D:\\emp.txt");
FileOutputStream fout=new FileOutputStream("D:\\emp.txt",true);
System.out.println("Enter the employee name");
Scanner in=new Scanner(System.in);
s=in.nextLine();
s=s+" ";
byte b[]=s.getBytes();
fout.write(b);
System.out.println("Enter the employee id");
s=in.nextLine();
s=s+" ";
b=s.getBytes();
fout.write(b);
System.out.println("Enter the employee designation");
s=in.nextLine();
s=s+" ";
b=s.getBytes();
fout.write(b);
System.out.println("Enter the employee salary");
s=in.nextLine();
s=s+"\n";
b=s.getBytes();
fout.write(b);
fout.close();
System.out.println("success...");
}catch(Exception e){System.out.println(e);}
}
static void display(){
try{
FileInputStream fin=new FileInputStream("D:\\emp.txt");
int i=0;
while((i=fin.read())!=-1){
System.out.print((char)i);

}
fin.close();
}catch(Exception e){System.out.println(e);}
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n;
int x=1;
do{
System.out.println("1. Add Employee");
System.out.println("2. Display all");
System.out.println("3. Exit");

System.out.println("Enter your choice");


n=in.nextInt();
switch(n){
case 1: {
adde();
System.out.println("do you want to continue\n1 for yes\n2 for no");

x=in.nextInt();
break;
}
case 2:{
display();
System.out.println("do you want to continue\n1 for yes\n2 for no");

x=in.nextInt();
break;
}
case 3:{
System.exit(0);
}
default:{
System.out.println("Invalid Input");
System.out.println("\ndo you want to continue\n1 for yes\n2 for no");
x=in.nextInt();
break;
}
}
}while(x==1);
}
}

SCREENSHOTS :
EXPERIMENT 8
1. AIM: Create a palindrome creator application for making a longest possible palindrome out
of given input string.

Source code: import java.util.*;


public class lpalindrome {
public static String ppalin(String s) {
Map<Character,Integer>map = new HashMap<>();
for (char ch : s.toCharArray()){
if (map.containsKey(ch)){
int temp = map.get(ch);
map.put(ch,temp+1);
}else map.put(ch,1);
}
String beg = "", mid = "", end = "";
boolean isValid = false;
for (char ch : map.keySet()){
if (map.get(ch)%2==0)
{
for (int i = 0; i<map.get(ch)/2 ; i++)
beg=ch+beg; }
else if (map.get(ch)%2==1){
mid= Character.toString(ch);
int temp = map.get(ch);
map.put(ch,temp-1);
for (int i = 0; i<map.get(ch)/2 ; i++)
beg=ch+beg; }}
end = beg;
StringBuilder input1 = new StringBuilder();
input1.append(end);
input1 = input1.reverse();
return beg + mid + input1;}
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
String b=in.nextLine();
System.out.println(ppalin(b));}}
SCREENSHOTS:

You might also like