In: Computer Science
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.
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;
}
}