Question

In: Computer Science

Java public class UpperCaseString { //1      protected String content; // 2      public UpperCaseString(String content)...

Java

public class UpperCaseString { //1

     protected String content; // 2

     public UpperCaseString(String content) { // 3

          this.content = content.toUpperCase(); // 4

     } // 5

     public String toString() { // 6

          return content.toUpperCase(); // 7

     } // 8

     public static void main(String[] args) { // 9

          UpperCaseString upperString =

             new UpperCaseString("Hello, Cleo!"); // 10

          System.out.println(upperString); // 11

     } // 12

} // 13

THE FOLLOWING 3 QUESTIONS REFER TO THE CODE FRAGMENT ABOVE   

  1. When UpperCaseString is executed/run, what gets displayed to the console?

__________________________________________________

  1. Review line 11 above. What UpperCaseString method is implicitly being invoked in order to display a value to the Console?

______________________________________________________________

  1. Review line 2 above. Assuming that every implementation detail was deliberate, what might the developer have been thinking when he/she decided to specify protected access?

______________________________________________________________

Solutions

Expert Solution

Answer 1:

Display on Console: HELLO, CLEO!

Explanation: When we are creagting object upperString of UpperCaseString class, we are passing value 'HELLO, CLEO! ' So it will be stored in content of the object (using the constructor). And when we print the object it's printing its value.

Answer 2:

toString() method is being invoked in order to print to the value of the object on the console.

When we print any object, java compiler internally invokes the toString() method on the object.

In Java, all classes inherit from the Object class. And toString() is the method of object class. toString() method in Object prints “class name @ hash code”. But here we have override toString() method. So when we print object it will print the value accordingly.

Answer 3:

protected access modifier makes attribute accessible in the same package and subclasses. So to prevent further access content is made protected.


Related Solutions

For Questions 1-3: consider the following code: public class A { private int number; protected String...
For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {    super();    System.out.println(“B() called”);...
Base Class class TransportationLink { protected: string _name; double _distance; public: TransportationLink(const string &name, double distance);...
Base Class class TransportationLink { protected: string _name; double _distance; public: TransportationLink(const string &name, double distance); const string & getName() const; double getDistance() const; void setDistance(double); // Passes in the departure time (as minute) and returns arrival time (as minute) // For example: // 8 am will be passed in as 480 minutes (8 * 60) // 2:30 pm will be passed in as 870 minutes (14.5 * 60) virtual unsigned computeArrivalTime(unsigned minute) const = 0; }; #endif Derived Classes...
ALL IN JAVA public class Employee {    public String strName, strSalary;       public Employee()...
ALL IN JAVA 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;  ...
java program public class FavoriteList { protected PositionList> fList; // List of entries /** Constructor; O(1)...
java program public class FavoriteList { protected PositionList> fList; // List of entries /** Constructor; O(1) time */ public FavoriteList() { fList = new NodePositionList>(); } // Complete your work and write out a main function to test top, access and remove methods. }
Class Employee (All IN JAVA) public class Employee {public String strName, strSalary; public Employee(){strName = "...
Class Employee (All IN JAVA) 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 toString(){return(strName + " has a salary of " + strSalary); Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
public class Book{     public String title;     public String author;     public int year;    ...
public class Book{     public String title;     public String author;     public int year;     public String publisher;     public double cost;            public Book(String title,String author,int year,String publisher,double cost){        this.title=title;         this.author=author;         this.year=year;         this.publisher=publisher;         this.cost=cost;     }     public String getTitle(){         return title;     }         public String getAuthor(){         return author;     }     public int getYear(){         return year;     }     public String getPublisher(){         return publisher;...
1. public class MyString { private char[] data; MyString(String string) { data = string.toCharArray(); } public...
1. public class MyString { private char[] data; MyString(String string) { data = string.toCharArray(); } public int compareTo(MyString other) { /* code from HW1 here */ } public char charAt(int i) { return data[i]; } public int length() { return data.length; } public int indexOf(char c) { /* code from HW1 here */ } public boolean equals(MyString other) { /* code from HW1 here */ } /* * Change this MyString by removing all occurrences of * the argument character...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
// problem2.java import java.util.*; public class problem_a { public static void main(String[] args) { // test...
// problem2.java import java.util.*; public class problem_a { public static void main(String[] args) { // test the smallest method System.out.print("smallest(1, 0, 2) -> "); System.out.println( smallest(1, 0, 2) ); // test the average method System.out.print("average(95, 85, 90) -> "); System.out.println( average(95, 84, 90) ); } // end main /* * smallest(double, double, double) -> double * * method is given 3 numbers, produces the smallest of the three * * examples: * smallest(1, 0, 2) -> 0.0 */ public static...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT