Object-Oriented Programming: Composition, Inheritance, StringTokenizer, AWT, Abstract Clas, Exams of Computer Science

Various concepts in object-oriented programming, including composition and inheritance relationships, the stringtokenizer class, awt, abstract classes, and writing a class definition in java. It also includes good practices for writing a class definition and examples of using the graphics class to draw lines, shapes, and text.

Typology: Exams

Pre 2010

Uploaded on 08/18/2009

koofers-user-wb7
koofers-user-wb7 🇺🇸

10 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Practice Test
Name:_CIS121 vest______________________
Part 1: Fill in the blank (1 point each) with the best word to complete the thought.
1) The process of connecting one string to the end of another string is called
_____concatenation____________.
2) In object-oriented programming, a(n) ___composition(has a)_ relationship
indicates that a class object contains a reference to an object of another class.
Whereas, in a(n) ___inheritance (is a)___ relationship, an object of a subclass
type may also be treated as an object of the superclass type.
3) Three methods of the StringTokenizer class involving tokens are
_nextToken____________, ____countTokens____, and
___hasMoreTokens_______.
4) The method used to initialize an object at its creation is called a(n)
_____constructor_____.
5) AWT stands for __abstract window toolkit___________________.
6) A class for which programmers never intend to instantiate objects is a(n)
___abstract____________ class.
7) The wrapper class for char is _Character_____________.
8) The _StringBuffer__________ class has capacity, insert, delete, and length
methods.
9) Each __color/pixel__________ in a window has a RGB value which displays the
intensity of the _____red_______________, __green________________, and
_____blue______________ color elements.
10) The Container class and the JComponent class are both subclasses the
____Component_________ class.
11) Mouse clicks, button presses, and radio button selection are examples of user
generated ______events_________________ that a program listens for.
12) When we implement the interface ActionListener we are required to
__define______ a method called _actionPerformed_______________.
13) Every class inherits a ___toString____________ method from the Object class.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Object-Oriented Programming: Composition, Inheritance, StringTokenizer, AWT, Abstract Clas and more Exams Computer Science in PDF only on Docsity!

Practice Test Name:_CIS121 vest______________________ Part 1 : Fill in the blank (1 point each) with the best word to complete the thought.

  1. The process of connecting one string to the end of another string is called _____ concatenation ____________.
  2. In object-oriented programming, a(n) ___ composition(has a) _ relationship indicates that a class object contains a reference to an object of another class. Whereas, in a(n) ___ inheritance (is a) ___ relationship, an object of a subclass type may also be treated as an object of the superclass type.
  3. Three methods of the StringTokenizer class involving tokens are _ nextToken ____________, ____ countTokens ____, and ___ hasMoreTokens _______.
  4. The method used to initialize an object at its creation is called a(n) _____ constructor _____.
  5. AWT stands for __ abstract window toolkit ___________________.
  6. A class for which programmers never intend to instantiate objects is a(n) ___ abstract ____________ class.
  7. The wrapper class for char is _ Character _____________.
  8. The _ StringBuffer __________ class has capacity, insert, delete, and length methods.
  9. Each __ color/pixel __________ in a window has a RGB value which displays the intensity of the _____ red _______________, __ green ________________, and _____ blue ______________ color elements.
  10. The Container class and the JComponent class are both subclasses the ____ Component _________ class.
  11. Mouse clicks, button presses, and radio button selection are examples of user generated ______ events _________________ that a program listens for.
  12. When we implement the interface ActionListener we are required to __ define ______ a method called _ actionPerformed _______________.
  13. Every class inherits a ___ toString ____________ method from the Object class.

Part 2 : Discussion

  1. Briefly describe good practices in writing a class definition. 1. Group members by member access modifier in a class definition for clarity and readability. (8.1) 2. Always define a class so its instance vars are maintained in a consistent state (always contain valid data) (GPP 8.2) 3. Initialize instance vars of a class in that class’s constructor. 4. Despite the fact that private and public members may be repeated and intermixed, list all the private members of a class first in one group and then list all the public members in another group. 5. Every method that modifies the private instance variables of an object should ensure that the data remains in a consistent state. 6. Avoid using method parameter names that conflict with class member names. 7. Explicitly using this can increase program clarity in some contexts in which this is optional. 8. Always invoke static methods using the class name and the dot operator. This emphasizes to other programmers reading your code that the method being called is a static method. 9. The last statement in a finalize method should always be super.finalize(); to ensure that the superclass’s finalize method is called.
  2. Describe how you would enable a program to respond a purchase button click. What would you have to set up? Event Handling 1. register an appropriate action listener 2. define an event handling method purchaseButton = new JButton("Make Purchase"); purchaseButton.addActionListener(this); // Whenever our class "implements ActionListener", we must have an // actionPerformed method to say what to do whenever our applet "hears"

5. When would you use the StringBuffer class as opposed to the String class and why? When given the choice between using a String object to represent a string versus a StringBuffer object to represent that string, always use a String object if indeed the object will not change; this improved performance. String objects are constant strings and StringBuffer objects are modifiable strings., Java distinguishes constant strings and modifiable strings for optimization purposes; in particular, Java can perform certain optimizations involving String objects (such as sharing one String object among multiple references) because it knows these objects will not change.

  1. Explain how lines, shapes, and text are drawn within an applet or JFrame. Using objects of the Graphics class (abstract) and defining the Paint method. Graphics Contexts and Objects 1. Graphics Context – enables drawing on the screen 2. Graphics Object – manages graphics context by controlling how information is drawn. Contains: Methods for drawing, font manipulation, color manipulation, etc. 3. Graphics Class is abstract (supports portability since drawing is performed differently on different platforms.) a. Paint is seldom called directly; it is an event driven process. When an applet executes, the paint methods is called automatically with init and start. To executive paint again, an event must occur. b. To paint again, a call to the component repaint is made. c. Repaint calls update that calls paint as part of its behavior. d. Do not override repaint (it performs some system dependent tasks!!!)

Part 3 : Programming Note: In the programming section of the real test, you should know how to set up a class definition, write main and instance methods, construct a class diagram, draw using the Graphics object, use the basic JOptionPane dialog boxes, and how to add JLabels, JTextFields, and JButtons to a JFrame object.

  1. Fill in the class diagram below for a basic Time class (you might refer to page 342 to check your work). Time Hour Minute second constructor(s) setTime(int, int, int) getHour() getMinutes() getSeconds() setHour(int) setMinute(int) setSecond(int) toUniversalString() toString()
  2. For a driver class, write a method that prompts the user for the current time and creates a Time object to store that time. // Class TimeTest to use imported class Time import javax.swing.JOptionPane; import edu.usouthal.c120.ch08.Time2; // import Time2 class public class TimeTest1 { public static void main( String args[] ) {

 draw an orange square with a height of 40 pixels in the upper left hand side of the window,  draw a filled circle with a diameter of 100 pixels in the center of the window,  draw 10 evenly spaced red vertical lines in the lower half of the window. // Java core packages import java.awt.; import java.awt.event.; // Java extension packages import javax.swing.*; public class LinesRectsOvals extends JFrame { // set window's title bar String and dimensions public LinesRectsOvals() { super( "Practice Test Graphics Demo" ); setBackground(Color.blue); setSize( 400, 300 ); show(); } // display various lines, rectangles and ovals public void paint( Graphics g ) { // call superclass's paint method super.paint( g ); g.setColor( Color.orange ); g.drawRect( 35, 50, 40, 40 ); g.setColor( Color.magenta ); g.drawOval( 150, 100, 100, 100 ); g.setColor( Color.red ); for(int I = 200; I <= 300; I+=10) g.drawLine( 5, I, 350, I ); } // execute application public static void main( String args[] ) { LinesRectsOvals application = new LinesRectsOvals(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } } // end class LinesRectsOvals

Character isDigit isLetterOrDigit toUpperCase toLowerCase isUpperCase isLowerCase StringTokenizer nextToken countTokens moreTokens

Assume g is an object of the Graphics class. Write the statement(s) to do the following: a. Assign the color blue to g. b. Draw a rectangle at the top right corner of the window that is 50 pixels wide and 100 pixels high. c. Using g, write the String “CIS 121 Test 1” in Dialog font, in bold, italic style. d. Output the size of the font currently assigned to g. e. Output the size of the font currently assigned to g. KEY::Assume g is an object of the Graphics class. Write the statement(s) to do the following: a. Assign the color blue to g. g.setColor(Color.blue); b. Draw a rectangle at the top right corner of the window that is 50 pixels wide and 100 pixels high. g.drawRect(20,20,50,100); c. Using g, write the String “CIS 121 Test 1” in Dialog font, in bold, italic style, size 36 pixels. g.setFont(new Font(“Dialog”), Font.BOLD + Font.ITALIC, 36)); g.drawString(“CIS 121 Test 1”); d. Output the size of the font currently assigned to g. g.drawString(g.getFont().getSize()); e. Output the size of the font currently assigned to g. g.drawString(g.getFont().getName()); 11