• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 04:37
CET 10:37
KST 18:37
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
TL.net Map Contest #21: Winners11Intel X Team Liquid Seoul event: Showmatches and Meet the Pros10[ASL20] Finals Preview: Arrival13TL.net Map Contest #21: Voting12[ASL20] Ro4 Preview: Descent11
Community News
[TLMC] Fall/Winter 2025 Ladder Map Rotation4Weekly Cups (Nov 3-9): Clem Conquers in Canada4SC: Evo Complete - Ranked Ladder OPEN ALPHA8StarCraft, SC2, HotS, WC3, Returning to Blizzcon!45$5,000+ WardiTV 2025 Championship7
StarCraft 2
General
[TLMC] Fall/Winter 2025 Ladder Map Rotation Mech is the composition that needs teleportation t Weekly Cups (Nov 3-9): Clem Conquers in Canada Craziest Micro Moments Of All Time? SC: Evo Complete - Ranked Ladder OPEN ALPHA
Tourneys
Constellation Cup - Main Event - Stellar Fest Tenacious Turtle Tussle Sparkling Tuna Cup - Weekly Open Tournament $5,000+ WardiTV 2025 Championship Merivale 8 Open - LAN - Stellar Fest
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 499 Chilling Adaptation Mutation # 498 Wheel of Misfortune|Cradle of Death Mutation # 497 Battle Haredened Mutation # 496 Endless Infection
Brood War
General
Rapidtags: The Ultimate Tool for Hashtag and Keywo Terran 1:35 12 Gas Optimization FlaSh on: Biggest Problem With SnOw's Playstyle BW General Discussion BGH Auto Balance -> http://bghmmr.eu/
Tourneys
[BSL21] RO32 Group D - Sunday 21:00 CET [BSL21] RO32 Group C - Saturday 21:00 CET [ASL20] Grand Finals [Megathread] Daily Proleagues
Strategy
Current Meta PvZ map balance How to stay on top of macro? Soma's 9 hatch build from ASL Game 2
Other Games
General Games
Nintendo Switch Thread Stormgate/Frost Giant Megathread Should offensive tower rushing be viable in RTS games? Path of Exile Dawn of War IV
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread SPIRED by.ASL Mafia {211640}
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine Canadian Politics Mega-thread The Games Industry And ATVI
Fan Clubs
White-Ra Fan Club The herO Fan Club!
Media & Entertainment
[Manga] One Piece Anime Discussion Thread Movie Discussion! Korean Music Discussion Series you have seen recently...
Sports
2024 - 2026 Football Thread Formula 1 Discussion NBA General Discussion MLB/Baseball 2023 TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
SC2 Client Relocalization [Change SC2 Language] Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List
Blogs
Dyadica Gospel – a Pulp No…
Hildegard
Coffee x Performance in Espo…
TrAiDoS
Saturation point
Uldridge
DnB/metal remix FFO Mick Go…
ImbaTosS
Reality "theory" prov…
perfectspheres
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1220 users

The Big Programming Thread - Page 815

Forum Index > General Forum
Post a Reply
Prev 1 813 814 815 816 817 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
tofucake
Profile Blog Joined October 2009
Hyrule19151 Posts
December 13 2016 14:02 GMT
#16281
Usually homework programs like that have a UI supplied by the professor/TAs exactly so that automated tests can be written, and the assignment is to make all of the programmatic logic.
Liquipediaasante sana squash banana
Yurie
Profile Blog Joined August 2010
11928 Posts
Last Edited: 2016-12-13 14:54:59
December 13 2016 14:45 GMT
#16282
On December 13 2016 23:02 tofucake wrote:
Usually homework programs like that have a UI supplied by the professor/TAs exactly so that automated tests can be written, and the assignment is to make all of the programmatic logic.


Our previous phonebook had a defined UI with exact menu options and buttons we should add.

The Sudoku one or calculator we are doing now does not. It has pretty loose requirements as well things like it could be textfields on tilepanes. The only requirements is that there should be a solve button, clear button, it should work, it should show a sudoku board and should tell the user if it isn't possible to solve.

Testcases are pretty clear, is why I could include the two that took longest to enter as buttons to get it done faster. After that there are like 20 things they should consider when reading the code and grade on (which I did not read).

Test cases (roughly translated and shortened)
+ Show Spoiler +

1. Hit solve on empty board, see that a correct sudoku shows up.

2. Test three cases and that it can't solve when it shouldn't.
Two 5's on first two places in first row.
Two 5's on first two places in first column.
Two 5's in the first 3x3 grid.

3. First grid
123
456
Second grid
000
000
007
Should not solve, remove 7, should solve.

4. Two 5's in first row, not in first grid. Should not solve.
Clear.
Should solve empty board.

5. Board in my previous code resulting in correct solution.

6. Wrong inputs, -1, a, 10, 0


I think me using and showing 0 as empty cell value is incorrect but the only thing that says that is the mock up pictures not the text. I assume that is easy to fix if they complain using .clear() on textfields and an if that sends a 0 to the solver if the textfield is blank or 0 instead of always keeping it 0 as now.
phar
Profile Joined August 2011
United States1080 Posts
December 13 2016 17:32 GMT
#16283
Was there a given lower level non GUI interface that you had to implement? Or does your prof just hate your TAs?
Who after all is today speaking about the destruction of the Armenians?
enigmaticcam
Profile Blog Joined October 2010
United States280 Posts
Last Edited: 2016-12-13 22:43:29
December 13 2016 22:42 GMT
#16284
.NET question. I have a client program that copies a file to a server via windows UNC, e.g. \\servername\directory. Sometimes the file is very large. I'd like to be able to zip it first, send to server, and unzip on server. Is there any way to do this? I can't see that it can be done without having some type of service on the server to do it. I figured you can't, that any zipping/unzipping function would have to be performed on the client, but thought I'd ask just in case.
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
Last Edited: 2016-12-13 23:21:33
December 13 2016 23:12 GMT
#16285
On December 14 2016 07:42 enigmaticcam wrote:
.NET question. I have a client program that copies a file to a server via windows UNC, e.g. \\servername\directory. Sometimes the file is very large. I'd like to be able to zip it first, send to server, and unzip on server. Is there any way to do this? I can't see that it can be done without having some type of service on the server to do it. I figured you can't, that any zipping/unzipping function would have to be performed on the client, but thought I'd ask just in case.

.NET has a compression library System.IO.Compression if that's what you're looking for. Idk how you're doing the file transfer and reception but a good transfer library should support compression in its options.
There is no one like you in the universe.
Yurie
Profile Blog Joined August 2010
11928 Posts
Last Edited: 2016-12-14 02:17:38
December 14 2016 02:15 GMT
#16286
On December 14 2016 02:32 phar wrote:
Was there a given lower level non GUI interface that you had to implement? Or does your prof just hate your TAs?


Hate the TAs. The previous 6 were all well defined like you asked for, classes we had to complete, GUI for done classes and things like that. JUnit tests were even supplied for 4 of them beforehand. Since this is the last one they wanted us to do it from scratch.

Their method for grading is peer review into a quick go through with the TA. There is a 2h block where we are supposed to turn it in with up to 11 people/groups so they know it takes time to do. Mine is only 7 people since it is the last group with the spill over and there usually are 2 TAs at them, so shouldn't be too bad for them.
3FFA
Profile Blog Joined February 2010
United States3931 Posts
Last Edited: 2016-12-14 06:23:00
December 14 2016 06:21 GMT
#16287
On December 13 2016 16:48 3FFA wrote:
What are some good Discord IRC chat groups for helping learn? Asking for both myself and others, since a chat community is helpful for both answering questions as well as morale, preventing burnout.

I MEANT to say IRC, not Discord! Lol whoops!
"As long as it comes from a pure place and from a honest place, you know, you can write whatever you want."
Isualin
Profile Joined March 2011
Germany1903 Posts
December 14 2016 18:15 GMT
#16288
We are converting ~300k lines of groovy tests to java. I don't ever want to see groovy code again :D
| INnoVation | The literal god TY | ByuNjwa | LRSL when? |
Deleted User 101379
Profile Blog Joined August 2010
4849 Posts
December 14 2016 19:32 GMT
#16289
On the topic of Java. Does anyone have experience with any HMVC web frameworks? I really dislike MVC since I've learned how HMVC works, but all the web frameworks in java seem to be purely MVC based or do HMVC terribly. I've adapted Spring to do HMVC with a few ugly workarounds and replacing it's whole routing, so at least that works, but I'd rather have something that does it out of the box.
Manit0u
Profile Blog Joined August 2004
Poland17428 Posts
December 14 2016 21:53 GMT
#16290
On December 15 2016 04:32 Morfildur wrote:
On the topic of Java. Does anyone have experience with any HMVC web frameworks? I really dislike MVC since I've learned how HMVC works, but all the web frameworks in java seem to be purely MVC based or do HMVC terribly. I've adapted Spring to do HMVC with a few ugly workarounds and replacing it's whole routing, so at least that works, but I'd rather have something that does it out of the box.


There's Scope but it hasn't been updated in 3 years...
Time is precious. Waste it wisely.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
December 15 2016 00:42 GMT
#16291
It's that time. That time where my final exam is tomorrow.

And therefore I flood the forum with review questions. Let's start with this one.

(all questions pertain to java if they require code)

"What technique is used (instead of looping) to create a server that can efficiently handle
numerous clients?"

I have no idea. sockets? threads?
Birdie
Profile Blog Joined August 2007
New Zealand4438 Posts
December 15 2016 00:47 GMT
#16292
On December 15 2016 09:42 travis wrote:
It's that time. That time where my final exam is tomorrow.

And therefore I flood the forum with review questions. Let's start with this one.

(all questions pertain to java if they require code)

"What technique is used (instead of looping) to create a server that can efficiently handle
numerous clients?"

I have no idea. sockets? threads?

Sounds to me like it's asking for threads, looping means everyone basically just joins a queue and you loop through their requests one by one which is horrible compared to threads. If a server has a thread for each client it is far more efficient than any loop-based method.
Red classic | A butterfly dreamed he was Zhuangzi | 4.5k, heading to 5k as support!
RoomOfMush
Profile Joined March 2015
1296 Posts
December 15 2016 08:30 GMT
#16293
On December 15 2016 09:47 Birdie wrote:
Show nested quote +
On December 15 2016 09:42 travis wrote:
It's that time. That time where my final exam is tomorrow.

And therefore I flood the forum with review questions. Let's start with this one.

(all questions pertain to java if they require code)

"What technique is used (instead of looping) to create a server that can efficiently handle
numerous clients?"

I have no idea. sockets? threads?

Sounds to me like it's asking for threads, looping means everyone basically just joins a queue and you loop through their requests one by one which is horrible compared to threads. If a server has a thread for each client it is far more efficient than any loop-based method.

Well, depends. I bet with an average of like 3 clients the loop would be far more efficient. Context switching is expensive.
teamamerica
Profile Blog Joined July 2010
United States958 Posts
Last Edited: 2016-12-15 08:55:28
December 15 2016 08:50 GMT
#16294
On December 15 2016 09:42 travis wrote:
It's that time. That time where my final exam is tomorrow.

And therefore I flood the forum with review questions. Let's start with this one.

(all questions pertain to java if they require code)

"What technique is used (instead of looping) to create a server that can efficiently handle
numerous clients?"

I have no idea. sockets? threads?


Sounds like callbacks/event driven programming. Example epoll/kqueue vs select. Select takes linearly more time per client as more to go through in select loop - epoll/kqueue are O(1) growth. Weird kinda question to have depending on what the class was like. You can read some example such as https://docs.oracle.com/javaee/7/tutorial/servlets012.htm to understand why this asynchronous style might be more scaleable in a Java context.

You can also read for well known example http://www.kegel.com/c10k.html.

On December 15 2016 09:47 Birdie wrote:
Show nested quote +
On December 15 2016 09:42 travis wrote:
It's that time. That time where my final exam is tomorrow.

And therefore I flood the forum with review questions. Let's start with this one.

(all questions pertain to java if they require code)

"What technique is used (instead of looping) to create a server that can efficiently handle
numerous clients?"

I have no idea. sockets? threads?

Sounds to me like it's asking for threads, looping means everyone basically just joins a queue and you loop through their requests one by one which is horrible compared to threads. If a server has a thread for each client it is far more efficient than any loop-based method.


Event driven programming is explicitly to avoid thread/client although threads are pretty cheap, it's obviously cheaper to have M threads for N>M clients rather than N threads per N clients.
RIP GOMTV. RIP PROLEAGUE.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
December 15 2016 14:46 GMT
#16295
ok its the morning of. time for question flood

here is a few i missed on my first exam

"when does a non-static initialiation block run"

I answered:

before anything else, including the main method

I got this wrong. When I do a google search on this, I find many different answers. So what is accurate?




Okay now let's go over typecasting. I had so much trouble on this on the first exam, it confuses me so badly.
Let's look at it's example, where Fish extends Lifeform, and that Plant also extends Lifeform.


Fish fish = new Fish();
Lifeform life = new Lifeform();
Lifeform x = new Fish();


So what do you call each part of this line
Is it "Type variablename = new object from class" ?

Ok anyways let's move onto the questions. Each one you have to answer "won't compile", "throws exception" or "works fine"

1. Plant a = x; //x is a fish, no relation to plants, I say this won't compile
2. Plant b = (Plant) x; //x is a fish, I put it won't compile on the exam and missed it. correct answer is it throws exception because It compiles because of the cast to plant?
3. Lifeform c = fish; //I put this works fine on exam and got it. So, you can downcast fine when instantiating?
4. Fish d = (Fish) life; //I put throws exception.. I got that right. This doesn't work? then what is the point of casting?
5. Fish e = (Fish) x; // Oh I guess this is the point of casting. For when the object was typed as the super but an instantiation of the subclass. so this works?
6. Plant p = (Plant) fish //I think this doesn't compile because fish is of type fish which is unrelated to plant



Now it says "assume there is a methhod called move in the Lifeform class that has been overridden in the Plant class."


Lifeform x = new Plant();
x.move();


Which version of the move will run.
Lifeform version, or Plant version

I put lifeform version and missed it. So the rule is what? That the version of the method that is ran is based on the object that was instantiated and not the type it was cast to? But that's the opposite for when you want to look at it's field values, right?


Now it has the question

public void foo(Lifeform x) {...}


Which of these could you pass in as a parameter
1.) Fish
2.) Plant
3.) Lifeform
4.) Object

I put 1, 2, 3 and got it right. So you can only pass in the class and subclasses? What if I tried to put in a subclass of Fish as a parameter. Would it work?


Okay now it says we have an arraylist "list" of type LifeForm. Fish have a method called swim.
using a for each loop, call the swim method on all the fish. Do I do this(i missed this on the exam so I am unsure):


for((Fish fish : list) {
fish.swim();
}

so yeah I missed this. They tell me to do "instanceof cast". Where am I doing an instanceof cast? Also they crossed out my "Fish" after the for. What is the correct structure for this? Is it


for(Lifeform fish : list) {
if(instanceOf(fish)) {
fish.swim();
} }



thanks... that's all my questions from exam 1
Nesserev
Profile Blog Joined January 2011
Belgium2760 Posts
Last Edited: 2016-12-15 15:22:30
December 15 2016 15:02 GMT
#16296
--- Nuked ---
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
December 15 2016 15:09 GMT
#16297
sorry, I meant when are static initialization blocks ran

so the answer is "when the class is first loaded"
But... what does that actually mean? When are classes loaded?
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
December 15 2016 15:21 GMT
#16298
This was a random question on the 3rd exam, I am not sure why the prof put it on, I guess to be a dick

Calculate the maximum number of edges possible in a directed graph with n vertices (no edges lead from a vertex to itself)

Each vertices leads to (n-1) other vertices. But for each new vertices we can use 1 less of those edges.

(n-1) + (n-2) + (n-3) .... + (1) + (0)

5 vertices this is 4+3+2+1 = 10
6 vertices this is 5+4+3+2+1 = 15

Is the answer N(n-1) / 2 ?

I put n(n+1)/2 on the exam and missed it

oh wait no I did this wrong didn't I. Each node connects to n-1 other nodes
it's

n(n -1) ... no over 2
is n(n-1) correct?

This probably doesn't matter I don't know why I am distracting myself
Blitzkrieg0
Profile Blog Joined August 2010
United States13132 Posts
Last Edited: 2016-12-15 15:39:23
December 15 2016 15:28 GMT
#16299
On December 15 2016 23:46 travis wrote:
"when does a non-static initialiation block run"

A non static initialization block should run when the constructor is called

On December 15 2016 23:46 travis wrote:
Okay now let's go over typecasting. I had so much trouble on this on the first exam, it confuses me so badly.
Let's look at it's example, where Fish extends Lifeform, and that Plant also extends Lifeform.


Fish fish = new Fish();
Lifeform life = new Lifeform();
Lifeform x = new Fish();


So what do you call each part of this line
Is it "Type variablename = new object from class" ?

Ok anyways let's move onto the questions. Each one you have to answer "won't compile", "throws exception" or "works fine"

1. Plant a = x; //x is a fish, no relation to plants, I say this won't compile
2. Plant b = (Plant) x; //x is a fish, I put it won't compile on the exam and missed it. correct answer is it throws exception because It compiles because of the cast to plant?
3. Lifeform c = fish; //I put this works fine on exam and got it. So, you can downcast fine when instantiating?
4. Fish d = (Fish) life; //I put throws exception.. I got that right. This doesn't work? then what is the point of casting?
5. Fish e = (Fish) x; // Oh I guess this is the point of casting. For when the object was typed as the super but an instantiation of the subclass. so this works?
6. Plant p = (Plant) fish //I think this doesn't compile because fish is of type fish which is unrelated to plant


If it's a valid typing then the code will compile. For something like Plant b = (Plant) fish, it will compile because the code itself has matching types, but then a ClassCastException is thrown when the code actually runs because you can't cast a fish to a plant. Others should follow similar logic; feel free to ask more questions.

On December 15 2016 23:46 travis wrote:
Now it says "assume there is a methhod called move in the Lifeform class that has been overridden in the Plant class."


Lifeform x = new Plant();
x.move();


Which version of the move will run.
Lifeform version, or Plant version

I put lifeform version and missed it. So the rule is what? That the version of the method that is ran is based on the object that was instantiated and not the type it was cast to? But that's the opposite for when you want to look at it's field values, right?


Think back to when you were doing the Tree assignment. The concept is called Polymorphism if you want to read up on how it works.

On December 15 2016 23:46 travis wrote:
Now it has the question

public void foo(Lifeform x) {...}


Which of these could you pass in as a parameter
1.) Fish
2.) Plant
3.) Lifeform
4.) Object

I put 1, 2, 3 and got it right. So you can only pass in the class and subclasses? What if I tried to put in a subclass of Fish as a parameter. Would it work?



A subclass of Fish would work because it is a Fish and a Fish is a Lifeform.

On December 15 2016 23:46 travis wrote:
Okay now it says we have an arraylist "list" of type LifeForm. Fish have a method called swim.
using a for each loop, call the swim method on all the fish. Do I do this(i missed this on the exam so I am unsure):


for((Fish fish : list) {
fish.swim();
}

so yeah I missed this. They tell me to do "instanceof cast". Where am I doing an instanceof cast? Also they crossed out my "Fish" after the for. What is the correct structure for this? Is it


for(Lifeform fish : list) {
if(instanceOf(fish)) {
fish.swim();
} }



You have an ArrayList of Lifeform. For each lifeform, check if its a fish. If it is then you want it to swim. This ArrayList could also contain plants which don't have a swim method so you can't just call swim on everything.

for (Lifeform lifeform : list) {
if (Lifeform instanceof Fish {
lifeform.swim();
}


On December 16 2016 00:09 travis wrote:
sorry, I meant when are static initialization blocks ran

so the answer is "when the class is first loaded"
But... what does that actually mean? When are classes loaded?


When the code is compiled makes more sense to me.
I'll always be your shadow and veil your eyes from states of ain soph aur.
Acrofales
Profile Joined August 2010
Spain18114 Posts
Last Edited: 2016-12-15 15:35:03
December 15 2016 15:29 GMT
#16300
On December 15 2016 23:46 travis wrote:
ok its the morning of. time for question flood

here is a few i missed on my first exam

"when does a non-static initialiation block run"

I answered:

before anything else, including the main method

I got this wrong. When I do a google search on this, I find many different answers. So what is accurate?

A non-static initialization block is part of the class. It is therefore run upon initializing an object of that class. Because it is not explicitly in the constructor, but separate, it is run BEFORE the constructor is run. E.g.



public class Foo {
int bar = 5;

public Foo() {
bar = 3;
}

After initialization an object of type Foo will have bar = 3. However, being non-static, bar is part of the object. It is thus only initialized when an object is initialized. There is no such thing as a static Foo.bar.


Okay now let's go over typecasting. I had so much trouble on this on the first exam, it confuses me so badly.
Let's look at it's example, where Fish extends Lifeform, and that Plant also extends Lifeform.


Fish fish = new Fish();
Lifeform life = new Lifeform();
Lifeform x = new Fish();


So what do you call each part of this line
Is it "Type variablename = new object from class" ?

Not quite sure what you mean, but in your example:

fish is a variable of type Fish.
life and x are variables of type Lifeform.
life is assigned an object of type Lifeform.
x is assigned an object of type Fish.

So in those lines there are the following operations:
1. Variable declarations (with their type)
2. Assignments (the = symbol)
3. Object initialization

If I were to write it completely out in text, I'd say something like
The variable fish is declared of type Fish. Additionally, a Fish object is initialized and assigned to the fish variable.
Same for the other lines.

But that might be more verbose than your prof wants from you.



Ok anyways let's move onto the questions. Each one you have to answer "won't compile", "throws exception" or "works fine"

1. Plant a = x; //x is a fish, no relation to plants, I say this won't compile
2. Plant b = (Plant) x; //x is a fish, I put it won't compile on the exam and missed it. correct answer is it throws exception because It compiles because of the cast to plant?

It'll compile, because the compiler isn't clever enough to know that x cannot possibly be cast to a Plant. Not sure, but I don't even think your lint checker will give you a warning. It'll start running, try to cast an object of type Fish to type Plant, realize that cannot be done and throw a ClassCastException.


3. Lifeform c = fish; //I put this works fine on exam and got it. So, you can downcast fine when instantiating?

Yup, you can abstract away when assigning variables without explicitly casting (you just did so in the example above where you assigned a new object of type Fish to variable x (of type Lifeform)).


4. Fish d = (Fish) life; //I put throws exception.. I got that right. This doesn't work? then what is the point of casting?
5. Fish e = (Fish) x; // Oh I guess this is the point of casting. For when the object was typed as the super but an instantiation of the subclass. so this works?

You figured it out.


6. Plant p = (Plant) fish //I think this doesn't compile because fish is of type fish which is unrelated to plant

Correct. Here the compiler is smart enough to figure it out.


Now it says "assume there is a methhod called move in the Lifeform class that has been overridden in the Plant class."


Lifeform x = new Plant();
x.move();


Which version of the move will run.
Lifeform version, or Plant version

I put lifeform version and missed it. So the rule is what? That the version of the method that is ran is based on the object that was instantiated and not the type it was cast to? But that's the opposite for when you want to look at it's field values, right?

You should probably do a bit more reading on polymorphism.

You have a variable of type Lifeform. That means you can only ask it about things that Lifeform has. E.g. you cannot ask it how many leaves it has, despite knowing that objects of type Plant have a public variable numleaves. Nevertheless, the object that is assigned to that variable is of type Plant. Therefore any time you call upon the lifeform to do something, it will do it like a plant would do things. I'm probably not the best at explaining this, which is why I suggested you read up a bit more on this


Now it has the question

public void foo(Lifeform x) {...}


Which of these could you pass in as a parameter
1.) Fish
2.) Plant
3.) Lifeform
4.) Object

I put 1, 2, 3 and got it right. So you can only pass in the class and subclasses? What if I tried to put in a subclass of Fish as a parameter. Would it work?

Yes. Any object of type Shark, for instance, would also be a Fish, and thus also be a Lifeform. That's why, if you make your method:


public void foo(Object o)

you can literally pass it any object of any type you like in Java.


Okay now it says we have an arraylist "list" of type LifeForm. Fish have a method called swim.
using a for each loop, call the swim method on all the fish. Do I do this(i missed this on the exam so I am unsure):


for((Fish fish : list) {
fish.swim();
}

so yeah I missed this. They tell me to do "instanceof cast". Where am I doing an instanceof cast? Also they crossed out my "Fish" after the for. What is the correct structure for this? Is it


for(Lifeform fish : list) {
if(instanceOf(fish)) {
fish.swim();
} }


Kinda, you got a bunch of syntax errors there, so that won't pass the compiler. instanceof is a binary operator, not a unary method. Moreover, you forgot that lifeforms can't swim. You thus use it as follows:


for(Lifeform lf : list) {
if(lf instanceof Fish) {
((Fish) lf).swim(); //first cast to Fish, then call swim.
}
}



thanks... that's all my questions from exam 1

You're welcome
Prev 1 813 814 815 816 817 1032 Next
Please log in or register to reply.
Live Events Refresh
Next event in 1h 54m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
SortOf 286
StarCraft: Brood War
Britney 50679
Rain 3081
Hyuk 2902
Soma 365
Backho 358
Rush 231
Pusan 166
JulyZerg 48
sSak 35
NaDa 17
[ Show more ]
ZerO 16
zelot 14
Noble 13
Hm[arnc] 10
Terrorterran 7
Dota 2
XaKoH 419
XcaliburYe139
Counter-Strike
fl0m1575
olofmeister680
shoxiejesuss450
oskar63
Super Smash Bros
Mew2King237
Other Games
ceh9502
Happy244
Pyrionflax126
ZerO(Twitch)4
Organizations
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 14 non-featured ]
StarCraft 2
• Berry_CruncH257
• LUISG 21
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Rush1347
• Stunt515
Upcoming Events
OSC
1h 54m
Kung Fu Cup
2h 24m
Classic vs Solar
herO vs Cure
Reynor vs GuMiho
ByuN vs ShoWTimE
Tenacious Turtle Tussle
13h 24m
The PondCast
1d
RSL Revival
1d
Solar vs Zoun
MaxPax vs Bunny
Kung Fu Cup
1d 2h
WardiTV Korean Royale
1d 2h
PiGosaur Monday
1d 15h
RSL Revival
2 days
Classic vs Creator
Cure vs TriGGeR
Kung Fu Cup
2 days
[ Show More ]
CranKy Ducklings
3 days
RSL Revival
3 days
herO vs Gerald
ByuN vs SHIN
Kung Fu Cup
3 days
IPSL
3 days
ZZZero vs rasowy
Napoleon vs KameZerg
BSL 21
3 days
Tarson vs Julia
Doodle vs OldBoy
eOnzErG vs WolFix
StRyKeR vs Aeternum
Sparkling Tuna Cup
4 days
RSL Revival
4 days
Reynor vs sOs
Maru vs Ryung
Kung Fu Cup
4 days
WardiTV Korean Royale
4 days
BSL 21
4 days
JDConan vs Semih
Dragon vs Dienmax
Tech vs NewOcean
TerrOr vs Artosis
IPSL
4 days
Dewalt vs WolFix
eOnzErG vs Bonyth
Wardi Open
5 days
Monday Night Weeklies
5 days
WardiTV Korean Royale
6 days
Liquipedia Results

Completed

Proleague 2025-11-07
Stellar Fest: Constellation Cup
Eternal Conflict S1

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
SOOP Univ League 2025
YSL S2
BSL Season 21
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual

Upcoming

SLON Tour Season 2
BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
HSC XXVIII
RSL Offline Finals
WardiTV 2025
RSL Revival: Season 3
META Madness #9
BLAST Bounty Winter 2026
BLAST Bounty Winter 2026: Closed Qualifier
eXTREMESLAND 2025
ESL Impact League Season 8
SL Budapest Major 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.