#Begginer Help
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
You call non static method in static method
Also you use not good ide
Use IneligeIdea
For student they have subscription
I used the "add" method in my static main and it works fine
Aren't "add" and "getIn" the same types of methods?
Since they are both "public Complex"
Give more information
You use object method like static
But it only object method
Before use it you should creat instance of class which method you wanna use
I dont get the difference between a static method and a non-static one
Since they both dont have the word "static" like public static Complex getIn or public static Complex add, aren't they both non-static?
I want to use getIn to get the user's input of a complex number
I know it in theory
I am just a bit confused on applying it
Do I need to call the method through an instance?
If its not static
Yes
But that made sense since I wanted to add both of them
From which instance would I call getIn()
From any instance of Complex class
But each instance have self value in there fields
getIn should be static
I'm lost
Complex shouldn’t read value from console
?
{
Scanner myScan = new Scanner(System.in);
System.out.println("Enter \"real\" part");
Double complexreal = myScan.nextDouble();
System.out.println("Enter \"Imaginary\" part");
Double complexim = myScan.nextDouble();
return new Complex(complexreal,complexim);
}```
Detected code, here are some useful tools:
public Complex getIn() {
Scanner myScan = new Scanner(System.in);
System.out.println("Enter \"real\" part");
Double complexreal = myScan.nextDouble();
System.out.println("Enter \"Imaginary\" part");
Double complexim = myScan.nextDouble();
return new Complex(complexreal, complexim);
}
Find book Full Java Rerbert Shild
Other class should read value from console, after call Complex constructor and you get instance with needed value
And if you want use “fabric” make getIn() static
You should understand design pattern
I moved it to a seperate class
I feel like this is really simple
I just want to get the Complex that getIn() returns and save it to a variable
It's this, isnt it?
The assignment says to make a method that reads the value
U create a new Scanner everytime u call thst method
You should not do that
Make the Scanner a static field and use that, since you have to read the values in ur class itself
?
private static Scanner sc = new Scanner(System.in);
Outside the method obviously
But in which class
Like how methods are can be static and non static, even fields cam be both
Ur Complex class
Because that is where u use ur scanner
I am having trouble understanding what you mean by "fields"
Fields meaning ur instance variables
Like a and b
This is like my 4th lesson in java
teacher aint said nothing about either private or fields
That is why I am a bit lost here
the "real" and the "imaginary" part of the Complex number respectively
Or do you mean like what type of variables they are?
It's the default variables for every new Complex Instance.
Just make getInt static
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
import java.util.*;
/**
*
* @author royverd
*/
public class Complex {
double a;
double b;
public Complex(double a, double b) {
this.a = a;
this.b = b;
}
public Complex() {
// this is empty so that we can create a new instance without a,b
}
/*
public Complex add2(Complex x, Complex y){
Complex retVal = new Complex();
retVal.a = x.a+y.a;
retVal.b = x.b + y.b;
return retVal;
}*/
public Complex add(Complex z) {
return new Complex(this.a + z.a, this.b + z.b);
}
Scanner myScan = new Scanner(System.in);
public Complex getIn()
{
System.out.println("Enter \"real\" part");
Double complexreal = myScan.nextDouble();
System.out.println("Enter \"Imaginary\" part");
Double complexim = myScan.nextDouble();
return new Complex(complexreal,complexim);
}
public Complex sub(Complex z) {
return new Complex(this.a - z.a, this.b - z.b);
}
//public double metro(){
// return Math.sqrt(Math.pow(this.a, 2) + Math.pow(this.b, 2));
//}
public double metro(){
return Math.sqrt(a*a+b*b);
}
//public String toString() {
// return a + "+" + b + "i";
//}
}
Detected code, here are some useful tools:
* Click nbfs : //nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs : //nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
* / import java.util. * ;
/**
*
* @author royverd
*/
public class Complex {
double a;
double b;
public Complex(double a, double b) {
this.a = a;
this.b = b;
}
public Complex() {
// this is empty so that we can create a new instance without a,b
}
/*
public Complex add2(Complex x, Complex y){
Complex retVal = new Complex();
retVal.a = x.a+y.a;
retVal.b = x.b + y.b;
return retVal;
}*/
public Complex add(Complex z) {
return new Complex(this .a + z.a, this .b + z.b);
}
Scanner myScan = new Scanner(System.in);
public Complex getIn() {
System.out.println("Enter \"real\" part");
Double complexreal = myScan.nextDouble();
System.out.println("Enter \"Imaginary\" part");
Double complexim = myScan.nextDouble();
return new Complex(complexreal, complexim);
}
public Complex sub(Complex z) {
return new Complex(this .a - z.a, this .b - z.b);
}
//public double metro(){
// return Math.sqrt(Math.pow(this.a, 2) + Math.pow(this.b, 2));
//}
public double metro() {
return Math.sqrt(a * a + b * b);
}
//public String toString() {
// return a + "+" + b + "i";
//}
}
This seems to work
Make getIn static please
I hesitate because my teacher hasn't mentioned that yet. Also I recall him doing the similar thing without it being static. I just dont wanna get ahead of myself
It doesn't make any sense to not have it static
I can't tell the difference if I am being honest
Your code is currently saying
"In order to create a complex from the user, you first need to create a complex, then ask this complex to ask user input and then the complex will create a complex"
Let me process that for a bit
tl:dr it doesn't make sense that creating a complex is needed to ask the user and create a complex from this
I think I see your point
I am asking the "Complex" getIn to create a new Complex out of the user inputs.
A complex shouldn't ask the user data to create a complex
I am unfamiliar with programming talk
When you say "A Complex" are you referring to the getIn() method?
I am talking about a Complex object
Oh
So you're saying I should make the scanning process completely independant and then call it when I need it for the creation of the complex number?
Complex z = new Complex()
Complex c = z.getIn()
println(c)
It doesn't make sense that a Complex asks user input and create a complex
A Complex is a number
I see
Since when a number ask things to someone and since when a number creates numbers
That's why it should be static
So you're saying to create a new java class for the scanner then
Either that or make them static
I am having trouble wrapping my head around the model
Which one ?
if I make it static, doesnt that just mean that getIn would be the same for all instances of Complex?
That's not the point of static
It is the same for everything
Or rather
It's associated to the clasd
Not the object
Instead of calling the method on the object, you call it on the clasd
That's what static means
Would that make a difference? Since the method uses it's own variables?
I have a hard time seeing the effect that static would have in terms of how the algorithm would work
The difference is that one is called on the object, the other on the class
What does it actually mean "called on", I can't envision it in my head
Right now, I don't see what difference it would make if it was being called from either the class or the object
Well yeah, but what difference would it make programm-wise
Didn't I just showed you ?
I just dont get it
What do you not understand here ?
what difference does it make who calls the method
It's not how call it
It's on who you call it on
If you call it on z, then it means you must have an object to call it and this object is responsible for what happens in the method, which doesn't make any sense here
A number is not supposed to ask user input that's what I mean
Let me process everything for a sec
I think I get it
shouldnt this be it then?
You know you could have just made it static...
Anyway, don't name it scanner and make the scanner a field
Like that?
or
Are both the same?
btw its double not Double. hence its also yellow
Oh, didnt notice that, thanks
no. the first creates a scanner when u do new ScanInput() and keeps it alive, reusing it. the second creates a fresh scanner each time u call getIn()
So the first one is better then
yes
but ur probably supposed to make the scanner a static field
then its created once and reused all the time
instead of once per new ScanInput()
My brain is fried
for whatever reason, even though I know what static is in theory, it bamboozles me in practise
How do I do that
The keyword static controls ownership (not accessibility). A method or field that is static belongs to the class instead of an instance of a class. Suppose there is a simple class called Person:
class Person {
String name;
Person(String name) {
this.name = name;
}
String getName() {
return name;
}
}
Now, you can create as many instances of this class as you want:
Person first = new Person("John");
Person second = new Person("Jane");
And each person has his own name, because it is non-static. So we can do
System.out.println(second.getName()); // prints Jane
In total, since there exist 2 instances of the Person class now, we also have 2 names and also 2 getName() methods. Each belonging to its corresponding instance of the class.
Now, if something is static, it belongs to the class and not to instances of it. While we just created 2 instances of Person, there exists exactly one class Person itself. So if we write:
class Person {
static int amountOfEyes = 2;
String name;
Person(String name) {
this.name = name;
}
String getName() {
return name;
}
}
Then amountOfEyes belongs to the Person class, not to the instances we created. As a result, there exist in total 2 names, 2 getName() methods but only one amountOfEyes variable.
As such, we also access this variable through the class, not through instances of it:
// Like this
System.out.println(Person.amountOfEyes);
So neither first nor second nor third have their own amountOfEyes. There is only one amountOfEyes and it belongs to the Person class itself.
To summarize, static things belong to the class, there is only one class no matter how many instances you create of it. Non-static things belong to instances of a class, each instance has its own set of non-static fields and methods.
oh I see
Thank you!
Thank you all for your time, I am just getting the grasp of programming and it just doesnt come natural to me :3
/done
!done
/solved
Closed the thread.
/inactive