Why the output is "021"? Why there are "0" and "1"(since "i" get "2" why it changes to "1")?
public class C {
protected int i;
public C(int i){
this(i,i);
System.out.print(this.i);
this.i=i;
}
public C(int i, int j) {
System.out.print(this.i);
this.i=i+j;
}
public C(){
this(1);
System.out.print(i);
}
public static void main(String[] args) {
C c=new C();
}}
C() calls C(1) which calls C(1,1)
C(1,1) prints 0 (the default value of this.i) and assigns 2 (i+j) to this.i
then C(1) prints 2 and assigns 1 to this.i
then C() prints 1
I think this is better for understanding:
public C(int i) {
this(i, i);
System.out.println("*"+this.i);
this.i = i;
}
public C(int i, int j) {
System.out.println("#"+this.i);
this.i = i + j;
}
public C() {
this(1);
System.out.println("#"+i);
}
Now, you can get the sequence of these methods when you invoke C();
Here the code commented, you will understand your problem now,
public class C {
protected int i;
public C(int i) {
this(i, i); // got to two parameter constructer and after the result print the next line
System.out.print(" + second "+this.i); // print value of i which is come from C(int i, int j) = 2
this.i = i; // set the value of i to 1
}
public C(int i, int j) {
System.out.print("first "+this.i); // print the value of i (in this case 0 the default value)
this.i = i + j; // set i to 2
}
public C() {
this(1); // got to one parameter constructer and after the result print the next line
System.out.print(" + Third is "+i); // print value of i which is come from C(int i) = 1
}
public static void main(String[] args) {
C c = new C();
}
}
I hope that help.
Related
I am writing code in Java which has multiple methods and these methods have multiple variables. I want the other methods to access the variables of another method using actual and formal parameters. How can I do it?
I am pasting an example of the problem I'm facing.
Error : variable is not defined.
Code
public class example {
public void addition() {
int a = 0;
int b = 10;
int c = a + b;
}
public void result() {
System.out.println("The result for the above addition is" + c);
}
}
IM GETTING AN ERROR SAYING VARIABLE IS NOT DEFINED
You should declare c as global variable
public class Example {
int c;
public void addition() {
int a = 0;
int b = 10;
c = a + b;
}
public void result() {
System.out.println("The result for the above addition is " + c);
}
public static void main(String[] args) {
Example e = new Example();
e.addition();
e.result();
}
}
well, your java syntax is quite wrong... if you need to do an addition, you can do as follows:
public class Addition {
public static int addition(int a, int b)
{
int c= a + b;
return c;
}
public static void main(String[] args) {
int a = 1;
int b = 10;
int c = addition(a,b);
System.out.println("The result for the above addition is " + c);
}
}
where addition function does add a + b and return the result to your main method.
class A {
int a, b;
A(int i, int j) {
a = i;
b = j;
}
}
class B extends A {
int c, d;
B(int i, int j) {
c = i;
d = j;
}
}
public class HelloWorld {
public static void main(String[] args) {
A aa = new A(5, 6);
B bb = new B(3, 4);
System.out.println(aa.a + aa.b + bb.a + bb.b + bb.c + bb.d);
}
}
it gives error as
HelloWorld.java:9: error: constructor A in class A cannot be applied to given types;
{
^
required: int,int
found: no arguments
reason: actual and formal argument lists differ in length
1 error
As you class B extends class A, you somewhere have to call the constructor of the class you are extending.
As your class A does not have a constructor with no arguments, you need to explicitly call the super constructor (the constructor from class A) as the first statement inside the constructor of your class B:
B(int i, int j) {
super(i, j);
// Your code
}
If you would have a no-args constructor in A, you would not need to do this, as the no-args constructor is implicitly called if no constructor call is specified:
B(int i, int j) {
// Your code
}
Is actually doing:
B() {
super();
// Your code
}
And as you do not have A() as a constructor, you get the error.
Check this one...
JVM always looking for no argument constructor.
class A {
int a, b;
A(int i, int j) {
a = i;
b = j;
}
public A() {
// TODO Auto-generated constructor stub
}
}
class B extends A {
int c, d;
B(int i, int j) {
c = i;
d = j;
}
}
public class pivot {
public static void main(String[] args) {
A aa = new A(5, 6);
B bb = new B(3, 4);
System.out.println(aa.a + aa.b + bb.a + bb.b + bb.c + bb.d);
}
}
I am trying to call the parent method in subclass. But I am getting stackoverflow error. The method name in both the class is same. If I change the method name it doesn't gives error. I am not understanding what is the reason behind it. Below is the code where I am calling display() method, in both the class which gives me stackoverflow error. If I change the name of method in any of one class it doesn't gives me error.
class inherit1 {
int i;
int j;
int k;
public inherit1() {
}
inherit1(int i, int j, int k) {
this.i = i;
this.j = j;
this.k = k;
}
void display() {
System.out.println("i=" + i + "j=" + j + "k=" + k);
}
}
class in1 extends inherit1 {
int l;
in1() {
}
public in1(int i, int j, int k, int l) {
super(i, j, k);
this.l = l;
}
void display() {
display();
System.out.println("l=" + l);
}
public static void main(String[] args) {
in1 i = new in1(12, 12, 12, 12);
i.display();
}
}
Change
void display() {
super.display();
System.out.println("k=" + k);
}
when you are calling display() it calls same child method againg and again causing stackoverflow.Instead you should use super keyword to reference parent method.
void display() {
display();
System.out.println("k=" + k);
}
With this method you keep calling the same method again, which in turn leads to a StackOverFlow error because you have created an infinite loop.
This will work better:
void display() {
super.display();
System.out.println("k=" + k);
}
Or even this would work:
void display() {
System.out.println(super.display() + ("k=" + k));
}
StackOverflow Error
Thrown when a stack overflow occurs because an application recurses
too deeply.
Problem is
void display() {
display(); // this line is culprit you are calling the same function again and again
System.out.println("k=" + k);
}
You should call parent's display method instead
I was trying to make a program that searches for a random number, but i had problems importing the "a" variable in the other method. I would be happy if i could get some explanation. I have already tried to make the variable static, but that doesn't work
import java.util.Random;
public class verschlüsselung {
private static void nummber(int a) {
Random r = new Random();
a = r.nextInt(999);
System.out.println(a);
}
private static void search(int b) {
b = 0;
if(b =! a) {
for(b = 1; b =! a ; b++) {
if(b == a) {
System.out.println("found the number " + b);
}
}
}
}
public static void main(String args[]){
nummber(0);
search(0);
}
}
There is no such thing as using local variables in other methods.
You can return the variable from one method. Than call this method from other and get there the variable.
Declare the variable 'a' to be static and remove the parameter 'a' passed in the nummber()
function. This function does not need any input as it assigns the value of a random number to the global static variable 'a' which is accessed in the method search().
your declaration and method signature should read :
private static int a;
private static void nummber(){....}
May this help you:
private static int nummber( int a){
Random r = new Random();
a =r.nextInt(999);
System.out.println(a);
return a;
}
private static void search(int b, int a){
b = 0;
if(b =! a){
for(b =1; b =! a ; b++){
if(b == a){
System.out.println("found the number " + b);
}
}
}
}
public static void main(String args[]){
int a = nummber(0);
search(0, a);
}
Can you help me find my error?
I'm trying to use these two methods here but my output is not working.
class Nine {
public static void Nine(String[] args) {
int x,y,z;
y = 3;
x = 7;
z = addEm(a, b);
System.out.println("answer= " +x);
}
public static addEm (double a, double b){
int c;
c = a+b;
}
}
Actually there are a lot of error in your code:
z=addEm(a, b);
here a and b are meaningless, you should use z=addEm(y,x); (if your intent is to sum three with seven)
System.out.println("answer= " +x);
I guess that you want to show the the results of the sum, therefore you should print z (and not x), so you should substitute with System.out.println("answer= " +z);
public static addEm (double a, double b) {
Here you missed the return type, and you need to consider also the type of parameters a and b. Since y,x and z are int, it is better if also a and b are int, and therefore specify also the return type as int:
public static int addEm (int a, int b) {
Or you can declare everything (y,x,z,a,b and return type) as a double: the important here is that they should be all of the same type. Moreover you miss also the return statement of the function addEm, that summarizing becomes:
public static int addEm (int a, int b)
{
int c;
c=a+b;
return c;
}
And finally also the function
public static void Nine(String[] args)
it is not right named for an entry point: its names should be main.
So in conclusion, if you apply all the fix (by modifying as less as possible your original code) a code that compile, run and works following some 'logic' is:
class Nine {
public static void main(String[] args) {
int x, y, z;
y = 3;
x = 7;
z = addEm(y, x);
System.out.println("answer= " + z);
}
public static int addEm(int a, int b) {
int c;
c = a + b;
return (c);
}
}
Man, this is a very basic java lesson:
every prog need an entry point, which is in java:
public static void main(String args[]){}
And then your code will execute.
You're passing arguments a and b to addEm, but those variables aren't initialized. I'm expecting you wanted to pass x and y instead.
class Nine
{
public static void Nine(String[] args)
{
int x,y,z;
y=3;
x=7;
z=addEm(x, y);
System.out.println("answer= " +x);
}
public static addEm (double a, double b)
{
int c;
c=a+b;
}
}
Your code will not work because your addEm method does not have any return type. In addition, the method you wrote takes Double params but while using you are trying to pass int to it. You also do not have any main method. I am assuming you misspelled or misunderstood the main method so below is the code which should work
class Nine
{
public static void Main(String[] args)
{
int x,y,z;
y=3;
x=7;
z=addEm(x, y);
System.out.println("answer= " + x);
}
public static int addEm (int a, int b)
{
int c;
c=a+b;
return c;
}
}