#Begginer Help

1 messages · Page 1 of 1 (latest)

polar orioleBOT
#

<@&987246399047479336> please have a look, thanks.

polar orioleBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

swift cliff
#

You call non static method in static method

#

Also you use not good ide

#

Use IneligeIdea

#

For student they have subscription

cerulean karma
#

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"

swift cliff
#

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

cerulean karma
#

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

cerulean karma
#

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

swift cliff
#

Yes

cerulean karma
#

I see

#

When I used add I called it from the instance x

#

x.add(y)

swift cliff
#

)

#

Use same for getIn()

cerulean karma
#

But that made sense since I wanted to add both of them

#

From which instance would I call getIn()

swift cliff
#

From any instance of Complex class

#

But each instance have self value in there fields

cerulean karma
#

self value?

#

Like that? Complex z = z.getIn();

lofty plaza
#

getIn should be static

swift cliff
#

Self double a and self double b

#

Also you have problem with solid

cerulean karma
#

I'm lost

swift cliff
#

Complex shouldn’t read value from console

swift cliff
cerulean karma
#
    {
        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);
    }```
polar orioleBOT
swift cliff
#

Find book Full Java Rerbert Shild

swift cliff
#

And if you want use “fabric” make getIn() static

#

You should understand design pattern

cerulean karma
#

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?

cerulean karma
rough nest
#

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

cerulean karma
#

a static field as in?

#

A seperate class

rough nest
#

Outside the method obviously

cerulean karma
#

But in which class

rough nest
#

Like how methods are can be static and non static, even fields cam be both

rough nest
#

Because that is where u use ur scanner

cerulean karma
#

I am having trouble understanding what you mean by "fields"

rough nest
#

Fields meaning ur instance variables

cerulean karma
rough nest
#

Like a and b

rough nest
#

I said a field

cerulean karma
#

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

rough nest
#

Hmmm

#

So according to you what are the variables a and b?

#

In your complex class

cerulean karma
#

the "real" and the "imaginary" part of the Complex number respectively

cerulean karma
#

It's the default variables for every new Complex Instance.

lofty plaza
#

Just make getInt static

cerulean karma
#
 * 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";
    //}
}
polar orioleBOT
# cerulean karma ```/* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default...

Detected code, here are some useful tools:

Formatted code
 * 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";
//}
}
cerulean karma
#

This seems to work

lofty plaza
cerulean karma
# lofty plaza 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

lofty plaza
cerulean karma
#

I can't tell the difference if I am being honest

lofty plaza
#

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"

cerulean karma
#

Let me process that for a bit

lofty plaza
#

tl:dr it doesn't make sense that creating a complex is needed to ask the user and create a complex from this

cerulean karma
#

I think I see your point

#

I am asking the "Complex" getIn to create a new Complex out of the user inputs.

lofty plaza
#

A complex shouldn't ask the user data to create a complex

cerulean karma
#

I am unfamiliar with programming talk

#

When you say "A Complex" are you referring to the getIn() method?

lofty plaza
cerulean karma
#

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?

lofty plaza
#
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

cerulean karma
#

I see

lofty plaza
#

Since when a number ask things to someone and since when a number creates numbers

#

That's why it should be static

cerulean karma
#

So you're saying to create a new java class for the scanner then

lofty plaza
cerulean karma
#

I am having trouble wrapping my head around the model

lofty plaza
#

Which one ?

cerulean karma
#

if I make it static, doesnt that just mean that getIn would be the same for all instances of Complex?

lofty plaza
#

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

cerulean karma
#

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

lofty plaza
cerulean karma
#

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

lofty plaza
#

z.getIn()
Vs
Complex.getIn()

#

@cerulean karma

cerulean karma
#

Well yeah, but what difference would it make programm-wise

lofty plaza
cerulean karma
#

I just dont get it

lofty plaza
cerulean karma
#

what difference does it make who calls the method

lofty plaza
#

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

cerulean karma
#

is responsible?

#

How does that work?

#

Does the method use it's variables?

lofty plaza
cerulean karma
cerulean karma
#

shouldnt this be it then?

lofty plaza
azure acorn
#

btw its double not Double. hence its also yellow

cerulean karma
#

Oh, didnt notice that, thanks

azure acorn
# cerulean karma Are both the same?

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()

cerulean karma
#

So the first one is better then

azure acorn
#

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()

cerulean karma
#

My brain is fried

#

for whatever reason, even though I know what static is in theory, it bamboozles me in practise

cerulean karma
polar orioleBOT
#

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.

cerulean karma
#

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

polar orioleBOT
# cerulean karma !done

Looks like you attempted to use a command? Please note that we only use slash-commands on this server 🙂

Try starting your message with a forward-slash / and Discord should open a popup showing you all available commands.
A command might then look like /foo 👍

cerulean karma
#

/solved

polar orioleBOT
#

Closed the thread.

cerulean karma
#

/inactive