CS 111 Java Exam

advertisement
CS 111 Java Exam
Date: 22.07.2006
Time: 12:15-14:45
Name and Surname:
Signature:
Question 1: Classes (50 points)
Write a class named Book that keeps track of book objects such that the instance data contains the
book’s title and author and a unique identification number, say id that starts form 1 for the first
book object and is incremented by 1 each time a new book with at least the title specified is created.
The required methods in the Book class are as follows:
 3 constructors: One without parameters that sets the title and author to “unknown” and id to
0; one with a single parameter that sets the title to the given value as parameter, sets the
author to “unknown”, increments the sequence by 1 and sets id to this sequence number; one
with two parameters for title and author, setting the corresponding instance data, and id as in
the second constructor.
 The setter methods for title and author.
 The getter methods for title, author, and id.
 The equals method that compares the current book object with another Book object given as
a parameter and returns true if both objects have the same title and author properties, and
false otherwise.
 The toString method that returns a text including the book’s title, author and id. Refer to the
sample execution window for the test program for details.
 The getInitials method that returns the initial letters of the author’s first name(s) and last
name, if the author’s name is known (not equal to “unknown”, or not null).Assume that
there can be at most two names and one surname separated by a single blank. Refer to the
sample execution window for the test program for details.
Write a driver class that tests the Book class. Allow the user to enter as many book objects as s/he
wants. Take care of necessary object initializations. Store the first and last book objects separately.
Print each book object. Compare the first and last book objects, if any, and display a proper
message if they are the same. Display the last book’s author’s initials, if the user has input any valid
book object.
Sample execution windows:
To end the input process bypass each question by typing the enter key!
Type the title of the book:
Type the name of the author:
Press any key to continue...
To end the input process bypass each question by typing the enter key!
Type the title of the book: Windows NT Server 4.0
Type the name of the author: Russel
Book No: 1 entitled "Windows NT Server 4.0" written by Russel
Type the title of the book:
Type the name of the author:
First and last books are same
Last book's author has the initials, R.
Press any key to continue...
Page 1
To end the input process bypass each question by typing the enter key!
Type the title of the book: Java Software Solutions
Type the name of the author: Lewis Loftus
Book No: 1 entitled "Java Software Solutions" written by Lewis Loftus
Type the title of the book: Introduction to Java Programming with JBuilder
Type the name of the author: Yvet Daniel Liang
Book No: 2 entitled "Introduction to Java Programming with JBuilder" written by
Yvet Daniel Liang
Type the title of the book:
Type the name of the author:
Last book's author has the initials, Y.D.L.
Press any key to continue...
Page 2
public class Book {
/* class variable */
private static int numBooks = 0;
/* instance data */
private String title, author;
private int id;
/* constructors */
public Book () {
title=author="unknown";
id=0;
}
public Book (String title) {
this.title=title;
author="unknown";
numBooks ++;
id = numBooks;
}
public Book (String title, String author) {
this.title=title;
this.author=author;
numBooks ++;
id = numBooks;
}
/* getter methods */
public String getTitle () {
return title;
}
public String getAuthor () {
return author;
}
public int getId () {
return id;
}
/* setter methods */
public void setTitle (String title) {
this.title = title;
}
public void setAuthor (String author) {
this.author = author;
}
/* toString method to be used along with String
concatenationa and print statements */
public String toString ( ) {
String S = "Book No: "+id;
S = S+" entitled \""+title+"\"";
S = S+" written by "+author;
return S;
}
/* equals method that compare this Book object with
other Book object */
public boolean equals (Book other) {
String otherTitle = other.getTitle();
String otherAuthor = other.getAuthor();
boolean same = false;
if (otherTitle.equals(title) && otherAuthor.equals(author))
same = true;
return same;
}
/*getInitials method that return the initials of the author
assuming that there can be at most two names and a surname
seperated by a single blank */
public String getInitials() {
String S="";
if (author.equals("unknown")==false &&
author.equals("")==false){
S = S + author.charAt(0)+ ".";
int ix1 = author.indexOf(" ");
if (ix1 != -1 && ix1!=author.length()-1) {
S = S + author.charAt(ix1+1)+".";
int ix2 = author.indexOf(" ",ix1+1);
if (ix2 != -1 && ix2!=author.length()-1) {
Page 3
S = S + author.charAt(ix2+1)+".";
}
}
}
return S;
}
}
Page 4
import java.util.Scanner;
public class BookTest {
public static void main ( String [ ] args ) {
Scanner oku = new Scanner (System.in) ;
String title, author;
System.out.println("To end
"question by typing
System.out.print("Type the
title = oku.nextLine();
System.out.print("Type the
author = oku.nextLine();
the input process bypass each "+
the enter key!");
title of the book: ");
name of the author: ");
Book aBook, firstBook, lastBook;
aBook = firstBook = lastBook = new Book();
while (!(title.equals("") && author.equals(""))) {
if (author.equals(""))
aBook = new Book(title);
else
aBook = new Book(title, author);
System.out.println(aBook);
if (aBook.getId()==1)
firstBook=aBook;
System.out.print("Type the title of the book: ");
title = oku.nextLine();
System.out.print("Type the name of the author: ");
author = oku.nextLine();
}
lastBook = aBook;
if (firstBook.equals(new Book()) == false) {
if (firstBook.equals(lastBook))
System.out.println("First and last books are same");
System.out.println("Last book\'s author has the initials, "
+ lastBook.getInitials() );
}
}
}
Page 5
Question 2. Simple Java Application (50 points)
Write a Java application that does a single computation (square root or square) based on the answer
of the user. When computing the square root, use two approaches and compare the results: First
approach is to use Math.sqrt and the other approach is to approximate the root by performing the
following calculation repeatedly until nextGuess and lastGuess are almost identical (their absolute
difference is less then a small number, say epsilon with a constant value of 0.0001):
nextGuess = (lastGuess + (number/lastGuess)) / 2
Start with an initial guess of number/2. Display the results to 5 decimal places.
Make sure that the user enters a positive value in both computations.
Sample execution windows:
Do you want to compute sqrt (Yes) or square (No) ?
You should have typed YES or NO!
Press any key to continue...
Do you want to compute sqrt (Yes) or square (No) ? yEs
Type a positive number: -5
Type a positive number: 9
The square root of 9.0 is 3,00000 (Math) = 3,00000 (computed).
Press any key to continue...
Do you want to compute sqrt (Yes) or square (No) ? nO
Type a positive number: 4,3
The square of 4.3 is 18.49
Press any key to continue...
Page 6
import java.util.Scanner;
import java.text.DecimalFormat;
public class AppSqrt {
public static void main (String [] args) {
Scanner oku = new Scanner (System.in) ;
System.out.print("Do you want to compute sqrt (Yes) or square (No) ? ");
String cevap = oku.nextLine().toLowerCase();
double number=0;
if (cevap.equals("yes") || cevap.equals("no")) {
System.out.print("Type a positive number: ");
number = oku.nextDouble();
while (number<=0) {
System.out.print("Type a positive number: ");
number = oku.nextDouble();
}
}
if (cevap.equals("yes")) { //sqrt
double sqrtFromMath = Math.sqrt(number);
final double epsilon = 0.0001;
double nextGuess=number/2, lastGuess;
do {
lastGuess=nextGuess;
nextGuess=(lastGuess+(number/lastGuess))/2;
}
while ( Math.abs(lastGuess-nextGuess) >= epsilon);
DecimalFormat fmt = new DecimalFormat("0.00000");
System.out.println("The square root of "+number+" is "+
fmt.format(sqrtFromMath)+" (Math) = "+fmt.format(nextGuess)+"
(computed).");
}
else if (cevap.equals("no")) { // square
double numberSquared = Math.pow(number,2);
System.out.println("The square of "+number+" is "+numberSquared);
}
else //incorrect data
System.out.println("You should have typed YES or NO!");
}
}
Page 7
Download