Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android Development for Beginners : Cola Wars Example
public class MainActivity extends AppCompatActivity {
...
public void voteUdacicola(View view) {
int udacicolaVotes = 0;
udacicolaVotes = udacicolaVotes + 1;
}
public void votePepcity(View view) {
int pepcityVotes;
pepcityVotes = pepcityVotes + 1;
}
public void showMeVotes(View view) {
display(udacicolaVotes + " vs. " + pepcityVotes);
}
...
}
@shinabarger

This comment has been minimized.

Copy link

commented Aug 5, 2016

Nice. Thanks!

@jandri78

This comment has been minimized.

Copy link

commented Nov 11, 2016

thank you

@bulletforyou

This comment has been minimized.

Copy link

commented Dec 25, 2016

Variable 'pepcityVotes' might not have been initialized

@Kukkulum

This comment has been minimized.

Copy link

commented Feb 20, 2017

I didn't understand the difference between local and global variables before this (Udacity) course. Thanks.

@evoxtorm

This comment has been minimized.

Copy link

commented Feb 27, 2017

@Kukkulum
A global variable is initialized at the starting of the code it is global means other methods can be used this variable but local variable is initialized inside a method and there local members can used it not all the methods.

@vmasullo

This comment has been minimized.

Copy link

commented Mar 29, 2017

thanks

@nnannos

This comment has been minimized.

Copy link

commented Apr 9, 2017

It is local because declared only inside the voteUdacicola() method, to be global should have been declared at the begiming of MainActivity().
But it should have been Global , because udacicolaVotes variable is used in the showMeVotes() method and if it is not global variable is destroyed.

@oluwaseunmusa

This comment has been minimized.

Copy link

commented May 20, 2017

WOW COol

@tbanj

This comment has been minimized.

Copy link

commented May 22, 2017

am enjoying this course

@Mahmoudtarek59

This comment has been minimized.

Copy link

commented Aug 17, 2017

public class MainActivity extends AppCompatActivity {
...
int udacicolaVotes = 0;
int pepcityVotes = 0;

public void voteUdacicola(View view) {
           udacicolaVotes = udacicolaVotes + 1;
}

public void votePepcity(View view) {
           pepcityVotes = pepcityVotes + 1;
}

public void showMeVotes(View view) {
    display(udacicolaVotes + " vs. " + pepcityVotes);
}
...

}

@lixrg

This comment has been minimized.

Copy link

commented Nov 14, 2017

if someone is still looking for a good explanation of local vs. global variables, I really liked this short, easy to understand explanation https://www.codecademy.com/en/forum_questions/514900b642e721e65d0003f1

@DeusXmac

This comment has been minimized.

Copy link

commented Jan 6, 2018

Both of variables are local and both of them must be global for reachable from anypoint.

@mathsways

This comment has been minimized.

Copy link

commented Feb 1, 2018

good job thank you

@Nogaeman

This comment has been minimized.

Copy link

commented Feb 3, 2018

This Code have some error
1- int pepcityVotes; not set initial value of variable name
2- udacicolaVotes & pepcityVotes are local variable not Global variable so make error

The Correct Code Is

int udacicolaVotes = 0, pepcityVotes = 0;

public void voteUdacicola(View view) {
    udacicolaVotes = udacicolaVotes + 1;
}

public void votePepcity(View view) {
    pepcityVotes = pepcityVotes + 1;
}

public void showMeVotes(View view) {
    display(udacicolaVotes + " vs. " + pepcityVotes);
}
@burobot2016

This comment has been minimized.

Copy link

commented Feb 11, 2018

globbal is in the begining
local is in lines

@abdahma01

This comment has been minimized.

Copy link

commented Feb 13, 2018

Errors:

  • local variables out of scope.
  • local variables cannot be used as counters.
  • one of the local variables is not initialized.
@oddmentiusmaximus

This comment has been minimized.

Copy link

commented Feb 20, 2018

thanks

@first-hero

This comment has been minimized.

Copy link

commented Feb 24, 2018

both variables are local and they should be global for all methods to know about them

@louayeldin

This comment has been minimized.

Copy link

commented Mar 16, 2018

Udacicola and Pepcity should be both global to be used in showMeVotes() method. Cool example

@ahmedalfalahi

This comment has been minimized.

Copy link

commented Mar 30, 2018

thanks

@Erikcht

This comment has been minimized.

Copy link

commented May 2, 2018

soo... but how will become this code on
MainActivity.java and how will become the acitivityMain.xml with this ?

@Mohhmd

This comment has been minimized.

Copy link

commented May 7, 2018

it gives me error

@SaeedAlbaradie

This comment has been minimized.

Copy link

commented May 13, 2018

should be global Variable so other method can use it . As example here ShowMeVotes , otherwise it will give us error.

@mohammed74

This comment has been minimized.

Copy link

commented May 16, 2018

im need the xml code of voting ...

@OluwatobilobaLight

This comment has been minimized.

Copy link

commented May 17, 2018

@mohammed74 you can use mine below but you need to change android:src to your own image
`

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<TextView
    android:id="@+id/display_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="WHICH DO YOU PREFER?"
    android:textColor="#000"
    android:shadowColor="#3e0909"
    android:layout_marginBottom="30dp"
    android:textSize="45sp" />

    </LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="3dp"
    android:background="#000"/>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="35dp">

    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/cocacola"
        android:layout_alignParentLeft="true"
        android:src="@drawable/cola"
        android:layout_marginBottom="20dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/cocacola"
        android:gravity="center"
        android:text="Vote"
        android:layout_alignParentLeft="true"
        android:background="#b71f1f"
        android:onClick="voteUdacicola"
        android:textColor="#fff"
        android:layout_marginLeft="35dp"/>

    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:src="@drawable/pepsi"
        android:id="@+id/pepsi"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="20dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/pepsi"
        android:gravity="center"
        android:text="Vote"
        android:layout_alignParentRight="true"
        android:background="#285fbf"
        android:textColor="#fff"
        android:onClick="votePepcity"
        android:layout_marginRight="30dp"/>

</RelativeLayout>

`

image

@tooptooptoop

This comment has been minimized.

Copy link

commented Jun 2, 2018

no

@rdgmh777

This comment has been minimized.

Copy link

commented Jun 2, 2018

@Haapiiiii

This comment has been minimized.

Copy link

commented Jun 8, 2018

very nice .. thanx

@find1010

This comment has been minimized.

Copy link

commented Jun 18, 2018

thank you

@mhd73383

This comment has been minimized.

Copy link

commented Jul 3, 2018

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp">

    <TextView
        android:id="@+id/display_text_view1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="0"
        android:textSize="45sp" />

    <TextView
        android:id="@+id/display_text_view2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".55"
        android:gravity="center"
        android:text="vs"
        android:textSize="45sp" />

    <TextView
        android:id="@+id/display_text_view3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="0"
        android:textSize="45sp" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp">

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="votePepcity"
        android:text="pepcity" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="voteUdacicola"
        android:text="udacicola" />
</LinearLayout>
@mhd73383

This comment has been minimized.

Copy link

commented Jul 3, 2018

public class MainActivity extends AppCompatActivity {
int pepcityVotes = 0;
int udacicolaVotes = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void votePepcity(View view) {
    pepcityVotes = pepcityVotes + 1;
    display(pepcityVotes);
    return;
}

public void voteUdacicola(View view) {
    udacicolaVotes = udacicolaVotes + 1;
    display1(udacicolaVotes);
    return;
}

public void display(int p) {
    TextView t1 = (TextView) findViewById(R.id.display_text_view1);
    t1.setText("" + p);
}

public void display1(int c) {
    TextView t2 = (TextView) findViewById(R.id.display_text_view3);
    t2.setText("" + c);
}

}

@mhd73383

This comment has been minimized.

Copy link

commented Jul 3, 2018

the parent layout is linearLayout vertical

@Dharmesh40

This comment has been minimized.

Copy link

commented Sep 2, 2018

can somebody please provide full code of xml and java for these examples............... i am getting very confused in this lesson 3

@waelfareed

This comment has been minimized.

Copy link

commented Nov 17, 2018

Both of variables are local and both of them must be global for reachable from anypoint.

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.