Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android Development for Beginners : Define a Method
/**
* Get the email account name.
*
* @return the name of the account.
*/
private String getAccountName() {
return "android@gmail.com";
return "droid@gmail.com";
}
/**
* Add $4 of tip onto the current bill.
*
* @return the total price of the bill (including tip).
*/
private addTip(int bill) {
return bill + 4;
}
/**
* Sets up the app for the current city.
*/
private nothing setup() {
cityName = "London";
}
/**
* Get the number of students in a class.
*
* @return the number of students.
*/
private int getStudentClassSize() {
return "20";
}
@tonymayo

This comment has been minimized.

Copy link

tonymayo commented Jul 10, 2015

I dont agree. "nothing" could be the type of an object made by us.

@ramonMontanhes

This comment has been minimized.

Copy link

ramonMontanhes commented Jul 12, 2015

You don't agree, ok. But put in your head that this course is made for beginners. Not everyone knows that we can create object types.

@ghost

This comment has been minimized.

Copy link

ghost commented Jul 26, 2015

-nothing- could be a return type only if -nothing- is a class, but a class in java should have the first letter capitalized like -Nothing-. if we had -Nothing- in place of -nothing- then it will be correct and the method will return an object of the class Nothing.

@ghost

This comment has been minimized.

Copy link

ghost commented Jul 26, 2015

-nothing- could be a return type only if -nothing- is a class and cityName = "London" is an object of this class, but a class in java should have the first letter capitalized like -Nothing-.
If we had -Nothing- in place of -nothing- and the method return an object of the class Nothing and not cityName = "London" then it will be correct.
example:
private Nothing setup() {
return new Nothing();
}
In this case Nothing class should have a constructor of type Nothing() like:
public class Nothing {

 public Nothing(){
                ......
                }

}

@M7ammad-eb

This comment has been minimized.

Copy link

M7ammad-eb commented Aug 7, 2015

well actually @tonymayo 's answer let me know new things by the explanation by @Hommani .. so wondering is always healthy, and explaining is healthier.. thanks all of you guys 👍 😃

@ar-android

This comment has been minimized.

Copy link

ar-android commented Aug 23, 2015

nice

@martipello

This comment has been minimized.

Copy link

martipello commented Sep 16, 2015

Cannot view line number on mobile

@Ajimi

This comment has been minimized.

Copy link

Ajimi commented Dec 2, 2015

On method 3 there is two mistakes the void and the cityName there's no dataType before(except she was declared before) but in our example are not

@Auwalms

This comment has been minimized.

Copy link

Auwalms commented Jan 17, 2016

Method 1: LN8, Method 2: LN6, Method 3: LN4, Method 4: LN7.

@postace

This comment has been minimized.

Copy link

postace commented Jan 19, 2016

@homani, in this example, we need to find the line contain error.
If you persistent nothing could be a return data type, so it's no line contain error huh?
It's just missing return data value, like : return something;

@osamacs

This comment has been minimized.

Copy link

osamacs commented Apr 17, 2016

8 - can't return two values.
6 - forget return data type.
4 - nothing is not return data type.
7 - "20" not correct return data type,must be integer and this string.

@NazirHakim

This comment has been minimized.

Copy link

NazirHakim commented Aug 3, 2016

Method 1) : 8_ there is extra return line
Method 2) : 6_need data type then method name, data type is missing
Method 3) : 4_need data type String to get return value
Method 4) : 7_must be int or string , not proper data type

@browndm31

This comment has been minimized.

Copy link

browndm31 commented Aug 13, 2016

I agree that a new object could be called "nothing", which would be usable if it was in the code example. However, in their example code it is not declared anywhere as an object declared as "nothing". So in this code example "nothing" doesn't exist, which would fail as an exception.

@MundakaZgz

This comment has been minimized.

Copy link

MundakaZgz commented Aug 31, 2016

@tonymayo It Noting is a class defined by us... where is the import statement?

@dgutie10

This comment has been minimized.

Copy link

dgutie10 commented Sep 9, 2016

@Ajimi this are just snips of part of a java program. This does not necessarily mean that this is the whole code.

@dgutie10

This comment has been minimized.

Copy link

dgutie10 commented Sep 9, 2016

@homani, @postace is correct. Even if the proper case for the data type is not present when declaring the Nothing object. There is a missing return statement where the "nothing" object is return to the caller.

@AHMADanis

This comment has been minimized.

Copy link

AHMADanis commented Jan 28, 2017

First Method
8. There is two return statements, program will never run ahead after occurring of first return statement.
Second Method
6. Method's return data type is missing
Third Method
4. "nothing" is invalid return data type
Forth Method
7. "20" is invalid return type. a mothod only return that what we declare in method signature.

@zutamacka

This comment has been minimized.

Copy link

zutamacka commented Jan 29, 2017

@Ajimi - "On method 3 there is two mistakes the void and the cityName there's no dataType before(except she was declared before) but in our example are not"

Not really. You must take in account the scope of the variables. cityName in this context is - has to be - a global variable which gets modified by Method 3 to contain the value "London" Now how do I just know this: even if I haven't seen it written anywhere in the actual gist? I know, because this method has no return value, meaning it just has to be used to modify a global value otherwise it's whole existence would make no sense.

In other words, if you wrote the method like thus:
private void setup() {
String cityName = "London";
}
the method would just modify the local variable cityName which would get destroyed upon the ending of the method execution. Thiswould have no impact on the program at all, and the existence of the method would make no sense at all. So just by looking at the original method, I know cityName must be declared somewhere as a global variable.

If this method was meant to be used with cityName NOT being a global variable, it would have been written like this:
private String setup() {
String cityName = "London";
return cityName;
}
You see in THIS version, the method returns a value which allows this value to be picked up by something later in the code (ie: globalCityName = setup(); ).

@jcunanan05

This comment has been minimized.

Copy link

jcunanan05 commented Mar 6, 2017

thanks

@ganobii

This comment has been minimized.

Copy link

ganobii commented Sep 13, 2017

Method 1: There is an issue in line 8 due to having two return values.
Method 2: There is an issue in line 6 since there is no return data type.
Method 3: There is an issue in line 4 since the return data type cannot be 'nothing' in this case.
Method 4: There is an issue in line 7 since the return data type is int but a string "20" is being returned.

@Soundharyaam

This comment has been minimized.

Copy link

Soundharyaam commented Sep 18, 2017

Method 1: (Line Number:8)Cannot have two return statement
Method 2: (Line Number:6)No return type is given
Method 3: (Line Number:4)No return type is given
Method 4: (Line Number:7)function should return int instead of string "20"

@susanwere

This comment has been minimized.

Copy link

susanwere commented Dec 8, 2017

Method 1: (Line Number:8) The last return statement will not be reached
Method 2: (Line Number:6) The return data type is missing
Method 3: (Line Number:4) nothing is not a valid return data type
Method 4: (Line Number:7) "20" is not an integer. It's a string. function should return an integer.

@inv-Ayiba

This comment has been minimized.

Copy link

inv-Ayiba commented Jan 19, 2018

errors!!!!!!!!!!!!!!!!!!!!!!!

@Maxwe1

This comment has been minimized.

Copy link

Maxwe1 commented Feb 5, 2018

Method 1: (Line Number:8)Cannot have two return statement
Method 2: (Line Number:6)No return type is given
Method 3: (Line Number:4)No return type is given
Method 4: (Line Number:7)function should return int instead of string "20"

@mattlarry

This comment has been minimized.

Copy link

mattlarry commented Feb 6, 2018

@Maxwe1 - you are correct!

@Nogaeman

This comment has been minimized.

Copy link

Nogaeman commented Feb 10, 2018

Method 1 >> Error Line 8 return "droid@gmail.com";
The Correct
private String getAccountName() {
return "android@gmail.com";
}

Method 2 >> Error Line 6 private addTip(int bill) {
The Correct
private int addTip(int bill) {
return bill + 4;
}

Method 3 >> Error Line 4 private nothing setup() {
The Correct
private string setup() {
cityName = "London";
}

Method 4 >> Error Line 7 return "20";
The Correct
private int getStudentClassSize() {
return 20;
}

@Bendzi98

This comment has been minimized.

Copy link

Bendzi98 commented Feb 13, 2018

hey guys..would anybody be so kind and paste me the whole code because apparently I didn't save it.

@abdahma01

This comment has been minimized.

Copy link

abdahma01 commented Feb 16, 2018

Method 1 Ln 8 unreachable code after the first return statement.
Method 2 Ln 6 The data type of the return value must match the method's declared return type. It should be int
Method 3 Ln 4 There is not a method's declared return type of "nothing". It should be void.
Method 4 Ln 7 The data type of the return value must match the method's declared return type.20 should be without quotes

@shirimaG

This comment has been minimized.

Copy link

shirimaG commented Feb 25, 2018

you are correct @abdahma01

@badrddinb

This comment has been minimized.

Copy link

badrddinb commented Feb 26, 2018

DONE

@sherifhisham

This comment has been minimized.

Copy link

sherifhisham commented Mar 4, 2018

DONE . thank god !

@vyruss-zz

This comment has been minimized.

Copy link

vyruss-zz commented Mar 5, 2018

Please stop putting the solution in the comments.

@AbdelrhmanMHsn

This comment has been minimized.

Copy link

AbdelrhmanMHsn commented Mar 13, 2018

Method1------>this is correct
private String getAccountName() {
return "android@gmail.com";
}

Method2------>this is correct
private int addTip(int bill) {
return bill + 4;
}

Method3------>this is correct
private nothing setup() {
return cityName = "London";
}

Method4------>this is correct
private int getStudentClassSize() {
return 20;
}

@louayeldin

This comment has been minimized.

Copy link

louayeldin commented Mar 18, 2018

#1 Method: line 8: extra return statement line at the end of the method code.
#2 Method: line 6: Missing datatype of the method itself to be returned.
#3 Method: line 4: The method just set the value to cityName and doesn't return a value or we should define a data type for the method to be able to return something such as String value.
#4 Method: line 7: int not a proper data type used in the method to return a value of type String.

@enpaca

This comment has been minimized.

Copy link

enpaca commented May 10, 2018

Method #3 does not include a return statement in its definition. Keeping in mind that except void methods, any other method should have a return statement, setup() should return a reference to a nothing object.

@shoooka

This comment has been minimized.

Copy link

shoooka commented May 11, 2018

8 - can't return two values.
6 - forget return data type.
4 - nothing is not return data type.
7 - "20" not correct return data type,must be integer and this string.

@megaunknown

This comment has been minimized.

Copy link

megaunknown commented May 20, 2018

why we comment here ??

@Eduese

This comment has been minimized.

Copy link

Eduese commented May 21, 2018

Are we suppose to provide answers here?

I don't think so guys

@NtaoMoloi

This comment has been minimized.

Copy link

NtaoMoloi commented May 30, 2018

@Eduese I don't think so too.

@MijeLafe

This comment has been minimized.

Copy link

MijeLafe commented Jun 8, 2018

Done

@OmarAtia

This comment has been minimized.

Copy link

OmarAtia commented Jun 11, 2018

thx

@Mostafa1A

This comment has been minimized.

Copy link

Mostafa1A commented Jun 12, 2018

can't return two values

@Fury4hwxc

This comment has been minimized.

Copy link

Fury4hwxc commented Jun 16, 2018

Method 1) : 8_ there is extra return line
Method 2) : 6_need data type then method name, data type is missing
Method 3) : 4_need data type String to get return value
Method 4) : 7_must be int or string , not proper data type

@ahmedmtaher

This comment has been minimized.

Copy link

ahmedmtaher commented Jun 19, 2018

done

@raDIUM2010

This comment has been minimized.

Copy link

raDIUM2010 commented Jun 21, 2018

8,6,4,7

@johninit

This comment has been minimized.

Copy link

johninit commented Jul 21, 2018

I agree Nogaeman keep it like that lad

@emanhamad

This comment has been minimized.

Copy link

emanhamad commented Sep 7, 2018

Method (1) : 8 there is extra return line
Method (2) : 6 data type is missing
Method(3) : 4 need data type String to return value
Method (4) : 7 not proper data type

@Hanaa94

This comment has been minimized.

Copy link

Hanaa94 commented Sep 26, 2018

8,6,4,7

@djalilo24

This comment has been minimized.

Copy link

djalilo24 commented Mar 25, 2019

methode1------>line8-Return 2 values
methode2------>line6-return data type
methode3------>line4-return data type and methode name
methode4------>line7-" 10"return type int not string

@AhmedMohammed11

This comment has been minimized.

Copy link

AhmedMohammed11 commented Apr 5, 2019

Method 1: LN8,
Method 2: LN6,
Method 3: LN4,
Method 4: LN7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.