• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 19:56
CEST 01:56
KST 08:56
  • 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
Serral wins HomeStory Cup 2914Serral wins Maestros of the Game 243ByuL, and the Limitations of Standard Play3Team Liquid Map Contest #22: Results and Winners7Code S Season 2 (2026): RO4 and Finals Preview12
Community News
Balance hotfix patch 5.0.16b (July 16)35Reynor: GSL Loss Wasn't About Preparation Format16[IPSL] Spring 2026 Grand Finals - This Weekend!5Weekly Cups (July 6 - 12): Protoss strike back12BSL Season 22 Full Overview & Conclusion8
StarCraft 2
General
Balance hotfix patch 5.0.16b (July 16) [D] Wireframe Casting Removed Clem: "I don't have that much hope in Blizzard" Reynor: GSL Loss Wasn't About Preparation Format Is the larve respawn broken?
Tourneys
Master Swan Open (Global Bronze-Master 2) WardiTV Summer Cup 2026 GSL CK #5 Race War RSL Revival: Season 6 - Qualifiers and Main Event HomeStory Cup 29
Strategy
[G] Having the right mentality to improve
Custom Maps
New Map Maker - Looking for Advice - Love or Hate Work In Progress Melee Maps [D]RTS in all its shapes and glory <3
External Content
The PondCast: SC2 News & Results Mutation # 534 Burning Evacuation Mutation # 533 Die Together Mutation # 532 Nuclear Family
Brood War
General
Etiquete rules in Asl? BW General Discussion Recent recommended BW games Recommended FPV games (post-KeSPA) Pros Debate: Zerg Unfairly Nerfed? (ASL S22 map)
Tourneys
Escore Tournament - Season 3 Small VOD Thread 2.0 [IPSL] Spring 2026 Grand Finals - This Weekend! [Megathread] Daily Proleagues
Strategy
Fighting Spirit mining rates Simple Questions, Simple Answers Creating a full chart of Zerg builds Relatively freeroll strategies
Other Games
General Games
General RTS Discussion Thread Path of Exile Nintendo Switch Thread Beyond All Reason Stormgate/Frost Giant Megathread
Dota 2
Looking for a Dota Mentor 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
TL Mafia
TL Mafia Power Rank NeO.D_StephenKing vs This Guy From 1 Million Dance TL Mafia Community Thread Vanilla Mini Mafia
Community
General
US Politics Mega-thread The Games Industry And ATVI Russo-Ukrainian War Thread UK Politics Mega-thread YouTube Thread
Fan Clubs
The IdrA Fan Club The HerO Fan Club!
Media & Entertainment
Movie Discussion! Anime Discussion Thread [Req][Books] Good Fantasy/SciFi books Series you have seen recently...
Sports
2024 - 2026 Football Thread MLB/Baseball 2023 McBoner: A hockey love story Tennis[sport] Formula 1 Discussion
World Cup 2022
Tech Support
Simple Questions Simple Answers FPS when play League Of Legend on laptop How to clean a TTe Thermaltake keyboard?
TL Community
Northern Ireland Global Starcraft The Automated Ban List
Blogs
Poker (part 2)
Nebuchad
The Experiences We Want and …
TrAiDoS
An Exploration of th…
waywardstrategy
ramps on octagon
StaticNine
Funny Nicknames
LUCKY_NOOB
Evil Gacha Games and the…
ffswowsucks
StarCraft improvement
iopq
Customize Sidebar...

Website Feedback

Closed Threads



Active: 7280 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
Hyrule19228 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
12121 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
12121 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
Poland17794 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
Spain18375 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
PSISTORM Gaming Misc
23:00
FSL playoffsTeamLeague STvsASH
Freeedom30
Liquipedia
The PiG Daily
22:00
Best Games of SC
ByuN vs Clem
MaxPax vs TBD
Serral vs ByuN
GgMaChine vs Bunny
PiGStarcraft535
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
PiGStarcraft535
ViBE234
Vindicta 30
SpeCial 27
StarCraft: Brood War
Leta 18
Dota 2
NeuroSwarm137
Counter-Strike
summit1g3577
Super Smash Bros
hungrybox295
Mew2King145
Heroes of the Storm
Trikslyr76
Other Games
gofns9802
tarik_tv5432
shahzam1163
XaKoH 164
ToD133
PPMD25
Organizations
Other Games
gamesdonequick1664
BasetradeTV182
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
[ Show 18 non-featured ]
StarCraft 2
• musti20045 38
• davetesta31
• RyuSc2 16
• Response 1
• LaughNgamezSOOP
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• sooper7s
• Migwel
StarCraft: Brood War
• Pr0nogo 1
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• masondota21742
Other Games
• Shiphtur790
• tFFMrPink 9
Upcoming Events
Replay Cast
4m
CranKy Ducklings1
RSL Revival
9h 4m
Clem vs Lambo
Scarlett vs Cure
CranKy Ducklings
10h 4m
Epic.LAN
13h 4m
IPSL
16h 4m
Dragon vs Hawk
RSL Revival
1d 9h
Classic vs Trap
herO vs SHIN
Sparkling Tuna Cup
1d 10h
OSC
1d 13h
IPSL
1d 16h
Bonyth vs Ret
WardiTV Weekly
2 days
[ Show More ]
Monday Night Weeklies
2 days
PiGosaur Cup
4 days
The PondCast
4 days
Replay Cast
5 days
CrankTV Team League
5 days
Replay Cast
6 days
CrankTV Team League
6 days
Liquipedia Results

Completed

Proleague 2026-07-13
HSC XXIX
Eternal Conflict S2 E2

Ongoing

IPSL Spring 2026
Acropolis #4
YSL S3
CSL 2026 Summer (S21)
KCM Race Survival 2026 Season 3
Escore Tournament S3: W3
RSL Revival: Season 6
CranK Gathers Season 4: BW vs SC2 Team League
SCTL 2026 Spring
Stake Ranked Episode 3
XSE Pro League 2026
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
Asian Champions League 2026
IEM Atlanta 2026
PGL Astana 2026

Upcoming

ASL S22 SEASON OPEN Day 1
Escore Tournament S3: W4
ASL S22 SEASON OPEN Day 2
Escore Tournament S3: W5
CSLAN 4
Blizzard Classic Cup 2026
HSC XXX
SC4ALL II: StarCraft II
Kung Fu Cup 2026 Grand Finals
Light Tournament 2026
Eternal Conflict S2 Finale
Eternal Conflict S2 E3
Logitech G Connect 2026
StarSeries Fall 2026
FISSURE Playground #5
BLAST Open Fall 2026
Esports World Cup 2026
BLAST Bounty Summer 2026
BLAST Bounty Summer Qual
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 © 2026 TLnet. All Rights Reserved.