what’s the difference between these two pieces of code?

I was experimenting with these two pieces to see if they still have the same output and I thought they would but I keep running into an error for the replit one (the one with the dark mode bg) The one in the light mode bg is the original piece of code and I tried to create the same output differently in the one with the dark mode bg but idk what is wrong Can anyone pls explain/help me
No description
No description
No description
No description
47 Replies
JavaBot
JavaBot12mo ago
This post has been reserved for your question.
Hey @applesandhoney! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
applesandhoney
applesandhoneyOP12mo ago
The output for the light mode bg is supposed to be 30 And that’s what im trying to get in the dark mode bg code too but idk what is wrong
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
noComment | Kez
noComment | Kez12mo ago
There is no this.nthat is part of the Overtime class only n since it is the argument of the constructor Other than that I think it's pretty much accurate
applesandhoney
applesandhoneyOP12mo ago
So I just write Overtime ot = new Overtime(n); ? But doesn’t Overtime ot = new Overtime(this.n); Basically just pass a reference of the parameter variable n? So isn’t it basically the same thing then?
noComment | Kez
noComment | Kez12mo ago
no this.n is an instance variable call there is no instance variable called n only an argument that is only accessible within the method it`s an argument of see ./run
public class Main {
int i = 5; //this is an instance variable;
public static void main(String[] args) {
Main m = new Main();
m.foo(10);
}
void foo(int i){//the i here is only reachable within these brackets
System.out.println(i);//calls the argument
System.out.println(this.i);//calls the instance variable
}
}
public class Main {
int i = 5; //this is an instance variable;
public static void main(String[] args) {
Main m = new Main();
m.foo(10);
}
void foo(int i){//the i here is only reachable within these brackets
System.out.println(i);//calls the argument
System.out.println(this.i);//calls the instance variable
}
}
I Run Code
I Run Code12mo ago
Here is your java(15.0.2) output @noComment | Kez
10
5
10
5
applesandhoney
applesandhoneyOP12mo ago
ohh okay wait so in the code with the light mode bg, Overtime ot = new Overtime(this) does it just take the entire object that is passed to it?
noComment | Kez
noComment | Kez12mo ago
this in this context passes the current Pay object into the constructor of Overtime an object is an instance of a class it is basically the same
applesandhoney
applesandhoneyOP12mo ago
and since its one.calculatePayWithOverTime it passes one into overtime later so it depends on which object we call the calculatePayWithOverTime then right,
noComment | Kez
noComment | Kez12mo ago
Well I don't get why you gave calculatePayWithOverTime an argument anyways just delete the argument make the call Overtime ot = new Overtime(this); and be done with it
applesandhoney
applesandhoneyOP12mo ago
i did it cause i thought it'd be the same as Overtime ot = new Overtime(this); but just that its passing the object as a parameter variable?? im not sure if i make sense but got it, this. is just an instance variable call
noComment | Kez
noComment | Kez12mo ago
well it is the same as long as you pass the same object into it can also be an instance method call and this without the . just represents the instance
applesandhoney
applesandhoneyOP12mo ago
so isnt Overtime ot = new Overtime(this.n); passing the object? or not cause like u said n is a parameter and not instance also if i did just Overtime ot = new Overtime(n) would it mean the same thing?
noComment | Kez
noComment | Kez12mo ago
this.nwont work here because there is no instance variable called n
applesandhoney
applesandhoneyOP12mo ago
okay
noComment | Kez
noComment | Kez12mo ago
and making it Overtime ot = new Overtime(n) kind of defeats the purpose of the method
applesandhoney
applesandhoneyOP12mo ago
oh cause then i can just call overtime from main
noComment | Kez
noComment | Kez12mo ago
The method is supposed to act upon the instance it is called from
applesandhoney
applesandhoneyOP12mo ago
okay
noComment | Kez
noComment | Kez12mo ago
so we call this not n the method is not supposed to have an argument why would you do that?
applesandhoney
applesandhoneyOP12mo ago
no like it was in response to how u said it defeats the purpose of the method, like thats why it defeats the purpose?
noComment | Kez
noComment | Kez12mo ago
it defeats the purpose because you could just pass another pay object which wouldnt make sense
applesandhoney
applesandhoneyOP12mo ago
okay
noComment | Kez
noComment | Kez12mo ago
Pay kezsPay = new Pay(5);
Pay apllesandhoneysPay = new Pay(10);
applesandhoneysPay.calculatePayWithOverTime(kezsPay);
Pay kezsPay = new Pay(5);
Pay apllesandhoneysPay = new Pay(10);
applesandhoneysPay.calculatePayWithOverTime(kezsPay);
doesn't really make sense right?
applesandhoney
applesandhoneyOP12mo ago
yeah it doesnt
noComment | Kez
noComment | Kez12mo ago
exactly
john
john12mo ago
public class Pay
{
private double pay;
private Pay n;

public Pay(double p)
{
pay = p;
}

public double getPay()
{
return pay;
}

public void calculatePayWithOvertime(Pay j)
{
// this Pay object is passed to the Overtime constructor
n = j;
Overtime ot = new Overtime(this.n);
pay = ot.getOvertimePay();
}
}
{
private double pay;
private Pay n;

public Pay(double p)
{
pay = p;
}

public double getPay()
{
return pay;
}

public void calculatePayWithOvertime(Pay j)
{
// this Pay object is passed to the Overtime constructor
n = j;
Overtime ot = new Overtime(this.n);
pay = ot.getOvertimePay();
}
}
This message has been formatted automatically. You can disable this using /preferences.
applesandhoney
applesandhoneyOP12mo ago
it gives me 30 now sry ik i made it extra complicated but i just wanted to see if there was any other way to get 30 using this
noComment | Kez
noComment | Kez12mo ago
are you trying to make fun of me
applesandhoney
applesandhoneyOP12mo ago
no i just was trying to get another way
noComment | Kez
noComment | Kez12mo ago
okay
applesandhoney
applesandhoneyOP12mo ago
sry i didnt mean it that way
noComment | Kez
noComment | Kez12mo ago
yeah I was a bit harsh sorry
applesandhoney
applesandhoneyOP12mo ago
i shared cause i wanted to ask that in the real world would this still be acceptable or not cause ik u said last time that theres a style of writing code
noComment | Kez
noComment | Kez12mo ago
well not really
applesandhoney
applesandhoneyOP12mo ago
oh okay yeah im just overcomplicating it
noComment | Kez
noComment | Kez12mo ago
since you still have this issue
applesandhoney
applesandhoneyOP12mo ago
ohh
noComment | Kez
noComment | Kez12mo ago
the method is very clearly not intended to have an argument
applesandhoney
applesandhoneyOP12mo ago
and since i call the method on the one object, it should just pass the one object?
noComment | Kez
noComment | Kez12mo ago
exactly in this case at least since it is a method that is intended to alter the instance from which it is called
applesandhoney
applesandhoneyOP12mo ago
okay
noComment | Kez
noComment | Kez12mo ago
anything else I can help you with?
applesandhoney
applesandhoneyOP12mo ago
nope thats good ty for ur help also sry abt that, i didnt intend to come off as rude or anything
noComment | Kez
noComment | Kez12mo ago
Nah I just misinterpretet you are in the right If you don't need further help you can just close this post 🙂
JavaBot
JavaBot12mo ago
Post Closed
This post has been closed by <@1127841130252357733>.
Want results from more Discord servers?
Add your server