Java OOP SHORT
ANSWER QUESTIONS INSTANCE
METHODS QUESTIONS
a)
The
three main components of classes are __________, __________ and ___________. b)
What
is the difference between instance methods and static methods in terms of the
method header (method prototype)? c)
What
is the difference between instance methods and static methods in terms of
calling the method? d)
What
is the difference between instance methods and static methods in terms of the
data (variables) they have access to? e)
Consider
the class below. Write the line of
code that will create an instance of type Thing named ted. public class Thing { public String stuff; public int amount; } f)
Consider
the Thing class above. Write the
instance method thatsOdd() that will output the content of stuff if the value
of amount is an even number. However,
when amount is an odd number, the method should output “That’s odd”. g)
Call
the thatsOdd() method from the previous question on the Thing object named
ted. h) The class below allows one to create
objects that represent squares. Add
the instance method named area() that returns the area of the square. public class Square { public double sideLength; public Square(double tsl) { sideLength = tsl; } } i)
Consider
the Vote class below. It allows you to
create a Vote object that holds two voting options. Over the next few questions, you will add
functionality to make it track votes for the different options. public class Vote { public String option1; public String option2; public int count1; public int count2; public Vote(String op1, String op2) { option1 = op1; option2 = op2; count1 = 0; count2 = 0; } } j)
Using the Vote class from above, create a Vote object with the
options “Donald Trump” and “Joe Biden”.
You can also use other options if you prefer. k)
Inside the class Vote, write the instance method called
outputOptions that will demonstrate the voting options. The outputted statements should look like:
l)
Inside
the class Vote from above, add the instance method named voteFor(int op) that
will get a value of 1 or 2. The method
then increases the count for the corresponding option. m)
Again
inside the Vote class, add a results method that will output the current
results of the vote. It should look
something like this: n)
Still
inside the Vote class, add a winner method that will return the winner based
on the current results. The method can
return either option1, option2 or the word “tie” in the case that the result
is a tie. SOLUTIONS Click here. |
|