-
How to I possibly compress a java string to say byte space?
[/COLOR]
Code java:
<I pledge that every program with my name on it shall be written by me
(and my co-authors, if any) and that I fully understand the program.
Every program I submit shall be entirely my own work unless otherwise
attributed. I understand that academic dishonesty not only includes
copying other people's work, but also abetting or facilitating
copying. Code that is similar to any other submission past or present
will get no credit whatever the explanation. I understand that the
consequence of academic dishonesty is a grade of 'F' for the class. I
pledge to devote my efforts to learning Java by writing my own
programs. I shall strive to be attentive to detail and write programs
that not only compile and execute correctly, but are easily
understandable by myself and other programmers.
>
This is what I have to print out. It is currently 804 bytes. I made a program here to print this paragraph out:
Code java:
<public class pledge1 {
public static void main(String[] args) {
System.out.println("I pledge that every program with my name on it shall be written by me");
System.out.println("(and my co-authors, if any) and that I fully understand the program.");
System.out.println("Every program I submit shall be entirely my own work unless otherwise");
System.out.println("attributed. I understand that academic dishonesty not only includes");
System.out.println("copying other people's work, but also abetting or facilitating");
System.out.println("copying. Code that is similar to any other submission past or present");
System.out.println("will get no credit whatever the explanation. I understand that the");
System.out.println("consequence of academic dishonesty is a grade of 'F' for the class. I");
System.out.println("pledge to devote my efforts to learning Java by writing my own");
System.out.println("programs. I shall strive to be attentive to detail and write programs");
System.out.println("that not only compile and execute correctly, but are easily");
System.out.println("understandable by myself and other programmers.");
}
}>
How, the program is 1191 Bytes, and I have to compress that (presumably) to under 793 bytes. Maybe I don't have to use system.out.println so many times, and I can save byte space. Also, the paragraph is 804 bytes. I have to make the paragraph no more than 655 bytes, with a total of 793 bytes for the whole entire program. How do I go about doing this?
--- Update ---
I believe this is a way to compress texts, and I can pass in that compressed text to java to execute. Maybe that will save space? The teacher required that the program cannot take any input. I am assuming he means human input. So if that's the case, I can pass in a compressed text file into java.
-
Re: How to I possibly compress a java string to say byte space?
I'm not sure I quite understand what you want, do you have a code example?
As a side note I doubt you'd need to worry about compressing 10 lines of string data.
-
Re: How to I possibly compress a java string to say byte space?
-
Re: How to I possibly compress a java string to say byte space?
How about create a byte array the same size as the paragraph and simply put the lower order bytes of the chars into the byte array, and then save it as a non-text file.
--- Update ---
Heck, String has a getBytes() method. Have you tried working with this?
-
Re: How to I possibly compress a java string to say byte space?
Where is the code that outputs the unicode? Where is the unicode text written? How did you copy and paste here the unicode values shown in your post?
When I compile and execute the program I get the text not unicode.
What does this mean:
Quote:
When you convert the string to unicode
-
Re: How to I possibly compress a java string to say byte space?
So your program takes a file input which you have to be able to parse, write to an output file (presumably smaller than the original input file), then be able to re-construct the original content and display it?
A few questions:
1. Is unicode output support a requirement? If it isn't, you can quickly chop your data size in half by switching to an 8-bit encoding like ASCII.
2. Does your input file look exactly like what you posted? That file only has ~100 actual characters. You can use various functions in the Character class to parse the string "\u0063" into the actual unicode character.
3. I doubt you're being asked to implement a fully-fledged compression algorithm. If your professor is looking for use of an actual compression algorithm, take a look at the java.util.zip package. It has implementations of the zip and gzip algorithms which you can call to compress/decompress files.
-
Re: How to I possibly compress a java string to say byte space?
-
Re: How to I possibly compress a java string to say byte space?
Quote:
unicode was supplied by the teacher
I am confused about what the program is doing.
You said:
Finally, the unicode is:
\u0063\u006C...
Is that the contents of a text file that you are supposed to read and convert to a Java String?
\u0063 to c
\u006C to l
etc
-
Re: How to I possibly compress a java string to say byte space?
how do i undelete? if not, I can repost
--- Update ---
Code java:
<I pledge that every program with my name on it shall be written by me
(and my co-authors, if any) and that I fully understand the program.
Every program I submit shall be entirely my own work unless otherwise
attributed. I understand that academic dishonesty not only includes
copying other people's work, but also abetting or facilitating
copying. Code that is similar to any other submission past or present
will get no credit whatever the explanation. I understand that the
consequence of academic dishonesty is a grade of 'F' for the class. I
pledge to devote my efforts to learning Java by writing my own
programs. I shall strive to be attentive to detail and write programs
that not only compile and execute correctly, but are easily
understandable by myself and other programmers.
>
This is what I have to print out. It is currently 804 bytes. I made a program here to print this paragraph out:
Code java:
<public class pledge1 {
public static void main(String[] args) {
System.out.println("I pledge that every program with my name on it shall be written by me");
System.out.println("(and my co-authors, if any) and that I fully understand the program.");
System.out.println("Every program I submit shall be entirely my own work unless otherwise");
System.out.println("attributed. I understand that academic dishonesty not only includes");
System.out.println("copying other people's work, but also abetting or facilitating");
System.out.println("copying. Code that is similar to any other submission past or present");
System.out.println("will get no credit whatever the explanation. I understand that the");
System.out.println("consequence of academic dishonesty is a grade of 'F' for the class. I");
System.out.println("pledge to devote my efforts to learning Java by writing my own");
System.out.println("programs. I shall strive to be attentive to detail and write programs");
System.out.println("that not only compile and execute correctly, but are easily");
System.out.println("understandable by myself and other programmers.");
}
}>
How, the program is 1191 Bytes, and I have to compress that (presumably) to under 793 bytes. Maybe I don't have to use system.out.println so many times, and I can save byte space. Also, the paragraph is 804 bytes. I have to make the paragraph no more than 655 bytes, with a total of 793 bytes for the whole entire program. How do I go about doing this?
--- Update ---
I believe this is a way to compress texts, and I can pass in that compressed text to java to execute. Maybe that will save space? The teacher required that the program cannot take any input. I am assuming he means human input. So if that's the case, I can pass in a compressed text file into java.
-
Re: How to I possibly compress a java string to say byte space?
Quote:
This is what I have to print out
You could put all of the text into a single String. Use the line separator character in the String at the places where you want a new line to begin.
Quote:
I have to make the paragraph no more than 655 bytes, with a total of 793 bytes for the whole entire program.
So the total text of the program outside of the paragraph has to be 138 bytes?
-
Re: How to I possibly compress a java string to say byte space?
Quote:
Originally Posted by
Norm
You could put all of the text into a single String. Use the line separator character in the String at the places where you want a new line to begin.
So the total text of the program outside of the paragraph has to be 138 bytes?
This is what the teacher said:
Code java:
< The total number of characters in the program should be fewer that the total number of characters in the pledge (655 printable, 793 total). Your programs file size should be under 793 bytes>
And as for putting the text together in a single string, they want no more than 100 characters/words across a single line, as per proper programming style. So even if I use \n, it looks like this:
Code java:
<public class pledge1 {
public static void main(String[] args) {
System.out.println("I pledge that every program with my name on it shall be written by me \n(and my co-authors, if any) and that I fully understand the program. \nEvery program I submit shall be entirely my own work unless otherwise \nattributed. I understand that academic dishonesty not only includes \ncopying other people's work, but also abetting or facilitating \ncopying. Code that is similar to any other submission past or present \nwill get no credit whatever the explanation. I understand that the \nconsequence of academic dishonesty is a grade of 'F' for the class. I \npledge to devote my efforts to learning Java by writing my own \nprograms. I shall strive to be attentive to detail and write programs \nthat not only compile and execute correctly, but are easily \nunderstandable by myself and other programmers.");
}
}
>
--- Update ---
The previous program I pasted, I stand at 934 bytes. So I have to cut out about 150 bytes
--- Update ---
You know what, screw style, I just want this under 793. Also, in the "public static void main (String [] args)", I just changed the "args" to "a", saving 3 bytes
-
Re: How to I possibly compress a java string to say byte space?
Seems a big waste of time. What is supposed to be learned in this exercise?
-
Re: How to I possibly compress a java string to say byte space?
ok I just added the "+" at the end of end line, to make it look more readable. But it adds a "+", which are extra characters.
--- Update ---
Quote:
Originally Posted by
Norm
Seems a big waste of time. What is supposed to be learned in this exercise?
please, this is not the time to be asking these questions, my time deadline is coming up.
--- Update ---
I need to compress this string now, to save me some space.
--- Update ---
Quote:
Originally Posted by
Norm
Seems a big waste of time. What is supposed to be learned in this exercise?
IDK, how to use oracles java docs, or java api. Or how to learn compression by yourself?
-
Re: How to I possibly compress a java string to say byte space?
How are you learning compression by retyping the lines of a program?
-
Re: How to I possibly compress a java string to say byte space?
ok, I have another idea. I'm going to use an external program to convert string to ascii. Then I am going to copy and paste that into a java program, and compress that ascii code. Will that work? But java will have to read that ascii code. Hopefully there is a java class for that.
-
Re: How to I possibly compress a java string to say byte space?
Norm is commenting on the assignment itself, not your questions. I agree that this looks like a pointless exercise.
Are you counting bytes of source code, or bytes of the compiled program?
I just compiled a pretty much as bare-bones program I that will work by reading an uncompressed text file in and the printing it out and that's an 1008 byte binary.
-
Re: How to I possibly compress a java string to say byte space?
Quote:
changed the "args" to "a", saving 3 bytes
Putting all the code on one line will save the lineend characters for each line.
-
Re: How to I possibly compress a java string to say byte space?
Quote:
Originally Posted by
helloworld922
Norm is commenting on the assignment itself, not your questions. I agree that this looks like a pointless exercise.
Are you counting bytes of source code, or bytes of the compiled program?
I just compiled a pretty much as bare-bones program I that will work by reading an uncompressed text file in and the printing it out and that's an 1008 byte binary.
I am counting bytes of the java file. Like suppose I save it to the desktop, I'll right click it, check the properties, and check the size there.
I cannot pass in any inputs for this. Here's what I was thinking. Convert this string to ascii, and compress that ascii code (because there may be repeating characters). Then place this compressed ascii code in a java program, and somehow find a class to uncompress it??? That way, this should not take up too much space.
The paragraph is 804 bytes alone. If the program has to be less than 793 bytes, it's obvious that I will not be able to type this paragraph in this java program. So I will have to type in a compress ascii code (unicode will take up more space) in this program, and just uncompress and execute that code
--- Update ---
sh*t, i think the ascii codes for this paragraph will be more than the byte size of the entire text
--- Update ---
for example, "abc" is like "979899"
--- Update ---
Quote:
Originally Posted by
Norm
Putting all the code on one line will save the lineend characters for each line.
I will do that
--- Update ---
String to hex?
-
Re: How to I possibly compress a java string to say byte space?
That's rather silly to count the bytes of the source file (that's the .java file, the .class file is the compiled binary). It encourages bad programming practices like single letter variable names, no tabbing, no comments, and no line separators. Even taking all of these things out the source file is 860 characters long.
Quote:
I cannot pass in any inputs for this. Here's what I was thinking. Convert this string to ascii, and compress that ascii code (because there may be repeating characters). Then place this compressed ascii code in a java program, and somehow find a class to uncompress it??? That way, this should not take up too much space.
I wouldn't bother, Java source files are usually UTF-8 encoded, meaning if you only use characters in the lower 128 ASCII charset you'll get the exact same file either way (that's the case here).
The only way I can think of to get your source file smaller is to use an external file, then your java program opens that file and prints it out.
-
Re: How to I possibly compress a java string to say byte space?
that's what I will have to do. I am going to use an external program to get the hex codes for this string, compress that string in hex codes, then bring these hex codes into my program. The compressed hex codes should be less than the paragraph (i am hoping). Then decompress those hex codes and print it???
-
Re: How to I possibly compress a java string to say byte space?
When is your assignment due? I would recommend asking a member of the teaching staff if you're able to. These seem like rather silly requirements, and they might shed some light on what path you're expected to take.
Having an externally compressed text file won't help much because then you'll need to have Java code which will decompress it. If you do end up compressing it, leave the text "as-is" so you don't have to do an extra step converting hex to a character code. The best solution I can think of is reading from an uncompressed text file and printing it out.
-
Re: How to I possibly compress a java string to say byte space?
Quote:
Originally Posted by
helloworld922
When is your assignment due? I would recommend asking a member of the teaching staff if you're able to. These seem like rather silly requirements, and they might shed some light on what path you're expected to take.
Having an externally compressed text file won't help much because then you'll need to have Java code which will decompress it. If you do end up compressing it, leave the text "as-is" so you don't have to do an extra step converting hex to a character code. The best solution I can think of is reading from an uncompressed text file and printing it out.
how do you do that? He did hint on changing the extension, possibly to a text
--- Update ---
No, the program can't take any input. I heard someone else mention something about converting this text into an image.
-
Re: How to I possibly compress a java string to say byte space?
There's no way to directly store an image in a java source file, besides you're storing the same data either way. The character 'A' has a value of 65, which is 1 byte regardless of if it's interpreted as a pixel or as an UTF-8 character.
-
Re: How to I possibly compress a java string to say byte space?
i am totally lost...
--- Update ---
ok, maybe it involves javap -c <class>....
-
Re: How to I possibly compress a java string to say byte space?
OK, the assignment is past due and was extra credit. How do I do this? Reduce my program from 930 bytes to under 793 bytes. And most of my program is java string. Last, I cannot import any files into my program. Supposedly its a very complicated problem.