Monday, November 30, 2015

errors in comp sci test 12

4)Consider the following class definition:
  public class Money
  {
    private double myAmount;

    public void print()
    {
      System.out.println( "$" + myAmount );
    }

    public void setAmount( double m )
    {
      myAmount = m;
    }
  }
Exactly one of the following statements is true. Which one?
  1. The class will not compile because there is no accessor method for myAmount.
  2. The class will not compile because there is no public constructor.
  3. The class will compile but no instance can be created because there is no public constructor.
  4. The class will compile but no instance can be created because there is no accessor method for myAmount.
  5. The class will compile and instances of the class can be created using the expression new Money()
here I forgot that you can create classes without constructors with the new command.

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;
    }
  }
Value: 1 point.
How many modifier methods does the Book class have?
  1. 0
  2. 1
  3. 2
  4. 3
  5. 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;
    }
Which of the alternatives below is the output when the following code is executed?
    Book b = new Book();
    b.setState( "Read Me", "", "123456-78-9-0", -1 );
    System.out.println( b.toString() );
  1. Title: Read Me
    Author:
    ISBN: 123456-78-9-0
    Publication Year: -1
  2. Title: Read Me
    Author: null
    ISBN: 123456-78-9-0
    Publication Year: 0
  3. Title: Read Me
    Author:
    ISBN: 123456-78-9-0
    Publication Year: 0
  4. Title: Read Me Author: ISBN: 123456-78-9-0 Publication Year: -1
  5. Title: Read Me Author: null ISBN: 123456-78-9-0 Publication Year: 0
here I did not notice that:
 if ( year > 0 )
        myPubYear = year;
thus I chose 1 believing that it would set the date to -1 instead of leaving it 0.