Computer Questions Not Worth Their Own Thread II

THIS.

Should not be happening.

Why is it happening? :mad:

Spoiler :
Jump to about 0:20 to see it. It's sideways because my phone is too damn confusing. :mad: If it won't play, convert it yourself. Don't ask me to do something for you that I don't know how. VLC, Avidemux, et al, all are huge, steaming piles of horse doo doo. :mad:


EDIT: This is on a different computer, btw; the one you see in the video; so not the one I've been posting about for the past few days. They are my mouse and keyboard (see below).
 
What am I even looking at here? I see some object blinking red occasionally and some other object blinking green occasionally, but I don't even know waht the objects are. Only answer I can possibly give you is... (go to 1:56)

Spoiler :
 
Those are my mouse and keyboard (the green light is the wireless transmitter to the keyboard; the red light is from the mouse itself), both are connected directly to the laptop, each in their own slot, so no multi-port hub for this setup.
 
Java problem:

I'm a noob to Java and I wanted to try using multiple classes, parameters and return; method. However my program returns always "0" back to me, instead of the number I feeded there...

Here's my code:
Spoiler :

import java.util.Scanner;

public class parametrit {
static Scanner skan = new Scanner(System.in);
static int n = skan.nextInt();
static apuluokka otus= new apuluokka();

public static void main(String args[]){
otus.AsetaLuku(n);
otus.tulo();
}

}

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

public class apuluokka {
private static int numero;

public void AsetaLuku(int luku){
luku=numero;
}

public static int palauttaja() {
return numero;
}

public void tulo() {
System.out.println("Luku on "+ palauttaja());
}
}


Can you help me?

Edit: Sorry for weird class/ variable names :p!
 
Java problem:

I'm a noob to Java and I wanted to try using multiple classes, parameters and return; method. However my program returns always "0" back to me, instead of the number I feeded there...

Here's my code:
Spoiler :

import java.util.Scanner;

public class parametrit {
static Scanner skan = new Scanner(System.in);
static int n = skan.nextInt();
static apuluokka otus= new apuluokka();

public static void main(String args[]){
otus.AsetaLuku(n);
otus.tulo();
}

}

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

public class apuluokka {
private static int numero;

public void AsetaLuku(int luku){
luku=numero;
}

public static int palauttaja() {
return numero;
}

public void tulo() {
System.out.println("Luku on "+ palauttaja());
}
}


Can you help me?

Edit: Sorry for weird class/ variable names :p!

I have not looked in detail, but I suspect your problem is with:

public void AsetaLuku(int luku){
luku=numero;
}

This will NOT make the class variable numero equal the passed variable luku but vice versa. I think you want:

numero=luku;

A more usual pattern for this would be:

public void AsetaLuku(int numero){
this.numero=numero;
}
 
I have not looked in detail, but I suspect your problem is with:

public void AsetaLuku(int luku){
luku=numero;
}

This will NOT make the class variable numero equal the passed variable luku but vice versa. I think you want:

numero=luku;

A more usual pattern for this would be:

public void AsetaLuku(int numero){
this.numero=numero;
}
Oh sweet, you are the boss :king:!! Thanks :)! I really didn't know that x=y is not always the same thing with y=x in Java. Its like x equals y means you put y's value to be x's value. Nice. I will learn this language someday!
 
Yeah x=y in most computer languages is not a statement of equality or equivalence but one of assignment: you are taking the variable "x" and assigning it the value of "y"; or put another way, you are setting x to whatever y currently is. I was taught that "=" meant "becomes equal to", rather than simply "equals".

In some languages, the assignment symbol is the same as the comparison symbol, but in other languages they are different.
 
Yeah x=y in most computer languages is not a statement of equality or equivalence but one of assignment: you are taking the variable "x" and assigning it the value of "y"; or put another way, you are setting x to whatever y currently is. I was taught that "=" meant "becomes equal to", rather than simply "equals".

In some languages, the assignment symbol is the same as the comparison symbol, but in other languages they are different.

Yeah, that's right! In Java, don't you test the equality with == mark? By the way, you guys are so helpful so I have another question: what is the meaning of static statement? I don't know when to use it and what it actually does. My Eclipse won't run the program sometimes, if I don't use static- word in specific method/var. Thanks.
 
Yeah, that's right! In Java, don't you test the equality with == mark? By the way, you guys are so helpful so I have another question: what is the meaning of static statement? I don't know when to use it and what it actually does. My Eclipse won't run the program sometimes, if I don't use static- word in specific method/var. Thanks.

It is a little complex, but I shall try. You may find other sources better (more so after I have written it, feel free to ignore this):

Static means the "thing" refers to the class, rather than individual individual objects of that class. "Thing" in this context can be a variable or method. So for variables your could have a class:

public SomeClass {
static int staticVariable = 0;
int dynamicVariable = 0;
}
...
SomeClass a = new SomeClass();
SomeClass b = new SomeClass();
a.staticVariable = a.staticVariable + 1;
b.staticVariable = b.staticVariable + 1;
a.dynamicVariable = a.dynamicVariable + 1;
b.dynamicVariable = b.dynamicVariable + 1;

After this a and b.staticVariable == 2, and a and b.dynamicVariable == 1.

Functions are similar, but are mostly used to group code with a method, rather than effect a particular instance of a class.

I hope this makes sense, but after writting it I am sure you would be better with google, or the link above.
 
Put slightly differently, if you change the static variable in instance "a" of the class, it will also change the static variable in instance "b" of the same class. In Samson's code, adding 1 to "a.staticVariable" also added 1 to "b.staticVariable", because the variable "staticVariable" refers not to "a" or "b" specifically, but to the class "SomeClass" itself. OTOH, a variable declared without the static keyword will only pertain to the particular instance of the class; hence, changing a.dynamicVariable didn't also change b.dynamicVariable, as a.dynamicVariable is particular to the instance "a" of the class, rather than to all instances of SomeClass.

Your program probably isn't working without declaring the variables as static because you're changing a variable in one instance of the class and expecting it to change in another instance of the same class. If this is what you want then you need to declare it as static.
 
Your program probably isn't working without declaring the variables as static because you're changing a variable in one instance of the class and expecting it to change in another instance of the same class. If this is what you want then you need to declare it as static.

It could be, but as you say:

My Eclipse won't run the program sometimes, if I don't use static- word

my money is on you trying to access a dynamic variable within a static method, most likely main().
 
OK, thanks again Samson and Mise. It is still hard to decide if useing 'static' or not. I should practice it myself since I now know the logic somehow.
 
Is the English word "static" part of the confusion?

Is it better to think of Static as meaning "status-ly" or "state-ly" instead of "unchanging"?
 
Is the English word "static" part of the confusion?

Is it better to think of Static as meaning "status-ly" or "state-ly" instead of "unchanging"?

Good point. If you want "unchanging" then "final" is what you want (which should frequently be used with "static").
 
More problems:

Spoiler :


public class array {

public static void main(String args[]){
int array[]={15, 17, 90, 78, 14};

System.out.println("Index\tArray");

for (int counter = 0;counter>array.length;counter++){
System.out.println(counter+ "\t"+ array[counter]);
}
}
}



That program should print an array table but it wont. Just Brings me back these words:

"Index Array"

I wanted to print the array too... Why this?
 
the 2nd term in the "for" statement doesn't mean "do until this is true", it means "do while this is true" (or alternatively, "do until this is false"). In other words, it will do everything in the for loop only as long as "counter>array.length". Obviously counter starts off at 0, so it will never execute anything in the for loop. You need to change it to < instead of >.

see here for more info: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

specifically:
for (initialization; termination;
increment) {
statement(s)
}

When using this version of the for statement, keep in mind that:

The initialization expression initializes the loop; it's executed once, as the loop begins.
When the termination expression evaluates to false, the loop terminates.
The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.
 
More problems to solve:

I'd like to make multidimensional array table. Watched YT Video and made similar program. My program doesn't change line when it should do it:
Spoiler :

class array {
public static void main(String args[]) {

int table[][]= {{2, 5, 7, 2, 1},{12, 8, 2, 3, 5, 67, 12, 1},{1, 2}};
int table2[][]= {{1, 6, 7, 9},{12, 1},{1, 2, 3, 4, 5, 6, 7}};
System.out.println("First table:");
printer(table);
System.out.println("\nSecond table:");
printer(table2);

}

public static void printer(int x[][]) {
for (int row=0;row<x.length;row++){
for (int column=0;column<x[row].length;column++){
System.out.print(x[row][column]+"\t");
}
}
}
}


It prints:

First table:
2 5 7 2 1 12 8 2 3 5 67 12 1 1 2
Second table:
1 6 7 9 12 1 1 2 3 4 5 6 7
(just prints it to 1 line per table)

Instead of:


First table:
2 5 7 2 1
12 8 2 3 5 67 12 1
1 2
Second table:
1 6 7 9
12 1
1 2 3 4 5 6 7


 
Back
Top Bottom