Podcast
Questions and Answers
What will this code print: Person myPerson = new Person("Bob"); myPerson.changeName("Joe"); myPerson.name = "John"; myPerson.printName();?
What will this code print: Person myPerson = new Person("Bob"); myPerson.changeName("Joe"); myPerson.name = "John"; myPerson.printName();?
John
What is missing in the Athlete class code snippet?
What is missing in the Athlete class code snippet?
Need to declare the name instance variable
What is wrong with the Pokemon class definition?
What is wrong with the Pokemon class definition?
Must use this in constructor when the constructor parameters have the same name as instance variables.
What will the static method bar() output after creating three Main objects?
What will the static method bar() output after creating three Main objects?
Signup and view all the answers
What would be the output of the program if bar() is changed to m3.foo(); after m1.setN(5);?
What would be the output of the program if bar() is changed to m3.foo(); after m1.setN(5);?
Signup and view all the answers
What will the getString() method return in the given Main class?
What will the getString() method return in the given Main class?
Signup and view all the answers
Why can't you set the jersey in the Athlete class?
Why can't you set the jersey in the Athlete class?
Signup and view all the answers
How do you obtain the jersey number of an Athlete object?
How do you obtain the jersey number of an Athlete object?
Signup and view all the answers
What method would you implement to allow setting a jersey number in the Athlete class?
What method would you implement to allow setting a jersey number in the Athlete class?
Signup and view all the answers
What is a valid instantiation of the Athlete class?
What is a valid instantiation of the Athlete class?
Signup and view all the answers
What is the output when instantiating Foo like this: Foo fee; fee = new Foo();
What is the output when instantiating Foo like this: Foo fee; fee = new Foo();
Signup and view all the answers
What is the return type for the getName method in the TvShow class?
What is the return type for the getName method in the TvShow class?
Signup and view all the answers
Which methods are available in the Foo class?
Which methods are available in the Foo class?
Signup and view all the answers
Study Notes
Person Class
- Code initializes a
Person
object with a name. - The changeName method updates the person's name.
- The final output after multiple name changes is "John".
Athlete Class Initialization
- An instance variable for name must be declared in the
Athlete
class for proper functionality.
Pokemon Class Constructor
- Constructors must use
this
keyword when parameter names match instance variable names to avoid shadowing.
Counter in Main Class
- Each time a
Main
object is instantiated, a countern
increments. - The output of the method
foo()
after creating threeMain
instances is 3.
Modified Output in Main Class
- If
bar()
method setsn
to 5 and callsfoo()
on instance m3, the output is 5.
Main Class String Handling
- The constructor’s parameter does not change the instance variable
str
due to lack ofthis
keyword. - The output remains "bar" when calling
getString()
.
Athlete Class Properties
- The jersey variable is private, preventing direct modification without a setter method.
-
getJersey()
method can access the private jersey variable.
Jersey Getter in Athlete Class
- To retrieve the value of jersey instance variable, call
athlete.getJersey()
.
Setting Jersey in Athlete Class
- A setter method like
public void setJersey(int jersey)
allows control over the private jersey variable.
Athlete Object Instantiation
- To create an
Athlete
object, use the syntax:athlete = new Athlete("Dirk", "Nowitzki", 41);
.
Foo Class Constructors
-
Foo
class has overloaded constructors initializingbar
andstoo
attributes. - Example of instantiation:
Foo fee = new Foo();
initializes default values.
Valid Instantiations of Athlete Class
- Valid instances of
Athlete
should include both first and last names, with the last parameter being an integer jersey number. - Only I and III are valid instantiations based on specified constructors.
Static and Instance Methods in Foo Class
- Static methods in Java, like
foo()
, can be called without creating an instance of the class. - Non-static methods (
bar()
andbaz()
) require an object of the class.
TvShow Class Method Return Type
- The
getName()
method lacks a return type declaration; it should return aString
.
Storm Class Overloaded Constructors
- The
Storm
class features overloaded constructors to initialize with different parameters: type only, type with total precipitation, and type with lightning count, ensuring flexibility in object creation.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Test your understanding of writing classes in Java with this quiz. Analyze code snippets and predict outcomes based on object-oriented programming principles. Perfect for beginners looking to solidify their knowledge of class functionalities.