Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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);
@nolteman

This comment has been minimized.

Copy link

@nolteman nolteman commented Mar 5, 2016

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

@c0mputergenie

This comment has been minimized.

Copy link

@c0mputergenie c0mputergenie commented Apr 27, 2016

Thanks for adding that @nolteman. Helped me save time.

@angel-tren

This comment has been minimized.

Copy link

@angel-tren angel-tren commented May 8, 2016

Excelente. Gracias

@srafay

This comment has been minimized.

Copy link

@srafay srafay commented May 31, 2016

I would continue play games or code rather than sleeping..

@rohit-yadav

This comment has been minimized.

Copy link

@rohit-yadav rohit-yadav commented Jun 20, 2016

@c0mputergenie but you wasted time commenting here.

@RamyBittar

This comment has been minimized.

Copy link

@RamyBittar RamyBittar commented Dec 15, 2016

I think that in the line number 5 the weekdays need to be multiplied by 5.

@NyashaDuri

This comment has been minimized.

Copy link

@NyashaDuri NyashaDuri commented Dec 23, 2016

@c0mputergenie Thank you for showing appreciation to @nolteman thus making this a better community

@iAshishVerma

This comment has been minimized.

Copy link

@iAshishVerma iAshishVerma commented Dec 24, 2016

i also think weekday should be multiplied with 5

@wassimoss

This comment has been minimized.

Copy link

@wassimoss wassimoss commented Jan 25, 2017

By the way, we just copy and past the code without knowing from where it came from, is it covered in the future lessons ?

@roger712

This comment has been minimized.

Copy link

@roger712 roger712 commented Jan 26, 2017

Hi,

I placed the entire code except the last line, that is, display(solution); outside the onCreate() method

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

}

...
...
}

Is it wrong to do so?
I get an error message saying, error: expected
and when I place the cursor on actualHours or weekend from the line actualHours = actualHours + weekend * 2; It says, Unknown class: 'actualHours' and Unknown class: 'weekend'

Thanks in advance!!!

@TerraMaficco

This comment has been minimized.

Copy link

@TerraMaficco TerraMaficco commented Jan 26, 2017

@roger712
onCreate is a seperate function/class build to do the specific things that you decide to let it do inside this function. It is not made to call anything from outside this function. As well as onCreate immediately displays things after the start of the app.

In some ways you interchange the usage of a function... a function is used to outsource code or repeating things that you want to call several times. This said, the calculation you want to make with this code is usually outsourced in a function to call it with a simple argument that is connected to a function. You can't rip parts of this calculation away.

About those errors. It seems that this java file expects the building of several classes. As I'm not so much used to Java so far I'm not exactly certain, wether it only expects classes, and you did not define any classes, or wether those errors refer to something else.

I hope you didn't get confused and understand now, why this can't work and how a function is meant to work.

Beste Regards

@Tyupi01

This comment has been minimized.

Copy link

@Tyupi01 Tyupi01 commented Jan 29, 2017

Hi maybe it helps someone like me to say that to place the code :
public void display (int i) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText(""+i);
}

to the place of

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

if you have it.

I don't know why I have that but i do anyway... so just replace the second code to the first one and it should work.

@madmat27

This comment has been minimized.

Copy link

@madmat27 madmat27 commented Jan 31, 2017

The code that nolteman suggested didn't work for me. So, I found a more complex workaround, but it seems to work:

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

    int weekday = 5;
    int weekdayOpt = 8 * 5;
    int weekdayActual = weekday * 5;
    int weekDebt = weekdayOpt-weekdayActual;

    int weekend = 9;
    int weekendOpt =  8 * 2;
    int weekendActual =  weekend * 2;
    int weekendDebt = weekendOpt - weekendActual;

    int solution = weekDebt + weekendDebt;
    display(solution);
}`
@gielmuda

This comment has been minimized.

Copy link

@gielmuda gielmuda commented Feb 4, 2017

Yes, as Tyupi01 said above, the "display" code should replace the code that is already in the java file. otherwise the computer will get confused which one it should use when is asked to "display" something.

@peterstieg

This comment has been minimized.

Copy link

@peterstieg peterstieg commented Feb 20, 2017

Thanks for adding the display method to this Gist, @nolteman.

@Serdarhakkioglu

This comment has been minimized.

Copy link

@Serdarhakkioglu Serdarhakkioglu commented Mar 2, 2017

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

}

@Theoharoula

This comment has been minimized.

Copy link

@Theoharoula Theoharoula commented Mar 3, 2017

A little help please... On my code it says error and "Cannot resolve symbol 'R'".
Why?

@jcunanan05

This comment has been minimized.

@krishsam02

This comment has been minimized.

Copy link

@krishsam02 krishsam02 commented Mar 10, 2017

problem

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

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

solution

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

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

then accomplished the goal

@Piotr-muzyka

This comment has been minimized.

Copy link

@Piotr-muzyka Piotr-muzyka commented Mar 22, 2017

@Theoharoula

Check if the *.java file belongs to the same package or try importing R with :

import com.example.android..R;

@victordobre

This comment has been minimized.

Copy link

@victordobre victordobre commented Mar 22, 2017

I have modified the code a bit...here is my version:
int weekday = 5;
int weekend = 9;
int optimalHours = 7 * 8;
int weekDayHours = weekday * 5;
int weekendHours = weekend * 2;
int solution = optimalHours - (weekDayHours + weekendHours);
display(solution);

@razajaveid

This comment has been minimized.

Copy link

@razajaveid razajaveid commented Apr 2, 2017

i have done with this,

@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;
   int actualHours = (weekday * 5) + weekend * 2;
   int soltion = optimalHours - actualHours;
    display(soltion);

}
@nagyklaudiu

This comment has been minimized.

Copy link

@nagyklaudiu nagyklaudiu commented Apr 11, 2017

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

This comment has been minimized.

Copy link

@codejockie codejockie commented Apr 14, 2017

package com.example.android.practiceset2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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);
}

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

}

@rjoshi91

This comment has been minimized.

Copy link

@rjoshi91 rjoshi91 commented Apr 16, 2017

why every time import codes are wrongs & missed

@slickr1ck

This comment has been minimized.

Copy link

@slickr1ck slickr1ck commented Apr 24, 2017

this is what i have and im still not getting it. ahhhhhhhh
package com.example.android.practiceset2;

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

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.
 */

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

}

image

@slickr1ck

This comment has been minimized.

Copy link

@slickr1ck slickr1ck commented May 2, 2017

Thank you for the response i was able to get it everyone

@diskandarnerd

This comment has been minimized.

Copy link

@diskandarnerd diskandarnerd commented May 17, 2017

awesome guys

@TheLegendMac

This comment has been minimized.

Copy link

@TheLegendMac TheLegendMac commented May 19, 2017

`package com.example.android.practiceset2;

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

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    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);
}

}
`

Why does my app keep on crashing every time I load it up. I made sure I did it word for word, above is the Java file I have. When I debugged the app it seemed to be coming from the second to last bracket but I don't understand why.

@femiayo

This comment has been minimized.

Copy link

@femiayo femiayo commented May 20, 2017

the copied code is not working it keep saying it cant resolve R and its not running what can be do

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

}

@abirAbuAsim

This comment has been minimized.

Copy link

@abirAbuAsim abirAbuAsim commented Jun 5, 2017

@femiayo, try building and cleaning the project.

@abirAbuAsim

This comment has been minimized.

Copy link

@abirAbuAsim abirAbuAsim commented Jun 5, 2017

@TheLegendMac, did you make sure you included the XML portion that Alice just showed in the video. See if the XML code like following is there in you activity_main.xml file and you have a display_text_view id set there.

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

This comment has been minimized.

Copy link

@AhmadAyaad AhmadAyaad commented Oct 17, 2017

package com.example.android.practiceset2;

import android.support.v7.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 equ = 7*8;

    int actualHours = 5*weekday+ weekend*9;
    int sol =equ -actualHours;
    display(sol);
}

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

The Output is >>> -50 i don't know why also i tried to debug the app but i can't step over
anyone can help?!!!!

@taniakolesnik

This comment has been minimized.

Copy link

@taniakolesnik taniakolesnik commented Nov 22, 2017

my code:

    int weekDayActualHoursSlept = 5;
    int weekEndActualHoursSlept = 9;
    int weekDayNumber = 5;
    int weekEndNumber = 2;
    int optimalHoursToSleep = 8;
    int optimalHours = (weekDayNumber + weekEndNumber) * optimalHoursToSleep;
    int actualHours = (weekDayActualHoursSlept * weekDayNumber) + (weekEndActualHoursSlept * weekEndNumber);
    int solution = optimalHours - actualHours;
    display(solution);

Joox,

your code in "int actualHours = 5weekday+ weekend9;" is 55 +99. but should be 5(hours)*5(days) + 9(hours)*2(days)
to easy fix your issue just update "int weekend =9;" to "int weekend =2;" (witout double quotes)

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Dec 13, 2017

Thanks for the code!!

@DeusXmac

This comment has been minimized.

Copy link

@DeusXmac DeusXmac commented Dec 31, 2017

Weekday must multiply with 5, as you see on my code;

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

public class MainActivity extends AppCompatActivity {

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

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

}

@spurs66

This comment has been minimized.

Copy link

@spurs66 spurs66 commented Jan 8, 2018

In case anyone did the exercise which was optional and worked with the layout that has 3 textViews here is the main code which displays the wrong answer in textView 1 and the correct one in textView 2:

`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;
    int actualCorrect = weekday*5;
    actualHours = weekday + weekend * 2;
    actualCorrect = weekday * 5 + weekend * 2;
    int solution = optimalHours - actualHours;
    int goodSolution = optimalHours - actualCorrect;
    display(solution);
    String good = Integer.toString(goodSolution);
    display2(good);

}`
@felipedurli

This comment has been minimized.

Copy link

@felipedurli felipedurli commented Jan 10, 2018

I've solve the problem which should not display 33, but 13 as a result!
Great exercise though!

@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;
actualHours = actualHours + weekend * 2;
int solution = optimalHours - actualHours;
display(solution);
}

@pomochieng

This comment has been minimized.

Copy link

@pomochieng pomochieng commented Jan 11, 2018

I concur @Tyupi01 one has to replace the second line on display methods with the display
practiseset1
method given.

This worked out for me.

`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);
    // 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);
}`
@HOLARO

This comment has been minimized.

Copy link

@HOLARO HOLARO commented Jan 13, 2018

Hi Everyone!

That's what the code looks like in my "MainActivity.java":

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);
    // 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:

<TextView
    android:id="@+id/display_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

With these the program should display: 33

In order to display the proper value for the amount of weekly sleep debt I modified the code in the "MainActivity.java" as proposed by @taniakolesnik (see: above thread). After the modifications my code in the "MainActivity.java" looks like this:

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 weekDayActualHoursSlept = 5;
    int weekEndActualHoursSlept = 9;
    int weekDayNumber = 5;
    int weekEndNumber = 2;
    int optimalHoursToSleep = 8;
    int optimalHours = (weekDayNumber + weekEndNumber) * optimalHoursToSleep;
    int actualHours = (weekDayActualHoursSlept * weekDayNumber) + (weekEndActualHoursSlept * weekEndNumber);
    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);}

}

Hope that helps.

Best regards,

Aaron

@drszabodavid

This comment has been minimized.

Copy link

@drszabodavid drszabodavid commented Jan 14, 2018

For me this code worked:

@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 * 5 + weekend * 2;
    int solution = optimalHours - actualHours;
    display(solution);
@NisanTarhan

This comment has been minimized.

Copy link

@NisanTarhan NisanTarhan commented Jan 16, 2018

@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 * 5;** // multiply weekday by 5
    actualHours = actualHours + weekend * 2;
    int solution = optimalHours - actualHours;
    display(solution);
}
@danialabdl

This comment has been minimized.

Copy link

@danialabdl danialabdl commented Jan 28, 2018

thanks alot.

@rayof1hope

This comment has been minimized.

Copy link

@rayof1hope rayof1hope commented Jan 30, 2018

Tyupi01, thank you for explaining. I just replaced the last one. Once I did undo and moved it to the second one, it worked.

@Maxwe1

This comment has been minimized.

Copy link

@Maxwe1 Maxwe1 commented Jan 31, 2018

Weekday*5 is needed

@Nogaeman

This comment has been minimized.

Copy link

@Nogaeman Nogaeman commented Feb 3, 2018

Thanks
untitled

@ohi4fi

This comment has been minimized.

Copy link

@ohi4fi ohi4fi commented Feb 3, 2018

why they cant put the *5 in the weekday?

@MarwaMoaz

This comment has been minimized.

Copy link

@MarwaMoaz MarwaMoaz commented Feb 8, 2018

thnx 4 sharing

@mohamedkh77

This comment has been minimized.

Copy link

@mohamedkh77 mohamedkh77 commented Feb 11, 2018

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 * 5 + weekend * 2;
    int solution = optimalHours - actualHours;
    display(solution);
    // PASTE CODE YOU WANT TO TEST HERE
@abdahma01

This comment has been minimized.

Copy link

@abdahma01 abdahma01 commented Feb 13, 2018

untitled

@badrddinb

This comment has been minimized.

Copy link

@badrddinb badrddinb commented Feb 21, 2018

DONE
image

@first-hero

This comment has been minimized.

Copy link

@first-hero first-hero commented Feb 23, 2018

int weekday = 5;
int weekend = 9;
int optimalHours = 7 * 8;........................56
int actualHours = weekday;.....................5
actualHours = actualHours + weekend * 2;......................5+(9*2)=23
int solution = optimalHours - actualHours;...................56-23=33
display(solution);
screenshot_20180223-234554 1

@louayeldin

This comment has been minimized.

Copy link

@louayeldin louayeldin commented Mar 15, 2018

Yes, works fine as expected

image

@adroit22h

This comment has been minimized.

Copy link

@adroit22h adroit22h commented Mar 19, 2018

Thank you at @Serdarhakkioglu for posting the whole code....it worked for me, since I was getting some form of error before.
Thanks

@camango

This comment has been minimized.

Copy link

@camango camango commented Apr 3, 2018

but why is? t.setText(""+i);
}
cant it be just t.setText(text);
Can somebody explain pls

@mohamedsaleh1984

This comment has been minimized.

Copy link

@mohamedsaleh1984 mohamedsaleh1984 commented May 17, 2018

The program is working fine but I don't understand when I submit the answer on the website it gives me Wrong Answer.

@iven86

This comment has been minimized.

Copy link

@iven86 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

This comment has been minimized.

Copy link

@ghufran86 ghufran86 commented May 30, 2018

the code must be

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

@serdoune

This comment has been minimized.

Copy link

@serdoune serdoune commented Jun 1, 2018

image

@tooptooptoop

This comment has been minimized.

Copy link

@tooptooptoop tooptooptoop commented Jun 2, 2018

<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

This comment has been minimized.

Copy link

@ZahraaElhaj ZahraaElhaj commented Jun 2, 2018

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

This comment has been minimized.

Copy link

@mohammeddbadawy mohammeddbadawy commented Jun 6, 2018

worked

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Jun 12, 2018

Works

@joekaslow

This comment has been minimized.

Copy link

@joekaslow joekaslow commented Jun 14, 2018

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

This comment has been minimized.

Copy link

@find1010 find1010 commented Jun 18, 2018

goooooood work

@ahmademad007

This comment has been minimized.

Copy link

@ahmademad007 ahmademad007 commented Jun 25, 2018

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

@KobiPeeri

This comment has been minimized.

Copy link

@KobiPeeri KobiPeeri commented Jul 1, 2018

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

@DynamOo

This comment has been minimized.

Copy link

@DynamOo DynamOo commented Jul 6, 2018

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

@Manhans

This comment has been minimized.

Copy link

@Manhans 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

This comment has been minimized.

Copy link

@abdellaah abdellaah commented Aug 20, 2018

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

@Stas0050

This comment has been minimized.

Copy link

@Stas0050 Stas0050 commented Oct 29, 2018

Привет,

Я поместил весь код, кроме последней строки, то есть 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

@shankar-shiv

This comment has been minimized.

Copy link

@shankar-shiv shankar-shiv commented Dec 23, 2018

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

This comment has been minimized.

Copy link

@xMagicXs 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

This comment has been minimized.

Copy link

@al130m 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

This comment has been minimized.

Copy link

@LaszloLajosT LaszloLajosT commented Nov 10, 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

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

This comment has been minimized.

Copy link

@rajan3300 rajan3300 commented Dec 7, 2019

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

This comment has been minimized.

Copy link

@rajan3300 rajan3300 commented Dec 7, 2019

help

@singer0000

This comment has been minimized.

Copy link

@singer0000 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

This comment has been minimized.

Copy link

@Ashishkumar-sys Ashishkumar-sys commented Mar 27, 2020

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

This comment has been minimized.

Copy link

@yyssimon 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

This comment has been minimized.

Copy link

@yyssimon 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?

@yohsita

This comment has been minimized.

Copy link

@yohsita yohsita 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);
}
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.