public class Money
{
private double myAmount;
public void print()
{
System.out.println( "$" + myAmount );
}
public void setAmount( double m )
{
myAmount = m;
}
}
{
private double myAmount;
public void print()
{
System.out.println( "$" + myAmount );
}
public void setAmount( double m )
{
myAmount = m;
}
}
- The class will not compile because there is no accessor method for myAmount.
- The class will not compile because there is no public constructor.
- The class will compile but no instance can be created because there is no public constructor.
- The class will compile but no instance can be created because there is no accessor method for myAmount.
- The class will compile and instances of the class can be created using the expression new Money().
6)Questions 6 – 10 are concerned with the following class definition:
public class Book
{
private String myTitle = null;
private String myAuthor = null;
private String myISBN = null;
private int myPubYear = 0;
public Book()
{
}
public Book( String isbn, int year )
{
myISBN = isbn;
myPubYear = year;
}
public String getAuthor()
{
return myAuthor;
}
public void setTitle( String title )
{
myTitle = title;
}
public void setAuthor( String author )
{
myAuthor = author;
}
}
{
private String myTitle = null;
private String myAuthor = null;
private String myISBN = null;
private int myPubYear = 0;
public Book()
{
}
public Book( String isbn, int year )
{
myISBN = isbn;
myPubYear = year;
}
public String getAuthor()
{
return myAuthor;
}
public void setTitle( String title )
{
myTitle = title;
}
public void setAuthor( String author )
{
myAuthor = author;
}
}
How many modifier methods does the Book class have?
- 0
- 1
- 2
- 3
- 4
my error in question 6 was quite silly I read the question as: How many modifier methods can the Book class have? instead of How many modifier methods does the Book class have?
8)For Questions 8 – 10, suppose that the following two methods have been added to the definition of the Book class:
public void setState( String title, String author, String isbn, int year )
{
if ( !title.equals( "" ) )
myTitle = title;
if ( !author.equals( "" ) )
myAuthor = author;
if ( !isbn.equals( "" ) )
myISBN = isbn;
if ( year > 0 )
myPubYear = year;
}
public String toString()
{
return "Title: " + myTitle
+ "\nAuthor: " + myAuthor
+ "\nISBN: " + myISBN
+ "\nPublication Year: " + myPubYear;
}
{
if ( !title.equals( "" ) )
myTitle = title;
if ( !author.equals( "" ) )
myAuthor = author;
if ( !isbn.equals( "" ) )
myISBN = isbn;
if ( year > 0 )
myPubYear = year;
}
public String toString()
{
return "Title: " + myTitle
+ "\nAuthor: " + myAuthor
+ "\nISBN: " + myISBN
+ "\nPublication Year: " + myPubYear;
}
Book b = new Book();
b.setState( "Read Me", "", "123456-78-9-0", -1 );
System.out.println( b.toString() );
b.setState( "Read Me", "", "123456-78-9-0", -1 );
System.out.println( b.toString() );
- Title: Read Me
Author:
ISBN: 123456-78-9-0
Publication Year: -1 - Title: Read Me
Author: null
ISBN: 123456-78-9-0
Publication Year: 0 - Title: Read Me
Author:
ISBN: 123456-78-9-0
Publication Year: 0 - Title: Read Me Author: ISBN: 123456-78-9-0 Publication Year: -1
- Title: Read Me Author: null ISBN: 123456-78-9-0 Publication Year: 0
if ( year > 0 )
myPubYear = year;
thus I chose 1 believing that it would set the date to -1 instead of leaving it 0.
No comments:
Post a Comment