A Blog About Computer Science Questions and Answers

AD

Cmis 142 | Java GUI questions solution

 Note:- Give a definition and sample code for all 6 questions every question explain it in detail what you need to do.
1 – Exception propagation
What is exception propagation? Give an example of a class that contains at least two methods, in which one method calls another. Ensure that the subordinate method will call a predefined Java method that can throw a checked exception. The subordinate method should not catch the exception. Explain how exception propagation will occur in your example.
Give an example of a program that creates a GUI with at least one button and several textfields. Some of the textfields should be for input and others for output. Make the output textfields uneditable. When the button is clicked, the input fields should be read, some calculation performed and the result displayed in the output textfield(s).
2 - GUIs and event handlers

Give an example of a program that creates a GUI with at least one button and several textfields. Some of the textfields should be for input and others for output. Make the output textfields uneditable. When the button is clicked, the input fields should be read, some calculation performed and the result displayed in the output textfield(s).
Abstraction is the basis for good Object-Oriented design that is modular, reusable, and maintainable.
Interfaces and Abstract classes are two mechanisms in Java that provide high-level abstractions.  An interface or abstract class is something which is not concrete, something which is incomplete.
For this discussion, what do you think is meant by “Programming to an interface”?  What are some differences between Abstract Classes and Interfaces?  Provide a simple example application with an interface definition and a class which implements the interface.
3 - Interfaces and Abstract Classes
Abstraction is the basis for good Object-Oriented design that is modular, reusable, and maintainable.
Interfaces and Abstract classes are two mechanisms in Java that provide high-level abstractions.  An interface or abstract class is something which is not concrete, something which is incomplete.
what do you think is meant by “Programming to an interface”?  What are some differences between Abstract Classes and Interfaces?  Provide a simple example application with an interface definition and a class which implements the interface.
4 – Writing recursive methods
Provide a recursive definition of some sequence of numbers or function (e.g. log, exponent, polynomial). Choose one different from that of any posted thus far. Write a recursive method that given n, computes the nth term of that sequence. Also provide an equivalent iterative implementation. How do the two implementations compare?

5– Generic classes
Give an example of a program that uses the nongeneric version of a class from the Java Collection Framework and the equivalent program that uses the generic version. How do the two implementations differ? Why is having the syntax of generics better?
6– Lists

Array lists and linked lists are both implementations of lists. Give an example of code a situation where an array list would be the better choice and one where a linked list would. Explain the reasons in each case.

SOLUTION:

1. Exception Propagation :

          If a method throws an exception then if we call that method, the method will throw the exception and if we will not caught it drops down to previous method, if not caught there again then the exception drop to the previous method and so on until it is caught or reach to the bottom of call stack of the method. This is called Exception Propagation.

Example :-

public static BufferedReader getBufferedReader() throws IOException {

        BufferedReader bufferedReader = new BufferedReader(new       InputStreamReader(System.in));

      return bufferedReader;

  }

    public static void readInput() {

        try {

            BufferedReader bufferedReader = getBufferedReader();

            System.out.println(bufferedReader.readLine());

        } catch (IOException exp) {

            System.out.println(exp.getMessage());

        }

    }

 

In the given example method readInput() calls another method getBufferedReader() which calls a predefined method that throws an exception. So, at first an exception will reach at the getBufferedReader() method which throws the method, so exception will reach at readInput() method where exception is being caught.

 

2. GUI and Event handlers :

 

Below is the code for a GUI example which takes two inputs and when the user clicks the button it shows the sum of the inputs.

 

public class Main {

    public static void main(String[] args) {

        JFrame frame = new JFrame();

        frame.setLayout(new FlowLayout());

 

        JLabel heading = new JLabel("Java GUI app for Add of two numbers");

        frame.add(heading);

 

        JTextField t1 = new JTextField(3);

        frame.add(t1);

 

        JLabel l1 = new JLabel("+");

        frame.add(l1);

 

        JTextField t2 = new JTextField(3);

        frame.add(t2);

 

        JLabel l2 = new JLabel(" = ");

        frame.add(l2);

 

        JLabel ans = new JLabel();

        frame.add(ans);

 

        JButton button = new JButton("Get The Answer");

        frame.add(button);

 

        button.addActionListener(x -> {

            try {

                double res = 0;

                double first = Double.parseDouble(t1.getText());

                double second = Double.parseDouble(t2.getText());

                res = first + second;

                ans.setText(String.valueOf(res));

            } catch (NumberFormatException e) {

                ans.setText("Please Enter Number !");

            }

           

        });

 

        frame.setVisible(true);

        frame.setSize(260, 500);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

Output :

 

3. Interfaces and abstract classes :

          Interfaces provide abstraction, code reusability and some methods which have to be implemented in the class which will implement the interface.

Differences between abstract class and Interface :

          Interfaces can have only abstract methods and abstract classes have abstract as well as non - abstract methods.

          Variables declared in abstract class may be final as well non-final variables but interfaces may have only final variables.

          A java class uses the keyword “implements” to implement an interface and “extends” to extend an abstract class “extends” keyword is used.

          We can implement many interfaces in a class but we could extend only one abstract class and we can create constructor in abstract classes but not in interfaces.
 
 

Example :-

    public interface Computer {

        void compute();

    }

    public class Apple implements Computer {

        public void compute() {

            // instructions

        }

    }

4. Writing recursive methods :

Recursive code  to compute m natural number:

    public int getSum(int m) {

        // base case

        if(m == 0) {

            return 0;

        }

        else {

            return m + getSum(m - 1);

        } 

}

Recursive code for m-th fibonacci number:

    public int getFiboNumber(int m) {

        if(m == 0) return 0;

   if(m == 1) return 1;

        return getFiboNumber(m-1) + getFiboNumber(m-2);

    }

Iterative code for n-th fibonacci number:

    public int getFiboNumber(int m) {

        int first = 0, sec = 1, res = 0;

        if(m == 0) return 0;

   if(m == 1) return 1;

        for(int i = 2; i <= m; ++i) {

            res = first + sec;

            sec =  first;

            first = res;

        }

        return res;

    }

In the recursive approach, we start from n and go till base case of the function and in iterative approach we start from base case and go to n.

5. Generic classes :

          Example of non-generic version of class from collection framework:

class NonGeneric {

    public void printList(List numbers) {

        for(Object numbers: number) {

            System.out.println(numbers);

        }

    }

}

Example of Generic class from collection framework:

class Generic<T> {

    public void printList(List<T> numbers) {

        for(T numbers: number) {

            System.out.println(number);

        }

    }

}

In the generic class first we have to declare the type using the <> symbol and in the Non-generic class we can use it without declaring the type.

Generic syntax is better because we can use the same function or class for the different data types.

6. List :

          We use arraylist where the main functionality of the method will be searching and use LinedList where we have to use delete operation very frequently.

          Because, deleting an element from arraylist gives O(n) performance and searching gives O(1) and in LinkedList deletion gives O(1) and searching gives O(n) performance.

Example of ArrayList:

    public <T> T getElementAtIndex(ArrayList<T> list, int n) {

        return list.get(n);

    }

Example of LinkedList :

    public <T> void removeElement(LinkedList<T> ll, T ele) {

        ll.remove(ele);

    }
 

Share:

0 Post a Comment:

Post a Comment

Copyright © CS Answer Template By: NewBloggerThemes