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

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 +...
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...
Suppose we have a Java class called Person.java public class Person { private String name; private...
Suppose we have a Java class called Person.java public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName(){return name;} public int getAge(){return age;} } (a) In the following main method which lines contain reflection calls? import java.lang.reflect.Field; public class TestPerson { public static void main(String args[]) throws Exception { Person person = new Person("Peter", 20); Field field = person.getClass().getDeclaredField("name"); field.setAccessible(true); field.set(person, "Paul"); } }...
Write 2 short Java programs based on the description below. 1) Write a public Java class...
Write 2 short Java programs based on the description below. 1) Write a public Java class called WriteToFile that opens a file called words.dat which is empty. Your program should read a String array called words and write each word onto a new line in the file. Your method should include an appropriate throws clause and should be defined within a class called TextFileEditor. The string should contain the following words: {“the”, “quick”, “brown”, “fox”} 2) Write a public Java...
protected String name;                                      &nbsp
protected String name;                                                                                                           protected ArrayList<Stock> stocks;     protected double balance;                                                                              + Account ( String name, double balance) //stocks = new ArrayList<Stock>() ; + get and set property for name; get method for balance + void buyStock(String symbol, int shares, double unitPrice ) //update corresponding balance and stocks ( stocks.add(new Stock(…..)); ) + deposit(double amount) : double //returns new balance                                                                 + withdraw(double amount) : double //returns new balance + toString() : String                                                                  @Override     public String toString(){         StringBuffer str =...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...
PREVIOUS CODE: InventroryItems class InventoryItem implements Cloneable{ // instance variables protected String description; protected double price;...
PREVIOUS CODE: InventroryItems class InventoryItem implements Cloneable{ // instance variables protected String description; protected double price; protected int howMany; // constructor public InventoryItem(String description, double price, int howMany) { this.description = description; this.price = price; this.howMany = howMany; } // copy constructor public InventoryItem(InventoryItem obj) { this.description = obj.description; this.price = obj.price; howMany = 1; } // clone method @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } // toString method @Override public String toString() { return description +...
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
INSERT STRING INTO SEPARATE CHAIN HASHTABLE & ITERATE THROUGH HASHTABLE: JAVA _________________________________________________________________________________________________________________ import java.util.*; public class...
INSERT STRING INTO SEPARATE CHAIN HASHTABLE & ITERATE THROUGH HASHTABLE: JAVA _________________________________________________________________________________________________________________ import java.util.*; public class HashTable implements IHash { // Method of hashing private HashFunction hasher; // Hash table : an ArrayList of "buckets", which store the actual strings private ArrayList<List<String>> hashTable; /** * Number of Elements */ private int numberOfElements; private int numberOfBuckets; /** * Initializes a new instance of HashTable. * <p> * Initialize the instance variables. <br> * Note: when initializing the hashTable, make sure to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT