Questions about Java programming

Solved
azerty0123 Posted messages 3 Status Member -  
 azerty0123 -
Bonjour,

I am a beginner in computer science and as part of my degree, I am working on Java language.

First of all, I would like to know what SetN means. In fact, in an exam, I had the following exercise:

What does the Java program containing these two classes do when executed?

Here are the two Java classes:
public class Main {
public static void main (String[] args) {
To to;
to = new To();
to.setN(1);
System.out.println(to.affiche());
to.setN(7);
System.out.println(to.affiche());
to.setN(8);
System.out.println(to.affiche());
to.setN(12);
System.out.println(to.affiche());
to.setN(37);
System.out.println(to.affiche());

}
}

public class To {
private int n;
public To() {
this.n = 0;
}

public void setN(int n) {
this.n = n;
}

public String affiche() {
int i;
int j = this.n;
String res = "";
while (j > 0) {
i = j % 8;
res = String.valueOf(i) + res;
j = j / 8;
}

return res;
}

I also have a problem with classes. I don't quite understand what they are. Is the second program a class?

I hope to hear from you soon. Thank you very much.

3 answers

KX Posted messages 19031 Status Moderator 3 020
 
The two files are classes (they start with the keyword "class")
"Running a program" means calling a main method of a class.
Here, it is the main method of the class Main where the object a is an instance of the class Toto, so a.setN is a call to the setN method defined in the class Toto...

PS: next time you copy-paste code, make sure it remains readable, because with spaces everywhere and missing braces, it's horrible to read!
--
Trust does not exclude control
0
azerty0123
 
Désolé, je n'ai pas fait attention ^^

public class Toto { Here we define the class we call toto
private int n; Here we define n, but why do we put private in front?
public Toto () {
this.n = 0;
We set n to 0, if I understand correctly we use this.n to define a value for n
}
public void setN (int n) { Here, we define the function SetN
this.n = n; We redefine n as being equal to n and no longer to 0
}
public String affiche () { Show function
int i;
int j = this.n;
String res = " "; Here, why do we put String res and not int res?
while (j > 0) {
i = j % 8;
The % confuses me, it's not divided, so what is it? for example 14 % 8 gives what?
res = String.valueOf(i) + res;
This means res=value of i + value of res? But res = " " nothing actually.
j = j / 8;
Why define j=j/8 after writing the value of res, the value of i depends on j, but we take j as being equal to n, and now we want j=j/8, I'm not really understanding.

}
return res;
}
}

Here is the program written correctly.
I understand some things better now, but what is currently bothering me is that the program should display 1, 7, 10, 14, 45. I have put in bold what I thought I understood and the questions I have.

It would be really nice to explain this to me. I think it could greatly help me.

Thank you very much :)
0
azerty0123
 
1 % 8 donne 1 et 7 % 8 donne 7.
0
KX Posted messages 19031 Status Moderator 3 020
 
"Here is the program rewritten correctly.", I'm sorry but there is a huge difference in readability between for example "p u b l i c c l a s s Toto" and "public class Toto"...

"Why do we put private": look at what accessibility is
"we put this.n to define a value for n": not just any value, only that of the current object (this), here doing this.n=0 is exactly the same as doing n=0 (this is implicit)
"we redefine n": no, we change its value, redefinition is something else (see inheritance)
"why do we put String and not int": that's because we don't want an integer anymore, but a string (see data types)
"The % bothers me", it's the remainder of integer division, so a%b==a-(a/b)*b, 1%8==7%8==0
res = " " nothing actually, only at the beginning, but then you modify res, so that's no longer true.
"Why define j=j/8": ask yourself what your program should do, by testing it for example, modifying it a bit, and retesting it, by adding some output... If you don't modify j, you'll always have the same values of i and j in your while loop, and your program will always do the same thing, without stopping (because of the stopping condition j>0)!
0
azerty0123
 
Ahhhhh thank you very much it's clearer for me.

But then for n=1, it means that j=1, so i=0 (1%8==0) and therefore res=0 (because value of i==0 and res=0 at the beginning).
Yet in the correction, the result is 1.
0
KX Posted messages 19031 Status Moderator 3 020
 
I made a mistake: 1%8==1, 7%8==7, and generally: (bq+r)%b==r
0
azerty0123
 
So if I do for n=2, in this case j=1, so i=1 (because 2%8=1) and therefore res= 1+1 because res = 1 since for n=1 res=1.

For n=3, j=3, so i=1, and res=1+2=3

For n=4, j=4, so i=1, and res=1+3

For n=5, j=5, so i=1, and res=1+4

For n=6, j=6 so i=1 and res=1+5

For n=7, j=7 so i=1, and res=1+6;

For n=8, j=8, and i=0, and thus res=0+7;

But in the correction for n=8, res returns 10.
0
KX Posted messages 19031 Status Moderator 3 020
 
It is necessary to break down your code to understand how it works

n=8, j=n(=8), res="" i=j%8(=0), res=i+res(=0+""="0"), j=j/8(=8/8=1) i=j%8(=1), res=i+res(=1+"0"="10"), j=j/8(=1/8=0) return res(="10")
0
azerty0123
 
Donc if I do the same for n=37:

n=37, j=37, res=" ";
i=j%8=37%8=5 so res=5+" "="5", j=j/8=37/8=4
i=4%8=1 res=1+"5"="15" j=j/8=4/8=0
return res (= "15")

Why do I find 15 when we should find 45

For n=12, j=12, i=12%8=4 so res=4+" "="4" j=12/8=1
i=1%8=1 so res=1+"4"="14" j=1/8=0
return res (=14)

Here I correctly obtain the right value so why do I not find 45 for n=37, where is my mistake?
0
KX Posted messages 19031 Status Moderator 3 020
 
i=4%8=4, res=4+"5"="45" j=j/8=4/8=0
0
azerty0123
 
Alright, I understand the program well now, it just took me some time. Thank you very much!!
0