Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What would be the best way to write a Java program that would simulate machine code? For example, I need to create a series of instructions such as add, subtract, increment, decrement, etc.
Let's say I'm writing the add instruction which accepts 3 parameters/registers (adding the values in the first 2 registers and storing the result in the 3rd). Is it as simple as writing a function such as:
int add(int x, int y) {
int result;
result = x + y;
return result; }
I'm also open to the possibility that I'm way off base here. Any input would be much appreciated.
If you just want to write Java code that will be more or less 1:1 with machine instructions I'd suggest you create variables for all of the registers and define methods for most of the instructions (similar to what you suggested). But this will not "restrict" what you can do the way real machine instructions do, since you can multiply the BX reg by the AX reg when the machine may not allow that.
Better would be to define a class that represents the machine state (ie, registers and RAM) and methods on the class for all of the instructions. Then you couldn't multiply BX times AX unless there were a MUL_BX_AX method. Many methods would not have parameters (because the registers are inside the "opaque" object), but some would have parms where the "real" instructions would accept an offset or whatever. (Eg, ADD_AX_IMMED(5).)
Added: There is the issue of branching, though, that would require some additional thought. Java doesn't have a GOTO equivalent that would fill the role very well, so initially (until you think of something better) you might have to use standard if/else logic, et al, testing "condition codes" in the machine state class.
The best way to simulate assembly would be to handle the raw bits and bytes and do the operations accordingly.
Sure you could do that, but the big thing is how to switch on the op-codes and do all the address-field calculations.
Typically, address fields can contain literal constants, global addresses, registers, offsets relative to registers, etc.
It depends if you're simulating a simple machine or a real one.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Sometimes I am not sure which variant I should use. I mean is the second method better thant the first? The first I think it needs more storage because I am storing one more variable. For clean code reason which would you prefer and why? Is my reasonable right or wrong?
public double area(){
double area = 2 * radius * radius;
return area;
}
public double area(){
return 2 * radius * radius;
}
This might be bordering to opinion based answering...
From the resulting machine code, both variants are the same (at least after the JVM had a go in optimizing them). So the only difference here ist human readability.
Modern style guides and tools favor the second variant because it's less to read and clear enough. Modern (static code analysis) tools and IDEs (using these tools) would mark the first one as a "dead store" (in case of for example sonar) or "redundant" (in case of for example intelliJ IDE) that should be avoided.
From JVM standpoint both versions of code compile to the same bytecode (after optimization). So, 2 reasons I would prefer the first version:
Obviously easier to read
When debugging, you can stop at line return area; and check the value before getting out of the method. In the second version, you can not check the returned value before returning from the method
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am not new in java but when I write program I always use int Type of my variables. I want to know when I need to use int data, when byte, when long and so on... Can U explain me this with examples please.
If you are asking when to use float, double and long etc. This document can help you to understand.
For example, int is 32 bit but long is 64 bit. If you need to set a value over 32 bit you should use long to store data.
Good luck.
It basically depends on how you wanna use them and what considerations would you have (size, data type,..etc). I would recommend going throw Oracle's docs.
The types used should be the product of a deep thought about various of things, including (but not limited to) int over long (32bit vs 64bit), char over byte (user friendliness vs performance) , complex data structures over simple ones (Performance), backwards compatibility (JRE version), platforms the program's gonna run on (Windows? Unix? Mac OS?), readability of the code (Sometimes writing "byte x = 0xFF; char ch = (char)x; is worse than char ch = 'a' and the list goes on... of course some of the stuff I mentioned fit into more than one category.
This usually comes with experience. The more you code, the more platforms you want to support, the faster you want your program to respond etc...
You should always have a plan regarding your program:
What platforms will I support?
Is the task more important than performance?
...
...
I'm not saying you should carefully consider every type you choose, I'm saying you should always make the effort to tick all the V's and be satisfied about it, accomplishing everything you wanted.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have a instance variable in a new object I am coding that will almost always be a certain value. In this case its "N". Is it OK to hard-code a default value to a String instance variable?
private String aBusinessSwitch= "N";
Later on in a method I have an if condition that will only set the value to a "Y" in a few rare situations.
Would it be better to not hard-code a default value of "N" when I declare the field and instead add else to my if statement that sets it a "N"
if (isCold) {aBusinessSwitch = "Y"; }
else {aBusinessSwitch = "N"; }
The direct answer is 'this is frowned upon unless you need the ability to store a virtually unlimited variety of values'.
Do you need more than two values? If not, consider a boolean. If yes, consider an enum. If you need a near-infinite number of values, there is a case to use a String.
Your program can still work perfectly if you choose to use strings for this purpose. However, always keep in mind that every place you add complexity to your code, there are more opportunities for things to go wrong, and it presents a maintainability challenge in the future.
Use a bool in this case? Enum if you need more than yes & no.
As others noted the usage of "N" and "Y" hint at String being the wrong type and a Boolean or an Enum might be a better choice. But that doesn't answer your question.
I wouldn't use a default value, because it splits the logic how this value is determined in two places. If you set it only in one place you might even be able to make it final if this place is inside the constructor, making the code a little simpler to reason about.
Yes use Boolean or normal Instance variable , Enum is best for large number of values.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I want to be able to compare a number of files (at most 30) against each other in order to find some sort of degree of similarity. It wouldn't need to be perfect I just want some sort of red flag if two files are unusually similar. What would be a good way to go about this?
You could use Regular Expressions (commonly known as regex: python regex docs). Using grouping, you could find variable and function names, unique lines of code (lines that aren't whitespace or comments), etc.
However, creating a system that is smart enough to be able to detect similarities on its own can be very difficult. If you had some way of getting a number between 0 and 1 of two files and their similarities, you could test it against a high threshold. Anything over the threshold (say for example, 0.97) could be considered suspicious.
Besides looking at the physical code, you could also observe code density in the files. Imagine if you printed out a page of code and turned it 90 degrees. You essentially get a graph of the number of lines on each file. Using that, you can observe where there are peaks and valleys to see where the code is more or less dense. Two similar files may have the same or very close code densities. Also, using this method you don't have to worry about looking for variable or function names that are the same as you aren't so much looking at the code itself but rather how it's organized
Fleshing out #mgilson's comment, here's a very simple start:
def file_similarity(path1, path2):
"Return float in [0., 1.] giving some measure of file similarity."
import difflib
with open(path1, "rb") as f1, open(path2, "rb") as f2:
s = difflib.SequenceMatcher(
lambda ch: ch in " \t", # don't sync on blanks or tabs
f1.read(),
f2.read())
return s.ratio()
Read the SequenceMatcher docs for more. In particular, if you have many files to compare, it's much more efficient to reuse a SequenceMatcher object (see the set_seq1() and set_seq2() methods). And if you're using a threshold, as the accepted answer suggested, see the real_quick_ratio() and quick_ratio() methods to slash the time more.
To get better results, I'd feed the files through a normalization transformation first, primarily to replace tab characters with spaces (tabs and spaces are as different to character comparison as, say, 'a' and '/', but the distinction is invisible to the human eye). Removing all-whitespace lines would probably help too.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am curious as to why Arrays are used when you could use an ArrayLists instead? Wouldn't it always be better to use an ArrayList?
Check out this comparison.
As you can see, there are important differences between the two constructs. You'll find APIs using one or the other (or both), and you have to understand the pros/cons and the functional differences between the two.
One particular difference is that a native array can store primitives without the inefficiencies of boxing/unboxing. That's significant when you have sizeable arrays representing data streams / data sets.
Note also that an ArrayList is not covariant. That is, an Integer[] is a Number[], but an ArrayList<Integer> is not a ArrayList<Number>. See here for more details.
Arrays and ArrayLists serve different, though sometimes overlapping, purposes.
But aside from that, the simple fact is that you don't code in a vacuum, and so you're going to use APIs that involve arrays, so learning about them isn't optional. Arrays are an absolutely fundamental structure in computer science.
There are many things you can do with arrays that you can't do with ArrayLists. For example,
Read multiple bytes or code-units from a file using a byte[] or a char[]
Call a native numeric or graphics library that deals with vectors, or matrices represented as float[]s or double[]s
You can use other non-primitive-array abstractions instead of ArrayList, but sometimes the best way to represent a contiguous region of memory divided into numeric units is using an array.
It is very important to have a proper foundation in programming fundamentals. These fundamentals include things like variable types, functions, arguments, arrays, and so on. Without a proper foundation, everything else you learn can collapse since you don't have a proper foundation. In other words, lets say you want to learn a language, you could start to learn whole phrases by memorization, but it is nice to get some words rolling off the tongue and start digesting and piecing how you can make sentences out of words instead of brute forcing it and memorizing all the sentences you need.
It is important for you to learn arrays if you wish to have a better understanding of what happens "behind the scenes", or if you want to learn a lower level language like C or assembly languages.
May I ask where you are learning Java? Is this for school/college? If so, please try to get something out of working with Arrays, because they will be used in courses like Data Structures and especially if you start using a language that doesn't have an ArrayList equivalent.
Learn all you can about it. Understanding what's happening is rewarding.
String []array = { "I","DO","NOT","RESIZE" };