Java Midterm Solution
Java Midterm Solution
Java Midterm Solution
Date: 22.07.2006
Name and Surname:
Time: 12:15-14:45
Signature:
Press any key to continue... 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
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