• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 07:14
CET 13:14
KST 21:14
  • 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
ByuL: The Forgotten Master of ZvT12Behind the Blue - Team Liquid History Book16Clem wins HomeStory Cup 289HomeStory Cup 28 - Info & Preview13Rongyi Cup S3 - Preview & Info8
Community News
Weekly Cups (Feb 9-15): herO doubles up2ACS replaced by "ASL Season Open" - Starts 21/0224LiuLi Cup: 2025 Grand Finals (Feb 10-16)46Weekly Cups (Feb 2-8): Classic, Solar, MaxPax win2Nexon's StarCraft game could be FPS, led by UMS maker15
StarCraft 2
General
ByuL: The Forgotten Master of ZvT How do you think the 5.0.15 balance patch (Oct 2025) for StarCraft II has affected the game? Weekly Cups (Feb 9-15): herO doubles up SpeCial on The Tasteless Podcast Nexon's StarCraft game could be FPS, led by UMS maker
Tourneys
PIG STY FESTIVAL 7.0! (19 Feb - 1 Mar) LiuLi Cup: 2025 Grand Finals (Feb 10-16) Master Swan Open (Global Bronze-Master 2) WardiTV Team League Season 10 $5,000 WardiTV Winter Championship 2026
Strategy
Custom Maps
Map Editor closed ? [A] Starcraft Sound Mod
External Content
Mutation # 513 Attrition Warfare The PondCast: SC2 News & Results Mutation # 512 Overclocked Mutation # 511 Temple of Rebirth
Brood War
General
Gypsy to Korea TvZ is the most complete match up Ladder maps - how we can make blizz update them? Brood War inspired Terran vs Zerg cinematic – feed Which units you wish saw more use in the game?
Tourneys
Escore Tournament StarCraft Season 1 [Megathread] Daily Proleagues Small VOD Thread 2.0 KCM Race Survival 2026 Season 1
Strategy
Simple Questions, Simple Answers Fighting Spirit mining rates Zealot bombing is no longer popular? Current Meta
Other Games
General Games
ZeroSpace Megathread Diablo 2 thread Nintendo Switch Thread Path of Exile Battle Aces/David Kim RTS Megathread
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 Mafia Game Mode Feedback/Ideas Vanilla Mini Mafia
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Ask and answer stupid questions here! Things Aren’t Peaceful in Palestine European Politico-economics QA Mega-thread
Fan Clubs
The IdrA Fan Club The herO Fan Club!
Media & Entertainment
[Req][Books] Good Fantasy/SciFi books [Manga] One Piece Anime Discussion Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion TL MMA Pick'em Pool 2013
World Cup 2022
Tech Support
TL Community
The Automated Ban List
Blogs
The Search For Meaning in Vi…
TrAiDoS
My 2025 Magic: The Gathering…
DARKING
Life Update and thoughts.
FuDDx
How do archons sleep?
8882
StarCraft improvement
iopq
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1735 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
Hyrule19193 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
12027 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
12027 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
Poland17664 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
Spain18216 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
WardiTV Winter Champion…
12:00
Group A
WardiTV327
IndyStarCraft 8
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Lowko277
SortOf 163
ProTech120
IndyStarCraft 8
StarCraft: Brood War
Sea 15379
GuemChi 2391
Horang2 1682
PianO 1261
Bisu 1239
firebathero 395
Larva 373
Stork 342
BeSt 305
Mini 259
[ Show more ]
EffOrt 215
Last 210
Pusan 182
ggaemo 161
Shuttle 157
Light 156
Snow 152
Soma 104
Zeus 102
Rush 73
Backho 65
hero 63
Sea.KH 57
Mong 51
JulyZerg 49
ajuk12(nOOB) 39
Barracks 34
JYJ 34
NaDa 32
Free 30
910 28
Sacsri 23
Shinee 20
Movie 20
ZergMaN 18
sorry 18
Terrorterran 17
IntoTheRainbow 16
Noble 15
GoRush 14
Bale 14
Purpose 13
HiyA 11
SilentControl 9
Icarus 7
Aegong 1
Dota 2
Gorgc3691
XaKoH 548
Dendi424
Counter-Strike
olofmeister1787
zeus1130
byalli317
edward95
Super Smash Bros
Mew2King99
Other Games
singsing1708
Liquid`RaSZi1129
B2W.Neo327
crisheroes230
Fuzer 203
RotterdaM76
Trikslyr25
Organizations
Counter-Strike
PGL17499
StarCraft: Brood War
UltimateBattle 93
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 13 non-featured ]
StarCraft 2
• 3DClanTV 9
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• escodisco84
• iopq 7
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Upcoming Events
Replay Cast
11h 47m
PiG Sty Festival
20h 47m
Maru vs Bunny
Classic vs SHIN
The PondCast
21h 47m
KCM Race Survival
21h 47m
WardiTV Winter Champion…
23h 47m
OSC
23h 47m
Replay Cast
1d 11h
PiG Sty Festival
1d 20h
Clem vs Percival
Zoun vs Solar
Epic.LAN
1d 23h
Replay Cast
2 days
[ Show More ]
PiG Sty Festival
2 days
herO vs NightMare
Reynor vs Cure
CranKy Ducklings
2 days
Epic.LAN
2 days
Replay Cast
3 days
PiG Sty Festival
3 days
Serral vs YoungYakov
ByuN vs ShoWTimE
Sparkling Tuna Cup
3 days
Replay Cast
4 days
Replay Cast
4 days
Wardi Open
4 days
Monday Night Weeklies
5 days
Replay Cast
5 days
WardiTV Winter Champion…
5 days
WardiTV Winter Champion…
6 days
Liquipedia Results

Completed

C-League Week 31
LiuLi Cup: 2025 Grand Finals
Underdog Cup #3

Ongoing

KCM Race Survival 2026 Season 1
WardiTV Winter 2026
Nations Cup 2026
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
eXTREMESLAND 2025
SL Budapest Major 2025

Upcoming

Escore Tournament S1: King of Kings
[S:21] ASL SEASON OPEN 1st Round
[S:21] ASL SEASON OPEN 1st Round Qualifier
Jeongseon Sooper Cup
Spring Cup 2026: China & Korea Invitational
[S:21] ASL SEASON OPEN 2nd Round
[S:21] ASL SEASON OPEN 2nd Round Qualifier
HSC XXIX
uThermal 2v2 2026 Main Event
Bellum Gens Elite Stara Zagora 2026
RSL Revival: Season 4
PiG Sty Festival 7.0
BLAST Rivals Spring 2026
CCT Season 3 Global Finals
FISSURE Playground #3
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
BLAST Open Spring 2026
ESL Pro League Season 23
ESL Pro League Season 23
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.