What happened to OC? - CLOSED Carnage?!
Sign in to follow this  
Followers 0
Daywu

Need Help in Java

I am learning some java on this site, and it gives me exercises to work on as I learn. but I'm stuck on this one and I can't figure it out. It's about array positioning.

 

http://www.codecademy.com/courses/javascript-beginner-en-NhsaT/2/2?curriculum_id=506324b3a7dffd00020bf661#

 

The answer I put was:

 

var junkData = ["Eddie Murphy", 49, "peanuts", 31];
junkData[3]
console.log(31)
 
it keeps giving me an error. someone please help me figure it out.

Image result for kimi no na gif

Share this post


Link to post
Share on other sites

Tiddy-bits:

First off, this is Javascript, not Java. Though their names are similar, the languages are much different.

Your syntax is correct, and I copied and pasted what you posted and it said it was correct as well.

Here's my answer:

var junkData = ["Eddie Murphy", 49, "peanuts", 31]
console.log(junkData[3])
Edited by 002
Daywu likes this

Share this post


Link to post
Share on other sites

thanks, that worked. i also tried that answer before the other code i posted above ur post and that didn't work before. but now it did so im guessing something was wrong with the website at that time.

Edited by Daywu

Image result for kimi no na gif

Share this post


Link to post
Share on other sites

I have been taking a Java class in my college and there is something that I am little confused on this code. I was just wondering what does the .this referring to in this code.

/**
 * @(#)BirdSighting.java
 *
 *
 * @author 
 * @version 1.00 2015/2/10
 */
 
 
public class BirdSighting {
String birdType;
int number;
int day;
 
    public BirdSighting() 
    {
     birdType="Robin";
     number=15;
     day=1;
    }
    
    public BirdSighting(String b, int n, int d)
    {
     birdType=b;
     number=n;
     day=d;
    }
    
    public void setBirdType(String birdType)
    {
     this.birdType=birdType;
    }
    
    public String getBirdType()
    {
     return birdType;
    }
    
    public void setNumber(int number)
    {
     this.number=number;
    }
    
    public int getNumber()
    {
     return number;
    }   
     
    public void setDay(int day)
    {
    this.day=day;
    }
    
    public int getDay()
    {
    return day;
    }
    
    public void display()
    {
     System.out.println("Bird Type: " + birdType);
     System.out.println("Number of Birds: " + 15);
     System.out.println("Day: " + 1);
     System.out.println("");
     
    }
    
    
}

Image result for kimi no na gif

Share this post


Link to post
Share on other sites

I have been taking a Java class in my college and there is something that I am little confused on this code. I was just wondering what does the .this referring to in this code.

this refers to the current object. It literally means "this". So, it would be referring to the BirdSighting object. Edited by 002

Share this post


Link to post
Share on other sites

To give some clarification and hopefully not confuse:

 

What really happens behind the scenes,

public void setBirdType(String birdType)
{
    ...
}

turns into something like..

public void BirdSighting_setBirdType(BirdSighting this, String birdType)
{
    ...
}

and

BirdSighting birdSighting = new BirdSighting();
birdSighting.setBirdType("falcon");

turns into something like...

BirdSighting birdSighting = new BirdSighting();
BirdSighting_setBirdType(birdSighting, "falcon");

Remember, you can create multiple BirdSighting's. Hopefully this helps..

 

[edit]

And what may be puzzling you more perhaps, is that in get* methods, you leave out 'this'. But you are allowed to do public int getDay() { return this.day; }. If you just type "day", Java will automatically turn that into "this.day". You shouldn't omit "this." in your set* methods though, because the parameters to the function passed in have the same name -- and it's a little confusing to tell a local variable and instance variable apart without specifying 'this'.

Edited by nil
WaeV and Takka like this

Share this post


Link to post
Share on other sites

idk what u mean. Is "this.birdType" referring to the global variable "birdType"? and it's making it equal to the local variable in the method setBirdType?

Edited by Daywu

Image result for kimi no na gif

Share this post


Link to post
Share on other sites

idk what u mean. Is "this.birdType" referring to the global variable "birdType"? and it's making it equal to the local variable in the method setBirdType?

 

Close. this.birdType refers to the birdType member of the object; it's called an instance variable; a member part of the object. All the methods in your class have "this" implicitly passed in as the first parameter. If you do obj.setBirdType("falcon"), obj is what would get passed as "this". It's not technically global because "this" is implicitly being passed into the function. Java just has a shorthand in some scenarios which doesn't make you have to type this in cases like "this.birdType" or "this.getNumber()", which can get kind of confusing.

Edited by nil

Share this post


Link to post
Share on other sites
Sign in to follow this  
Followers 0
  • Recently Browsing   0 members

    No registered users viewing this page.