Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Exercise a HelpMe test

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
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.
[Image: vp6iypF.gif]
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;
}
}
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.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by 1337ITWizKidJun 10, 2021 09:14 PM 2 194 Jun 10, 2021 09:14 PM
Last Post: 1337ITWizKid
[Java] by minerwOct 5, 2020 06:44 AM 2 302 Oct 5, 2020 06:44 AM
Last Post: minerw
[Python] by StreaxAug 26, 2019 04:30 AM 13 510 Aug 26, 2019 04:30 AM
Last Post: sid071