Objektorienterad programmering i Java Provmoment: Ladokkod: Tentamen ges för: Tentamen NDG011 Systemarkitektprogrammet 7,5 högskolepoäng Tentamensdatum: 2013-08-28 Tid: 09:00 14:00 Hjälpmedel: Inga hjälpmedel Totalt antal poäng på tentamen: För att få respektive betyg krävs: G=30 VG=45 60 poäng Allmänna anvisningar: Tentan har en teoretisk och en praktisk del. Fyll i ditt namn, personnummer och dina svar på svarsblanketten på nästa sida. Kontot för den praktiska delen lämnas ut efter att svarsblanketten lämnats in till en tentamensvakt. Notera att frågor där alternativen inleds med alternativen inleds med kan 0 till alla alternative vara korrekta. endast har ett korrekt alternativ. Om Spara alla filer på F: om inte annat specifikt anges. Stäng ner samtliga applikationer och logga ut (via ikon) när du är klar med tentamen. Vid olösliga problem med datorkonto, ta ett nytt och lämna in även detta i tentamensmappen. Den praktiska delen rättas endast om minst 50% av svaren på den teoretiska delen är körrekta. Rättningstiden är som längst tre veckor Viktigt! Glöm inte att skriva namn på alla blad du lämnar in. Lycka till! Ansvarig lärare: Rikard König Telefonnummer: 033 435 5945
Namn: (Ifylles av student) Personnummer: (Ifylles av student) Svarsblankett 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 A B C D E
Theory 1. An Java compiler translates Java source code into. A. Java bytecode B. machine code C. assembly code D. another high-level language code 2. Which of the following statements are true? A. Multiple constructors can be defined in a class. B. Constructors do not have a return type, not even void. C. Constructors must have the same name as the class itself. D. Constructors are invoked using the new operator when an object is created. 3. Which of the following alternatives is not a GRASP pattern A. Low coupling B. High Cohesion C. Polymorphism D. Facade 4. Which of the following statements are true? A. Information Expert regards which classes that should exist. B. Pure fabrication regards which classes that should be declared abstract in an application. C. Creator regards how an object should be created. D. GRASP stands for General Responsibility Assignment Software Patterns
5. Which of the following statements are true? A. To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass. B. Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them. C. It is a compilation error if two methods differ only in return type in the same class. D. A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated. E. A static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden. 6. Which component cannot be added to a container? A. JPanel B. JButton C. JFrame D. JComponent 7. You should override the method to draw things on a Swing component. A. repaint() B. update() C. paintcomponent() D. init()
8. Which of the following statements are true? A. You use the keyword throws to declare exceptions in the method heading. B. A method may declare to throw multiple exceptions. C. To throw an exception, use the key word throw. D. If a checked exception occurs in a method, it must be either caught or declared to be thrown from the method 9. Which of the following statements regarding abstract methods are true? A. Abstract classes have constructors. B. A class that contains abstract methods must be abstract. C. It is possible to declare an abstract class that contains no abstract methods. D. An abstract method cannot be contained in a nonabstract class. E. A data field can be declared abstract. 10. The handler (e.g., actionperformed) is a method in. A. a source object B. a listener object C. both source and listener object D. the Object class E. the EventObject class
11. Which of the following statements are true? A. Inner classes can make programs simple and concise. B. An inner class can be declared public or private subject to the same visibility rules applied to a member of the class. C. An inner class can be declared static. A static inner class can be accessed using the outer class name. A static inner class cannot access nonstatic members of the outer class. D. An inner class supports the work of its containing outer class and is compiled into a class named OuterClassName$InnerClassName.class. 12. are referred to as heavyweight components. A. AWT components B. Swing components C. GUI components D. Non-GUI components 13. If your applet does not have the init() method, which of the following will happen? A. Your program will not compile. B. Your program will compile, but not execute. C. You must have a main method. D. Your program will run just fine since the init() method is defined in the Applet class.
14. The Loan class given in the example does not implement java.io.serializable. Analyze the following code. public class Foo implements java.io.serializable { private int v1; private static double v2; private Loan v3 = new Loan(); } A. An instance of Foo can be serialized because Foo implements Serializable. B. An instance of Foo cannot be serialized because Foo contains a non-serializable instance variable v3. C. If you mark v3 as transient, an instance of Foo is serializable. 15. Fill in the code in Comparable c = new Date(); A. <String> B. <?> C. <Date> D. <E> 16. Which of the following statements are true? A. A synchronized instance method acquires a lock on the object for which the method was invoked. B. A synchronized instance method acquires a lock on the class of the object for which the method was invoked. C. A synchronized statement can be used to acquire a lock on any object, not just this object, when executing a block of the code in a method. D. A synchronized statement is placed inside a synchronized block. 17. To obtain an ObjectInputStream from a socket, use. A. socket.getinputstream() B. socket.getobjectstream() C. socket.getobjectinputstream() D. socket.objectinputstream() E. new ObjectInputStream(socket.getInputStream());
18. is a subinterface of java.rmi.remote that defines the methods for the server object. A. RMI Registry B. Server stub C. Server object interface D. Server Skeleton E. Server implementation 19. To execute a SELECT statement "select * from Address" on a Statement object stmt, use A. stmt.query("select * from Address"); B. stmt.execute("select * from Address"); C. stmt.executeupdate("select * from Address"); D. stmt.executequery("select * from Address"); 20. Analyze the following code: ResultSet resultset = statement.executequery ("select firstname, mi, lastname from Student where lastname " + " = 'Smith'"); System.out.println(resultSet.getString(1)); A. resultset.getstring(1) returns the firstname field in the result set. B. resultset.getstring(1) returns the mi field in the result set. C. If the SQL SELECT statement returns no result, resultset is null. D. The program will have a runtime error, because the cursor in resultset does not point to a row. You must use resultset.next() to move the cursor to the first row in the result set. Subsequently, resultset.next() moves the cursor to the next row in the result set.
Practical assignments 21. På ett zoo för däggdjur finns två apor (Bertil 4år och Strutharald 12 år), en kamel (Gösta 82år) och två japanska giraffer (Greger 15år och Knut 2år). Alla däggdjur har namn och ålder, dessutom låter de på ett specifikt sätt. Nedan återfinns de kodrader som skall ligga i en lämplig metod i respektive klass för att återge djurets läte på ett korrekt sätt. Apa: System.out.printline( heeeheeyaeeya ); Kamel: for(int i = 0 ; i < 3 ; i++) System.out.printline( uuuhre ); Giraff: for(int i = 0 ; i < 2 ; i++) System.out.printline( shitaaeete ); Konstruera en generell applikation som modellerar ovanstående beskrivning. Det är viktigt att ni skapar en metod public void DoZoo() vars syfte är att alla djur i parken få chansen att uttrycka sig, dvs. respektive djurs läte skrivs ut i consolefönstret (m.h.a. polymorfism). Skapa även en metod public double AvgAge() som beräknar genomsnittsåldern för djuren i parken. Sätt sedan upp ovanstående scenario i main, låt djuren yttra sig och beräkna sedan deras medelålder. (10p) 22. Create a program that draws circles and rectangles in frame similar to the GUI below: When one of the buttons are clicked a red, blue or green (decided by the JCombobox) circle or a rectangle should be drawn in a random position within the large JPanel below the buttons. The JPanel should never show more than a single figure at the same time. You should define the GUI and all listeners by hand, i.e. not by using any RAD features. (10p)
23. Morra (from Wikipedia) is a hand game that dates back thousands of years to ancient Roman and Greek times. It can be played to decide issues, much as two people might toss a coin, or for entertainment. While there are many variations of morra, most forms can be played with two, three, or more players. In the most popular version, all players throw out a single hand, each showing zero to five fingers, and call out loud their guess at what the sum of all fingers shown will be. If one player guesses the sum, that player earns one point. The first player to reach three points wins the game. Some variants of morra involve money, with the winner earning a number of currency units equal to the sum of fingers displayed. Morra players in Rome, 1809 Morra was known to the ancient Romans and is popular around the world, especially in Italy. In the Bible, it may have been referred to as "casting lots". In ancient Rome, it was called micatio, and playing it was referred to as micare digitis; literally, "to flash with the fingers". As time passed, the name became morra, a corruption of the verb micare. The game was so common in ancient Rome that there was a proverb used to denote an honest person which made reference to it: dignus est quicum in tenebris mices, literally, "he is a worthy man with whom you could play micatio in the dark". Micatio became so common that it came to be used to settle disputes over the sale of merchandise in the Roman forum. This practice was eventually banned by Apronius, prefect of the city. Programming task Your task is to create a simple Morra game. In each turn in the game all players throw out a single hand, each showing zero to five fingers (represented by a number). Furthermore, they also submit a guess at what the sum of all fingers shown will be. If one player guesses the sum, that player earns one point. The winner is the one who first reaches 3 points. The basic version of the game consists of a server that should be able to handle game sessions between two players and clients who represent the players that connect to the server. When both clients in a session have connected to the server a session should be created which handles the game session until one of the players win. Solutions with a server that can handle only one session at a time can maximum get 6 points. The input and output may be handled via the console. The players should be notified after each turn of the opponents move, their own number of points and the opponent s number of points.
You must define the protocol for the interaction from both players and the server. Obviously, your solution must also follow the protocol you have specified. A solution without a protocol can get maximum 8 points. One extra point can be had if each session can handle between 2-5 simultaneous players instead of exactly 2. Let the first player decide the size of the session and start the session when enough players have connected. (10p)