My java program

29
Question: Hashtable stores key/value pairs in a hash table. When using a Hashtable, you specify an object that is used as a key, and the value that you want linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table. Perform basic operations on Hashtable like creating hashtable object, adding key-value pair, getting the value based on key, checking hashtable is empty or not, removing an element, and size of the hashtable.

Transcript of My java program

Question: Hashtable stores key/value pairs in a hash table. When using a Hashtable, you specify an object that is used as a key, and the value that you want linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table. Perform basic operations on Hashtable like creating hashtable object, adding key-value pair, getting the value based on key, checking hashtable is empty or not, removing an element, and size of the hashtable.

AIM:To develop a program to implement equals and hashtable methods..

PROGRAM:\*File name: TigerAuthor: S.mahendranDescription:This is a class which gives details about the Tiger.*/package tiger;public class Tiger {

private String color;private String stripePattern;privateint height;

@Overridepublicboolean equals(Object object) {

boolean result = false;if (object == null || object.getClass() !=

getClass()) {result = false;

} else {Tiger tiger = (Tiger) object;if (this.color == tiger.getColor()

&&this.stripePattern == tiger.getStripePattern()) {

result = true;}

}return result;

}

publicinthashCode() {int hash = 3;hash = 7 * hash + this.color.hashCode();

EX.NO:3.2bDATE:

IMPLEMENTATION HASHTABLE METHODS

hash = 7 * hash + this.stripePattern.hashCode();return hash;

}

public static void main(String args[]) {Tiger bengalTiger1 = new Tiger("Yellow", "Dense",

3);Tiger bengalTiger2 = new Tiger("Yellow", "Dense",

2);Tiger siberianTiger = new Tiger("White", "Sparse",

4);System.out.println("bengalTiger1 and bengalTiger2: "

+ bengalTiger1.equals(bengalTiger2));System.out.println("bengalTiger1 and siberianTiger:

"+ bengalTiger1.equals(siberianTiger));

System.out.println("bengalTiger1 hashCode: " + bengalTiger1.hashCode());

System.out.println("bengalTiger2 hashCode: " + bengalTiger2.hashCode());

System.out.println("siberianTigerhashCode: "+ siberianTiger.hashCode());

}

public String getColor() {return color;

}

public String getStripePattern() {returnstripePattern;

}

public Tiger(String color, String stripePattern, int height) {

this.color = color;this.stripePattern = stripePattern;

this.height = height;}}

OUTPUT:

Question:

Student’s marks are stored in a File. Each and every row represents marks of particular

students and marks are separated by white space in a row. Calculate the total marks of each student by

reading an input file.In student management system student’s academic details are stored in a separate

file and personal details are stored in a separate file.

Aim:

To write a java program that prompts the user to enter thefile, reads the scores from the file, and displays their totaland average.

Program:

Calculate the Average of a row in the File

Ex. No:3.3 aDate:

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.util.Scanner;

/*File Name : Score.javaAuthor :S.mahendranDescription : This is the class that encapsulatesthe main function that will find the sum and average of scoresstored in a file*/public class Score {

/* Method : readFile()Description: Reads contents of the file and converts itinto stringReturn Value : String */

public static String readFile(String fileName) throwsIOException {

BufferedReader br = new BufferedReader(newFileReader(fileName));

try {

StringBuilder sb = new StringBuilder();

String line = br.readLine();

while (line != null)

{

sb.append(line);

sb.append("\n");

line = br.readLine();

}

return sb.toString();

}

finally {

br.close();

}

}

/* Method : main()Description: Entry point.Return Value : null */

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

Scanner obj=new Scanner(System.in);

System.out.print("Enter The File name : ");

String fileName=obj.nextLine();

String data = readFile(fileName);

System.out.print("The Scores in the file are "+data);

char dataArray[]= data.toCharArray();

String s="";

int cnt=0,sum=0;

for(int i=0;i<dataArray.length;i++)

{

if(dataArray[i]==' ')

{

cnt++;

s="";

continue;

}

else

{

s=s+dataArray[i];

sum+=Integer.parseInt(s.trim());

}

}

System.out.println("Sum of the scores = "+sum);

System.out.println("Average of Scores = "+(double)(sum/cnt));

}

}

Output:

Question:

Develop a utility program that combines the files together into a new file using the following command:

java Combine SourceFile1 . . . SourceFilen TargetFile

The command combines SourceFile1, . . . , and SourceFilen into TargetFile

Aim:

To develope a java program for merging a two files into an one file.

Program:

import java.io.*;

/*

File Name : MergerFiles.java

Author :S.mahendran

Description : This is the base class that provides information about Merger of two Files.

Merging of two Files

Ex. No:3.3 bDate:

. */

public class MergerFiles

{

/*

Method : main()

Description : Entry point.

Return Value : null

*/

public static void main(String[] args) {

String sourceFile1Path = "/C:/Users/KNR/Desktop/h1.txt";

String sourceFile2Path = "C:/Users/KNR/Desktop/h2.txt";

String mergedFilePath = "C:/Users/KNR/Desktop/h3.txt";

File[] files = new File[2];

files[0] = new File(sourceFile1Path);

files[1] = new File(sourceFile2Path);

File mergedFile = new File(mergedFilePath);

mergeFiles(files, mergedFile);

}

public static void mergeFiles(File[] files, File mergedFile) {

FileWriter fstream = null;

BufferedWriter out = null;

try {

fstream = new FileWriter(mergedFile, true);

out = new BufferedWriter(fstream);

} catch (IOException e1) {

e1.printStackTrace();

}

for (File f : files) {

System.out.println("merging: " + f.getName());

FileInputStream fis;

try {

fis = new FileInputStream(f);

BufferedReader in = new BufferedReader(newInputStreamReader(fis));

String aLine;

while ((aLine = in.readLine()) != null) {

out.write(aLine);

out.newLine();

}

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

try {

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

Output:

Question:

Encode the file content by adding 5 to every byte in the file.Write a program that prompts the

user to enter an input file name and an output file name and saves the encrypted version of

the input file to the output file.

Encrypt The Files

Ex. No:3.3 cDate:

Aim:

To develop a java program for Encode the content into the file.

Program:

import java.util.Scanner;

import java.io.*;

/*

File Name : EncryptFiles.java

Author :S.mahendran

Description : This is the base class that provides information about EncryptFiles the Files.

. */

public class EncryptFiles

{

/*

Method : main()

Description : Entry point.

Return Value : null

*/

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

Scanner input = new Scanner(System.in);

System.out.print("Enter a file to encrypt: ");

FileInputStream in = new FileInputStream(input.next());

System.out.print("Enter the output file: ");

FileOutputStream output = new FileOutputStream(input.next());

int value;

while ((value = in.read()) != -1) {

output.write(value + 5);

}

input.close();

output.close();

}

}

Output:

Question:

Consider that you need to find the IP address of a website. To implement this scenario develop a java program using java.net.InetAddress class and display the IP address of the following hostnames:

www.google.co.in

www.kce.ac.in

www.javatpoint.com

www.espncricinfo.com

Aim:

To develope a java program for the Url IP

Program:

import java.net.*;

public class UrlProperties

{

/*

File Name : UrlProperties.java

URL IP

Ex. No:3.4 cDate:

Author :S.mahendran

Description : This is the base class that provides information about UrlProperties*/

public static void main(String[] args) throws UnknownHostException { for(int i=0;i<4;i++) {System.out.print("Enter the URL : "); Scanner in= new Scanner(System.in); String url=in.nextLine();InetAddress obj= InetAddress.getByName(url);System.out.println("The IP address of the entered domain is " + obj.getHostAddress()); }}}Output:

Aim:

To develop java program to client server using echo server.

Echo ServerEx. No:3.4 aDate:

Program:

Client:

import java.io.*;

import java.net.*;

/*

File Name : Client.java

Author :S.mahendran

Description : This is the base class that

provides information about Client with the local

Host.

*/

public class Client

{

/*

Method : main()

Description : Entry point.

Return Value : null

*/

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

{

Socket sock = new Socket("127.0.0.1", 1235);

BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));

OutputStream ostream = sock.getOutputStream();

PrintWriter pwrite = new PrintWriter(ostream, true);

InputStream istream = sock.getInputStream();

BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

System.out.println("Start the chitchat, type and press Enter key");

String receiveMessage, sendMessage; while(true)

{

sendMessage = keyRead.readLine();

pwrite.println(sendMessage);

pwrite.flush();

if((receiveMessage = receiveRead.readLine()) != null)

{

System.out.println(receiveMessage);

}

}

}

}

server

import java.io.*;

import java.net.*;

/*

File Name : Server.java

Author :S.mahendran

Description : This is the base class that

provides information about Server.s

Host.

*/

public class Server

{

/*

Method : main()

Description : Entry point.

Return Value : null

*/

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

{

ServerSocket sersock = new ServerSocket(1235);

System.out.println("Server ready for chatting");

Socket sock = sersock.accept( );

BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));

OutputStream ostream = sock.getOutputStream();

PrintWriter pwrite = new PrintWriter(ostream, true);

InputStream istream = sock.getInputStream();

BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

String receiveMessage, sendMessage;

while(true)

{

if((receiveMessage = receiveRead.readLine()) != null)

{

System.out.println(receiveMessage);

}

sendMessage = keyRead.readLine();

pwrite.println(sendMessage);

pwrite.flush();

}

}

}

Aim:

To develop a java program for the TCP prortocol.

Program:

Client:

import java.io.*;

import java.net.*;

/*

File Name : Client.java

Author :S.mahendran

Date :

Description : This is the base class that

provides information about Client.

Host.

*/

public class Client

{

Chat Using TCP ProtocolEx. No:3.4 bDate:

/*

Method : main()

Description : Entry point.

Return Value : null

*/

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

{

Socket clientSock=new Socket("127.0.0.1", 1234);

System.out.println("Client connected to the server");

BufferedReader keyRead=new BufferedReader(new InputStreamReader(System.in));

OutputStream ostream=clientSock.getOutputStream();

PrintWriter pwrite=new PrintWriter(ostream,true);

InputStream istream=clientSock.getInputStream();

BufferedReader receiveRead=new BufferedReader(new InputStreamReader(istream));

System.out.println("to Start the chat, type message and press Enter key");

String receiveMessage , sendMessage;

while(true)

{

sendMessage=keyRead.readLine();

pwrite.println(sendMessage);

System.out.flush();

if((receiveMessage=receiveRead.readLine())!=null)

{

System.out.println("server:>"+receiveMessage);

}

if(sendMessage.equals("bye"))

{

System.exit(0);

}

}

}

}

Server:

import java.io.*;

import java.net.*;

/*

File Name : Server.java

Author :S.mahendran

Date :

Description : This is the base class that

provides information about Server

Host.

*/

public class Server

{

/*

Method : main()

Description : Entry point.

Return Value : null

*/

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

{

ServerSocket serverSock=new ServerSocket(1234);

System.out.println("waiting for client.........");

Socket socket=serverSock.accept();

System.out.println("client connected ");

BufferedReader keyRead=new BufferedReader(new InputStreamReader(System.in));//reading from keyboard(keyRead object)

OutputStream ostream=socket.getOutputStream();

PrintWriter pw=new PrintWriter(ostream,true);

InputStream istream=socket.getInputStream();

BufferedReader receiveRead=new BufferedReader(new InputStreamReader(istream));

String receiveMessage,sendMessage;

while(true)

{

if((receiveMessage=receiveRead.readLine())!= null)

{

System.out.println("client:>"+ receiveMessage);

}

sendMessage=keyRead.readLine();

pw.println(sendMessage);

System.out.flush();

if(sendMessage.equals("bye"))

{

System.exit(0);

}

}

}

}