Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Last active May 11, 2022 19:51
Show Gist options
  • Star 46 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
  • Save udacityandroid/6c8a127ff0a3c29721af to your computer and use it in GitHub Desktop.
Save udacityandroid/6c8a127ff0a3c29721af to your computer and use it in GitHub Desktop.
Android Development for Beginners : Practice Set 2 Sleep Debt Example
int weekday = 5;
int weekend = 9;
int optimalHours = 7 * 8;
int actualHours = weekday;
actualHours = actualHours + weekend * 2;
int solution = optimalHours - actualHours;
display(solution);
@iven86
Copy link

iven86 commented May 25, 2018

package com.iven.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

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


    int weekday = 5;
    int weekend = 9;
    int optimalHours = 7 * 8;
    int actualHours = weekday;
    actualHours = actualHours + weekend * 2;
    int solution = optimalHours - actualHours;
    display(solution);

}

private void display(int solution) {
    Log.d(TAG,"Output :" +solution);


}

}

Done.
05-25 11:59:24.146 10779-10779/com.iven.test D/MainActivity: Output :33

@ghufran86
Copy link

the code must be

int actualHours = weekday*5 + weekend * 2;
int solution = optimalHours - actualHours;
display(solution);

@serdoune
Copy link

serdoune commented Jun 1, 2018

image

@tooptooptoop
Copy link

<TextView
    android:id="@+id/display_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="45sp" />
package com.example.android.practiceset2; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int weekday = 5;
    int weekend = 9;
    int optimalHours = 7 * 8;

    int actualHours = weekday;
    actualHours = actualHours + weekend * 2;
    int solution = optimalHours - actualHours;
    display(solution);

}

/**
 * Display methods that allow the text to appear on the screen. Don't worry if you don't know
 * how these work yet. We'll be covering them in lesson 3.
 */
public void display(int i) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText(""+ i);
}

}
screenshot_2018-06-02-06-00-45

@ZahraaElhaj
Copy link

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@OverRide
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int weekday = 5;
int weekend = 9;
int optimalHours = 7 * 8;

int actualHours = weekday;
actualHours = actualHours + weekend * 2;
int solution = optimalHours - actualHours;
display(solution);
// PASTE CODE YOU WANT TO TEST HERE

}

/**

  • Display methods that allow the text to appear on the screen. Don't worry if you don't know
  • how these work yet. We'll be covering them in lesson 3.
    */

public void display (int i) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText(""+i);}
}

And that's what my "activity_main.xml" looks like:

@mohammeddbadawy
Copy link

worked

Copy link

ghost commented Jun 12, 2018

Works

@joekaslow
Copy link

package example.com.android.practiseset2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int weekday = 5;
    int weekend = 9;
    int optimalHours = 7 * 8;

    int actualHours = weekday;
    actualHours = actualHours + weekend * 2;
    int solution = optimalHours - actualHours;
    display(solution);
}
public void display (int i) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText(""+i);
}

}

@find1010
Copy link

goooooood work

@ahmademad007
Copy link

can someone plz write the full correct xml code for this quiz

@KobiPeeri
Copy link

Dear Guys (and girls :))
In this course, they mentions "instructor Notes", Where can I find them?

@DynamOo
Copy link

DynamOo commented Jul 6, 2018

@KobiPeeri, you can find them below the video (circled by red color in the screenshot)...
instructornotes

@Manhans
Copy link

Manhans commented Aug 9, 2018

This code works only in the cellphone. Running in your cell. In the preview it isn't appear.

Não vai funcionar no preview. Rode no celular que irá funcionar,

@abdellaah
Copy link

import android.widget.TextView;
if any one like me encountered a problem with textview
add the line above
import android.widget.TextView;

@Stas0050
Copy link

Привет,

Я поместил весь код, кроме последней строки, то есть display(solution);вне onCreate()метода

public class MainActivity extends AppCompatActivity {

int weekday = 5;
int weekend = 9;
int optimalHours = 7 * 8;

int actualHours = weekday;
actualHours = actualHours + weekend * 2;
int solution = optimalHours - actualHours;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // PASTE CODE YOU WANT TO TEST HERE

    display(solution);

}

...
...
}

Не так ли?
Я получаю сообщение об ошибке: ошибка: ожидается
и когда я помещаю курсор в строку actualHoursили weekendиз строки. actualHours = actualHours + weekend * 2;Он говорит: Неизвестный класс: «actualHours» и «Неизвестный класс:« выходные »

Заранее спасибо!!!

start learning again

@shiva-karthick
Copy link

Done!

package com.example.android.practiceset2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // PASTE CODE YOU WANT TO TEST HERE
        int weekday = 5;
        int weekend = 9;
        int optimalHours = 7 * 8;

        int actualHours = weekday;
        actualHours = actualHours + weekend * 2;
        int solution = optimalHours - actualHours;
        display(solution);

    }

    /**
     * Display methods that allow the text to appear on the screen. Don't worry if you don't know
     * how these work yet. We'll be covering them in lesson 3.
     */

    public void display(String text) {
        TextView t = (TextView) findViewById(R.id.display_text_view);
        t.setText(text);
    }

    public void display(int text) {
        TextView t = (TextView) findViewById(R.id.display_text_view);
        t.setText(text + "");
    }

    public void display1(String text) {
        display(text);
    }

    public void display2(String text) {
        TextView t = (TextView) findViewById(R.id.display_text_view_2);
        t.setText(text);
    }

    public void display3(String text) {
        TextView t = (TextView) findViewById(R.id.display_text_view_3);
        t.setText(text);
    }
}

@xMagicXs
Copy link

xMagicXs commented Jul 2, 2019

i wrote them manually and lol i didnt place a dot between t and setText now that i rewrote that part is all good t.setText
(i need to be more carefull with the punctuations and parantheses) works perfect :)

@al130m
Copy link

al130m commented Sep 13, 2019

Getting the following code when I try to copy and paste the MainActivity.java code

error: package android.support.v7.app does not exist

package com.example.android.practiceset2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // PASTE CODE YOU WANT TO TEST HERE
    int weekday = 5;
    int weekend = 9;
    int optimalHours = 7 * 8;

    int actualHours = weekday;
    actualHours = actualHours + weekend * 2;
    int solution = optimalHours - actualHours;
    display(solution);

}

/**
 * Display methods that allow the text to appear on the screen. Don't worry if you don't know
 * how these work yet. We'll be covering them in lesson 3.
 */

public void display(String text) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText(text);
}

public void display(int text) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText(text + "");
}

public void display1(String text) {
    display(text);
}

public void display2(String text) {
    TextView t = (TextView) findViewById(R.id.display_text_view_2);
    t.setText(text);
}

public void display3(String text) {
    TextView t = (TextView) findViewById(R.id.display_text_view_3);
    t.setText(text);
}

}

@LaszloLajosT
Copy link

Getting the following code when I try to copy and paste the MainActivity.java code

error: package android.support.v7.app does not exist

Hi @al130m

You have the same errors like everybody else from the moment when you create a new project and copy paste everything :)

Delete this import line from your code:

import android.support.v7.app.AppCompatActivity;

Then Select "AppCombatActivity" from your MainActivity class:

public class MainActivity extends AppCompatActivity

Press ALT + ENTER. -. new small window shows up ->. select "import class".

That's it.
Basically, you will change the import code to the recent package.
AppCombatActivity error

@rajan3300
Copy link

package com.example.android.practiceset2;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // PASTE CODE YOU WANT TO TEST HERE
    int weekday = 5;
    int weekend = 9;
    int optimalHours = 7 * 8;

    int actualHours = weekday;
    actualHours = actualHours + weekend * 2;
    int solution = optimalHours - actualHours;
    display(solution);

}

/**
 * Display methods that allow the text to appear on the screen. Don't worry if you don't know
 * how these work yet. We'll be covering them in lesson 3.
 */

public void display(String text) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText(text);
}

public void display(int text) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText(text + "");
}

}

@rajan3300
Copy link

help

@singer0000
Copy link

singer0000 commented Feb 22, 2020

this works for me
package com.example.android.practiceset2;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

    int weekday = 5;
    int weekend = 9;
    int opimalHours = 7 * 8;

    int actualHours = (weekday * 5) + (weekend * 2);
    int solution = opimalHours - actualHours;
    display(solution);
}

public void display(int n) {
    TextView textView = (TextView) findViewById(R.id.display_text_view);
    textView.setText(Integer.toString(n));
}

}

and in ActivityMain XML

<TextView
    android:id="@+id/display_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textSize="45sp" />

@Ashishkumar-sys
Copy link

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int weekday = 5;
    int weekend = 9;
    int optimalHours = 7 * 8;
    int actualHours = weekday * 5 + weekend * 2;
    int solution = optimalHours - actualHours;
    display(solution);

}

public void display(int i) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText("" + i);
}

@yyssimon
Copy link

yyssimon commented Jun 1, 2020

My compiler fail to run successfully. There's this error:
error: method display(int) is already defined in class MainActivity
public void display(int i) {

image

@yyssimon
Copy link

yyssimon commented Jun 4, 2020

My compiler fail to run successfully. There's this error:
error: method display(int) is already defined in class MainActivity
public void display(int i) {

image

Anyone can help me on this please?

@Sugimoto-yohsi
Copy link

Sugimoto-yohsi commented Oct 30, 2020

@yyssimon
I think you have a display method in the MainActivity onCreate{}.
Try putting it outside {} of onCreate.

Example:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // PASTE CODE YOU WANT TO TEST HERE
    int weekday = 5;
    int weekend = 9;
    int optimalHours = 7 * 8;

    int actualHours = weekday;
    actualHours = actualHours + weekend * 2;
    int solution = optimalHours - actualHours;
    display(solution);
}
public void display(int i) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText("" + i);
}

@nitish778191
Copy link

WhatsApp Image 2021-01-28 at 6 14 02 AM
M final output

@Baqtiyar
Copy link

Please help
33 is not showing me in the app after writing proper code as well
app b

@Baqtiyar
Copy link

This is the white screen but no 33
Screenshot_20210526-230756_Practice Set 2 (1)

@Sonualam-bot
Copy link

In case its 2021 with updated android studio.. and the old code has some buggs..
try this one it worked for me.

package com.example.android.practiceset2;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int weekday = 5;
    int weekend = 9;
    int optimalHours = 7 * 8;

    int actualHours = weekday;
    actualHours = actualHours + weekend * 2;
    int solution = optimalHours - actualHours;
    display(solution);
}

/**
 * Display methods that allow the text to appear on the screen. Don't worry if you don't know
 * how these work yet. We'll be covering them in lesson 3.
 */

public void display (int i) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText(""+i);
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment