Question

Write a JAVA code to do the following: create a class and add methods to count...

Write a JAVA code to do the following:

create a class and add methods to count the number of primitive fields, number of methods with primitive return types, a method that will also return the number of primitive in a given parameter. they are:

public int getNumberOfPrimitiveMethods(Class host)

public int getNumberOfPrimitiveFields(Class host)

public int getNumberOfPrimitiveParameters(Method host)

also

public int getNumberOfPrivateMethods (Class host)

public int getNumberOfPublicMethods (Class host)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
class Test{
    public int getNumberOfPrimitiveMethods(Class host){
        int count=0;
        //get all the methods declared in class
        Method[] methods=host.getDeclaredMethods();
        for (Method method:methods){
              
               //if the returnTpe is primitive and not void
                if (method.getReturnType().isPrimitive() &&(! method.getReturnType().equals(Void.TYPE))){
                 
                    count++;
                }
              
        }
        return count;
      
    }
  
    public int getNumberOfPrimitiveFields(Class host){
        int count=0;
        //get all the declared fields
        Field fields[]=host.getDeclaredFields();
        //iterate the fields and check if they are primitive
        for(int i=0;i<fields.length;i++){
            if(fields[i].getType().isPrimitive())
                count++;
        }
        return count;
    }

  

    public int getNumberOfPrimitiveParameters(Method host){
        int count=0;
        //get all the parameter of the method paased
        Parameter parameters[]=host.getParameters();
        //iterate each parameter in parameters[]
        for(Parameter parameter:parameters){
            //if the return type is primitive increment count
            if(parameter.getType().isPrimitive())
                count++;
          
        }
      
        return count;
      
    }

    public int getNumberOfPrivateMethods (Class host){
            int count=0;
            Method methods[]=host.getDeclaredMethods();
            for (Method method:methods){
                if(Modifier.isPrivate(method.getModifiers())){
                 count++;
                }
            }
            return count;
        }

    public int getNumberOfPublicMethods (Class host){
        int count=0;
            Method methods[]=host.getDeclaredMethods();
            for (Method method:methods){
                if(Modifier.isPublic(method.getModifiers())){
                 count++;
                }
            }
            return count;
        }
      
    }


class Employee{
     private int employeeNumber;
     public String name;
     private boolean female;
     public Test t;
   
     public void empDetails(){
       
     }
   
     private void private1(){
       
     }
     private void private2(){
       
     }
   
     public int add(int a,int b,Employee c,String d){
         return a+b;
     }
     private String name(){
         return name;
     }
   
   
  

  
}


public class CountTypes {


    public static void main(String[] args) throws NoSuchMethodException {
        Employee e=new Employee();
        Class host=e.getClass();
        Class []param_types=new Class[]{Integer.TYPE,Integer.TYPE,Employee.class,String.class};
      
      
        Method method=host.getDeclaredMethod("add",param_types);
       // method.invoke(,1,e,"");
        Test t=new Test();
      
        System.out.println("Number of primitive Method Type: "+ t.getNumberOfPrimitiveMethods(host));
        System.out.println("Number of primitive Fields: "+ t.getNumberOfPrimitiveFields(host));
        System.out.println("Number of primitive parameters: "+ t.getNumberOfPrimitiveParameters(method));
        System.out.println("Number of private Method: "+ t.getNumberOfPrivateMethods(host));
        System.out.println("Number of public Method: "+ t.getNumberOfPublicMethods(host));
    }
  
}


Output:

Add a comment
Know the answer?
Add Answer to:
Write a JAVA code to do the following: create a class and add methods to count...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • 42) Create a toString method for the LinkedStack class. This method should create and return a...

    42) Create a toString method for the LinkedStack class. This method should create and return a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the LinkedStack class and for testing and debugging applications that use the LinkedStack class. 46) Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The...

  • In Java programming language Please write code for the 6 methods below: Assume that Student class...

    In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...

  • Last name is Vhora 3. Write a java class named FinalyourLastName, which contains three methods: main,...

    Last name is Vhora 3. Write a java class named FinalyourLastName, which contains three methods: main, arrayMystery, and countTotalOdd. You need to create main method. Inside the main method...you need to create an integer array named myld that consists of each of the digits in your student ID, call arrayMystery method and print out myld, then call count TotaOdd method and print out the total number of odd digits in your ID. (8 points) The arrayMystery method is given as...

  • 5. Given the following class definition, write Java code that implements the addFirst method public class...

    5. Given the following class definition, write Java code that implements the addFirst method public class MyLinkedList<E> { private Node<E> head, tail; private int size 0; // Number of elements in the list /** Add an element to the beginning of the list */ public void addFirst (E e) { private static class Node<E> { E element; Node<E> next; public Node (E element) { = element; this.element

  • JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList...

    JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...

  • PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data...

    PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data fields: name (String), age(int), company(String) •Methods: •Public String getName(); •Public int getAge(); •Public String getCom(); •Public void setName(String n); •Public void setAge(int a); •Public void setCom(String c); •toString(): \\ can be used to output information on this name card 2, write a class DoublyNameCardList.java, which is a doubly linked list and each node of the list a name card. In the DoublyNameCardList.java: •Data field:...

  • Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file...

    Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file with the appropriate name. public class BasicJava4 { Add four methods to the class that return a default value. public static boolean isAlphabetic(char aChar) public static int round(double num) public static boolean useSameChars(String str1, String str2) public static int reverse(int num) Implement the methods. public static boolean isAlphabetic(char aChar): Returns true if the argument is an alphabetic character, return false otherwise. Do NOT use...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • Part I: Problem: Code the following in Java: Candy Class -price -number of pounds sold Create...

    Part I: Problem: Code the following in Java: Candy Class -price -number of pounds sold Create the following methods: -Two constructors – default and second - accessor and mutator methods for each of the fields. - check for invalid price. Negative price is invalid (use if then else and display error message) - a method named userInput to get user input for all the fields. - check for invalid price. Negative price is invalid (use a loop to prompt the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT