So i have to create a class and methods for getting userinput and one for printing them out
After i printed out the numbers. Im supposed to make inherited class and print out the same numbers but with their indexes, How can i get the numbers data from the first class?
public class Main {
public static void main(String[] args) {
// write your code here
OriginalMethod firstoutput = new OriginalMethod();
firstoutput.OriginalMethod();
firstoutput.out();
overki newki = new overki();
newki.out();
}
static class OriginalMethod {
public int []tomb = new int[3];
public void OriginalMethod(){
for(int i = 0; i<3;i )
{
Scanner beolvas = new Scanner(System.in);
System.out.println("Enter the " (i 1) ".number");
tomb[i] = beolvas.nextInt();
}
}
public void out()
{
for(int i = 0; i<3;i )
{
System.out.print(tomb[i] " ");
}
}
}
static class overki extends OriginalMethod {
@Override
public void out()
{
System.out.println();
for(int i = 0; i<3;i )
{
System.out.println(i ". " tomb[i]);
}
}
}
}
CodePudding user response:
I think you have the concept of class and instance (an object) mixed up.
It appears that you require only one instance - that of the subclass.
overki newki = new overki();
newki.OriginalMethod();
newki.out();
newki.out();
It also looks like the method OriginalMethod
should be a constructor - the spurious void
is a common mistake. So change:
public void OriginalMethod(){
to:
public OriginalMethod() {
and remove the line:
newki.OriginalMethod();
CodePudding user response:
This is not really the way I learned to code classes in Java (I learned about creating new Java classes inside the project's package rather than directly creating numerous classes inside the Main Class). That would allow you to create constructors, and then easily pass variables by using the super keyword, or even through some getter-setters. However, for the code you provided, the easiest solution I found was this :
public class Main {
public static void main(String[] args) {
int[]tomb;
OriginalMethod firstoutput = new OriginalMethod();
tomb = firstoutput.OriginalMethod();
firstoutput.out(tomb);
overki newki = new overki();
newki.out(tomb);
}
static class OriginalMethod {
public int[] OriginalMethod(){
int []tomb = new int[3];
for(int i = 0; i<3;i )
{
Scanner beolvas = new Scanner(System.in);
System.out.println("Enter the " (i 1) ".number");
tomb[i] = beolvas.nextInt();
}
return tomb;
}
public void out(int[]tomb)
{
for(int i = 0; i<3;i )
{
System.out.print(tomb[i] " ");
}
}
}
static class overki extends OriginalMethod {
@Override
public void out(int[]tomb)
{
System.out.println();
for(int i = 0; i<3;i )
{
System.out.println(i ". " tomb[i]);
}
}
}
}
Allow me to explain what I changed. First off, I moved the int[]tomb
variable out of the OriginalMethod
class and into the Main. Then, I changed the OriginalMethod
in order for it to return an int array. Therefore, you can easily "export" your array out of your OriginalMethod
from the main by writing tomb = firstoutput.OriginalMethod()
. From there, you simply have to change your out
methods so that they both receive this array we created in the Main (since the second out()
method is an Override, it is absolutely necessary that they both receive the same thing in their parameters).
And well, it does the trick. Now, is it perfect? Probably not. As I said, I probably would've used getter-setters had I done this myself, but starting with your original code, this is the easiest solution I found!