Question

In: Computer Science

data structure class: a. syntax for generics b. comparable interface c.how do you implement interface answer...

data structure class:

a. syntax for generics

b. comparable interface

c.how do you implement interface

answer for all of them please.

answer for all of them please

Solutions

Expert Solution

`Hey,

Note: If you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

1)

Like C++, we use <> to specify parameter types in generic class creation. To create objects of generic class, we use following syntax.

// To create an instance of generic class 
BaseType <Type> obj = new BaseType <Type>()

Note: In Parameter type we can not use primitives like 
      'int','char' or 'double'.

2)

Comparable interface is mainly used to sort the arrays (or lists) of custom objects.
Lists (and arrays) of objects that implement Comparable interface can be sorted automatically by Collections.sort (and Arrays.sort).

For example

 public int compareTo(Author au){
     /* 
      * Sorting by last name. compareTo should return < 0 if this(keyword) 
      * is supposed to be less than au, > 0 if this is supposed to be 
      * greater than object au and 0 if they are supposed to be equal.
      */
     int last = this.lastName.compareTo(au.lastName);
     //Sorting by first name if last name is same d
     return last == 0 ? this.firstName.compareTo(au.firstName) : last;
  }
}

3)

import java.io.*;

  

// A simple interface

interface In1

{

    // public, static and final

    final int a = 10;

  

    // public and abstract

    void display();

}

  

// A class that implements the interface.

class TestClass implements In1

{

    // Implementing the capabilities of

    // interface.

    public void display()

    {

        System.out.println("Geek");

    }

  

    // Driver Code

    public static void main (String[] args)

    {

        TestClass t = new TestClass();

        t.display();

        System.out.println(a);

    }

}

Kindly revert for any queries

Thanks.


Related Solutions

4) Define an abstract class Name Java class that implements interface Comparable   
4) Define an abstract class Name Java class that implements interface Comparable   
3.2. Unfortunately, you cannot modify the Rectangle class so that it implements the Comparable interface. The...
3.2. Unfortunately, you cannot modify the Rectangle class so that it implements the Comparable interface. The Rectangle class is part of the standard library, and you cannot modify library classes. Fortunately, there is a second sort method that you can use to sort a list of objects of any class, even if the class doesn't implement the Comparable interface. Comparator<T> comp = . . .; // for example, Comparator<Rectangle> comp = new RectangleComparator(); Collections.sort(list, comp); Comparator is an interface. Therefore,...
In this class add Comparable interface. In the driver program create a few objects and In...
In this class add Comparable interface. In the driver program create a few objects and In the driver program create a few objects and compare them . then create a list of those objects and sort them .A Quadratic is bigger than another Quadratic if it opens faster package pack2; /** * This is a program for defining a quadratic equation * @author sonik */ public class Quadratic { public int coeffX2 = 0; public int coeffX = 0; public...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with three int variables, indicating the arrivalTime, the timeForTheJob, and the priority. When the Job is created it is given the next sequential ID starting from 1. (You should use a static variable to keep track of where you are in ID assignment.) There are also int variables for startTime, waitTime (in queue) and endTime for the Job. The following methods are required: getID, set...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with three int variables, indicating the arrivalTime, the timeForTheJob, and the priority. When the Job is created it is given the next sequential ID starting from 1. (You should use a static variable to keep track of where you are in ID assignment.) There are also int variables for startTime, waitTime (in queue) and endTime for the Job. The following methods are required: getID, set...
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement...
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement the Stack.java in a class called ArrayStack.java that uses Generics. Write a main function that creates two stacks, one containing 10 Integers and a different one containing 10 Strings, and reverses there contents using a method called reverse that takes as a paramater a Generic array. You will need to implement all the methods of Stack.java as well as create a toString method in...
Implement the SimpleQueue interface with the MyQueue class. You can use either a linked list or...
Implement the SimpleQueue interface with the MyQueue class. You can use either a linked list or a dynamic array to implement the data structure. A queue is a specialised form of list in which you can only get and remove the first element in the queue. The class should be able to work with the following code: SimpleQueue queue = new MyQueue(); NB: You cannot import anything from the standard library for this task. The data structure must be of...
C++ Implement the array based Binary Heap data structure as discussed in class. This structure should...
C++ Implement the array based Binary Heap data structure as discussed in class. This structure should have a couple of constructures (default constructor, and a constructor that takes an array pointer and a size), a method for inserting items into the heap, a method for removing items from the heap, and a method that returns the number of items currently stored in the heap. This implementation should be templated so that it can store any type of data (you may...
Using the Comparable Interface Write a class Compare3 that provides a static method largest. Method largest...
Using the Comparable Interface Write a class Compare3 that provides a static method largest. Method largest should take three Comparable parameters and return the largest of the three (so its return type will also be Comparable). Recall that method compareTo is part of the Comparable interface, so largest can use the compareTo method of its parameters to compare them. Write a class Comparisons whose main method tests your largest method above. First prompt the user for and read in three...
For these exercises you are required to edit in the Comparable interface implementation, a compareTo() method,...
For these exercises you are required to edit in the Comparable interface implementation, a compareTo() method, starting with the class definition you obtained from the link, and submit that with a client that uses this ordering. If you are so inclined, you may submit a single client to drive your ordering enhancements for both classes, rather than one for each class. The exercises, for those without the text: 19. Modify the Point class from Chapter 8 so that it defines...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT