takehome-final2.zip

takehome-final/coins.db

takehome-final/coins.txt

itemType,price,onSale,denomination,year,condition coin,750.00,yes,10,1934,mint coin,1.50,no,5,1956,good coin,0.99,yes,25,2007,mint coin,23.00,no,5,1931,mint coin,16.00,no,1,1955,mint coin,3.50,no,10,1947,good coin,0.60,yes,1,1958,fine coin,1.65,yes,10,1978,fine coin,10.45,no,25,1947,good

takehome-final/contents.txt

Contents of IT 313 Final Exam Files -- final-exam.txt : The final exam questions. Submit a separate document (MS Word or Text File) with the answers to the multiple questions questions in Part A and the short essay questions in Part B. Also submit the justification for the output in Part C, Problem 1, if you choose to do this problem. Also submit a project FinalExamSmith (replace Smith with your last name). Place the document with the answers and the final project in a folder named FinalExamSmith and zip up this folder to obtain the submission zip file FinalExamSmith.zip. -- coins.db : The database for Part C, Problem 2, if you choose to do this problem. Place this file in the project folder your FinalExam project, not in the src folder. -- MainC2.java : The MainC2 class for Part C, Problem 2, if you choose to do this problem. The lines in this class are in scrambled order. Reorder the lines to that the class runs. -- LoadCoins.java : For your reference only. It creates the coins.db database from the coins.txt input file. -- coins.txt : The input file for Part D, Problem 3. Place this file in the project file of your FinalExam project, not in the src folder. -- Test2.java : The unit test file (JUnit5) for the CollectableItem and Coin classes. Place this class in your FinalExam project and add assertEquals methods to test the specified methods. -- MainD3.java : The Main class for Part D, Problem 3. Place this class in the src folder of your FinalExam project. This class has about 10 to 15 errors. Correct the errors.

takehome-final/final-exam.txt

IT 313 -- Takehome Final Exam Name _________________ Opens: March 17, 2020 Closes: March 22, 2020 at 11:59 pm. Include this line at the top of your submission: I completed this exam by myself without the help of other persons. Part A. Multiple Choice Questions. You may supply a reason or show work for partial credit. If your answer is correct, the reason will not be considered. 5 points for each question. Answer all ten questions. 1. Which method call converts the double value, defined by double x = 35.678; into the String object s? a. String s = Double.parseDouble(x); b. String s = String.valueOf(x); c. String s = (String) x; d. String s = x.toString( ); 2. Which of these statements returns true if the String reference variables s and t contain the same address? a. equals(s, t) b. s.equals(t) c. s.referenceEquals(t) d. s == t 3. What is the output of these statements? String s = 28 + 'R' + "U"; System.out.println(s); a. 28RU b. 28 R U c. 110U d. 195 4. Which statement declares and instantiates an array named arr that contains five double precision point zeros (0.0)? a. double[5] arr; b. double[ ] arr = new double[5]; c. double[5] arr = {0.0}; d. double[ ] arr = {0.0} * 5; 5. Which of these terms is NOT another name for an instance variable. a. Attribute b. Field c. Local Variable d. Property 6. For two classes A and B, which test can be used to check if it makes sense to make B a derived class of the base class A? a. combination test b. derived test c. has-a test d. is-a test 7. What happens if you don't close a PrintWriter object after you are finished using it? a. You can't open any other PrintWriter objects. b. Your program will not compile. c. The last page of the output buffer is not written to the output file. d. The PrintWriter object goes into an infinite loop. 8. How do you insert the int value 357 into the HashMap col using the key "abc"? a. col.add("abc", 327); b. col.add(327, "abc"); c. col.insert("abc", 327); d. col.put("abc", 327); 9. Which SQLite3 command lists the names of all the database tables in the currently open database? a. listtables b. .schemas c. .tables d. tables 10. Which software pattern does this: define a family of algorithms, encapsulates each one, and makes them interchangeable. It lets the algorithm vary independently from clients that use it. a. Algorithm b. State c. Strategy d. Visitor Part B: Short Essay. Answer 1 out of 3 questions. For full credit, your essay should use paragraphs and complete sentences with an introduction and conclusion. Include references to show where you obtained your information. About one half to one page or more per question. 1. Two important concepts of object oriented programming are encapsulation and inheritance. (Look up encapsulation in object oriented programming online if you wish.) Explain how encapsulation and inheritance are used in Java programming. 2. Explain what software design patterns are and why they are important for software design. Include in your explanation, the software patterns that we discussed in class. 3. Describe some of the technologies that are available in the Java ecosystem. Include Java-compatable languages that run on the JVM, technologies that use Java for example, build languages for Java, etc. Include references for your sources. Part C: Problems. You can check your answers by running the code. Work only one of the two problems in Part C. 10 points. 1. What is the output of the Main class. Show the calculations that you use to explain your answer. 10 points: 8 points for the explanation, 2 points for the correct answer // Source code file: A.java public class A { private int s; public A(int s) { this.s = s + 8; } public int getS( ) { return this.s; } public void augment( ) { this.s += 3; } @Override public String toString( ) { return "$" + (this.s + 5) + "$"; } } // Source code file: B.java public class B extends A { private int t; public B(int t) { super(2); this.t = t + 9; } @Override public void augment( ) { super.augment( ); this.t += 4; } @Override public String toString( ) { return "#" + (this.getS( ) + this.t) + "#"; } } // Source code file: // Source code file: Main.java public class Main { public static void main(String[] args) { A a = new A(10); B b = new B(11); a.augment( ); b.augment( ); System.out.printf("%s %s", a.toString( ), b.toString( )); } } 2. The table coins in the coins.db database is defined by this SQL create statement: create table if not exists coin ( price float, onsale varchar(3), denomination int, year int, condition varchar(4)); For your reference, the database coins.db was created by the LoadCoins class in Part C, Problem 2. In the MainC2 class in the MainC.java file, the main method is supposed to (a) create Coin objects from the input file coins.txt placing these objects in the HashMap col. (b) print the year and price of all the coins whose condition is "mint". However, the rows of this the MainE class are not in the correct order. Reorder the rows so that this class runs properly. Don't forget to add the sqlite-jdbc-3.30.jar JAR file as a dependency before running the project. Get is from the JDBC project. Also, add the coins.db file to the project folder (not the src folder) before running this class. Part D: CollectableItem, Coin, Test2, and MainD classes. In this part, you will add these classes to your FinalExamSmith project. 1. 20 points (10 points each for the CollectableItem and Coint classes). Write Java classes CollectableItem and Coin defined by these UML diagrams and add them to your FinalExamSmith project. CollectableItem is the base class for the derived class Coin. If you wish, use IntelliJ to write the constructors, getters, setters, and toString methods. +---------------------------------------------------------------+ | CollectableItem | +---------------------------------------------------------------+ | - id : int | | - itemType : String | | - price : double | | - onSale : boolean | +---------------------------------------------------------------+ | + CollectableItem(id : int, itemType : String, | | price : double, onSale : boolean) | | + getItemType( ) : String | | + getPrice( ) : double | | + isOnSale( ) : boolean | | + setOnSale(onSale : boolean) | | + toString( ) : String | +--------------------------------+------------------------------+ ^ (arrow means inherits from) | | +--------------------------------+------------------------------+ | Coin | +---------------------------------------------------------------+ | - denomination : String | | - year : int | | - condition : String | +---------------------------------------------------------------+ | + USCoin(itemType : String, price : double, onSale : boolean, | | denonimation : int, year : int, condition : String) | | + getDenomination( ) : String | | + getYear( ) : int | | + getCondition( ) : String | | + toString( ) : String | +---------------------------------------------------------------+ 2. 10 points. Complete the Test2 unit test class in the Test2.java source code file and add it to your FinalExamSmith project. assertEquals statements to test the specified Coin methods. 3. 10 points. For this problem, look at the MainC2 Class in the source code file MainC3.java. Add this MainD class in the MainD.java source code file to your FinalExamSmith project. The main method should a. create Coin objects from the input file coins.txt placing these objects in the HashMap col. b. print the year and price of all the coins whose condition is "mint". Find the errors in this MainC3. There are between 0 and 15 errors. Add the coins.txt file to your FinalExamSmith project folder, not the src folder.

takehome-final/LoadCoins.java

takehome-final/LoadCoins.java

// LoadCoins class, for your reference only.
// This class creates the coins.db database
// from the coins.txt file.

import  java . io . * ;
import  java . sql . * ;
import  java . util . * ;
public   class   LoadCoins   {
     public   static   void  main ( String []  args )   {
         // Define local variables.
         Connection  c  =   null ;
         Statement  s  =   null ;
         Scanner  fromFile  =   null ;
         String  sql1  =   null ,  sql2  =   null ;
         String  line  =   null ;
         String []  fields ;
         int  age  =   0 ;

         try   {
             // Define Connection and Statement objects.
             Class . forName ( "org.sqlite.JDBC" );
            c  =   DriverManager . getConnection ( "jdbc:sqlite:coins.db" );
            s  =  c . createStatement ();

             // Instantiate scanner to read from file.
            fromFile  =   new   Scanner ( new   File ( "coins.txt" ));

             // Create kids table.
            sql1  =   "create table if not exists "   +
                     "coins(price float, "   +
                     "onsale varchar(3), "   +
                     "denomination int, "   +
                     "year int, "   +
                     "condition varchar(4));" ;
             System . out . println ( "sql1: "   +  sql1 );
            s . executeUpdate ( sql1 );
            fromFile . nextLine ();
             while   ( fromFile . hasNextLine ())   {
                line  =  fromFile . nextLine (   );
                fields  =  line . split ( "," );
                sql2  =   String . format (
                     "insert into coins "   +
                         "(price, onsale, denomination, year, condition) "   +
                         "values (%f, '%s', %d, %d, '%s');" ,
                         Double . parseDouble ( fields [ 1 ]),
                        fields [ 2 ],
                         Integer . parseInt ( fields [ 3 ]),
                         Integer . parseInt ( fields [ 4 ]),
                        fields [ 5 ]);
                 System . out . println ( sql2 );
                s . executeUpdate ( sql2 );
             }
            c . close ();
         }   catch   ( FileNotFoundException  e )   {
             System . out . println ( "File queries.sql not found." );
             System . err . println ( e . getClass (). getName ()   +
                     ": "   +  e . getMessage ());
         }   catch   ( SQLException  e )   {
             System . out . println ( "SQLException." );
             System . err . println ( e . getClass (). getName ()   +
                     ": "   +  e . getMessage ());
         }   catch   ( ClassNotFoundException  e )   {
             System . err . println ( e . getClass (). getName ()   +
                     ": "   +  e . getMessage ());
         }   finally   {
            fromFile . close ();
         }
     }
}

takehome-final/MainC2.java

takehome-final/MainC2.java

// MainC2 Class for Part C, Problem 2.
// Source code file MainC2.java
// The main method should
// 1. create Coin objects from the input file coins.txt
//    placing these objects in the HashMap col.
// 2. print the year and price of all the coins
//    whose condition is "mint".
// However, the rows of this the MainE class are not in the correct 
// order. Reorder the rows so that this class runs properly.

// Don't forget to add the sqlite-jdbc-3.30.jar JAR file as a 
//      dependency before running this class.
// Also, add the coins.db file to the project folder (not the 
//    src folder) before running this class.
 
public   class   MainC2   {

     import  java . sql . * ;
     import  java . util . Scanner ;

     public   static   void  main ( String []  args )   {

         catch   ( ClassNotFoundException  e  )   {
             System . err . println (  e . getClass (   ). getName ()   +
                     ": "   +  e . getMessage (   )   );
         }
         catch ( SQLException  e )   {
             System . out . println ( "SQLException." );
             System . err . println (  e . getClass (). getName ()   +
                     ": "   +  e . getMessage (   )   );
         }

         try   {
             // Define Connection and Statement objects.
             Class . forName ( "org.sqlite.JDBC" );
            c  =   DriverManager . getConnection ( "jdbc:sqlite:coins.db" );
            
            sql  =   "select year, price from coins "   +
                   "where condition = 'mint'" ;
             System . out . println ( sql );
            rs  =  s . executeQuery ( sql );
            s  =  c . createStatement (   );
             while   ( rs . next (   ))   {
                 System . out . println ( year  +   " "   +  price );
                year  =  rs . getInt ( "year" );
                price  =  rs . getDouble ( "price" );
             }
         }
     }

     // Declare local variables.
     Connection  c  =   null ;
     Statement  s  =   null ;
     ResultSet  rs  =   null ;
     int  id  =   0 ,  age  =   0 ;
     String  sql  =   null ;
     int  year  =   0 ;
     double  price  =   0.0 ;
}

takehome-final/MainD3.java

takehome-final/MainD3.java

// MainD3 class for Part D, Problem 3 of the final exam.
// Source code file MainD3.java
// The main method should 
// 1. create Coin objects from the input file coins.txt
//    placing these objects in the HashMap col.
// 2. print the year and price of all the coins
//    whose condition is "mint".

import  java . io . File ;
import  java . io . FileNotFoundException ;
import  java . util . HashMap ;
import  java . util . Scanner ;

public   class   MainD3   {
     public   static   void   Main ( String  args )
             throws   FileNotFoundException   {

         HashMap < int ,   Coin >  col  =
                 new   HashMap < int ,   Coin > ( 100 ,   0.6f   );
         int  id  =   1001 ;
         Scanner  s  =   new   Scanner ( f );
        s . nextLine (   )
         File  f  =   new   File ( "coins.txt" );
        
         while   ( s . hasNext (   ))   {
             String  line  =  s . nextLine (   );
             String [   ]  fields  =  line . split ( "-" );
             Coin  c  =   new   Coin ( id ++ ,  fields [ 0 ],
                 Double . parseDouble ( fields [ 1 ]),
                fields [ 2 ]. equals ( "yes" ),
                 Integer . parseInt ( fields [ 3 ]),
                 Integer . parseInt ( fields [ 4 ]),
                fields [ 5 ]);
            col . insert ( id ,  c );

         for ( Coin  c in col . values (   ))   {
             if ( c . getCondition (   )   ==   "mint" )   {
                 System . out . println ( String . format ( "%d %.2f" ,
                        c . year ,  c . getPrice ));
             }
            s . close (   );
         }
     }
}

takehome-final/Test2.java

takehome-final/Test2.java

// Test2 class for Part D of final exam.
// Source code file Test2.java
// Enter code to test the specified Coin methods.

import  org . junit . jupiter . api . Test ;
import   static  org . junit . jupiter . api . Assertions . * ;
class   Test2   {
     private   Coin  c ;

    @org . junit . jupiter . api . BeforeEach
     void  setUp (   )   {
        c  =   new   Coin ( 1002 ,   "coin" ,   750.00 ,
            true ,   10 ,   1934 ,   "mint" );
     }

    @ Test
     void  testGetDenomination ()   {
         // TO DO: Test getDenomination.
     }

    @ Test
     void  testGetYear (   )   {
         // TO DO: Test getYear.
     }

    @ Test
     void  testGetCondition (   )   {
         // TO DO: Test getCondition.
     }

    @ Test
     void  testSetOnSale (   )   {
         // TO DO: Test setOnSale.
     }
}