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);
@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