Question

Consider the following code snippet: Employee programmer = new Employee(10254, "exempt"); String str = programmer.toString(); Assume...

Consider the following code snippet:

Employee programmer = new Employee(10254, "exempt");
String str = programmer.toString();

Assume that the Employee class has not implemented its own toString() method. What value will the variable str contain when this code is executed?

Group of answer choices

the variable will become a null reference

the code will not compile because Employee has not implemented toString()

the default from Object, which is the class name of the object and its hash code

the string representations of all of the field values

2

With very few exceptions, such as certain static constants, what access should fields of classes always have?

Group of answer choices

package

protected

public

private

3

How can a subclass modify a private field within its superclass?

Group of answer choices

use "super." followed by the name of the field

use a public method of the superclass

override the field with a field of the same name

use the name of the superclass field

4

Which is the difference between having a public field, and having a private field with public getter/setter methods?

Group of answer choices

there is effectively no difference between these approaches

getter/setter methods can be inherited, while fields cannot

setters can reject invalid values, and getters can do processing of the output

fields can be inherited, whereas getter/setter methods cannot

Homework Answers

Answer #1

ANSWERS -

1) (c) the default from Object, which is the class name of the object and its hash code

The variable str will contain the object name along with its hash code when this code is executed.

2) (c) Public

Most of the time classes should also have Public declared.

3) (b) use a public method of the superclass

A subclass can modify a private field by using public method within its superclass.

4) (c) setters can reject invalid values, and getters can do processing of the output

Setters and getters to make write only or read only, it can also make sure that correct value is being set by placing some validations in setters and getters.

=================================END================================

Please comment if u need any other info and DO LEAVE A LIKE, it would mean a lot. Thanks :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Consider the following code snippet: // A.java public class A { private String name; protected A(String...
Consider the following code snippet: // A.java public class A { private String name; protected A(String n) { name = n; } } // B.java public class B extends A { public B(String n) { super(n); } public String toString() { return name; } } What's wrong with the above code? Select all that apply. This code will encounter an access error at runtime. This code will not compile, because A's constructor is not declared public. This code will not...
Defining new classes from existing classes is called ________________. Select the correct answer overriding inheritance overloading...
Defining new classes from existing classes is called ________________. Select the correct answer overriding inheritance overloading polymorphism Which of the following is NOT true for ArrayList? Select the correct answer ArrayList uses a generic type. It is a Java class It stores and unlimited number of objects It creates arrays that are dynamic in size The keyword for a subclass to inherit data fields and methods from a superclass is __________________ Select the correct answer extends derives inherits private The...
Consider the following snippet of code: public class Animal { public String name; private int age;...
Consider the following snippet of code: public class Animal { public String name; private int age; public Animal() {   this.name = “defaultAnimal”;   this.age = 0; } public Animal (String name, int age) {   this.name = name;   this.age = age; } public String getName() { return this.name; } public int getAge() { return this.age; } } public class Dog extends Animal { //code } Of the following methods, which will lead to errors using the “super” keyword, if placed in the...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){    strName = " ";    strSalary = "$0";    } public Employee(String Name, String Salary){    strName = Name;    strSalary = Salary;    } public void setName(String Name){    strName = Name;    } public void setSalary(String Salary){    strSalary = Salary;    } public String getName(){    return strName;    } public String getSalary(){    return strSalary;    } public String...
Which statement is not true? Question 10 options: A subclass can add its own fields and...
Which statement is not true? Question 10 options: A subclass can add its own fields and methods. A subclass is more specific than its superclass The subclass exhibits the behaviors of its superclass and can only add upon behaviors that are defined in the superclass. Every subclass object is an object of its superclass. Interfaces are particularly used for ______. Question 8 options: polymorphysim. programming in general. assigning common functionality to possibly unrelated classes. assigning common functionality to similar classes....
Define a class named Employee that has four data fields: name: String hoursWorked: double hourlyPayrate: double...
Define a class named Employee that has four data fields: name: String hoursWorked: double hourlyPayrate: double bonusRate: double The class has two constructors: 1. constructor without arguments: initialize name to empty string, and all other data fields to 0.0 2. constructor with a String type arguments and three double type arguments. Use them to initialize the data fields properly The class also has: 1. Getter method for each data field to return the value of the data field 2. Setter...
**JAVA LANGUAGE** Write a program that models an employee. An employee has an employee number, a...
**JAVA LANGUAGE** Write a program that models an employee. An employee has an employee number, a name, an address, and a hire date. A name consists of a first name and a last name. An address consists of a street, a city, a state (2 characters), and a 5-digit zip code. A date consists of an integer month, day and year. All fields are required to be non-blank. The Date fields should be reasonably valid values (ex. month 1-12, day...
Modify the Employee9C superclass so that is an abstract superclass with a constructor to set its...
Modify the Employee9C superclass so that is an abstract superclass with a constructor to set its variables (firstName and lastName). It should contain an abstract method called payPrint. Below is the source code for the Employee9C superclass: public class Employee9C {    //declaring instance variables private String firstName; private String lastName; //declaring & initializing static int variable to keep running total of the number of paychecks calculated static int counter = 0;    //constructor to set instance variables public Employee9C(String...
java Considering the following code segment, answer the multiple choice questions. public String foo(String input) {...
java Considering the following code segment, answer the multiple choice questions. public String foo(String input) { String str = input.toLowerCase(); return bar(str); } private String bar(String s) { if (s.length() <= 1)   {return "";} else if (s.length() % 2 == 0)   {return "*" + bar(s.substring(2, s.length()));} else if (s.length() % 2 == 1) {return "*" + bar(s+"n");} else {return "";} } Which line(s) indicate the name of the helper method? Which line(s) contains recursive call(s)? How many * are in...
In this assignment, you will be building upon the work that you did in Lab #5A...
In this assignment, you will be building upon the work that you did in Lab #5A by expanding the original classes that you implemented to represent circles and simple polygons. Assuming that you have completed Lab #5A (and do not move on to this assignment unless you have!), copy your Circle, Rectangle, and Triangle classes from that assignment into a new NetBeans project, then make the following changes: Create a new Point class containing X and Y coordinates as its...