Question: Create a new version of the UserSandD class called UserSandDTransient. This new class makes the password attribute a transient field with respect to serialization and

Create a new version of the UserSandD class called UserSandDTransient. This new class makes the password attribute a transient field with respect to serialization and deserialization. Create a new version of your SerializeUserSandD class from problem 3 called SerializeUserSandDTransient. This class should instantiate a UserSandDTransient object and save a serialized copy of it into a file called usersandtransient.ser. Create a new version of your DeserializeUserSandD class called DeserializeUserSandDTransient. Use it to deserialize the contents of the usersanddtransient.ser file and show the values of the attributes of the deserialized object.

===UserSandD===

import java.io.Serializable;

public final class UserSandD implements Serializable { String username="n/a"; String password="n/a";

public UserSandD() { }

public UserSandD(String username, String password) { try { if (username==null || password==null) throw new IllegalArgumentException("Missing user data");

this.username=username; this.password=password; } catch (IllegalArgumentException e) { e.printStackTrace(); } }

public void setUsername(String username) { this.username=username; }

public void setPassword(String password) { this.password=password; }

public String getUsername() { return username; }

public String getPassword() { return password; }

public boolean validate(String username, String password) { boolean valid=false;

if (this.username.equals(username) && this.password.equals(password)) valid=true;

return valid; } }

===SerializeUserSandD===

import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream;

public class SerializeUserSandD {

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

// create an object of UserSandD class UserSandD userSandD = new UserSandD("CHEGG_USER", "12345");

// saving the object in file usersandd.ser FileOutputStream fileOutputStream = new FileOutputStream("usersandd.ser"); ObjectOutputStream outputStream = new ObjectOutputStream(fileOutputStream);

// serialization of object userSandD outputStream.writeObject(userSandD);

// close used resources fileOutputStream.close(); outputStream.close();

} }

===DeserializeUserSandD===

import java.io.*;

public class DeserializeUserSandD {

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

// reading the UserSandD object from file usersandd.ser FileInputStream fileInputStream = new FileInputStream("usersandd.ser"); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);

try { // deserialization of object UserSandD userSandD = (UserSandD) objectInputStream.readObject();

// print the username and password of deserialized object on-screen System.out.printf("USERNAME: " + userSandD.username + " PASSWORD: " + userSandD.password); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!