Forums Register Login

Very, very stuck on something unfamiliar to me.

+Pie Number of slices to send: Send
Hi everyone. I am currently in the final stages of writing code for an assignment, and I was given the task of adding a subclass onto my current assignment today in a test. Unfortunately, I completely froze up and I'm still trying to understand how to do this.

For context, here is the rest of my assignment. I'd post the code here but it's far too long, so if you have BlueJ please take the time to check it out, but that's not necessary.

Link to project is Here]

Basically, I need this class to add phones to my Store class and since it's boolean, android = true, apple phones = false. The UML for both parts goes something like this:

Constructor for class Phone:
-android (boolean)
Methods for class Phone:
ifAndroid (boolean)

And after that, I need to write a phoneReport similar to my tvReport, productReport etc etc.

The phoneReport is a method ran in the testing class I have set up (You can see how it works if you click in it) and basically it needs to show which phones are Android and which ones are Apple phones with suitable footers.

So now that is out of the way, it seems as if I'm supposed to run an if statement in the ifAndroid method. How can I do this? I was never taught anything properly about booleans, and I'd imagine it would go something like this:



But I have no idea what to do in the ELSE part. It doesn't have an Apple product, should I just say "return android" or what? Or do I need to run a "while" loop (Was never taught this so I'm assuming not)

As for the phoneReport, do I use an instanceof search through the arraylist like I did for tvReport? Or do I do a catch method?

Seriously, if anyone is willing to help me it would be very appreciated. Apologies if this didn't make sense my brain is basically fried after lack of sleep over this.

If you have any questions PLEASE PLEASE ask as I desperately need to do this to pass my course. I've looked at every class recording and I've never had to deal with boolean methods.
+Pie Number of slices to send: Send
Welcome to the Ranch

It feels that you got so much chaotic information at hand to work on. Try not to concentrate on too many things - as it really gets complicated that way.

Is by any chance you have instructions written somewhere as you were given initially by your teacher, so you could copy and paste them here? In addition of that would be great if you'd tell us which parts you accomplished already, and which you're stuck on.
+Pie Number of slices to send: Send
 

Liutauras Vilda wrote:Welcome to the Ranch

It feels that you got so much chaotic information at hand to work on. Try not to concentrate on too many things - as it really gets complicated that way.

Is by any chance you have instructions written somewhere as you were given initially by your teacher, so you could copy and paste them here? In addition of that would be great if you'd tell us which parts you accomplished already, and which you're stuck on.



Hey mate, sorry it was a test so I can't provide the actual details here. I can give more information if you are curious about anything. Basically, I need my project to display Android/Apple phones using a Boolean method and I have very little idea as to what I'm doing with it.

I'll draw up the proper UML tomorrow as I'm pretty tired, but thank you for the advice. I really appreciate it!
+Pie Number of slices to send: Send
Booleans are pretty simple. Variables of type boolean can only have one of two values: true or false. It's the simplest data type that Java has.

Do you understand how to write and call methods? Methods have a return type. That return type could ofcourse be boolean, which means the method returns either true or false. When you call the method, you can check what it returns using an if statement and then do something it it's true, and do something else when it's false.

Can you explain in more detail what exactly you have difficulties with here? Writing a boolean method, or calling a boolean method?
+Pie Number of slices to send: Send
 

I'd imagine it would go something like this:



Close, with one key difference:

if (mAndroid == true) { will compile, but it's bad programming style.
+Pie Number of slices to send: Send
 

Knute Snortum wrote:. . . if (mAndroid == true) { will compile, but it's bad programming style.

It is also very error‑prone; we see enough people who write = instead of == by mistake.
+Pie Number of slices to send: Send
 

Knute Snortum wrote:

I'd imagine it would go something like this:



Close, with one key difference:

if (mAndroid == true) { will compile, but it's bad programming style.



Thanks a lot. Yeah, I just typed that up as I deleted all of my previous work. So I imagine, for the else part I just write like:

else {
System.out.println " Apple Phones : "



But I'd imagine I have to write something there, as that's not a statement? I can't exactly just add on "android" again, right?

Really confused for some reason. Something about this just won't click.
+Pie Number of slices to send: Send
1. By looking at the information you provided us, seems that you need to write a Phone class, which has 1 instance variable whose value can be either true or false.

2. Phone class needs to have a constructor which accepts 1 parameter of type boolean and sets instance variable.

3. Phone class needs to have 1 method which tells if phone is android or ios. In case of true it is android, false - ios.

There are some kind of more tasks related with report you need to create, but that is for later.

So, you have 3 tasks at hand now, do one at a time, and tell us how is it going.
In case I understood your instructions wrongly - please clarify those.

Problem at the very beginning arise when too much things trying to do at once and all incomplete. From what you showed us earlier, I got an impression that you done first part already and now doing second.

Can you show us how you declared a class and constructor?
+Pie Number of slices to send: Send
 

Jesper de Jong wrote:Booleans are pretty simple. Variables of type boolean can only have one of two values: true or false. It's the simplest data type that Java has.

Do you understand how to write and call methods? Methods have a return type. That return type could ofcourse be boolean, which means the method returns either true or false. When you call the method, you can check what it returns using an if statement and then do something it it's true, and do something else when it's false.

Can you explain in more detail what exactly you have difficulties with here? Writing a boolean method, or calling a boolean method?



Hi mate. The boolean class itself is kinda easy to me now after you guys did great explaining it and helping, but now I need to do the phoneReport bit. The Phone report should be similar to my other report classes, it lists the products in my store and if it's true, it's an Android and if its false it's Apple.

Would using this kind of method work?



Do I say if nextproduct = true System.out.println "android phones" etc etc.

+Pie Number of slices to send: Send
 

Liutauras Vilda wrote:1. By looking at the information you provided us, seems that you need to write a Phone class, which has 1 instance variable whose value can be either true or false.

2. Phone class needs to have a constructor which accepts 1 parameter of type boolean and sets instance variable.

3. Phone class needs to have 1 method which tells if phone is android or ios. In case of true it is android, false - ios.

There are some kind of more tasks related with report you need to create, but that is for later.

So, you have 3 tasks at hand now, do one at a time, and tell us how is it going.
In case I understood your instructions wrongly - please clarify those.

Problem at the very beginning arise when too much things trying to do at once and all incomplete. From what you showed us earlier, I got an impression that you done first part already and now doing second.

Can you show us how you declared a class and constructor?



Hey mate. Thanks for the advice, below is the code I've written for the subclass Phone

+Pie Number of slices to send: Send
Small point, but typically a method like that would be called isAndroid rather than ifAndroid.
+Pie Number of slices to send: Send
 

Ron McLeod wrote:Small point, but typically a method like that would be called isAndroid rather than ifAndroid.



I agree, however that was on the UML for the test so I have to put it there lol!!
+Pie Number of slices to send: Send
Sorted it out guys, thanks for the help.
+Pie Number of slices to send: Send
 

callam falconer wrote:Sorted it out guys, thanks for the help.

No, you didn't. There are lots of issues in your solution.

  • Especially in constructor. Line 9 doesn't make sense. No check for parameters validity. Last parameter doesn't do anything.
  • ifAndroid() method doesn't look right either.
  • Not sure if toString() method is correct too.
  • Code formatting issues.

  • Why the instance variable is protected?

    Do you have a different opinion on those? Maybe evidence?
    Beauty is in the eye of the tiny ad.
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 608 times.
    Similar Threads
    Questions from beginner Android developer
    Android Training
    Welcome?
    Writing a Recursive Descent Parser, I think I'm done, well almost?
    Incomming call and Midlet
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 28, 2024 15:26:17.