Jump to content

Please help , JAVA, class with This. Its will take some time, so please..

Hey guys, i took a code we were suppose to write down in school. But i dont really get the [This] in the code, why it was used like that ?i mean i will write all the code, i will be more then appreciate if you could to explain me. I  mark the parts that i am not sure i understand, i mean , why in all parts we had to use getThis.

 

I really ppreciate if you could take few min to look on that and explain,. 

THANK YOU

 

 

 

.CODE [public class Box{
    
    private int _width;
    private int _length;
    private int _height;
    
    private final int DEFAULT_WIDTH = 10;
    private final int DEFAULT_LENGTH = 10;
    private final int DEFAULT_HEIGHT = 10;
    
    public Box () {
        _width = DEFAULT_WIDTH;
        _length = DEFAULT_LENGTH;
        _height = DEFAULT_HEIGHT;
    }
    
    public Box (int val){
        if (val <=0){
            _width = DEFAULT_WIDTH;
            _length = DEFAULT_LENGTH;
            _height = DEFAULT_HEIGHT;
        }
        
        else {
            _width = val;
            _length = val;
            _height = val;
        }
    }
    
    public Box (int length, int height, int width){
        if (width<=0) 
            _width = DEFAULT_WIDTH;
        else 
            _width = width;
            
        if (length<=0) 
            _length = DEFAULT_LENGTH;
        else 
            _length = length;
            
        if (height<=0) 
            _height = DEFAULT_HEIGHT;
        else 
            _height = height;
    }
        
    public void setLength (int val){
        if (val>0) 
            _length = val;
    }
        
    public void setHeight (int val){
        if (val>0)  
            _height = val; 
     }
     
    public void setWidth (int val){
        if (val>0) 
            _width = val;
    }
    
    public int getLength(){
        return _length;
    }
    
    public int getHeight(){
        return _height;
    }
    
     public int getWidth(){
        return _width;
    }
        
    public void resize (int factor) {
        if (factor >0){
            _width = this.getWidth()*factor; ;
            _length = this.getLength()*factor;
            _height = this.getHeight()*factor;
        }
    }
    
    public void resizeToLength() {
       int change = this.getLength();
       _width = change;
       _height = change;
    }
        
     public void resizeToHeight() {
        int change = this.getHeight();
        _width = change;
        _length = change;
    }   
    
     public void resizeToWidth() {
        int change = this.getWidth();
        _height = change;
        _length = change;
    }   
    
    public int volume(){
        return this.getHeight()*this.getWidth()*this.getLength();
    }
    
     public int surfaceArea(){
        return 2*((this.getHeight() * this.getWidth())+(this.getHeight() * this.getLength())+(this.getWidth() * this.getLength())) ;

    }
 }    ]

 

 

SECOND PART TO TEST THE CLASS

[import java.util.Scanner;
public class Driver{
    public static void main(String[]args) {
        
        Scanner scan = new Scanner(System.in);
        
        Box boxy1 = new Box ();
        
        System.out.println("boxy1- length: "+ boxy1.getLength() + 
        " height: " +  boxy1.getHeight() + " width: " + boxy1.getWidth());
        
        boxy1.resize(3);
        int volumeBoxy1 = boxy1.volume();
        int surfaceAreaBoxy1 = boxy1.surfaceArea();
        
        System.out.println("boxy1- volume: " + volumeBoxy1 +
        " surface area: " + surfaceAreaBoxy1);
        
        System.out.println("Please enter 3 positive numbers for length, height and width");
        int length = scan.nextInt();
        int height = scan.nextInt();
        int width = scan.nextInt();
        
        Box boxy2 = new Box (length, height, width); 
        
        int volumeBoxy2 = boxy2.volume();
        int surfaceAreaBoxy2 = boxy2.surfaceArea();
        
        System.out.println("boxy2- volume: " + volumeBoxy2 +
        " surface area: " + surfaceAreaBoxy2);
        
        boxy2.resizeToHeight();
        int volumeBoxy2AferChange = boxy2.volume();
        int surfaceAreaBoxy2AferChange = boxy2.surfaceArea();
        
        System.out.println("boxy2- length: "+ boxy2.getLength() + 
        " height: " +  boxy2.getHeight() + " width: " + boxy2.getWidth() +
        " volume: " + volumeBoxy2AferChange + " surface area: " +
        surfaceAreaBoxy2AferChange);
        
    }
}
        ]

 

CPU - AMD 5800XMotherboard - ROG STRIX B550-E GAMING , Memory  - G.SKILL TridentZ Series 16GB (2 x 8GB) 288-Pin DDR4 SDRAM DDR4 3600 ,

GPU - NVIDIA GeForce RTX 3080 Ti MSI SUPRIM X 12G,  Case - 4000D AIRFLOW Tempered Glass Mid - Tower ATX Case - Black ,

Storage - Samsung 970 EvoPlus 500GB - Samsung 870 EVO 1TB + 6TB HDD,

PSU - Corsair HX1000 , Display -  ASUS TUF Gaming VG27A 165HZ + Dell 24 UltraSharp Monitor , Cooling - Noctua NH-D15 Black , 

Keyboard - Razer Stalker , Mouse - Logitec G502 Wireless , Operating System - Win 10 Pro , 

Sound - Logitech Z906 5.1 THX Surround Sound Speaker System

Link to post
Share on other sites

this is basically the name of the object who's values is associated with it. If you want a more detailed explanation I'll try, just I should be sleeping now. :P In terms of width, length and height, you are getting the values from "this". So which value of width, length or height you want? You want the ones from "this". Sorry if I didn't use proper terms. 

Main Gaming and Streaming PC: http://pcpartpicker.com/user/Vinsinity/saved/TjwVnQ

Ultrabook and College Laptop:

Spoiler

XPS 13 9350:

i5-6200U

8GB RAM

Samsung PM951 250GB M.2 Solid State Drive

Workstation Laptop:

Spoiler

Sager NP8672 (P670SG):

i7-4720HQ

32GB (4 x 8GB) CORSAIR Vengeance Performance

Samsung 850 EVO 250GB M.2 Solid State Drive (Boot Drive)

Samsung 950 PRO 512GB M.2 Solid State Drive (Video Drive)

Crucial MX100 250GB 2.5" Solid State Drive (Secondary SDD Storage)

Western Digital (Blue or Black) 1TB 2.5" 7200RPM Internal Hard Drive (Storage Drive)

GeForce GTX 980M 4G

 

Link to post
Share on other sites

1 minute ago, VinsinityKT said:

this is basically the name of the object who's values is associated with it. If you want a more detailed explanation I'll try, just I should be sleeping now. :P In terms of width, length and height, you are getting the values from "this". So which value of width, length or height you want? You want the ones from "this". Sorry if I didn't use proper terms. 

Thx for your time first of all, sec. , i am not sure, i mean, we were told to multiply the size of the cube sides by 3 , as you can see in driver, but i donr really understand why , the sides were took from this.getHeight and so , i mean, there is no value from were it was taken.  

CPU - AMD 5800XMotherboard - ROG STRIX B550-E GAMING , Memory  - G.SKILL TridentZ Series 16GB (2 x 8GB) 288-Pin DDR4 SDRAM DDR4 3600 ,

GPU - NVIDIA GeForce RTX 3080 Ti MSI SUPRIM X 12G,  Case - 4000D AIRFLOW Tempered Glass Mid - Tower ATX Case - Black ,

Storage - Samsung 970 EvoPlus 500GB - Samsung 870 EVO 1TB + 6TB HDD,

PSU - Corsair HX1000 , Display -  ASUS TUF Gaming VG27A 165HZ + Dell 24 UltraSharp Monitor , Cooling - Noctua NH-D15 Black , 

Keyboard - Razer Stalker , Mouse - Logitec G502 Wireless , Operating System - Win 10 Pro , 

Sound - Logitech Z906 5.1 THX Surround Sound Speaker System

Link to post
Share on other sites

The this keyword is for accessing members of the object in which they belong to. In your case it would be an instance of the box class.

 

I don't know Java but I just prefer to prefix my variables with m_ for member as writing this-> (or this. in java's case) just gets annoying and causes clutter IMO.

 

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to post
Share on other sites

1 minute ago, trag1c said:

The this keyword is for accessing members of the object in which they belong to. In your case it would be an instance of the box class.

 

I don't know Java but I just prefer to prefix my variables with m_ for member as writing this-> (or this. in java's case) just gets annoying and causes clutter IMO.

 

We mast use THIS in this one 

CPU - AMD 5800XMotherboard - ROG STRIX B550-E GAMING , Memory  - G.SKILL TridentZ Series 16GB (2 x 8GB) 288-Pin DDR4 SDRAM DDR4 3600 ,

GPU - NVIDIA GeForce RTX 3080 Ti MSI SUPRIM X 12G,  Case - 4000D AIRFLOW Tempered Glass Mid - Tower ATX Case - Black ,

Storage - Samsung 970 EvoPlus 500GB - Samsung 870 EVO 1TB + 6TB HDD,

PSU - Corsair HX1000 , Display -  ASUS TUF Gaming VG27A 165HZ + Dell 24 UltraSharp Monitor , Cooling - Noctua NH-D15 Black , 

Keyboard - Razer Stalker , Mouse - Logitec G502 Wireless , Operating System - Win 10 Pro , 

Sound - Logitech Z906 5.1 THX Surround Sound Speaker System

Link to post
Share on other sites

Okay lets make an example of a classroom. Say the class (Java Class) is the classroom A1 and you are a property in the classroom. Say i declare you as " public string student = 'keavlar ' " . So while we are inside this class A1, i can refer to where you are as " this " classroom. Since we all know where " this " is, so in programmatic sense it would be " this.student ". Whereas if i was in a classroom, say C5 and someone asks me where you are, i can't say " this " classroom since you, the property of class A1 is not in C5. I would have to tell them where you are exactly so i would say " He is in classroom A1 ". Which in programmatic terms you say, " Classroom ClassA1 = new Classroom(); " then saying, " ClassA1.student ".

 

I hope this helps. Let me know if you were confused or need help understanding it xD

Intel i7 4790 3.6GHzGigabyte GTX 970 4GB WF3  | Thermaltake Chaser A31 | Z97 Guard Pro

 

Gaming | Anime | Programming 

 

Link to post
Share on other sites

24 minutes ago, Sketched360 said:

Okay lets make an example of a classroom. Say the class (Java Class) is the classroom A1 and you are a property in the classroom. Say i declare you as " public string student = 'keavlar ' " . So while we are inside this class A1, i can refer to where you are as " this " classroom. Since we all know where " this " is, so in programmatic sense it would be " this.student ". Whereas if i was in a classroom, say C5 and someone asks me where you are, i can't say " this " classroom since you, the property of class A1 is not in C5. I would have to tell them where you are exactly so i would say " He is in classroom A1 ". Which in programmatic terms you say, " Classroom ClassA1 = new Classroom(); " then saying, " ClassA1.student ".

 

I hope this helps. Let me know if you were confused or need help understanding it xD

not sure, you get what i dont understand , 

in  public void resize (int factor)

_width = this.getWidth()*factor;

 

so meaning it will take teh value from 

public getWidth()

return _width ; 

 

and there is no value here. 

 

I want to understand the part of why this,getWidth .  

 

 

 

CPU - AMD 5800XMotherboard - ROG STRIX B550-E GAMING , Memory  - G.SKILL TridentZ Series 16GB (2 x 8GB) 288-Pin DDR4 SDRAM DDR4 3600 ,

GPU - NVIDIA GeForce RTX 3080 Ti MSI SUPRIM X 12G,  Case - 4000D AIRFLOW Tempered Glass Mid - Tower ATX Case - Black ,

Storage - Samsung 970 EvoPlus 500GB - Samsung 870 EVO 1TB + 6TB HDD,

PSU - Corsair HX1000 , Display -  ASUS TUF Gaming VG27A 165HZ + Dell 24 UltraSharp Monitor , Cooling - Noctua NH-D15 Black , 

Keyboard - Razer Stalker , Mouse - Logitec G502 Wireless , Operating System - Win 10 Pro , 

Sound - Logitech Z906 5.1 THX Surround Sound Speaker System

Link to post
Share on other sites

5 minutes ago, keavlar said:

not sure, you get what i dont understand , 

in  public void resize (int factor)

_width = this.getWidth()*factor;

 

so meaning it will take teh value from 

public getWidth()

return _width ; 

 

and there is no value here. 

 

I want to understand the part of why this,getWidth .  

 

 

 

 

Okay, so yes this.getWidth() will take the value of 

public getWidth()

return _width ; 

 

I can't really help if i'm not sure what to help with o.O 

So do you need to know the reason why the " this.getWidth() " is being used??

or

is it that it does not have a value?

Intel i7 4790 3.6GHzGigabyte GTX 970 4GB WF3  | Thermaltake Chaser A31 | Z97 Guard Pro

 

Gaming | Anime | Programming 

 

Link to post
Share on other sites

  • 4 weeks later...

"this" is a keyword in Java. It simply means the current object. You might want to use standard naming convention for variables which is camelCaseNotation (this is just me nitpicking)

CPU: Intel Core i5-6600k 4.4GHz | Motherboard: Asus ROG STRIX Z270F Gaming | Cooler: Cryorig H7 | RAM: GSkill Ripjaws V 8GB 2x4 3200 MHz | GPU: MSI GTX 1070 Gaming X | PSU: Seasonic G-550w 80+ Gold Certified, Semi Modular | Storage: 250GB Samsung 850 EVO, 1TB Western Digital Caviar Blue | Case: NZXT S340 Elite (Black/Red) | Monitor: BenQ XL2411 144hz | Keyboard: Corsair STRAFE RGB Cherry MX Silent | Mouse: Corsair M65 Pro RGB

 

I'd like to make a Chemistry joke, but all the good ones ARGON. *nudgenudge *winkwink

Link to post
Share on other sites

So... You are gonna use the method like this:

 

myBox.resize(3);

 

it will take your box height length and height, multiply it by 3 and assign the new values to the variables

 

the this.getWidth() is the method getWidth()called with the object you called at the beginning, in this case, myBox. That will return the value of the width of the object myBox.

 

hope I got what you were asking for, if is not the case, try to explain better what s the part you don't get...

Intel i7 6700k @4.6 1.330v  cooled with H110i GT // 16Gb Kingston Fury DDR4 ram @2133MHz // Rx 480 Nitro+ // 2TB + 1 TB Hdd  // 250 GbSSD // on an Asus Z170-A // powered with Corsair RM750i // all  inside a Corsair 600C

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×