1
|
List two ways an interface class
is different from an abstract class?
The correct answer is:
Interfaces have no instance variables defined
Abstract classes can implement ( have full code for) some or all
methods
Abstract classes require the word "abstract" in their
abstract method headers
Abstract classes use the word class in their class header and
interfaces do not (abstract class X{} vs interface
X{})
In the class that uses the interface or abstract class:
- In the class header, you use the word
"implement" to include an interface and "extends" to
include an abstract class
- You can implement many interfaces but
only extend one class.
Note: Even though an abstract class can have a constructor
method, it cannot be constructed as an object. So interfaces and abstract
classes both CANNOT be created as objects on their own.
Update
on Java 8: Java 8 now allows interfaces to include default methods which do
define and fully code a method. They also now allow static methods to be
defined in an interface. Default methods use the keyword
"default". We have been
learning Java 7 so this Java 8 feature will not be considered on the test.
#
|
Responses from the class
|
1
|
Multiple interfaces can be
extended. Interfaces cannot have implementations, only declarations.
|
1
|
interface "contracts" on implementation abstract->
resource : Correct. I will just extend this
meaning: When you are in a class that uses an interface or abstract class,
you only get contracts for what you have to implement from the interface
while you get code (resources) from the abstract class that will save you
typing in your own class.
|
1
|
All methods inside of an
interface is abstract. You cannot create variables
in an interface. Correct I you say instance
variables. You can actually create class constants inside an interface, but
we have not done that in our exercises.
|
1
|
An interface class is implemented
by programs and abstract classes are extended. That
was correct. Interfaces provide information to the accessing code
while abstracts ask for the information to be provided. That is not correct. Both an interface method and an
abstract method inside an abstract class ask for the "information to
be provided". I think you mean that the method has to be coded by
"information to be provided".
|
1
|
One way is that an interface
class can be accessed by other classes. While it is
true that many classes can implement the same interface, many classes can
also extend the same abstract class.
|
1
|
I dont
know
|
1
|
An interface is more of a
"contract" in that any classes the implements the interface must define
all methods listed in the interface. An abstract class, however, gives you
the freedom of using the methods that already exist, the only issue being
that you have to define any abstract methods present. Furthermore you
implement interfaces and extend abstract classes.
|
1
|
You cannot extend an interface,
only implement. Correct. You can create
objects of an abstract class, not interface. This
is not correct. An abstract class cannot have an object constructed. This is
true even though an abstract class can have a constructor. That constructor
is only used as code to be extended and used by its subclass. The only time
that constructor will ever be used will be as "super()"
inside the subclass.
|
1
|
1) Methods can only be declared
in an abstract class, while it can be defined in an interface 2) An I think you have
it reversed. "Declare" means writing the method header.
"Define" means writing the code inside the method. To say it
differently, Inside an abstract class, methods can be either abstract with
only a method header declared or methods can be fully implemented with both
the method header declared and the method code defined. Inside an
interface, methods are only declared with the method header and cannot have
any code defining them.
|
1
|
1)In abstract class, you can write what a method does, in a interface you cannot. 2)In a abstract class, when making a
abstract method, you have to include the word "abstract" in the
method header, but you don't have to in interfaces.
|
1
|
All the interface methods have
to be used when an interface is implemented. Correct,
but when you use an abstract class, you still have to implement ALL
abstract methods. An abstract
class needs a header and it has abstract methods and normal methods which
are inherited. Correct, but both abstract class and
interface classes have class headers. The interface class header just has
the word "interface" replacing "class".
|
1
|
interface is like a blueprint for the other classes with promises
that the other classes will use those methods that are declared in the
interface. Correct, but I have been using blueprint
as a the class with instance variables that you
intend to use to make objects. I have been using blueprint to include all
the methods fully defined, so I don't like using blueprint for interface.
Contract is a better word. abstract class
is a basis If you mean that abstract class is the
starting code for a subclass, that is good.
|
1
|
abstract classes is different in that it can be used without
static. That is not correct. Static just means that the method can do
its job without knowing anything about the object, so that "this"
is not needed. Static classes cannot
be overridden because they are associated with the class, not the object.
This makes it a bit complicated if you create the same static method in an
inherited class. The static method will not follow the polymorphic method
overriding behavior that non-static methods follow. I will not give any
questions on the midterm involving static methods and inheritance.
|
1
|
In Abstract classes you can
define a behavior for them. You cannot create an object.
|
|
2
|
When you are coding a blueprint class
for an object, you can usually address the object's instance variables
as this.varname. When is one time you cannot
address the object's instance variables as this.varname?
Will using super. instead of this. help?
You cannot use "this" in a static method.
You cannot use "this" to access an instance variable
that is declared as a private instance variable inside a class you are
extending.
You can never use "super" to replace "this"
because "super" is used to reach into the superclass (the class
that is extended) to get methods, not variables. However, if the superclass
had a getVarname method, you could use super.getVarname to get the variable.
#
|
Response
|
1
|
You can't address the instance
variables in an extended class, instead you pass them to the original
constructor using super. Somewhat correct, but
might show a misunderstanding. You cannot address PRIVATE instance
variables of the superclass (the class that is extended). In your sub-class
constructor, you are very likely to have to take in those private
variable's values as a parameter to your constructor, and then give it to
the superclass constructor. You will call the superclass constructor using super( ).
|
1
|
When you are getting something
to the levvel above. This
is true only when the superclass declares the instance variable as private.
|
1
|
Inside of the main method. This is partially correct because main is static, and you
cannot access "this" inside a static method ever. However, I was
speaking of a blueprint class, one that you intend to use to create an
object. We don't usually put main methods inside those classes, so you
might not be talking of the same thing the question is asking. It is legal
to put a main method inside a blueprint class, but we don't usually do that
in this course.
No it will not because super. is only used inside of an extended class to access a
part of the parent class. The question did assume
you were inside the extended class. When you say that super gives access to
the parent class, say instead that it gives access the extended class's
methods.
|
1
|
you cannot use this.varname when
the variable is private and does not get declared in the class itself but
gets implemented or extended in some way. Correct.
If this is being used in a constructor that extends the one with the
variable, you can use super.varname. You cannot ever use super.varname.
. In your sub-class constructor, you are very likely to have to take in
those private variable's values as a parameter to your constructor, and
then give it to the superclass constructor. You will call the superclass
constructor using super( ).
|
1
|
One time is when you are
referencing a class that is the parent of the class you are in. Using
super. (the variable name) will help by letting
you change the class's variable you are in but won't help you in changing
the parent's. You cannot ever use super.varname.
|
1
|
Polymorphism may require some classes
to use super. in order to store instance variables
in another class. You cannot ever use super.varname.
|
1
|
You wouldn't be able to use this.varname in a child class if the instance variables
in the parent class are private. In this case, you would need to have mutator methods to help you change those fields. So no,
using super wouldn't help either. Very good answer.
|
1
|
yes, super will help, when the object is abstract.. Super is used to access methods when you extend a class.
While we often extend abstract classes, we can extend any class. Super only
helps with methods, not variables. Creating a method in the superclass to
return the variable will help though.
|
1
|
In a static class/method Super
would not help. This is partially correct because you cannot access
"this" from static methods.
However, I was speaking of a blueprint class, one that you intend to
use to create an object. We seldom put main methods inside those classes,
so you might not be talking of the same thing the question is asking. It is
legal to put a static method inside a blueprint class, but we just do not
often create them in this course.
|
1
|
If it's an inherited variable.
If the super class has a getter method, to get the variable.
|
1
|
If the super class has a getter
method, then you can address the instance variable, because you cannot Directly address an instance variable that is
being inherited.
|
1
|
You cannot address the instance
variables when they are being inherited if they are
declared as private by the superclass. Using super. will help if the super class has a getter method. This means you can use super.getter
method, but not super.varname.
|
1
|
no
|
1
|
In an Abstract Class. Super will
only look in the class above itself. Super is used
to access methods when you extend a class. While we often extend abstract
classes, we can extend any class. Super only helps with methods, not
variables. Creating a method in the superclass to return the variable will
help though.
|
|
3
|
What does it mean to pass a value
to a method by reference? How do you make your program pass a value by
reference to a method? And, how so you make your program pass a value by
value to a method?
Pass by reference or value explains whether the values you are
sending can be changed in the calling method by the method called.
·
Passing by
reference means that the changes to a variable made inside the method will effect the variable value
passed.
·
Passing by value
means that the changes made inside the method will not effect the variable value passed.
Some technical details:
·
Passing by reference : the
address of the data is being passed so that if you do not construct a new
object, all changes happening inside the method effect the passed variable.
·
Passing by value:
the actual data is being passed so that all the changes happening inside the
method do not effect the passed variable.
Which variable types will pass which way and how to do change
that?
·
Primitives pass by
value. (Examples of primitives are int, char, double) When you want to pass them by
reference, create an object that contains them, such as Integer and pass the
object.
·
Everything else
passes by reference. When you want to not effect the parameter's value, first use the object
passed to create a new object. (ex: myparm = new Whatever(myparm.getConstructorParmValue);
Exception:
·
Strings are immutable
so work as pass by value (even though a reference is actually passed). If you
put a new value into a String, it automatically creates a new object, so
changes to Strings only effect the method, not the
calling method. Without you controlling anything, Strings work as though they
initially passed by value.
#
|
Response
|
1
|
pass by reference means the original data will not be altered,
this is the case with primitive variable types. Objects are passed by
value.
|
1
|
you tell where something is located This answer is not what I was trying to get at.
|
1
|
For pass by value: The actual value (a copy of the
actual value) is passed to the method. For
Pass by Reference: if it is
changed in the method it is changed everywhere. You can place it inside of
a List to pass by reference. Pass it as a primitive data type to pass by value.
|
1
|
Passing a value by reference means
that rather than sending the value itself, data pointing to where the value
is located in memory is sent instead. Certain implemented tools will send
data like this. ArrayLists are an example. To
send the value, you can have the code (inside the
method) create a separate array with all the same data. Good answer
|
1
|
It means to use a method by
giving it a certain value the method can then take in. You can pass the
values by putting them in the parentheses in the method call. These are
called parameters. You correctly described how to
pass values to methods, but Pass by Value and Pass by Reference are terms
referring to whether the contents of the parameter sent will be changed by
the method called.
|
1
|
I don't know
|
1
|
If you declare and initialize
one object variable and then declare another variable of the same type and
initialize it to the old one, you are technically passing a value by
reference. (You are creating one variable that
points to another variable. Passing implied the method call while you are
describing how references are used when you create variables. You are
describing the use of the reference correctly though. ) To pass a
value by reference to a method, you could declare a variable to store that
value and then pass the variable into a method as an explicit parameter,
not the actual value itself. If you passed the value and not the variable
containing the value, that's an example of passing a value by value to a
method. Pass by value does not mean passing a literal as opposed to a
variable. Instead it refers to whether the values you are sending can be
changed in the calling method by the method called.
|
1
|
you are pointing to the value (holding the address), pass over
the address, create an object for reference and clone or copy, make a
primitive for value ex. method (int x, Toy t) This relates to references but does not answer the
question.
|
1
|
By passing reference, you are
granting access to one particular object to the method. Simple do this by
including a parameter with the object type. To pass a value, simple create
a parameter that takes in a primitive type.
|
1
|
You give the method the an object, which it can edit and change everywhere
in the program. You give the method call an object for its parameters. You
make the parameter a primitive rather than an object.
|
1
|
if you mean parameter passing then method(type paramater) for example public void (int
x, int y) { things } This
method will pass by value because the method takes in primitive data types.
|
1
|
pass a value to a method by reference is a direct location
to the value that will change altogether, by referring to the original
value rather than copying it, you pass a value to a value by copying
it. You may
be talking about creating objects versus copying their references inside
one method, but the question refers to parameters passed to a method.
|
|
4
|
I have a file named
"person.txt" that I want to read. I will start with File f = new
File("person.txt");
Did I have to create a File object
in order to read from the file? Yes. I could have created an anonymous File object by creating
the object inside the Scanner parameter as in Scanner x = new Scanner(new
File("x"));
Can I use the next()
method on my File object to read the file? No. The File
object does not have a next method.
If I wanted to read this file and
write to the same file, as well as reading from the terminal screen, what
objects would I need to create?
·
A Scanner pointing
to System.in to read from the terminal screen
·
A Scanner pointing
to the file to read from the file
·
A PrintStream pointing to the file to write to the file
Explanation of delimiters:
·
Delimiters
determine what separates data. That separated data is often called tokens. So
lastname,firstname is
comma delimited with lastname as the first token
and firstname as the second token.
·
Scanner defaults
to using space as its delimiter. You can change that default delimiter using
scanner's useDelimiter method.
·
Scanner's next and
nextInt methods stop at the delimiter.
#
|
Response
|
1
|
1) yes
2) if you set the delimiter 3) scanner, printsomething
??? See the answer above. Delimiter is not related to
this question but is explained in the answer.
|
1
|
No you can create a scanner and
simply put the file directly in to the constructor for the scanner without
storing it inside of a File variable. (though that really does create a file object, so I would
say that yes, you did create a file object.) No that requires a scanner object
that is reading from the file. To read you would require a scanner, to
write you would require a PrintStream object, and
to read from the terminal screen you require another scanner that is
reading from the terminal screen.
|
1
|
Yes you had to create the file
object. You can use next(), that would involve
using Scanner. For writing, you would need to use PrintStream.
|
1
|
Yes you had to establish the existence
of the file inside of the class. No you need to create a scanner that can
read the file.In order to write to the file you
will need a print screem. PrintStream
|
1
|
A file object isn't
necessary--you could just feed the file directly into a Scanner object. (though that really does create
a file object, so I would say that yes, you did create a file object.) You could then use the next()
method on the Scanner. If you wanted to read the file, write to a file, and
then take input from the terminal screen, you would need to declare two
Scanners (for input from screen and to read a file), and a PrintStream object (to write to the file).
|
1
|
scanner object Please refer to the
answer above.
|
1
|
You need a Scanner object to read
from a file You cannot use next() directly on the file Create Scanner and PrintStream objects
|
1
|
Yes, you needed to create a file
object tor read from it. You have to give the
file to a scanner, and then use the method on the scanner object. A Printstream object.
|
1
|
No you did not have to create a
File object in order to read from the file. (Yes,
it is necessary to create the file object.)
yes you can. you would need a printstream object, scanner object. (Two scanners.)
|
1
|
You can just create a Scanner
object to read from the file. (though
that really does create a file object, so I would say that yes, you did
create a file object.) You cannot use the next method on the File
object to read the file. You would need to create a File object, Scanner
object, and a PrintStream. You need 2 scanners.
|
1
|
You do not have to create a file
object in order to read from the file. . (Yes, it
is necessary to create the file object.)
You cannot use the next() method on
the File object to read the file. You would have to create scanner and printstream objects in order to read the file and then
write on the same file. Plus one more scanner for
the screen.
|
1
|
scanner input - new scanner (new file
("something.txt")); to construct a scanner and read yes you can
use hasNext whether it is int
or string to read froma fil.
|
1
|
Yes, a File object was created
to read from person.txt. next() should not be used
on the File itself, but should be read by a Scanner before using next(). To
read and write from a file, necessary objects would be File, Scanner, and FileStream. Plus one more
scanner for the screen.
|
1
|
No you didn't have to create a
File object in order to read from the file. Yes, it
is necessary to create the file object.)
No you can't use the next()method.
You could create: Scanner input = System.in(new
File("person.txt")); (though that really
does create a file object, so I would say that yes, you did create a file
object.)
|
1
|
Yes you have to create a file
object to read from the file, Yes you can use the next(); method, you would
need to create scanner for reading input, an input file to read from and an
output file to write to. The question speaks about
the same file, so you would need only one file, but 2 objects – scanner and
printstream. plus one
more scanner for the screen.
|
|
5
|
Why would I ever want to use a
linked list instead of an arraylist?
·
I would use a
linked list instead of an arraylist if I was planning
on often removing elements earlier than the last element from the array.
·
If I was always
planning to process the list from the first element to the last, I might use
a linked list.
·
Note: Arraylist has fastest access to a particular index
Why would I ever want to use a set
instead of a linked list or arraylist?
·
If I did not need
duplicates and did not care about the entry order or index of my list, I
would use a set for the fast access when searching for a value.
·
If I wanted to
have a list of one occurrence of every value in my list, I would copy the list to a set.
·
Note: Arraylist has fastest access to a particular index
#
|
Response
|
1
|
They each have their pros and
cons for optimization of data (memory) and
CPU usage
|
1
|
1) if
you are going to go in order 2) the antithesis of one (You are correct about sets not having any sense of index
or entry order. However, even if you did not care about order, you cannot
use a set when you have duplicates.)
|
1
|
A linkedlist
is less intensive when removing or adding things inside of a List that
contains many variables because it does not have to account for every
single variable (move every value after the removed
value) when removing. You would use a set when you want your data to
be ordered or not contain any duplicate values.
|
1
|
LinkedLists run through data faster then ArrayLists, (A linked list is
only as fast to navigate as arraylist when you
are starting from the beginning and using an iterator. Otherwise, an arraylist is faster to navigate or get to an exact
location or value than a linked list.) but ArrayLists save memory. A set saves memory and is
fairly efficient and doesn't allow duplicate values. You can think of set, arraylist
and linkedlist as taking the same memory though
there are small differences. In our class examples, we used extra memory by
creating whole new sets or lists with similar information rather than
modifying the existing lists. That was the real extra memory.
|
1
|
LinkedList is faster for processing, while a set is easier to use
for small arrays. See the answer
|
1
|
I don't know
|
1
|
You would use a linked list
instead of an arraylist if you will be removing
items from the list (at the beginning or middle of
the list). LinkedLists will simply create
a bridge over the gap you create, whereas ArrayLists
have to patch up the hole you create. You would want to use a Set if you
will only be storing unique values. Sets will automatically delete
duplicate items, so it would be better than having to sort and then loop
through a List and delete all duplicate values. good
|
1
|
a linked list makes it easier to pull out values without
shifting consecutive values, which happens in arraylists.
sets eliminate duplicates
|
1
|
Linked list over arraylist when you are removing from the beginning of the
list, and the indexes would have to shift. Set instead of any list to
remove duplicates.
|
1
|
When you want to remove
something from near the beginning, or add something near the beginning.
SET: When you want their to be only unique values inside it.
|
1
|
it is easier to go through a linked list instead of an arraylist. (See answer) you
would use a set in order to get rid of multiples and have it order it for
you.(Hashset has no
defined order and TreeSet orders by the object's compareto method)
|
1
|
A linked list is easier to
remove and add objects rather than an array list. at the beginning or middle of the list). Sets are more efficient at displaying
unique objects. (as long as you are not getting at
a particular index)
|
1
|
A linked list allows you to
remove and add objects with more ease than arraylists.
at the beginning or middle of the list). Sets remove
all duplicates, which can come in handy when you do not want more than one
of each element in the collection.
|
1
|
linked lists are just too 2gud for sorting arraylists
are not. Neither is always better for sorting.
|
1
|
They all require less processing
time under different circumstances. For example, linked lists work better
if the values inside the list are expected to change a lot, while ArrayLists work best if none of the values (except
perhaps the last one) are expected to be deleted. true
|
1
|
Easier Sorting. Neither is always better for sorting.
|
1
|
An array list could take too
long with larger values (length of the values is
not an issue) as well as shifting all values down in the list where
a linked list holds places of values.
|
|
6
|
1. Write just the class headers to
model this relationship: (On paper, write the uml diagram,
without any variables or methods)
A Dog is a Pet. Dogs implement the
Carnivore Interface. A Rabbit is a Pet. Rabbits do not implement the
Carnivore interface. A pet in general cannot exist, but a Pet type, such as
dog or rabbit can. Write the class header for Dog,
Carnivore, Pet and Rabbit.
·
class Dog
extends Pet implements Carnivore
·
class Rabbit
extends Pet
·
interface
Carnivore
·
abstract
class Pet
2. Can I put a Dog into a Rabbit
variable? No
3. Can I put a Pet into a Dog
variable? No
4. Can I put a Dog into a Pet
variable? Yes
5. Can I put a Pet into a Pet
variable? No, because I cannot create a Pet. It is an abstract class.
6. Could I create a list of Pets
and put two dogs and two rabbits inside the list? Sure
#
|
Response
|
1
|
class Dog extends Pet implements
Carnivore class Rabbit extends Pet interface Carnivore abstract class Pet
no no yes no yes
|
1
|
2) no 3)no 4)yes 5)no 6)yes
|
1
|
public class Dog extends Pet implements Carnivore public class
Rabbit extends Pet Yes.
|
1
|
1. public class Dog implements
Carnivore(){ public class Carnivore(){ public class Pet(){ public class
Rabbit implements Pet(){ 2. No 3. No 4. Yes 5. Yes 6. Yes see answer
|
1
|
2. no
3. no 4. yes 5. no 6. yes
|
1
|
1) public
class Dog extends Pet implements Carnivore public class Rabbit extends Pet
public interface Carnivore public abstract class Pet 2) You cannot put a
Dog into a Rabbit variable because a Rabbit variable can only store a
Rabbit object, and a Dog is not a Rabbit. 3) You cannot put a Pet into a
Dog variable because a Dog variable will only hold a Dog object, and a Pet
may not necessarily be a Dog. 4) You can put a Dog into a Pet variable
because a Pet variable can hold a Dog variable and a Dog is a Pet. 5) You can
put a Pet into a Pet variable. Pretty straight forward but abstract Pet so no. 6) Yes, you could put two
Dogs and two Rabbits in a list of Pets because Dogs and Rabbits, in this
case, are Pet objects.
|
1
|
public abstract class Pet implements Carnivore() public class
Dog extends Pet() public class Rabbit extends Pet() 2. no
3. yes 4. yes 5. no 6. what? see answer
|
1
|
1) public interface Carnivore
public abstract class Pet public class Dog extends implements Carnivore see answer
|
1
|
1) public class Dog extends Pet
implements Carnivore public interface Carnivore public abstract class Pet
public class Rabbit extends Pet 2) No 3) No 4) Yes 5) No 6) Yes
|
1
|
2. no
3. no 4. yes 5. yes 6. yes see answer
|
1
|
2. No 3. Yes 4. Yes 5. No 6. Yes
see answer
|
1
|
public class dog implements
Carnivore { methods(); } public class Rabbit { methods(); }` see answer
|
1
|
public class Dog extends Pet implements Carnivore public
Interface Carnivore public abstract class Pet public class Rabbit extends
Pet 2. A Dog cannot be put into a Rabbit variable. 3. A Pet cannot be put
into a Dog variable. 4. A Dog can be put into a Pet variable. 5. A Pet can
be put into a Pet variable. 6. Yes, two dogs and two rabbits could be put into
a list of Pets. see answer
|
1
|
2. No 3. No 4. Yes 5. Yes 6. Yes
see answer
|
1
|
public dog implements carnivore() forgot
to extend pet; public pet implements dog() the
dog extends the pet, while the pet knows nothing about the dog; public
rabbit implements pet(); You cannot put a dog into a rabbit variable. you can put a pet into a dog variable. you cannot put a dog into a pet variable. you can put a pet into a pet variable. yes i could create a list of
pets and but 2 dogs and rabbits in them because they implement Pets. see answer
|
|
7
|
Describe 3 different ways to
iterate through an Arraylist. Iterate through a
list means to examine the list items one at a time.
For this answer, assume my list is
created with: ArrayList<String>
a = new ArrayList<String>();
Use a for loop with a counter:
example: for (int c = 0; c < a.size(); c++) { do something
with a.get(c); }
Use a for each loop:
example: for (String myItem : a) { do
something with myItem;} (note that myItem is the same thing as a.get(c).)
Create an iterator:
Iterator<String> itr = a.iterator();
while (itr.hasNext()){
String myItem = itr.next();
do something with myItem or remove that
item from the list with itr.remove();}
#
|
Response
|
1
|
for loop with .size while loop
with Iterator object .next if with Iterator object .next see answer
|
1
|
?????? What does this mean???? see answer
|
1
|
A
for loop, a while loop, or a for
each loop.
|
1
|
1. Use a for
loop to check each value based on the size of the array 2. Use a sort (ArrayList<> a: i) 3.Use
the collections class to organize the values. The
second two answers sort. They do not step through the list.
|
1
|
Iterator, For loop, While Loop see answer though you are basically correct
|
1
|
Iterator, and see answer
|
1
|
You can declare an Iterator and
loop through the list "while" the Iterator hasnext(). You can use a
simple for loop and increment the dummy variable at the end of every loop.
You could also use a short-handed for loop to loop through each item in the
list without caring about the indexes.
|
1
|
object of iterator for loop for
each loop see
answer
|
1
|
1) Use a forloop
and the get method 2) use an iterator, and the next method 3) Use a for
each loop, which is essentially an iterator behind the scenes
|
1
|
1- Make an iterator 2- Make a loop
the starts from the back and goes down one 3- Make a loop that starts from
the front, and if it removes one, 1 is subtracted from the counter. see answer
|
1
|
Using a for
loop, using a while loop, or an Iterator. see
answer
|
1
|
You can use a while loop, or a
for loop, see answer
|
1
|
You can use an iterator, a for loop, or if statement within a while loop. see answer
|
1
|
111111111111111111111111111111111111
see answer
|
1
|
A for Loop can be used, a While Loop
and An Iterator see answer
|
|
8
|
What is the difference between
creating an object's method and using it?
Create the method using a method header and code inside { } to
define the method inside the blueprint class. Use the method by creating an object
and calling the method on the object with the dot notation.
·
Create method
example : public int
getNumber(){ return number;}
·
Use method
example: Point p = new Point(1,2); p.getX();
Would the blueprint class or class
that creates the object use the keywords super or this? Only the blueprint class uses this or super.
Would the blueprint class or class
that creates the object implement Comparable? Only the blueprint
class implements Comparable.
Can a blueprint class call its own
constructor and make new objects of itself? It actually can do that but it does not have to. For simplicity,
we seldom do, but certain can.
Can a blueprint class contain an arraylist? Yes. When you create the object of that blueprint class, it
creates an arraylist. Every time you create the
object, you get a new arraylist inside the object.
#
|
Response
|
1
|
One is writing the code and the
other is implementing it. The class that creates the object would use super
to refer to the blueprint of an inherited class.
Either or both classes can implement Comparable. Comparable
will be used on the object, so if you don't intend to create an object of
the class, don't bother implementing Comparable. It is a bad idea for every object
to have an object. It is okay for objects to have
objects. Sure, why not
|
1
|
1) I don't know 2)no 3) yes 4) I
don't know 5) I don't know see answer
|
1
|
Creating a method means it is
not in use and simply is there for use, using it makes the computer run the
code that is contained inside of that method. good answer No.
Both. No that would result in an infinite loop. – It
would not. Yes. see answer
|
1
|
Creating a method isn' the same as calling it.
see answer
|
1
|
I don't know what the first
question is asking. A bluprint class will have
super. The blueprint class will implement Comparable.
|
1
|
Creating a method is simply
defining it. Using a method is when you actually call the method on an
object, etc. The blueprint class would use this and the child class would use
super. The blueprint class would implement Comparable, and a blueprint
class cannot use its own constructor if it is abstract. A blueprint class
can be used as a variable of an arraylist, but
you wouldn't be able to store objects of the blueprint class if the
blueprint class is abstract.
|
1
|
create: (usually found in main
program) Toy t = new Toy(); using it: assume there is a toy class with a getColor method t.getColor();---calling
it from Toy class the blueprint class would use this blueprint class
implements comparable blueprint class cannot call its own constructor and
make new objects of itself yes, a blueprint class can contain an arraylist? see answer
|
1
|
Creating an object's method
involves creating parameters; using it involves passing parameters. If the
blueprint extends another class, it would use super to refer to The
blueprint class would implement Comparable. A blueprint class cannot call
its own constructor and make new objects of itself.
A blueprint class can contain arraylists. see answer
|
1
|
When creating the method, you
have to specify what type of parameters it will take in. correct When using it,
you just give it the appropriate parameters. correct When
making it, you have to tell what it does. The blueprint class would use
"this", "super" would be for anything that inherits the
blueprint class. The class that creates the object,
uses neither. The blueprint class would implement Comparable. No Yes see answer though yours is pretty good
|
1
|
The difference between creating
an object's method and using it is that creating the method just makes the a base blueprint, using the method is usually done
by calling the object class in a different class. correct The class that creates the object
uses the keywords super or this. incorrect The blueprint class implements Comparable. Yes,
it can make new objects of itself. Yes, a blueprint class can contain an arraylist. see answer
|
1
|
Creating an object's method
requires us to use public (variables) and using it requires us to make an object
and then use . see answer
|
1
|
creating an objects method is the actual creation of the method
and using it would be calling that method, The class that creates the
object class would use .this The blueprint class would implement comparable.
A blueprint class cannot call its own contractor and make new objects of
itself, A blue print class can contain and Arraylist.
see answer
|
|
9
|
I have an List
of Card objects called deck. A card has a suit (string) and a rank(number) and a getSuit()
method.
Write the statement that creates
the deck of cards. (Don't worry about adding cards now - assume
that we added 30 cards). List<Card> deck = new LinkedList<Card>
(); OR ArrayList<Card> deck –
new ArrayList<Card>();
Write the statement that prints
the suit of the second card in the list. System.out.println ((deck.get(1)).getSuit());
Can that deck of cards be an ArrayList, a LinkedList or a HashSet if my variable type is a List? Lists cannot be
made of Hashset, only of LinkedList
or ArrayList.
#
|
Response
|
1
|
List deck = new LinkedList(); System.out.println
((deck.get(1)).getSuit());
|
1
|
List deck = new ArrayList(); System.out.println(deck.get(1));
|
1
|
List deck = new LinkedList();
deck.get(1).getSuit();
Yes.
|
1
|
List deck = new ArrayList();
system.out.println(deck.get(1).getSuit()); The deck of card can be any type if its
type is list.
|
1
|
List deck = new ArrayList<>(); If the
deck is a List variable, it can be an ArrayList
or a LinkedList, but not any type of Set.
|
1
|
professor pepper is the best //create deck of cards deck state =
new deck("spades", "king"); //print suit of second card
System.out.println(state.getSuit(state.indexOf(1))); ...the deck can be an arraylist? (not sure why)
|
1
|
Card myDeck
= new Card();\ System.out.println(deck.get(2).getSuit());
|
1
|
List deck=new ArrayList();
System.out.println(deck(1).getSuit());
Not for HashSet, but you can for ArrayList and LinkedList.
|
1
|
List newDeck
= new ArrayList();
|
1
|
List deck = new Arraylist ();
|
1
|
List deck = new ArrayList();
System.out.println(deck.get(1).getSuit); The deck of cards can be an ArrayList, a LinkedList or a HashSet if your variable type is a List.
|
1
|
ArrayListCard deck = new ArrayListCard(); int value = deck.get(1); System.out.println("Deck:
" + value); Yes it can be and array list, linked or hash set.
|
|
10
|
The Pet class has:
A Dog is a Pet. Dogs implement the
Carnivore Interface. A Rabbit is a Pet. Rabbits do not implement the
Carnivore interface. A pet in general cannot exist, but a Pet type, such as dog
or rabbit can. Write the class header for Dog,
Carnivore and Rabbit.
The Pet has a speak method
that said "mm", and no guard method.
The Dog has a speak
method that says "bark" and a guard method with
code:
return "I will protect you" + super.speak();
The Rabbit has no methods.
Given this code:
List<Pet> p = new ArrayList();
p.add(new Dog("muffy")):
p.add(new Dog("Damien"));
p.add(new Rabbit("Fluffy"));
for (Pet apet : p) {apet.speak());
}
1. What will each pet say?
Muffy says bark
Damien says bark
Fluffy says mm
2. Can I add apet.guard(); ? apet is a Pet because it is filled by the for/each loop. Pets have no
guard method, so no apet.guard().
If not, is there code that would
allow me to have Damien guard? Just add an abstract guard method to Pet, but then rabbit has
to implement it also.
3. If I created a rabbit with
Rabbit r = new Rabbit("Fluffy"); could I
then use r.speak(), even though Rabbit has no speak
method? Yes, because Rabbit inherited the defined speak method from pet
#
|
Response
|
1
|
bark bark
mm no, use an if(apet.prototype == Dog) yes it
will return super.speak see
answer
|
1
|
The rabbit Cannot have no methods due to inheritance. see
answer
|
1
|
bark bark mm No because Rabbit nor
the Pet class has a guard method. you can use p.get(1).guard(); outside of the loop no Yes because the parent class has a speak
method.
|
1
|
1) muffy will say "bark". Damien will say
"bark". Fluffy will say "mm". 2) You would not be able
to use the guard method because they are stored as Pet variables, and the
Pet class does not have a guard method. If you add a guard method to the
Pet class, you would be able to call guard on every object. 3) You could
use the speak method because a Rabbit is a Pet, and since Rabbit extends
Pet, the Rabbit has access to a speak method.
|
1
|
1. dog
says mm I will protect you bark. rabbit says mm 2.
no. pet.guard.super()... 3. yes because it inherits pets speak method
|
1
|
1. Muffy
will say "bark" Damien will say "bark" Fluffy will say
"mm" 2. No, because only the Dog has a guard method, so the
object would have to have to be a Dog, which Damien is, therefore Damien can
guard. 3. Yes, because it inherited the speak method from the Pet.
|
1
|
public class Dog extends Pet implements Carnivore public
Interface Carnivore public class Rabbit extends Pet 1. muffy will say "bark", Damien will say
"bark", and Fluffy will say "mm". see answer
|
1
|
Each Pet will say mm. no You would need Pet implements guard Yes because
a Rabbit is a Pet which has a speak method see
answer
|
|