Question

In: Computer Science

1.What happens if you attempt to call a method using a referencevariable that is set...

1.What happens if you attempt to call a method using a reference variable that is set to null?

2.Consider the following class declaration:
public class Square
{
   private double side;
   public Square (double s)
{
    side = s;
}
public double getArea()
{
   return side * side;
}
public double getSide()
{
   return side;
}
}

a. Write a toString method for this class. The method should return a string containing the side and area of the square.


b. Write a equals method for this class. The method should accept a Square object as an argument. it should return true if the argument object contains the same data as the calling object, or false otherwise.

3.

a. Write a statement that declares a String array initialized with the following strings: "Einstein," "Newton", "Copernicus", "Kepler", and "Hawking". (2 points)


b. Write a loop that displays the total length of all the strings in the array that you declared in part (a). (4 points)

4.Look at the following partial class definition, and then respond to the questions that follow it:
public class Book
{
   private String title;
   private String author;
   private String publisher;
   private String isdn;
}

a. Write a constructor for this class. The constructor should accept an argument for each of the fields
b. Write accessor and mutator methods for each field.

Solutions

Expert Solution

1. When you call a method with reference variable that is set to null, we will get NULL POINTER EXCEPTION.

2. (a) and (b)

THE REQUIRED CODE IS HIGHLIGHTED.

public class Square
{
private double side;
public Square (double s)
{
side = s;
}
  
public double getArea()
{
return side * side;
}
  
public double getSide()
{
return side;
}
  
//Code for problem 2a   

public String toString()
{
return "The side is: "+getSide()+"and the area is: "+getArea();
}
  

//Code for problem 2b
public boolean equals(Square s1)
{
if(s1.getSide()==this.side)
{
return true;
}
else
{
return false;
}
}

  

}

=========================================

3. (a) and (b)

public class Demo
{
public static void main(String[] args)
{
//Code for 3a
String[] names={"Einstein," "Newton", "Copernicus", "Kepler", and "Hawking"};
  

//Code for 3b
int count=0;
for(String s:names)
{
count++;
}
System.out.println("The array length is: "+count);

  
}
}
  

====================================================

4.(a) and (b)

public class Book
{
private String title;
private String author;
private String publisher;
private String isdn;


//code for 4a
//Constructor for Book class
public Book(String title,String author,String publisher,String isdn)
{
this.title=title;
this.author=author;
this.publisher=publisher;
this.isdn=isdn;
}


//code for 4b
//Accessor and Mutator methods
public void setTitle(String title)
{
this.title=title;
}
public void setAuthor(String author)
{
this.author=author;
}
public void setPublisher(String publisher)
{
this.publisher=publisher;
}
public void setIsdn(String isdn)
{
this.isdn=isdn;
}

public String getTitle()
{
return title;
}
public String getPublisher()
{
return publisher;
}
public String getAuthor()
{
return author;
}
public String getIsdn()
{
return isdn;
}

}


Related Solutions

In Java please What happens if you call factorial( with a negative value of n? With...
In Java please What happens if you call factorial( with a negative value of n? With a large value of say 35? Write a recursive function that takes an integer n as its argument and returns ln(n!)
You set up a Covered Call position on a stock currently priced at $64, using the...
You set up a Covered Call position on a stock currently priced at $64, using the following call option: [C(S0=$64, T=3 months, X=70)] that has a current price of $1.90 in the market. Scale the transactions by one option contract. This problem has four different parts, a) to d) below, make sure and answer them all. (Show your calculations to get your final answer to earn partial credit, in case you miss the final answer.) a) What are your transactions...
Can you provide example for this using Financial calculator and explain? 1) What happens to the...
Can you provide example for this using Financial calculator and explain? 1) What happens to the PV of a set amount to be received in 10 years if your rate of return decreases? a) The PV increases 2) What happens to the PV of a set amount if the rate stays the same, but compounds more frequently? a) The PV decreases 3) What happens to the PV of a set amount if the length of time shrinks, but the rate...
What happens if three CPUs in a multiprocessor attempt to access exactly the same word of...
What happens if three CPUs in a multiprocessor attempt to access exactly the same word of memory at exactly the same instant?
Consider what happens when two people with different (or identical) love styles develop or attempt to...
Consider what happens when two people with different (or identical) love styles develop or attempt to develop a love relationship (there is potential humor and tragedy here!). Your task: Create two (2) profiles or scenarios of two couples using any combination of love styles outlined in chapter 7 (and handouts if necessary). What would this relationship look like? Could it work? Why or why not? For example, what would a relationship between the ludic lover and the storgic lover look...
1.What happens to the mean or median when each number in a set of data is multiplied by a constant?
  1.What happens to the mean or median when each number in a set of data is multiplied by a constant? 2.Predict how measures of spread are affected by new or altered individual entries in a dataset. That is, a small number is replaced by a very big number, for example. 3.What is the relationship between the standard deviation and the variance? What characteristic of a set of data causes a large standard deviation? A small one? 4.What procedure did...
What are the parameters affecting European call price on a non dividend paying stock? What happens...
What are the parameters affecting European call price on a non dividend paying stock? What happens to the call price when one of these parameters changes with all the others remaining the same? Make the table.
QUESTION 2 If you set up your initial review of the literature using the Matrix Method,...
QUESTION 2 If you set up your initial review of the literature using the Matrix Method, you already have a start on the Matrix Indexing System. However, the indexing system differs from the Matrix Method. The Matrix Method is a strategy for acquiring, analyzing, and writing a synthesis of a review of the scientific literature. In contrast, the Matrix Indexing System provides a way of coping with all the information and documents that you use or create as a result...
What happens to the PV of a set amount if the length of time shrinks from...
What happens to the PV of a set amount if the length of time shrinks from 10 to 7 years, but the rate stays the same? The PV increases The PV decreases The PV doesn’t change The PV goes to infinity
- What happens in the free market if the market price is set too high for...
- What happens in the free market if the market price is set too high for the equilibrium price? Draw a graph and explain the reason logically. - As the large-scale wild-fire in California is threatening the Napa Valley--which accounts for 90% of wine supply in the United States--a considerable extent of impact is expected on the wine industry. Analyze the effect of the wild-fire on a market with two goods: wines and wine openers.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT