• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 05:55
CEST 11:55
KST 18:55
  • 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
[ASL19] Finals Recap: Standing Tall9HomeStory Cup 27 - Info & Preview18Classic wins Code S Season 2 (2025)16Code S RO4 & Finals Preview: herO, Rogue, Classic, GuMiho0TL Team Map Contest #5: Presented by Monster Energy6
Community News
Flash Announces Hiatus From ASL62Weekly Cups (June 23-29): Reynor in world title form?13FEL Cracov 2025 (July 27) - $8000 live event21Esports World Cup 2025 - Final Player Roster16Weekly Cups (June 16-22): Clem strikes back1
StarCraft 2
General
The SCII GOAT: A statistical Evaluation Statistics for vetoed/disliked maps Program: SC2 / XSplit / OBS Scene Switcher Weekly Cups (June 23-29): Reynor in world title form? PiG Sty Festival #5: Playoffs Preview + Groups Recap
Tourneys
RSL: Revival, a new crowdfunded tournament series WardiTV Mondays FEL Cracov 2025 (July 27) - $8000 live event Korean Starcraft League Week 77 Master Swan Open (Global Bronze-Master 2)
Strategy
How did i lose this ZvP, whats the proper response Simple Questions Simple Answers
Custom Maps
[UMS] Zillion Zerglings
External Content
Mutation # 480 Moths to the Flame Mutation # 479 Worn Out Welcome Mutation # 478 Instant Karma Mutation # 477 Slow and Steady
Brood War
General
Flash Announces Hiatus From ASL Player “Jedi” cheat on CSL Practice Partners (Official) ASL20 Preliminary Maps SC uni coach streams logging into betting site
Tourneys
[BSL20] Grand Finals - Sunday 20:00 CET [Megathread] Daily Proleagues Small VOD Thread 2.0 [BSL20] GosuLeague RO16 - Tue & Wed 20:00+CET
Strategy
Simple Questions, Simple Answers I am doing this better than progamers do.
Other Games
General Games
Path of Exile Stormgate/Frost Giant Megathread Nintendo Switch Thread What do you want from future RTS games? Beyond All Reason
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
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Trading/Investing Thread Things Aren’t Peaceful in Palestine The Games Industry And ATVI
Fan Clubs
SKT1 Classic Fan Club! Maru Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece [\m/] Heavy Metal Thread
Sports
2024 - 2025 Football Thread Formula 1 Discussion NBA General Discussion TeamLiquid Health and Fitness Initiative For 2023 NHL Playoffs 2024
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
Blogs
Culture Clash in Video Games…
TrAiDoS
from making sc maps to makin…
Husyelt
Blog #2
tankgirl
StarCraft improvement
iopq
Trip to the Zoo
micronesia
Customize Sidebar...

Website Feedback

Closed Threads



Active: 675 users

The Big Programming Thread - Page 815

Forum Index > General Forum
Post a Reply
Prev 1 813 814 815 816 817 1031 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
Hyrule19030 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
11803 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
11803 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
Poland17243 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
Spain17971 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 1031 Next
Please log in or register to reply.
Live Events Refresh
Next event in 5m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Tasteless 309
Crank 103
MindelVK 32
StarCraft: Brood War
Sea 9322
Horang2 2962
Bisu 793
Hyuk 606
Leta 322
Soma 276
TY 173
PianO 164
Jaedong 160
Killer 144
[ Show more ]
ToSsGirL 130
EffOrt 110
ZerO 102
Rush 47
JulyZerg 41
HiyA 23
Free 21
zelot 18
Movie 7
Sacsri 7
ajuk12(nOOB) 6
ivOry 3
Dota 2
XaKoH 745
XcaliburYe693
Fuzer 214
League of Legends
JimRising 564
Counter-Strike
Stewie2K1875
Heroes of the Storm
Khaldor284
Other Games
Happy502
Pyrionflax244
crisheroes202
ZerO(Twitch)19
Organizations
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 14 non-featured ]
StarCraft 2
• Berry_CruncH319
• LUISG 23
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• lizZardDota2339
League of Legends
• Lourlo1223
Upcoming Events
RSL Revival
5m
Clem vs Classic
SHIN vs Cure
Tasteless309
Crank 103
3DClanTV 0
FEL
2h 5m
WardiTV European League
2h 5m
BSL: ProLeague
8h 5m
Dewalt vs Bonyth
Replay Cast
1d 14h
Sparkling Tuna Cup
2 days
WardiTV European League
2 days
The PondCast
3 days
Replay Cast
3 days
RSL Revival
4 days
[ Show More ]
Replay Cast
4 days
RSL Revival
5 days
FEL
5 days
RSL Revival
6 days
FEL
6 days
FEL
6 days
Liquipedia Results

Completed

BSL 2v2 Season 3
HSC XXVII
Heroes 10 EU

Ongoing

JPL Season 2
BSL Season 20
Acropolis #3
KCM Race Survival 2025 Season 2
CSL 17: 2025 SUMMER
Copa Latinoamericana 4
Championship of Russia 2025
RSL Revival: Season 1
Murky Cup #2
BLAST.tv Austin Major 2025
ESL Impact League Season 7
IEM Dallas 2025
PGL Astana 2025
Asian Champions League '25
BLAST Rivals Spring 2025
MESA Nomadic Masters
CCT Season 2 Global Finals
IEM Melbourne 2025

Upcoming

2025 ACS Season 2: Qualifier
CSLPRO Last Chance 2025
2025 ACS Season 2
CSLPRO Chat StarLAN 3
K-Championship
uThermal 2v2 Main Event
SEL Season 2 Championship
FEL Cracov 2025
Esports World Cup 2025
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1
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.