(Java) Fill in the following program at the three comment spots for combined lab
ID: 3714205 • Letter: #
Question
(Java)
Fill in the following program at the three comment spots for combined lab exercises 17-1, 17-2, 17-3. Convert the user input into a command line argument of the full path class name. Add the code in the getm() method for the loop to find all the get methods in the Class named cls. For our purpose, a get method would start with get, is not a void return, and has no arguments. Then add the code in the setm() method for the loop to find all the set methods in the Class named cls. For our purpose, a set method would start with set, is a void return, and has one argument.
For the getm() method, get all methods for the cls Class, loop through the methods and print all methods that are get methods - start with get, is not a void return, and has no arguments.
For the setm() method, get all methods for the cls Class, loop through the methods and print all methods that are set methods - start with set, is a void return, and has one argument.
For the void, check out the Void class and the TYPE data field. Try the program by inputting classes with a full path, such as: java.util.Date javax.swing.JLabel java.lang.reflect.Method. Save as ex173b.java and submit it.
/*
Exercise 17-3
This program will let the user give a class name to find the
get and set Methods. Test with:
java.util.Date
javax.swing.JLabel
java.lang.reflect.Method
*/
package rtti17;
import java.io.*;
import java.lang.reflect.Method;
public class ex173
{
public static void main(String[] args) throws IOException
{
String classname;
Class x;
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
/* Convert this user input to a command line argument */
System.out.print("Enter a full path class name: ");
classname = br.readLine();
try
{
x = Class.forName(classname);
getm(x);
System.out.println();
setm(x);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public static void getm(Class cls)
{
int i;
String methodname;
Class rettype;
Class[] paramtypes;
Method[] ma;
System.out.println("Listing get methods for " + cls);
/* get all methods for the cls Class, loop through methods and
print all methods that are get methods - start with get, is
not a void return, and has no arguments
*/
}
public static void setm(Class cls)
{
int i;
String methodname;
Class rettype;
Class[] paramtypes;
Method[] ma;
System.out.println("Listing set methods for " + cls);
/* get all methods for the cls Class, loop through methods and
print all methods that are set methods - start with set, is
a void return, and has one argument
*/
}
}
Explanation / Answer
Below is the code:
ex173.java
public class ex173 {
public static void main(String[] args) throws IOException {
String classname;
Class x;
// BufferedReader br = new BufferedReader(new
// InputStreamReader(System.in));
/* Convert this user input to a command line argument */
// System.out.print("Enter a full path class name: ");
// classname = br.readLine();
classname = args[0];// Argument passed
try {
x = Class.forName(classname);
getm(x);
System.out.println();
setm(x);
} catch (Exception ex) {
ex.printStackTrace();
}
}
/*
* get all methods for the cls Class, loop through methods and print all
* methods that are get methods - start with get, is not a void return, and
* has no arguments
*/
public static void getm(Class cls) {
Method[] ma = cls.getMethods();
System.out.println("Listing get methods for " + cls);
for (Method method : ma) {
if (method.getName().startsWith("get") && !(method.getReturnType().getName().equals("void")))
System.out.println(method.getName());
}
}
/*
* get all methods for the cls Class, loop through methods and print all
* methods that are set methods - start with set, is a void return, and has
* one argument
*/
public static void setm(Class cls) {
Method[] ma = cls.getMethods();
System.out.println("Listing set methods for " + cls);
for (Method method : ma) {
if (method.getName().startsWith("set") && method.getReturnType().getName().equals("void"))
System.out.println(method.getName());
}
}
}
Output for java.util.Date:
Listing get methods for class java.util.Date
getDate
getDay
getHours
getMinutes
getMonth
getSeconds
getTime
getTimezoneOffset
getYear
getClass
Listing set methods for class java.util.Date
setDate
setHours
setMinutes
setMonth
setSeconds
setTime
setYear
output for javax.swing.JLabel
Listing get methods for class javax.swing.JLabel
getAccessibleContext
getDisabledIcon
getDisplayedMnemonic
getDisplayedMnemonicIndex
getHorizontalAlignment
getHorizontalTextPosition
getIcon
getIconTextGap
getLabelFor
getText
getUI
getUIClassID
getVerticalAlignment
getVerticalTextPosition
getLocation
getSize
getActionForKeyStroke
getActionMap
getAlignmentX
getAlignmentY
getAncestorListeners
getAutoscrolls
getBaseline
getBaselineResizeBehavior
getBorder
getBounds
getClientProperty
getComponentPopupMenu
getConditionForKeyStroke
getDebugGraphicsOptions
getDefaultLocale
getFontMetrics
getGraphics
getHeight
getInheritsPopupMenu
getInputMap
getInputMap
getInputVerifier
getInsets
getInsets
getListeners
getMaximumSize
getMinimumSize
getNextFocusableComponent
getPopupLocation
getPreferredSize
getRegisteredKeyStrokes
getRootPane
getToolTipLocation
getToolTipText
getToolTipText
getTopLevelAncestor
getTransferHandler
getVerifyInputWhenFocusTarget
getVetoableChangeListeners
getVisibleRect
getWidth
getX
getY
getComponentAt
getComponentAt
getComponentZOrder
getContainerListeners
getFocusTraversalKeys
getLayout
getMousePosition
getComponent
getComponentCount
getComponents
getFocusTraversalPolicy
getName
getLocation
getParent
getSize
getGraphicsConfiguration
getMousePosition
getColorModel
getComponentListeners
getComponentOrientation
getCursor
getDropTarget
getFocusListeners
getFocusTraversalKeysEnabled
getHierarchyBoundsListeners
getHierarchyListeners
getIgnoreRepaint
getInputContext
getInputMethodListeners
getInputMethodRequests
getKeyListeners
getLocale
getLocationOnScreen
getMouseListeners
getMouseMotionListeners
getMouseWheelListeners
getToolkit
getBackground
getBounds
getFocusCycleRootAncestor
getFont
getForeground
getPeer
getPropertyChangeListeners
getPropertyChangeListeners
getTreeLock
getClass
Listing set methods for class javax.swing.JLabel
setDisabledIcon
setDisplayedMnemonic
setDisplayedMnemonic
setDisplayedMnemonicIndex
setHorizontalAlignment
setHorizontalTextPosition
setIcon
setIconTextGap
setLabelFor
setText
setUI
setVerticalAlignment
setVerticalTextPosition
setAlignmentX
setActionMap
setAlignmentY
setAutoscrolls
setBackground
setBorder
setComponentPopupMenu
setDebugGraphicsOptions
setDefaultLocale
setDoubleBuffered
setEnabled
setFocusTraversalKeys
setFont
setForeground
setInheritsPopupMenu
setInputMap
setInputVerifier
setMaximumSize
setMinimumSize
setNextFocusableComponent
setOpaque
setPreferredSize
setRequestFocusEnabled
setToolTipText
setTransferHandler
setVerifyInputWhenFocusTarget
setVisible
setComponentZOrder
setFocusCycleRoot
setFocusTraversalPolicyProvider
setLayout
setFocusTraversalPolicy
setName
setSize
setSize
setBounds
setBounds
setLocale
setLocation
setLocation
setComponentOrientation
setCursor
setDropTarget
setFocusTraversalKeysEnabled
setFocusable
setIgnoreRepaint
Output for java.lang.reflect.Method
Listing get methods for class java.lang.reflect.Method
getModifiers
getName
getAnnotation
getDeclaredAnnotations
getDeclaringClass
getParameterTypes
getReturnType
getTypeParameters
getParameterAnnotations
getParameterCount
getAnnotatedReturnType
getDefaultValue
getExceptionTypes
getGenericExceptionTypes
getGenericParameterTypes
getGenericReturnType
getAnnotationsByType
getAnnotatedParameterTypes
getAnnotatedExceptionTypes
getAnnotatedReceiverType
getParameters
getAnnotations
getDeclaredAnnotation
getDeclaredAnnotationsByType
getClass
Listing set methods for class java.lang.reflect.Method
setAccessible
setAccessible
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.