TRENDING NEWS

POPULAR NEWS

How Does This Output Comes In Java

What is the input and output to a java compiler?

The same as for any compiler: the input is source code file and the output is a binary executable file.

Actually the java sun compiler does in one step what the normal compiling process for C++ for example takes two steps: linking (input: source file, output: object file) and compiling (input: object file, output: binary executable file).

The .class file that results from java compiling is an executable, just that it's not a native executable but it can be run through the java virtual machine (using the "java " command).

The output of this Java code?

I'm taking an intro Java class, and having a hard time picking up on it. Could someone please help me out? I need to know what the output would be after these lines of Java code:


int enigma = 12;
int enigma2 = enigma * 2;
System.out.println("The value of enigma is: "+enigma);

Would it be "The value of enigma is 12"?
Because I feel like this is a trick question, and I'm missing some sort of error.
Thanks in advance.

Another Java Output question?

Simple. If you have a ++ it will modify the value of the variable. so where you have ++p > 10 one is added to p ie this evaluates to 7+1 therefore you get 8. As this is false the other two p++ are not executed.

The ++ operator is used to increment the variable if it is infront the variable (++p) the increment value will be used and if it is after the variable (p++) it will be incremented but the original value will be used. if you want to use the variable + 1 but do not want to modify the actual value of the variable you should use p + 1 therefore your code should look like this:

if ((p + 1) > 10 && (p + 1) > 11) p++;

Depending on what you actually want to do. That I do not know.

It uses any and all data types provided by the language or created by the user. If it’s user-created data, you often need to provide a toString() method. What your friend was likely thinking is that System.out.println takes a String as its argument — using the primitives value or the object’s toString method — but this is not the full story, in that System.out is a type of PrintStream. It is only a wrapper around OutputStream, which writes in byte arrays only and, in turn, is a wrapper around a method that writes in single bytes only.

How do I output multiple lines to a file in java?

I have been trying to write to multiple lines to one file in java. Whenever I do the below, it just ignores the \n part.
Here is part of my code:
try
{
Calendar cal = new GregorianCalendar();
int hour = cal.get(Calendar.HOUR);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
Writer output = null;
File file = new File("C:\\users\\"+user+"\\Desktop\\Stat...
output = new BufferedWriter(new FileWriter(file));
output.write( building[0]+": "+average[0]+"\n"+
building[1]+": "+average[1]+"\n"+
building[2]+": "+average[2]+"\n"+
building[3]+": "+average[3]+"\n"+
building[4]+": "+average[4]+"\n"+
building[5]+": "+average[5]+"\n"+
building[6]+": "+average[6]+"\n"+
building[7]+": "+average[7]+"\n"+
building[8]+": "+average[8] );
output.close();
}
catch (Exception e1)
{
JOptionPane.showMessageDialog(nul... "ERROR: Could not save file.");
}

What does the following code output to the screen? In Java?

Looks like someone's homework. I can answer the question for you (correctly unlike the other guy's answers) but let me explain something to you.

Primitives are passed by value while Objects are passed by reference. That is a basic rule when dealing with Java. (That is not exactly true, but for your purpose here it is).

When passing a primitive to a method, anything done to that variable from within the method does not affect the original variable. When passing an Object, manipulation of that object from within the method will be reflected on the original object.

In many programming languages you have functions.A simple math function would be addition:When you have a “+” sign, the parameters are on either side of the “+”.5+2Means that you take 5 and 2 and perform some function to it. In this case it’s addition, you add these numbers together.The RETURN VALUE would be what’s on the other side of the = sign.5+2 = 77 is the return value.In Java functions are defined life this: [static] ()for example: public string getName(person You){ return You.name;}string is the type that will be returned.The "return value" is the value you're expecting to be returned.In this case the return value is the person's name of the object you pass to the function.you can use this like:string yourName = getName(You);or if you have a class of person, you could make getName a method:public class person{ public string name;  public string getName(){ return this.name; }}person You;You.name = "bart";system.out.println(You.getName());Would print out: "bart"Obviously this is a contrived example, but I hope it conveys the message.The other part is, if a function is declared with a "void" it means there is no return value, it just does something.

Java - (logic error) Output of program does not make any sense?

You are not changing k inside the loop in calculateSeries, you just set it to 1.0/i at the beginning, when i is 1.0, and end up getting 10 copies of k=1.0 added into sum. Try this instead:

for (double i=1.0; i<=n; i+=1.0)
{
sum += 1.0/i;
}

...and delete the "double i=1.0;" statement outside.

I left that in double form to drop into your program, but for loops with floating point numbers are sketchy. No problems here, since integers are exactly representable. To see the problem, try the following:

int count = 0;
double x;
for (x=10.0; x < 11.0; x+=0.01)
{ ++count; }
System.out.println("count = " + count + ", final x=" + x);

That's intended to count for 10.0 to 10.9 in steps of 0.1, but the program output is:

count = 101, final x=11.009999999999978

It went one step too far because the step 0.01 is not exactly representable as a double, and the computations rounded down more than up in this case. The direction is not easy to predict. Take the same loop, only going from 0.0 to 1.0 instead, and you'll get the expected result. Either the rounding errors canceled out or rounding up won in that case. (I didn't look closely.).

The moral of all this is that you should prefer to use ints and longs to count things, and leave floats and double for precise-but-not-exact values.

I'd write that calculateSeries method as:

double calculateSeries(int n) // add n terms of the harmonic series
{
double sum = 0.0;
for (int i=1; i<=n; ++i)
{
sum += 1.0/i;
}
return sum;
}

The int values will count *exactly* from 1 to n with no fractions and no rounding.

Edit: Good answer AP! I *started* mine before you clicked Submit, but got diverted, mid-answer. Looks like we're on the same wavelength, though.

What does the "return" statement do in Java?

The return statement tells the function what value to give back, for example take the math functions, there's pow(x,n) which gives you x to the power of n, abs(x) gives you the absolute value of x, sin(x) gives you the sinus of x...etc

And you use them like this:
y=pow(6,2);
z=abs(-5 + 2 * y);
p = sin(z+1) + sin(3.14);

Now what happens is that the values that you give to the function between the parenthesis (those are called arguments) are COPIED into the arguments you define in the funcion, so for example if you have the function:

public static void myFunc(int x){
......//do the stuff with x;
}

public static void main(String[]args){
......int z=10;
......myFunc(z);
............//This is like writing, ......int x=z;
......................................... the stuff with x;
......myFunc(z+20-14 / 2);
............//This is like writing, ......int x=z+20-14 / 2;
......................................... the stuff with x;
}

So you see even when we wrote myFunc(z), z got COPIED into x, so from now on any change we do to x (in the function) WILL NOT apply to z.

Now as for the return function, imagine if we wanted to make a mathematical function like the ones above, so we can use it in statements like:

int a = myFunc(17) + myFunc(y+1);

What you do is that in the function declaration, in the "public static void funcName()" part, instead of void you choose the type of the variable you want your function to behave like, and then inside your function do all the calculations and then at the end of the function return the result

For excample if you want a functionthat will sum all the numbers from 1 to n, you write:

public static int sumNums(int n){
......int sum = 0;
......for(int i=1;i<=n;i++){
............sum += i;
......}
......return sum;
}

And then you use it like this:
int s = sum(50);
System.out.println("The sum of numbers from 1 to 50 is "+s);

Think of a two dimensional array as a chart. It has rows and columns.So if you have  int myArray [5][2]It has 5 columns and 2 rows.If you want to output it you need to first run through a column then in the same loop go through a row.For example in c++ if I wanted to output this array it would be like this :For (int col =0; col<5; col++){  For (int row=0; row <2; row++)    {        Cout << myArray [col][row]<

TRENDING NEWS