Hack Forums
[Java] Exercise a HelpMe test - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Exercise a HelpMe test (/showthread.php?tid=6112307)



Exercise a HelpMe test - bohh - Sep 28, 2020

given the two classes:

class Document {
private String type = "dummy";
private byte [] data;
// enter get and set //

public classTestClass {
public static void main (String [] args) {
Document d = new PdfDocument ();
System.out.prinln (d.getType ());

I will write the code for the PdfDocument class so that Test Class will print "pdf" when run.

??????
HELP ME HEL ME


RE: Exercise a HelpMe test - MavenGradle - Sep 28, 2020

What part isn't clear to you? It seems to be a rather simple exercise that should take no more than 2 minutes of your time.
Inheritance and system.out.println are your friends.


RE: Exercise a HelpMe test - bohh - Sep 29, 2020

I did it and print pdf, but I don't use that part of the code from the data array part ... but it works.

the pdfDocument
public class PdfDocument extends Document {

public String getType() {//Override del metodo in Document
return "PDF";
}
}

e il test:

public class TestClass {

public static void main(String[] args) {
Document d = new PdfDocument();
System.out.println(d.getType());
}
}

class Document {
private String type = "dummy";//dummy viene sovrascitto dalla classe PdfDocument
private byte[] data;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public byte[] getData() {
return data;
}

public void setData(byte[] data) {
this.data = data;
}
}


RE: Exercise a HelpMe test - G.P.D. - Sep 30, 2020

You can use the Apache Commons Java library :
Code
FileUtils.readFileToByteArray(File)

Where File is the object containing your pdf.
You can initialize it like so :
Code
File file = new File("/path/to/your/file.pdf");

So it'll look like :
Code
File myPdf = new File("/path/to/your/file.pdf");
byte[] pdfByteArray = FileUtils.readFileToByteArray(myPdf);

You'll maybe need to add a path variable to your Document object.

Whait.