Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I am having trouble understanding the following code we did in class. We

ID: 3756673 • Letter: H

Question

Hello,

I am having trouble understanding the following code we did in class. We are doing this in java and I have no experience in it.

The purpose of the following code is to use it to simulate the encapsulation of a letter, where each layer is copied into the next (It is shown in the picture below), however in this example we encapsulate the data of an array, and it fills the rest of the space with blanks. That is all i have understood so far on my own. In the code I left some comments where I did not understand what something did. Can you please explain those to me? What confused me the most was the for loops. I also did not understand the output of the program, which is below.

This is the code in java:

package encapsulationsimulation;

import encapsulationsimulation.Encapsulation;

public class EncapsulationSimulation {

public static void main(String[] args) {

// test the first encapsulation, create an object
Encapsulation encapsulationObject = new Encapsulation();
  

byte[] l1 = {4, 5};

// message converted to byte
// message padded

byte[] l1Padded= encapsulationObject.prepareHeader(l1,4);

// why do we add a for loop here?
for (int i = 0; i < l1Padded.length; i++) {
System.out.println("byte at "+i +" is "+ l1Padded[i]);
}

byte[] h1 = {0, 1, 2, 3};

// initializes an array payload, with the class function encapsulate
byte[] payload = encapsulationObject.encapsulate(h1, l1Padded);
for (int i = 0; i < payload.length; i++) {
System.out.println("byte at "+i +" is "+ payload[i]);
  
}
  
}
  
}

/////////////////////////////////////////////////
/////////////////////////////////////////////////
package encapsulationsimulation;


// encapsulation class
public class Encapsulation
{
// encapsulation function that returns combined array of header and payload on the notes
// the array takes in the header and the payload byte arrays
public byte[] encapsulate(byte[] header, byte[] payload)
{
// have the header at the beggining

byte[] nextPayload = new byte[header.length + payload.length];

// do a for loop to store the words of the message in the header array
for (int i = 0; i < header.length; i++) {
// the beginning bytes of the header array are stored in nextPayload  
nextPayload[i] = header[i];
  
}
// this for loop will store the rest of the empty space that was not used
for (int i = 0; i < payload.length; i++) {
// payload comes at the end
// start storing at the end of the header, stores the payload
nextPayload[header.length+i] = payload[i];
  
}
return nextPayload;

}
  
// this is where it gets confusing for me, what does prepare header do?
  
  
public byte[] prepareHeader(byte[] payload, int paddedLength)
{
// paddedLength is the total length
  
byte[] paddedLoaded = new byte[paddedLength];

// Length to pad is the left over space after header is filled
int lengthToPad = paddedLength - payload.length;

// this is the original data
for (int i = 0; i < payload.length; i++) {
// the beginning bytes of the header array are stored in nextPayload  
paddedLoaded[i] = payload[i];
  
}
// this is the rest of the data
for (int i = 0; i < lengthToPad; i++) {
  
paddedLoaded[payload.length+i] = (byte)' ';
  
}
return paddedLoaded;

}
  
  
  
}

pylord Emu) vevson t mss enupS addess Steate

Explanation / Answer

import encapsulationsimulation.Encapsulation;

public class EncapsulationSimulation {

public static void main(String[] args) {

// test the first encapsulation, create an object

Encapsulation encapsulationObject = new Encapsulation();

byte[] l1 = {4, 5};

// message converted to byte

// message padded

// here u have 4 bytes for message but l1 is only two bytes

// so u call prepare header method to add 2 bytes of padding

byte[] l1Padded= encapsulationObject.prepareHeader(l1,4);

// why do we add a for loop here?

// here u print the l1padded byte array which contains value with padding

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

System.out.println("byte at "+i +" is "+ l1Padded[i]);

}

// here u have another header h1 which is 4 bytes

byte[] h1 = {0, 1, 2, 3};

// initializes an array payload, with the class function encapsulate

// encapsulate method appends l1padded to h1 byte array

byte[] payload = encapsulationObject.encapsulate(h1, l1Padded);

// here u print whole message which contains l1 with padding and h1 at begining

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

System.out.println("byte at "+i +" is "+ payload[i]);

}

}

}

//encapsulation class

public class Encapsulation

{

//encapsulation function that returns combined array of header and payload on the notes

//the array takes in the header and the payload byte arrays

public byte[] encapsulate(byte[] header, byte[] payload)

{

//have the header at the beggining

byte[] nextPayload = new byte[header.length + payload.length];

//do a for loop to store the words of the message in the header array

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

//the beginning bytes of the header array are stored in nextPayload  

nextPayload[i] = header[i];

}

//this for loop will store the rest of the empty space that was not used

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

//payload comes at the end

//start storing at the end of the header, stores the payload

nextPayload[header.length+i] = payload[i];

}

return nextPayload;

}

//this is where it gets confusing for me, what does prepare header do?

// suppose your message is 50 bytes and the maximum bytes alloted for messgae is 1024bytes

//then this method adds remaining bytes as blanks

// or this method adds padding to messaage

public byte[] prepareHeader(byte[] payload, int paddedLength)

{

//paddedLength is the total length

// suppose total length for message is 1024 bytes

// but u have only 50 bytes of message

byte[] paddedLoaded = new byte[paddedLength];

//Length to pad is the left over space after header is filled

// here u determine how many bytes are left which needs to be padded

// example 1024-50 bytes

int lengthToPad = paddedLength - payload.length;

//this is the original data

// u copy ist those 50 bytes in new array of 1024 bytes let us suppose 1024 is total length

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

//the beginning bytes of the header array are stored in nextPayload  

paddedLoaded[i] = payload[i];

}

//this is the rest of the data

// the remaining u add as byte value of spaces

// that is padding

// example 1024-50 bytes of padding u add here and return full message with padding

for (int i = 0; i < lengthToPad; i++) {

paddedLoaded[payload.length+i] = (byte)' ';

}

return paddedLoaded;

}

}

//please go through comments which i have added

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote